9import { Just, Nothing } from 'folktale/maybe';
10import { includes } from 'lodash';
12import { walletFromMnemonic } from 'lib/wallet';
17} from 'lib/constants';
18import { getAuthToken } from 'lib/authToken';
19import { BRIDGE_ERROR } from 'lib/error';
21import { useNetwork } from 'store/network';
22import { publicToAddress } from 'lib/utils/address';
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)
68 const _wallet = wallet.value;
69 const _web3 = web3.value;
70 const token = await getAuthToken({
77 setAuthToken(Just(token));
79 }, [wallet, walletType, walletHdPath, web3]);
81 const setWalletType = useCallback(
83 if (!includes(WALLET_TYPES, walletType)) {
84 throw new Error(BRIDGE_ERROR.INVALID_WALLET_TYPE);
87 _setWalletType(walletType);
92 const setWallet = useCallback(
94 // force that public addresses are derived for each wallet
95 // NOTE wallet is Maybe<> and .map is Maybe#map
97 wal.address = wal.address || publicToAddress(wal.publicKey);
106 const setUrbitWallet = useCallback(
108 if (Just.hasInstance(urbitWallet)) {
109 // when an urbit wallet is set, also derive
110 // a normal bip32 wallet using the ownership address
111 const wallet = walletFromMnemonic(
112 urbitWallet.value.ownership.seed,
114 urbitWallet.value.meta.passphrase
119 setWallet(Nothing());
122 _setUrbitWallet(urbitWallet);
127 const resetWallet = useCallback(() => {
128 _setWalletType(DEFAULT_WALLET_TYPE);
129 setWalletHdPath(DEFAULT_HD_PATH);
130 _setWallet(Nothing());
131 _setUrbitWallet(Nothing());
132 setAuthMnemonic(Nothing());
133 setNetworkSeed(Nothing());
134 setNetworkRevision(Nothing());
173export function WalletProvider({ initialWallet, initialMnemonic, children }) {
174 const wallet = _useWallet(initialWallet, initialMnemonic);
177 <WalletContext.Provider value={wallet}>{children}</WalletContext.Provider>
182export function useWallet() {
183 return useContext(WalletContext);
187export const withWallet = Component =>
188 forwardRef((props, ref) => (
189 <WalletContext.Consumer>
190 {wallet => <Component ref={ref} {...wallet} {...props} />}
191 </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