UNPKG

932 BJavaScriptView Raw
1import { u8aEq, u8aToU8a } from '@polkadot/util';
2import { hasher } from './hasher.js';
3import { secp256k1Recover } from './recover.js';
4/**
5 * @name secp256k1Verify
6 * @description Verifies the signature of `message`, using the supplied pair
7 */
8export 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 // for Ethereum (keccak) the last 20 bytes is the address
17 return u8aEq(publicKey, inputAddr) || (hashType === 'keccak'
18 ? u8aEq(signerAddr.slice(-20), inputAddr.slice(-20))
19 : u8aEq(signerAddr, inputAddr));
20}