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(() =>
37 initialMnemonic.matchWith({
38 Nothing: () => DEFAULT_WALLET_TYPE,
39 Just: () => WALLET_TYPES.MNEMONIC,
42 const [walletHdPath, setWalletHdPath] = useState(DEFAULT_HD_PATH);
43 const [wallet, _setWallet] = useState(initialWallet);
44 const [urbitWallet, _setUrbitWallet] = useState(Nothing());
45 const [authMnemonic, setAuthMnemonic] = useState(initialMnemonic);
46 const [networkSeed, setNetworkSeed] = useState(Nothing());
47 const [networkRevision, setNetworkRevision] = useState(Nothing());
49 const setWalletType = useCallback(
51 if (!includes(WALLET_TYPES, walletType)) {
52 throw new Error(BRIDGE_ERROR.INVALID_WALLET_TYPE);
55 _setWalletType(walletType);
60 const setWallet = useCallback(
62 // force that public addresses are derived for each wallet
63 // NOTE wallet is Maybe<> and .map is Maybe#map
65 wal.address = wal.address || addressFromSecp256k1Public(wal.publicKey);
74 const setUrbitWallet = useCallback(
76 if (Just.hasInstance(urbitWallet)) {
77 // when an urbit wallet is set, also derive
78 // a normal bip32 wallet using the ownership address
79 const wallet = walletFromMnemonic(
80 urbitWallet.value.ownership.seed,
82 urbitWallet.value.meta.passphrase
90 _setUrbitWallet(urbitWallet);
95 const resetWallet = useCallback(() => {
96 _setWalletType(DEFAULT_WALLET_TYPE);
97 setWalletHdPath(DEFAULT_HD_PATH);
98 _setWallet(Nothing());
99 _setUrbitWallet(Nothing());
100 setAuthMnemonic(Nothing());
101 setNetworkSeed(Nothing());
102 setNetworkRevision(Nothing());
138export function WalletProvider({ initialWallet, initialMnemonic, children }) {
139 const wallet = _useWallet(initialWallet, initialMnemonic);
142 <WalletContext.Provider value={wallet}>{children}</WalletContext.Provider>
147export function useWallet() {
148 return useContext(WalletContext);
152export const withWallet = Component =>
153 forwardRef((props, ref) => (
154 <WalletContext.Consumer>
155 {wallet => <Component ref={ref} {...wallet} {...props} />}
156 </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