1 | import { secp256k1 } from '@noble/curves/secp256k1';
|
2 | import { bnToU8a, hasBigInt, u8aConcat } from '@polkadot/util';
|
3 | import { isReady, secp256k1Sign as wasm } from '@polkadot/wasm-crypto';
|
4 | import { BN_BE_256_OPTS } from '../bn.js';
|
5 | import { hasher } from './hasher.js';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export function secp256k1Sign(message, { secretKey }, hashType = 'blake2', onlyJs) {
|
11 | if (secretKey?.length !== 32) {
|
12 | throw new Error('Expected valid secp256k1 secretKey, 32-bytes');
|
13 | }
|
14 | const data = hasher(hashType, message, onlyJs);
|
15 | if (!hasBigInt || (!onlyJs && isReady())) {
|
16 | return wasm(data, secretKey);
|
17 | }
|
18 | const signature = secp256k1.sign(data, secretKey, { lowS: true });
|
19 | return u8aConcat(bnToU8a(signature.r, BN_BE_256_OPTS), bnToU8a(signature.s, BN_BE_256_OPTS), new Uint8Array([signature.recovery || 0]));
|
20 | }
|