on master help sign in
login: toggle auth token Legacy Compatibility
0b34ca9tomholford
src/lib/authToken.ts+11−4
@@ -1,3 +1,4 @@
1+import { crypto } from 'bitcoinjs-lib';
1import { ecdsaSign } from 'secp256k1';
2import { ecdsaSign } from 'secp256k1';
2import Web3 from 'web3';
3import Web3 from 'web3';
3import { hexToBytes } from 'web3-utils';
4import { hexToBytes } from 'web3-utils';
@@ -12,9 +13,11 @@ import { keccak256 } from 'ethereumjs-util';
12
13
13const MESSAGE = 'Bridge Authentication Token';
14const MESSAGE = 'Bridge Authentication Token';
14
15
15−function signMessage(privateKey: Buffer) {
16+function signMessage(privateKey: Buffer, useLegacyTokenSigning = false) {
16 const msg = '\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE;
17 const msg = '\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE;
18+ const hashed = useLegacyTokenSigning
19+ ? crypto.sha256(Buffer.from(msg))
17− const hashed = keccak256(Buffer.from(msg));
20+ : keccak256(Buffer.from(msg));
18 const { signature } = ecdsaSign(Buffer.from(hashed), privateKey);
21 const { signature } = ecdsaSign(Buffer.from(hashed), privateKey);
19
22
20 // add key recovery parameter
23 // add key recovery parameter
@@ -60,6 +63,7 @@ type WalletConnectAuthTokenArgs = {
60type DefaultAuthTokenArgs = {
63type DefaultAuthTokenArgs = {
61 wallet: BridgeWallet;
64 wallet: BridgeWallet;
62 walletType?: symbol;
65 walletType?: symbol;
66+ useLegacyTokenSigning?: boolean;
63};
67};
64
68
65type GetAuthTokenArgs =
69type GetAuthTokenArgs =
@@ -100,8 +104,11 @@ const getWalletConnectAuthToken = ({
100 return connector.signPersonalMessage([MESSAGE, address]);
104 return connector.signPersonalMessage([MESSAGE, address]);
101};
105};
102
106
103−const getDefaultAuthToken = ({ wallet }: DefaultAuthTokenArgs) => {
107+const getDefaultAuthToken = ({
108+ wallet,
109+ useLegacyTokenSigning = false,
110+}: DefaultAuthTokenArgs) => {
104− const signature = signMessage(wallet.privateKey!);
111+ const signature = signMessage(wallet.privateKey!, useLegacyTokenSigning);
105
112
106 const token = `0x${Buffer.from(signature).toString('hex')}`;
113 const token = `0x${Buffer.from(signature).toString('hex')}`;
107
114
src/store/wallet.js+10−1
@@ -53,6 +53,11 @@ function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
53
53
54 const [authToken, setAuthToken] = useState(Nothing());
54 const [authToken, setAuthToken] = useState(Nothing());
55
55
56+ // See: https://github.com/urbit/bridge/issues/549#issuecomment-1048359617
57+ // This is used for legacy compatibility; this flow should eventually be
58+ // be removed after ~1 year.
59+ const [useLegacyTokenSigning, setUseLegacyTokenSigning] = useState(false);
60+
56 const { web3 } = useNetwork();
61 const { web3 } = useNetwork();
57
62
58 useEffect(() => {
63 useEffect(() => {
@@ -72,11 +77,12 @@ function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
72 walletType,
77 walletType,
73 walletHdPath,
78 walletHdPath,
74 web3: _web3,
79 web3: _web3,
80+ useLegacyTokenSigning,
75 });
81 });
76
82
77 setAuthToken(Just(token));
83 setAuthToken(Just(token));
78 })();
84 })();
79− }, [wallet, walletType, walletHdPath, web3]);
85+ }, [wallet, walletType, walletHdPath, web3, useLegacyTokenSigning]);
80
86
81 const setWalletType = useCallback(
87 const setWalletType = useCallback(
82 walletType => {
88 walletType => {
@@ -132,6 +138,7 @@ function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
132 setAuthMnemonic(Nothing());
138 setAuthMnemonic(Nothing());
133 setNetworkSeed(Nothing());
139 setNetworkSeed(Nothing());
134 setNetworkRevision(Nothing());
140 setNetworkRevision(Nothing());
141+ setUseLegacyTokenSigning(false);
135 }, [
142 }, [
136 _setWalletType,
143 _setWalletType,
137 setWalletHdPath,
144 setWalletHdPath,
@@ -167,6 +174,8 @@ function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
167 //
174 //
168 authToken,
175 authToken,
169 setAuthToken,
176 setAuthToken,
177+ useLegacyTokenSigning,
178+ setUseLegacyTokenSigning,
170 };
179 };
171}
180}
172
181
src/views/Login/Mnemonic.tsx+13−1
@@ -35,7 +35,13 @@ interface MnemonicProps {
35export default function Mnemonic({ className, goHome }: MnemonicProps) {
35export default function Mnemonic({ className, goHome }: MnemonicProps) {
36 useLoginView(WALLET_TYPES.MNEMONIC);
36 useLoginView(WALLET_TYPES.MNEMONIC);
37
37
38+ const {
39+ setWallet,
38− const { setWallet, setAuthMnemonic, setWalletHdPath }: any = useWallet();
40+ setAuthMnemonic,
41+ setWalletHdPath,
42+ setUseLegacyTokenSigning,
43+ useLegacyTokenSigning,
44+ }: any = useWallet();
39
45
40 const [skipValidation, setSkipValidation] = useState(false);
46 const [skipValidation, setSkipValidation] = useState(false);
41 const [phraseHdPath, setPhraseHdPath] = useState(false);
47 const [phraseHdPath, setPhraseHdPath] = useState(false);
@@ -96,6 +102,12 @@ export default function Mnemonic({ className, goHome }: MnemonicProps) {
96 label: 'Passphrase & HD Path',
102 label: 'Passphrase & HD Path',
97 onClick: () => setPhraseHdPath(!phraseHdPath),
103 onClick: () => setPhraseHdPath(!phraseHdPath),
98 },
104 },
105+ {
106+ selected: useLegacyTokenSigning,
107+ key: 'useLegacyTokenSigning',
108+ label: 'Use Legacy Compatibility',
109+ onClick: () => setUseLegacyTokenSigning(!useLegacyTokenSigning),
110+ },
99 ];
111 ];
100
112
101 return (
113 return (
src/views/Login/Ticket.tsx+11−1
@@ -43,7 +43,11 @@ interface TicketProps {
43export default function Ticket({ className, goHome }: TicketProps) {
43export default function Ticket({ className, goHome }: TicketProps) {
44 useLoginView(WALLET_TYPES.TICKET);
44 useLoginView(WALLET_TYPES.TICKET);
45
45
46+ const {
46− const { setUrbitWallet }: any = useWallet();
47+ setUrbitWallet,
48+ setUseLegacyTokenSigning,
49+ useLegacyTokenSigning,
50+ }: any = useWallet();
47 const { setPointCursor }: any = usePointCursor();
51 const { setPointCursor }: any = usePointCursor();
48 const impliedPoint = useImpliedPoint();
52 const impliedPoint = useImpliedPoint();
49 const didWarn = useRef(false);
53 const didWarn = useRef(false);
@@ -172,6 +176,12 @@ export default function Ticket({ className, goHome }: TicketProps) {
172 label: 'Shards',
176 label: 'Shards',
173 onClick: () => setUseShards(!useShards),
177 onClick: () => setUseShards(!useShards),
174 },
178 },
179+ {
180+ selected: useLegacyTokenSigning,
181+ key: 'useLegacyTokenSigning',
182+ label: 'Use Legacy Compatibility',
183+ onClick: () => setUseLegacyTokenSigning(!useLegacyTokenSigning),
184+ },
175 ];
185 ];
176
186
177 const ticketInputClass = usePassphrase && useShards ? 'ticket-input' : '';
187 const ticketInputClass = usePassphrase && useShards ? 'ticket-input' : '';