UNPKG

981 BJavaScriptView Raw
1import { ed25519 } from '@noble/curves/ed25519';
2import { hasBigInt, u8aToU8a } from '@polkadot/util';
3import { ed25519Sign as wasmSign, isReady } from '@polkadot/wasm-crypto';
4/**
5 * @name ed25519Sign
6 * @summary Signs a message using the supplied secretKey
7 * @description
8 * Returns message signature of `message`, using the `secretKey`.
9 * @example
10 * <BR>
11 *
12 * ```javascript
13 * import { ed25519Sign } from '@polkadot/util-crypto';
14 *
15 * ed25519Sign([...], [...]); // => [...]
16 * ```
17 */
18export function ed25519Sign(message, { publicKey, secretKey }, onlyJs) {
19 if (!secretKey) {
20 throw new Error('Expected a valid secretKey');
21 }
22 else if (!publicKey) {
23 throw new Error('Expected a valid publicKey');
24 }
25 const messageU8a = u8aToU8a(message);
26 const privateU8a = secretKey.subarray(0, 32);
27 return !hasBigInt || (!onlyJs && isReady())
28 ? wasmSign(publicKey, privateU8a, messageU8a)
29 : ed25519.sign(messageU8a, privateU8a);
30}