Skip to content

Commit

Permalink
fix: msgHash length fix in signature verify function
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshuchawla009 committed Nov 12, 2021
1 parent f2b26fe commit 589b126
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 22 additions & 1 deletion __tests__/ellipticalCurve.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { getKeyPair, getStarkKey, hashCalldata, hashMessage, pedersen, sign } from '../src';
import {
ec,
getKeyPair,
getStarkKey,
hashCalldata,
hashMessage,
pedersen,
sign,
verify,
} from '../src';
import { removeHexPrefix } from '../src/utils/encode';
import { toBN, toHex } from '../src/utils/number';

Expand Down Expand Up @@ -47,3 +56,15 @@ test('hashMessage()', () => {
toBN('2362979021721299440845279407227912881357338080403308888611869245024056250189').toString()
);
});

test('verify signed message()', () => {
const pk = '0x019800ea6a9a73f94aee6a3d2edf018fc770443e90c7ba121e8303ec6b349279';
const account = '0x33f45f07e1bd1a51b45fc24ec8c8c9908db9e42191be9e169bfcac0c0d99745';
const price = '1';
const hashMsg = pedersen([account, price]);
const keyPair = getKeyPair(pk);
const signature = sign(keyPair, removeHexPrefix(hashMsg));
const pubKey = keyPair.getPublic('hex');
const pubKeyPair = ec.keyFromPublic(pubKey, 'hex');
expect(verify(pubKeyPair, removeHexPrefix(hashMsg), signature)).toBe(true);
});
2 changes: 1 addition & 1 deletion src/ellipticalCurve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ export function verify(keyPair: KeyPair, msgHash: string, sig: Signature): boole
assertInRange(s, ONE, toBN(addHexPrefix(EC_ORDER)), 's');
assertInRange(w, ONE, toBN(addHexPrefix(MAX_ECDSA_VAL)), 'w');

return keyPair.verify(msgHash, sig);
return keyPair.verify(fixMessage(msgHash), sig);
}

0 comments on commit 589b126

Please sign in to comment.