9import { Just, Nothing } from 'folktale/maybe';
10import { includes } 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';
25export const WalletContext = createContext(null);
27const DEFAULT_WALLET_TYPE = WALLET_TYPES.TICKET;
31// Note that the 'wallet' prop has type depending on the 'walletType' prop.
32// These could be bound together in a single structure (so that
33// 'walletType' acted more explicitly as a data constructor of sorts) but
34// it doesn't necessarily help us much, since we branch on 'walletType'
35// before setting 'wallet'.
37// Wallets are always wrapped in For most authentication
38// mechanisms, Maybe wraps a BIP32 HD wallet; for Ethereum private keys,
39// JSON keystore files, and Metamask authentication, it wraps an
41function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
42 const [walletType, _setWalletType] = useState(() =>
43 initialMnemonic.matchWith({
44 Nothing: () => DEFAULT_WALLET_TYPE,
45 Just: () => WALLET_TYPES.MNEMONIC,
48 const [walletHdPath, setWalletHdPath] = useState(DEFAULT_HD_PATH);
49 const [wallet, _setWallet] = useState(initialWallet);
50 const [urbitWallet, _setUrbitWallet] = useState(Nothing());
51 const [authMnemonic, setAuthMnemonic] = useState(initialMnemonic);
52 const [networkSeed, setNetworkSeed] = useState(Nothing());
53 const [networkRevision, setNetworkRevision] = useState(Nothing());
55 const [authToken, setAuthToken] = useState(Nothing());
56 // Allow users to skip signing the auth token on login
57 const [skipLoginSigning, setSkipLoginSigning] = useState(false);
59 const setFakeToken = () => {
60 setAuthToken(Just(randomHex(32)));
63 // See: https://github.com/urbit/bridge/issues/549#issuecomment-1048359617
64 // This is used for legacy compatibility; this flow should eventually be
65 // be removed after ~1 year.
66 const [useLegacyTokenSigning, setUseLegacyTokenSigning] = useState(false);
68 const { web3 } = useNetwork();
73 !Just.hasInstance(wallet) ||
74 !Just.hasInstance(web3) ||
75 NONCUSTODIAL_WALLETS.has(walletType)
80 const _wallet = wallet.value;
81 const _web3 = web3.value;
82 if (skipLoginSigning) {
87 const token = await getAuthToken({
92 useLegacyTokenSigning,
95 setAuthToken(Just(token));
102 useLegacyTokenSigning,
107 const setWalletType = useCallback(
109 if (!includes(WALLET_TYPES, walletType)) {
110 throw new Error(BRIDGE_ERROR.INVALID_WALLET_TYPE);
113 _setWalletType(walletType);
118 const setWallet = useCallback(
120 // force that public addresses are derived for each wallet
121 // NOTE wallet is Maybe<> and .map is Maybe#map
123 wal.address = wal.address || publicToAddress(wal.publicKey);
132 const setUrbitWallet = useCallback(
134 if (Just.hasInstance(urbitWallet)) {
135 // when an urbit wallet is set, also derive
136 // a normal bip32 wallet using the ownership address
137 const wallet = walletFromMnemonic(
138 urbitWallet.value.ownership.seed,
140 urbitWallet.value.meta.passphrase
145 setWallet(Nothing());
148 _setUrbitWallet(urbitWallet);
153 const resetWallet = useCallback(() => {
154 _setWalletType(DEFAULT_WALLET_TYPE);
155 setWalletHdPath(DEFAULT_HD_PATH);
156 _setWallet(Nothing());
157 _setUrbitWallet(Nothing());
158 setAuthMnemonic(Nothing());
159 setNetworkSeed(Nothing());
160 setNetworkRevision(Nothing());
161 setUseLegacyTokenSigning(false);
197 useLegacyTokenSigning,
198 setUseLegacyTokenSigning,
205export function WalletProvider({ initialWallet, initialMnemonic, children }) {
206 const wallet = _useWallet(initialWallet, initialMnemonic);
209 <WalletContext.Provider value={wallet}>{children}</WalletContext.Provider>
214export function useWallet() {
215 return useContext(WalletContext);
219export const withWallet = Component =>
220 forwardRef((props, ref) => (
221 <WalletContext.Consumer>
222 {wallet => <Component ref={ref} {...wallet} {...props} />}
223 </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