8import Maybe from 'folktale/maybe';
9import { includes } from 'lodash';
14 addressFromSecp256k1Public,
16} from '../lib/wallet';
17import { BRIDGE_ERROR } from '../lib/error';
19export const WalletContext = createContext(null);
23// Note that the 'wallet' prop has type depending on the 'walletType' prop.
24// These could be bound together in a single structure (so that
25// 'walletType' acted more explicitly as a data constructor of sorts) but
26// it doesn't necessarily help us much, since we branch on 'walletType'
27// before setting 'wallet'.
29// Wallets are always wrapped in Maybe. For most authentication
30// mechanisms, Maybe wraps a BIP32 HD wallet; for Ethereum private keys,
31// JSON keystore files, and Metamask authentication, it wraps an
34 initialWallet = Maybe.Nothing(),
35 initialMnemonic = Maybe.Nothing()
37 const [walletType, _setWalletType] = useState(WALLET_TYPES.MNEMONIC);
38 const [walletHdPath, setWalletHdPath] = useState(DEFAULT_HD_PATH);
39 const [wallet, _setWallet] = useState(initialWallet);
40 const [urbitWallet, _setUrbitWallet] = useState(Maybe.Nothing());
41 const [authMnemonic, setAuthMnemonic] = useState(initialMnemonic);
42 const [networkSeed, setNetworkSeed] = useState(Maybe.Nothing());
43 const [networkRevision, setNetworkRevision] = useState(Maybe.Nothing());
45 const setWalletType = useCallback(
47 if (!includes(WALLET_TYPES, walletType)) {
48 throw BRIDGE_ERROR.INVALID_WALLET_TYPE;
51 _setWalletType(walletType);
56 const setWallet = useCallback(
58 // force that public addresses are derived for each wallet
60 wal.address = wal.address || addressFromSecp256k1Public(wal.publicKey);
69 const setUrbitWallet = useCallback(
71 if (Maybe.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
80 // manually set the public address
82 wal.address = urbitWallet.value.ownership.keys.address;
89 _setUrbitWallet(urbitWallet);
117export function WalletProvider({ initialWallet, initialMnemonic, children }) {
118 const wallet = _useWallet(initialWallet, initialMnemonic);
121 <WalletContext.Provider value={wallet}>{children}</WalletContext.Provider>
126export function useWallet() {
127 return useContext(WalletContext);
131export const withWallet = Component =>
132 forwardRef((props, ref) => (
133 <WalletContext.Consumer>
134 {wallet => <Component ref={ref} {...wallet} {...props} />}
135 </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