8import Maybe from 'folktale/maybe';
9import { includes } from 'lodash';
14 addressFromSecp256k1Public,
17import { BRIDGE_ERROR } from 'lib/error';
19export const WalletContext = createContext(null);
21const kDefaultWalletType = 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 Maybe. 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
36 initialWallet = Maybe.Nothing(),
37 initialMnemonic = Maybe.Nothing()
39 const [walletType, _setWalletType] = useState(kDefaultWalletType);
40 const [walletHdPath, setWalletHdPath] = useState(DEFAULT_HD_PATH);
41 const [wallet, _setWallet] = useState(initialWallet);
42 const [urbitWallet, _setUrbitWallet] = useState(Maybe.Nothing());
43 const [authMnemonic, setAuthMnemonic] = useState(initialMnemonic);
44 const [networkSeed, setNetworkSeed] = useState(Maybe.Nothing());
45 const [networkRevision, setNetworkRevision] = useState(Maybe.Nothing());
47 const setWalletType = useCallback(
49 if (!includes(WALLET_TYPES, walletType)) {
50 throw BRIDGE_ERROR.INVALID_WALLET_TYPE;
53 _setWalletType(walletType);
58 const setWallet = useCallback(
60 // force that public addresses are derived for each wallet
62 wal.address = wal.address || addressFromSecp256k1Public(wal.publicKey);
71 const setUrbitWallet = useCallback(
73 if (Maybe.Just.hasInstance(urbitWallet)) {
74 // when an urbit wallet is set, also derive
75 // a normal bip32 wallet using the ownership address
76 const wallet = walletFromMnemonic(
77 urbitWallet.value.ownership.seed,
79 urbitWallet.value.meta.passphrase
82 // manually set the public address
84 wal.address = urbitWallet.value.ownership.keys.address;
90 setWallet(Maybe.Nothing());
93 _setUrbitWallet(urbitWallet);
98 const resetWallet = useCallback(() => {
99 _setWalletType(kDefaultWalletType);
100 setWalletHdPath(DEFAULT_HD_PATH);
101 _setWallet(Maybe.Nothing());
102 _setUrbitWallet(Maybe.Nothing());
103 setAuthMnemonic(Maybe.Nothing());
104 setNetworkSeed(Maybe.Nothing());
105 setNetworkRevision(Maybe.Nothing());
141export function WalletProvider({ initialWallet, initialMnemonic, children }) {
142 const wallet = _useWallet(initialWallet, initialMnemonic);
145 <WalletContext.Provider value={wallet}>{children}</WalletContext.Provider>
150export function useWallet() {
151 return useContext(WalletContext);
155export const withWallet = Component =>
156 forwardRef((props, ref) => (
157 <WalletContext.Consumer>
158 {wallet => <Component ref={ref} {...wallet} {...props} />}
159 </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