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
59 wal.address = wal.address || addressFromSecp256k1Public(wal.publicKey);
68 const setUrbitWallet = useCallback(
70 if (Just.hasInstance(urbitWallet)) {
71 // when an urbit wallet is set, also derive
72 // a normal bip32 wallet using the ownership address
73 const wallet = walletFromMnemonic(
74 urbitWallet.value.ownership.seed,
76 urbitWallet.value.meta.passphrase
84 _setUrbitWallet(urbitWallet);
89 const resetWallet = useCallback(() => {
90 _setWalletType(DEFAULT_WALLET_TYPE);
91 setWalletHdPath(DEFAULT_HD_PATH);
92 _setWallet(Nothing());
93 _setUrbitWallet(Nothing());
94 setAuthMnemonic(Nothing());
95 setNetworkSeed(Nothing());
96 setNetworkRevision(Nothing());
132export function WalletProvider({ initialWallet, initialMnemonic, children }) {
133 const wallet = _useWallet(initialWallet, initialMnemonic);
136 <WalletContext.Provider value={wallet}>{children}</WalletContext.Provider>
141export function useWallet() {
142 return useContext(WalletContext);
146export const withWallet = Component =>
147 forwardRef((props, ref) => (
148 <WalletContext.Consumer>
149 {wallet => <Component ref={ref} {...wallet} {...props} />}
150 </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