on master help sign in
deps: upgrade @ethereumjs/utils

also: - simplify signMessage - add note on why tokens generated with MM vs seed phrase do not tie out

commit08b1cbf4507879611fde84fd23b169e526a05e01 authortomholford <tomholford@users.noreply.github.com> dateTue 18 Oct 2022, 20:29 (4 years ago) parent2268655
5 files changed+22−10
package-lock.json+5−2 package.json+1−1 src/lib/authToken.ts+13−4 src/lib/utils/address.ts+2−2 src/views/Release/TransferForm.tsx+1−1
package-lock.json+5−2
@@ -11,6 +11,7 @@
1111 "dependencies": {
1212 "@ethereumjs/common": "^3.0.0",
1313 "@ethereumjs/tx": "^4.0.0",
14+ "@ethereumjs/util": "^8.0.0",
1415 "@ledgerhq/hw-transport-u2f": "^5.36.0-deprecated",
1516 "@metamask/detect-provider": "^1.2.0",
1617 "@reach/disclosure": "^0.15.0",
@@ -47,7 +48,6 @@
4748 "copy-to-clipboard": "^3.2.0",
4849 "date-fns": "^2.25.0",
4950 "ethereum-blockies-base64": "^1.0.2",
50− "ethereumjs-util": "^7.1.4",
5151 "file-saver": "^2.0.0",
5252 "final-form": "^4.18.4",
5353 "final-form-arrays": "^3.0.1",
@@ -1145,7 +1145,8 @@
11451145 },
11461146 "node_modules/@ethereumjs/util": {
11471147 "version": "8.0.0",
1148− "license": "MPL-2.0",
1148+ "resolved": "https://registry.npmjs.org/@ethereumjs/util/-/util-8.0.0.tgz",
1149+ "integrity": "sha512-Zz/Ovar0VU/CaYHLvigBTjG5PmkG0OzjvRjjoXRJbJcdWJoTCN2N4BYgoCBOZomKjaAT4ABpQ7wer6ZFOglvjg==",
11491150 "dependencies": {
11501151 "@ethereumjs/rlp": "^4.0.0-beta.2",
11511152 "ethereum-cryptography": "^1.1.2"
@@ -14248,6 +14249,8 @@
1424814249 },
1424914250 "@ethereumjs/util": {
1425014251 "version": "8.0.0",
14252+ "resolved": "https://registry.npmjs.org/@ethereumjs/util/-/util-8.0.0.tgz",
14253+ "integrity": "sha512-Zz/Ovar0VU/CaYHLvigBTjG5PmkG0OzjvRjjoXRJbJcdWJoTCN2N4BYgoCBOZomKjaAT4ABpQ7wer6ZFOglvjg==",
1425114254 "requires": {
1425214255 "@ethereumjs/rlp": "^4.0.0-beta.2",
1425314256 "ethereum-cryptography": "^1.1.2"
package.json+1−1
@@ -17,6 +17,7 @@
1717 "dependencies": {
1818 "@ethereumjs/common": "^3.0.0",
1919 "@ethereumjs/tx": "^4.0.0",
20+ "@ethereumjs/util": "^8.0.0",
2021 "@ledgerhq/hw-transport-u2f": "^5.36.0-deprecated",
2122 "@metamask/detect-provider": "^1.2.0",
2223 "@reach/disclosure": "^0.15.0",
@@ -53,7 +54,6 @@
5354 "copy-to-clipboard": "^3.2.0",
5455 "date-fns": "^2.25.0",
5556 "ethereum-blockies-base64": "^1.0.2",
56− "ethereumjs-util": "^7.1.4",
5757 "file-saver": "^2.0.0",
5858 "final-form": "^4.18.4",
5959 "final-form-arrays": "^3.0.1",
src/lib/authToken.ts+13−4
@@ -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
src/lib/utils/address.ts+2−2
@@ -1,5 +1,5 @@
11import { isAddress, toChecksumAddress } from 'web3-utils';
2−import { pubToAddress } from 'ethereumjs-util';
2+import { pubToAddress } from '@ethereumjs/util';
33import { ETH_ZERO_ADDR, ETH_ZERO_ADDR_SHORT } from 'lib/constants';
44
55export const stripHexPrefix = (hex: string) => {
@@ -30,5 +30,5 @@ export const eqAddr = (addr0: string, addr1: string) =>
3030 : toChecksumAddress(addr0) === toChecksumAddress(addr1);
3131
3232export const publicToAddress = (publicKey: Buffer): string => {
33− return '0x' + pubToAddress(Buffer.from(publicKey), true).toString('hex');
33+ return '0x' + pubToAddress(publicKey, true).toString('hex');
3434};
src/views/Release/TransferForm.tsx+1−1
@@ -14,7 +14,7 @@ import { useStarReleaseCache } from 'store/starRelease';
1414import { useWallet } from 'store/wallet';
1515import { eqAddr, isZeroAddress } from 'lib/utils/address';
1616import { useTransferLockup } from './useTransferLockup';
17−import { isValidAddress } from 'ethereumjs-util';
17+import { isValidAddress } from '@ethereumjs/util';
1818import { getLockupKind } from 'lib/starRelease';
1919
2020interface TransferFormProps {