on master help sign in
add authToken for custodial wallets
e11c85bLiam Fitzgerald
src/lib/authToken.jsnew file+14
@@ -0,0 +1,14 @@
1+import * as secp256k1 from 'secp256k1';
2+
3+import { keccak256 } from './wallet';
4+
5+const MESSAGE = 'Bridge Authentication Token';
6+
7+export const getAuthToken = ({ wallet, walletType, walletHdPath, web3 }) => {
8+ const { signature } = secp256k1.sign(
9+ keccak256('\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE),
10+ wallet.privateKey
11+ );
12+
13+ return signature;
14+};
src/store/wallet.js+24
@@ -4,6 +4,7 @@ import React, {
4 useCallback,
4 useCallback,
5 useContext,
5 useContext,
6 useState,
6 useState,
7+ useEffect,
7} from 'react';
8} from 'react';
8import { Just, Nothing } from 'folktale/maybe';
9import { Just, Nothing } from 'folktale/maybe';
9import { includes } from 'lodash';
10import { includes } from 'lodash';
@@ -14,8 +15,11 @@ import {
14 addressFromSecp256k1Public,
15 addressFromSecp256k1Public,
15 walletFromMnemonic,
16 walletFromMnemonic,
16} from 'lib/wallet';
17} from 'lib/wallet';
18+import { getAuthToken } from 'lib/authToken';
17import { BRIDGE_ERROR } from 'lib/error';
19import { BRIDGE_ERROR } from 'lib/error';
18
20
21+import { useNetwork } from 'store/network';
22+
19export const WalletContext = createContext(null);
23export const WalletContext = createContext(null);
20
24
21const DEFAULT_WALLET_TYPE = WALLET_TYPES.TICKET;
25const DEFAULT_WALLET_TYPE = WALLET_TYPES.TICKET;
@@ -46,6 +50,26 @@ function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
46 const [networkSeed, setNetworkSeed] = useState(Nothing());
50 const [networkSeed, setNetworkSeed] = useState(Nothing());
47 const [networkRevision, setNetworkRevision] = useState(Nothing());
51 const [networkRevision, setNetworkRevision] = useState(Nothing());
48
52
53+ const [authToken, setAuthToken] = useState(Nothing());
54+
55+ const { web3 } = useNetwork();
56+
57+ useEffect(() => {
58+ if (!Just.hasInstance(wallet) || !Just.hasInstance(web3)) {
59+ return;
60+ }
61+ const _wallet = wallet.value;
62+ const _web3 = web3.value;
63+ const token = getAuthToken({
64+ wallet: _wallet,
65+ walletType,
66+ walletHdPath,
67+ web3: _web3,
68+ });
69+
70+ setAuthToken(token);
71+ }, [wallet, walletType, walletHdPath, web3]);
72+
49 const setWalletType = useCallback(
73 const setWalletType = useCallback(
50 walletType => {
74 walletType => {
51 if (!includes(WALLET_TYPES, walletType)) {
75 if (!includes(WALLET_TYPES, walletType)) {