1 | import { u8aEq, u8aToU8a } from '@polkadot/util';
|
2 | import { hasher } from './hasher.js';
|
3 | import { secp256k1Recover } from './recover.js';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export function secp256k1Verify(msgHash, signature, address, hashType = 'blake2', onlyJs) {
|
9 | const sig = u8aToU8a(signature);
|
10 | if (sig.length !== 65) {
|
11 | throw new Error(`Expected signature with 65 bytes, ${sig.length} found instead`);
|
12 | }
|
13 | const publicKey = secp256k1Recover(hasher(hashType, msgHash), sig, sig[64], hashType, onlyJs);
|
14 | const signerAddr = hasher(hashType, publicKey, onlyJs);
|
15 | const inputAddr = u8aToU8a(address);
|
16 |
|
17 | return u8aEq(publicKey, inputAddr) || (hashType === 'keccak'
|
18 | ? u8aEq(signerAddr.slice(-20), inputAddr.slice(-20))
|
19 | : u8aEq(signerAddr, inputAddr));
|
20 | }
|