on master help sign in
crypto: sign messages with sha256

Use sha256 instead of keccak256 to ensure 32-byte output for ecdsaSign

commit003c08396edb548527e8ffd56218fca90d7f2d8b authortomholford <tomholford@users.noreply.github.com> dateMon 28 Jun 2021, 22:24 (5 years ago) parent100b34b
1 file changed+4−8
src/lib/authToken.ts+4−8
src/lib/authToken.ts+4−8
@@ -1,5 +1,5 @@
1+import { crypto } from 'bitcoinjs-lib';
12import { ecdsaSign } from 'secp256k1';
2−import { keccak256 } from 'web3-utils';
33
44import { WALLET_TYPES } from './constants';
55import { ledgerSignMessage } from './ledger';
@@ -9,13 +9,9 @@ const MESSAGE = 'Bridge Authentication Token';
99
1010function signMessage(privateKey: Buffer) {
1111 const msg = '\x19Ethereum Signed Message:\n' + MESSAGE.length + MESSAGE;
12− // TODO: Signing function expects 32 length Buffer,
13− // confirm if it is okay / expected to slice it like this?
14− // Based on reading docs and comment threads, it seems that
15− // this limit is sometimes enforced, and varies depending on
16− // the implementation
17− const msgHash = Buffer.from(keccak256(msg)).slice(0, 32);
18− const { signature } = ecdsaSign(msgHash, privateKey);
12+ // #ecdsaSign requires a 32-byte buffer, hence sha256
13+ const hashed = crypto.sha256(Buffer.from(msg));
14+ const { signature } = ecdsaSign(Buffer.from(hashed), privateKey);
1915
2016 // add key recovery parameter
2117 const ethSignature = new Uint8Array(65);