on master help sign in
login: toggle auth token Legacy Compatibility

For custodial login flows (Ticket and Mnemonic), this change adds an additional option checkbox that opts the user in to deriving token using the legacy technique.

commit0b34ca9cf7e109dbff37ee530e9209e22023611b authortomholford <tomholford@users.noreply.github.com> dateWed 23 Feb 2022, 12:18 (5 years ago) parent2fec545
4 files changed+45−7
src/lib/authToken.ts+11−4 src/store/wallet.js+10−1 src/views/Login/Mnemonic.tsx+13−1 src/views/Login/Ticket.tsx+11−1
src/lib/authToken.ts+11−4
@@ -1,3 +1,4 @@
1+import { crypto } from 'bitcoinjs-lib';
12import { ecdsaSign } from 'secp256k1';
23import Web3 from 'web3';
34import { hexToBytes } from 'web3-utils';
@@ -12,9 +13,11 @@ import { keccak256 } from 'ethereumjs-util';
1213
1314const MESSAGE = 'Bridge Authentication Token';
1415
15−function signMessage(privateKey: Buffer) {
16+function signMessage(privateKey: Buffer, useLegacyTokenSigning = false) {
1617 const msg = '\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE;
17− const hashed = keccak256(Buffer.from(msg));
18+ const hashed = useLegacyTokenSigning
19+ ? crypto.sha256(Buffer.from(msg))
20+ : keccak256(Buffer.from(msg));
1821 const { signature } = ecdsaSign(Buffer.from(hashed), privateKey);
1922
2023 // add key recovery parameter
@@ -60,6 +63,7 @@ type WalletConnectAuthTokenArgs = {
6063type DefaultAuthTokenArgs = {
6164 wallet: BridgeWallet;
6265 walletType?: symbol;
66+ useLegacyTokenSigning?: boolean;
6367};
6468
6569type GetAuthTokenArgs =
@@ -100,8 +104,11 @@ const getWalletConnectAuthToken = ({
100104 return connector.signPersonalMessage([MESSAGE, address]);
101105};
102106
103−const getDefaultAuthToken = ({ wallet }: DefaultAuthTokenArgs) => {
104− const signature = signMessage(wallet.privateKey!);
107+const getDefaultAuthToken = ({
108+ wallet,
109+ useLegacyTokenSigning = false,
110+}: DefaultAuthTokenArgs) => {
111+ const signature = signMessage(wallet.privateKey!, useLegacyTokenSigning);
105112
106113 const token = `0x${Buffer.from(signature).toString('hex')}`;
107114
src/store/wallet.js+10−1
@@ -53,6 +53,11 @@ function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
5353
5454 const [authToken, setAuthToken] = useState(Nothing());
5555
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+
5661 const { web3 } = useNetwork();
5762
5863 useEffect(() => {
@@ -72,11 +77,12 @@ function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
7277 walletType,
7378 walletHdPath,
7479 web3: _web3,
80+ useLegacyTokenSigning,
7581 });
7682
7783 setAuthToken(Just(token));
7884 })();
79− }, [wallet, walletType, walletHdPath, web3]);
85+ }, [wallet, walletType, walletHdPath, web3, useLegacyTokenSigning]);
8086
8187 const setWalletType = useCallback(
8288 walletType => {
@@ -132,6 +138,7 @@ function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
132138 setAuthMnemonic(Nothing());
133139 setNetworkSeed(Nothing());
134140 setNetworkRevision(Nothing());
141+ setUseLegacyTokenSigning(false);
135142 }, [
136143 _setWalletType,
137144 setWalletHdPath,
@@ -167,6 +174,8 @@ function _useWallet(initialWallet = Nothing(), initialMnemonic = Nothing()) {
167174 //
168175 authToken,
169176 setAuthToken,
177+ useLegacyTokenSigning,
178+ setUseLegacyTokenSigning,
170179 };
171180}
172181
src/views/Login/Mnemonic.tsx+13−1
@@ -35,7 +35,13 @@ interface MnemonicProps {
3535export default function Mnemonic({ className, goHome }: MnemonicProps) {
3636 useLoginView(WALLET_TYPES.MNEMONIC);
3737
38− const { setWallet, setAuthMnemonic, setWalletHdPath }: any = useWallet();
38+ const {
39+ setWallet,
40+ setAuthMnemonic,
41+ setWalletHdPath,
42+ setUseLegacyTokenSigning,
43+ useLegacyTokenSigning,
44+ }: any = useWallet();
3945
4046 const [skipValidation, setSkipValidation] = useState(false);
4147 const [phraseHdPath, setPhraseHdPath] = useState(false);
@@ -96,6 +102,12 @@ export default function Mnemonic({ className, goHome }: MnemonicProps) {
96102 label: 'Passphrase & HD Path',
97103 onClick: () => setPhraseHdPath(!phraseHdPath),
98104 },
105+ {
106+ selected: useLegacyTokenSigning,
107+ key: 'useLegacyTokenSigning',
108+ label: 'Use Legacy Compatibility',
109+ onClick: () => setUseLegacyTokenSigning(!useLegacyTokenSigning),
110+ },
99111 ];
100112
101113 return (
src/views/Login/Ticket.tsx+11−1
@@ -43,7 +43,11 @@ interface TicketProps {
4343export default function Ticket({ className, goHome }: TicketProps) {
4444 useLoginView(WALLET_TYPES.TICKET);
4545
46− const { setUrbitWallet }: any = useWallet();
46+ const {
47+ setUrbitWallet,
48+ setUseLegacyTokenSigning,
49+ useLegacyTokenSigning,
50+ }: any = useWallet();
4751 const { setPointCursor }: any = usePointCursor();
4852 const impliedPoint = useImpliedPoint();
4953 const didWarn = useRef(false);
@@ -172,6 +176,12 @@ export default function Ticket({ className, goHome }: TicketProps) {
172176 label: 'Shards',
173177 onClick: () => setUseShards(!useShards),
174178 },
179+ {
180+ selected: useLegacyTokenSigning,
181+ key: 'useLegacyTokenSigning',
182+ label: 'Use Legacy Compatibility',
183+ onClick: () => setUseLegacyTokenSigning(!useLegacyTokenSigning),
184+ },
175185 ];
176186
177187 const ticketInputClass = usePassphrase && useShards ? 'ticket-input' : '';