8import { Just, Nothing } from 'folktale/maybe';
9import { includes } from 'lodash';
14 addressFromSecp256k1Public,
17import { BRIDGE_ERROR } from 'lib/error';
19export const WalletContext = createContext(null);
21const DEFAULT_WALLET_TYPE = WALLET_TYPES.TICKET;
25// Note that the 'wallet' prop has type depending on the 'walletType' prop.
26// These could be bound together in a single structure (so that
27// 'walletType' acted more explicitly as a data constructor of sorts) but
28// it doesn't necessarily help us much, since we branch on 'walletType'
29// before setting 'wallet'.
31// Wallets are always wrapped in For most authentication
32// mechanisms, Maybe wraps a BIP32 HD wallet; for Ethereum private keys,
33// JSON keystore files, and Metamask authentication, it wraps an
35function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
36 const [walletType, _setWalletType] = useState(DEFAULT_WALLET_TYPE);
37 const [walletHdPath, setWalletHdPath] = useState(DEFAULT_HD_PATH);
38 const [wallet, _setWallet] = useState(initialWallet);
39 const [urbitWallet, _setUrbitWallet] = useState(Nothing());
40 const [authMnemonic, setAuthMnemonic] = useState(initialMnemonic);
41 const [networkSeed, setNetworkSeed] = useState(Nothing());
42 const [networkRevision, setNetworkRevision] = useState(Nothing());
44 const setWalletType = useCallback(
46 if (!includes(WALLET_TYPES, walletType)) {
47 throw BRIDGE_ERROR.INVALID_WALLET_TYPE;
50 _setWalletType(walletType);
55 const setWallet = useCallback(
57 // force that public addresses are derived for each wallet
58 // NOTE wallet is Maybe<> and .map is Maybe#map
60 wal.address = wal.address || addressFromSecp256k1Public(wal.publicKey);
69 const setUrbitWallet = useCallback(
71 if (Just.hasInstance(urbitWallet)) {
72 // when an urbit wallet is set, also derive
73 // a normal bip32 wallet using the ownership address
74 const wallet = walletFromMnemonic(
75 urbitWallet.value.ownership.seed,
77 urbitWallet.value.meta.passphrase
85 _setUrbitWallet(urbitWallet);
90 const resetWallet = useCallback(() => {
91 _setWalletType(DEFAULT_WALLET_TYPE);
92 setWalletHdPath(DEFAULT_HD_PATH);
93 _setWallet(Nothing());
94 _setUrbitWallet(Nothing());
95 setAuthMnemonic(Nothing());
96 setNetworkSeed(Nothing());
97 setNetworkRevision(Nothing());
133export function WalletProvider({ initialWallet, initialMnemonic, children }) {
134 const wallet = _useWallet(initialWallet, initialMnemonic);
137 <WalletContext.Provider value={wallet}>{children}</WalletContext.Provider>
142export function useWallet() {
143 return useContext(WalletContext);
147export const withWallet = Component =>
148 forwardRef((props, ref) => (
149 <WalletContext.Consumer>
150 {wallet => <Component ref={ref} {...wallet} {...props} />}
151 </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