UNPKG

1.3 kBJavaScriptView Raw
1import { ed25519 } from '@noble/curves/ed25519';
2import { hasBigInt, u8aToU8a } from '@polkadot/util';
3import { ed25519Verify as wasmVerify, isReady } from '@polkadot/wasm-crypto';
4/**
5 * @name ed25519Sign
6 * @summary Verifies the signature on the supplied message.
7 * @description
8 * Verifies the `signature` on `message` with the supplied `publicKey`. Returns `true` on sucess, `false` otherwise.
9 * @example
10 * <BR>
11 *
12 * ```javascript
13 * import { ed25519Verify } from '@polkadot/util-crypto';
14 *
15 * ed25519Verify([...], [...], [...]); // => true/false
16 * ```
17 */
18export function ed25519Verify(message, signature, publicKey, onlyJs) {
19 const messageU8a = u8aToU8a(message);
20 const publicKeyU8a = u8aToU8a(publicKey);
21 const signatureU8a = u8aToU8a(signature);
22 if (publicKeyU8a.length !== 32) {
23 throw new Error(`Invalid publicKey, received ${publicKeyU8a.length}, expected 32`);
24 }
25 else if (signatureU8a.length !== 64) {
26 throw new Error(`Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
27 }
28 try {
29 return !hasBigInt || (!onlyJs && isReady())
30 ? wasmVerify(signatureU8a, messageU8a, publicKeyU8a)
31 : ed25519.verify(signatureU8a, messageU8a, publicKeyU8a);
32 }
33 catch {
34 return false;
35 }
36}