9import { Just, Nothing } from 'folktale/maybe';
10import { includes, noop } from 'lodash';
11import { randomHex } from 'web3-utils';
13import { walletFromMnemonic } from 'lib/wallet';
18} from 'lib/constants';
19import { getAuthToken } from 'lib/authToken';
20import { BRIDGE_ERROR } from 'lib/error';
22import { useNetwork } from 'store/network';
23import { publicToAddress } from 'lib/utils/address';
25const initialContext = {
27 walletType: Nothing(),
31 setWalletHdPath: noop,
36 urbitWallet: Nothing(),
38 authMnemonic: Nothing(),
39 setAuthMnemonic: noop,
41 networkSeed: Nothing(),
43 networkRevision: Nothing(),
44 setNetworkRevision: noop,
50 useLegacyTokenSigning: Nothing(),
51 setUseLegacyTokenSigning: noop,
52 skipLoginSigning: false,
53 setSkipLoginSigning: noop,
57export const WalletContext = createContext(initialContext);
59const DEFAULT_WALLET_TYPE = WALLET_TYPES.TICKET;
63// Note that the 'wallet' prop has type depending on the 'walletType' prop.
64// These could be bound together in a single structure (so that
65// 'walletType' acted more explicitly as a data constructor of sorts) but
66// it doesn't necessarily help us much, since we branch on 'walletType'
67// before setting 'wallet'.
69// Wallets are always wrapped in For most authentication
70// mechanisms, Maybe wraps a BIP32 HD wallet; for Ethereum private keys,
71// JSON keystore files, and Metamask authentication, it wraps an
73function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
74 const [walletType, _setWalletType] = useState(() =>
75 initialMnemonic.matchWith({
76 Nothing: () => DEFAULT_WALLET_TYPE,
77 Just: () => WALLET_TYPES.MNEMONIC,
80 const [walletHdPath, setWalletHdPath] = useState(DEFAULT_HD_PATH);
81 const [wallet, _setWallet] = useState(initialWallet);
82 const [urbitWallet, _setUrbitWallet] = useState(Nothing());
83 const [authMnemonic, setAuthMnemonic] = useState(initialMnemonic);
84 const [networkSeed, setNetworkSeed] = useState(Nothing());
85 const [networkRevision, setNetworkRevision] = useState(Nothing());
87 const [authToken, setAuthToken] = useState(Nothing());
88 // Allow users to skip signing the auth token on login
89 const [skipLoginSigning, setSkipLoginSigning] = useState(false);
91 const setFakeToken = () => {
92 setAuthToken(Just(randomHex(32)));
95 // See: https://github.com/urbit/bridge/issues/549#issuecomment-1048359617
96 // This is used for legacy compatibility; this flow should eventually be
97 // be removed after ~1 year.
98 const [useLegacyTokenSigning, setUseLegacyTokenSigning] = useState(false);
100 const { web3 } = useNetwork();
105 !Just.hasInstance(wallet) ||
106 !Just.hasInstance(web3) ||
107 NONCUSTODIAL_WALLETS.has(walletType)
112 const _wallet = wallet.value;
113 const _web3 = web3.value;
114 if (skipLoginSigning) {
119 const token = await getAuthToken({
124 useLegacyTokenSigning,
127 setAuthToken(Just(token));
134 useLegacyTokenSigning,
139 const setWalletType = useCallback(
141 if (!includes(WALLET_TYPES, walletType)) {
142 throw new Error(BRIDGE_ERROR.INVALID_WALLET_TYPE);
145 _setWalletType(walletType);
150 const setWallet = useCallback(
152 // force that public addresses are derived for each wallet
153 // NOTE wallet is Maybe<> and .map is Maybe#map
155 wal.address = wal.address || publicToAddress(wal.publicKey);
164 const setUrbitWallet = useCallback(
166 if (Just.hasInstance(urbitWallet)) {
167 // when an urbit wallet is set, also derive
168 // a normal bip32 wallet using the ownership address
169 const wallet = walletFromMnemonic(
170 urbitWallet.value.ownership.seed,
172 urbitWallet.value.meta.passphrase
177 setWallet(Nothing());
180 _setUrbitWallet(urbitWallet);
185 const resetWallet = useCallback(() => {
186 _setWalletType(DEFAULT_WALLET_TYPE);
187 setWalletHdPath(DEFAULT_HD_PATH);
188 _setWallet(Nothing());
189 _setUrbitWallet(Nothing());
190 setAuthMnemonic(Nothing());
191 setNetworkSeed(Nothing());
192 setNetworkRevision(Nothing());
193 setUseLegacyTokenSigning(false);
229 useLegacyTokenSigning,
230 setUseLegacyTokenSigning,
237export function WalletProvider({ initialWallet, initialMnemonic, children }) {
238 const wallet = _useWallet(initialWallet, initialMnemonic);
241 <WalletContext.Provider value={wallet}>{children}</WalletContext.Provider>
246export function useWallet() {
247 return useContext(WalletContext);
251export const withWallet = Component =>
252 forwardRef((props, ref) => (
253 <WalletContext.Consumer>
254 {wallet => <Component ref={ref} {...wallet} {...props} />}
255 </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