@@ -3,11 +3,10 @@ import { ecdsaSign } from 'secp256k1';
33import Web3 from 'web3';
44import { hexToBytes } from 'web3-utils';
55import WalletConnect from '@walletconnect/client';
6−
6+import { hashPersonalMessage } from '@ethereumjs/util';
77import { WALLET_TYPES } from './constants';
88import BridgeWallet from './types/BridgeWallet';
99import { Hash } from '@urbit/roller-api';
10−import { keccak256 } from 'ethereumjs-util';
1110
1211const MESSAGE = 'Bridge Authentication Token';
1312
@@ -15,12 +14,22 @@ function signMessage(privateKey: Buffer, useLegacyTokenSigning = false) {
1514 const msg = '\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE;
1615 const hashed = useLegacyTokenSigning
1716 ? crypto.sha256(Buffer.from(msg))
18− : keccak256(Buffer.from(msg));
19− const { signature } = ecdsaSign(Buffer.from(hashed), privateKey);
17+ : hashPersonalMessage(Buffer.from(MESSAGE)) // msg prefix is handled by lib
18+
19+ const hashAry = new Uint8Array(hashed.buffer);
20+ const { signature, recid: _recid } = ecdsaSign(hashAry, privateKey);
2021
2122 // add key recovery parameter
2223 const ethSignature = new Uint8Array(65);
2324 ethSignature.set(signature);
25+
26+ // https://eips.ethereum.org/EIPS/eip-155
27+ // This method for setting the recovery parameter is the "legacy" way of doing
28+ // so; and in fact, does not match up with output of Metamask / Brave Wallet.
29+ // MM uses 28 instead of 27.
30+ // TODO: Should we bring this up to latest standard? The problem is, users
31+ // who have set network keys or generated invites with an older token will
32+ // not be able to rederive them
2433 const v = (ethSignature[32] & 1) + 27;
2534 ethSignature[64] = v;
2635