9import { Just, Nothing } from 'folktale/maybe';
10import { includes } from 'lodash';
15 addressFromSecp256k1Public,
19import { getAuthToken } from 'lib/authToken';
20import { BRIDGE_ERROR } from 'lib/error';
22import { useNetwork } from 'store/network';
24export const WalletContext = createContext(null);
26const DEFAULT_WALLET_TYPE = WALLET_TYPES.TICKET;
30// Note that the 'wallet' prop has type depending on the 'walletType' prop.
31// These could be bound together in a single structure (so that
32// 'walletType' acted more explicitly as a data constructor of sorts) but
33// it doesn't necessarily help us much, since we branch on 'walletType'
34// before setting 'wallet'.
36// Wallets are always wrapped in For most authentication
37// mechanisms, Maybe wraps a BIP32 HD wallet; for Ethereum private keys,
38// JSON keystore files, and Metamask authentication, it wraps an
40function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
41 const [walletType, _setWalletType] = useState(() =>
42 initialMnemonic.matchWith({
43 Nothing: () => DEFAULT_WALLET_TYPE,
44 Just: () => WALLET_TYPES.MNEMONIC,
47 const [walletHdPath, setWalletHdPath] = useState(DEFAULT_HD_PATH);
48 const [wallet, _setWallet] = useState(initialWallet);
49 const [urbitWallet, _setUrbitWallet] = useState(Nothing());
50 const [authMnemonic, setAuthMnemonic] = useState(initialMnemonic);
51 const [networkSeed, setNetworkSeed] = useState(Nothing());
52 const [networkRevision, setNetworkRevision] = useState(Nothing());
54 const [authToken, setAuthToken] = useState(Nothing());
56 const { web3 } = useNetwork();
61 !Just.hasInstance(wallet) ||
62 !Just.hasInstance(web3) ||
63 NONCUSTODIAL_WALLETS.has(walletType)
67 const _wallet = wallet.value;
68 const _web3 = web3.value;
69 const token = await getAuthToken({
76 setAuthToken(Just(token));
78 }, [wallet, walletType, walletHdPath, web3]);
80 const setWalletType = useCallback(
82 if (!includes(WALLET_TYPES, walletType)) {
83 throw new Error(BRIDGE_ERROR.INVALID_WALLET_TYPE);
86 _setWalletType(walletType);
91 const setWallet = useCallback(
93 // force that public addresses are derived for each wallet
94 // NOTE wallet is Maybe<> and .map is Maybe#map
96 wal.address = wal.address || addressFromSecp256k1Public(wal.publicKey);
105 const setUrbitWallet = useCallback(
107 if (Just.hasInstance(urbitWallet)) {
108 // when an urbit wallet is set, also derive
109 // a normal bip32 wallet using the ownership address
110 const wallet = walletFromMnemonic(
111 urbitWallet.value.ownership.seed,
113 urbitWallet.value.meta.passphrase
118 setWallet(Nothing());
121 _setUrbitWallet(urbitWallet);
126 const resetWallet = useCallback(() => {
127 _setWalletType(DEFAULT_WALLET_TYPE);
128 setWalletHdPath(DEFAULT_HD_PATH);
129 _setWallet(Nothing());
130 _setUrbitWallet(Nothing());
131 setAuthMnemonic(Nothing());
132 setNetworkSeed(Nothing());
133 setNetworkRevision(Nothing());
172export function WalletProvider({ initialWallet, initialMnemonic, children }) {
173 const wallet = _useWallet(initialWallet, initialMnemonic);
176 <WalletContext.Provider value={wallet}>{children}</WalletContext.Provider>
181export function useWallet() {
182 return useContext(WalletContext);
186export const withWallet = Component =>
187 forwardRef((props, ref) => (
188 <WalletContext.Consumer>
189 {wallet => <Component ref={ref} {...wallet} {...props} />}
190 </WalletContext.Consumer>
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list