UNPKG

1.26 kBJavaScriptView Raw
1// Copyright 2017-2023 @polkadot/util-crypto authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3
4import nacl from 'tweetnacl';
5import { u8aToU8a } from '@polkadot/util';
6import { ed25519Verify as wasmVerify, isReady } from '@polkadot/wasm-crypto';
7
8/**
9 * @name ed25519Sign
10 * @summary Verifies the signature on the supplied message.
11 * @description
12 * Verifies the `signature` on `message` with the supplied `publicKey`. Returns `true` on sucess, `false` otherwise.
13 * @example
14 * <BR>
15 *
16 * ```javascript
17 * import { ed25519Verify } from '@polkadot/util-crypto';
18 *
19 * ed25519Verify([...], [...], [...]); // => true/false
20 * ```
21 */
22export function ed25519Verify(message, signature, publicKey, onlyJs) {
23 const messageU8a = u8aToU8a(message);
24 const publicKeyU8a = u8aToU8a(publicKey);
25 const signatureU8a = u8aToU8a(signature);
26 if (publicKeyU8a.length !== 32) {
27 throw new Error(`Invalid publicKey, received ${publicKeyU8a.length}, expected 32`);
28 } else if (signatureU8a.length !== 64) {
29 throw new Error(`Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
30 }
31 return !onlyJs && isReady() ? wasmVerify(signatureU8a, messageU8a, publicKeyU8a) : nacl.sign.detached.verify(messageU8a, signatureU8a, publicKeyU8a);
32}
\No newline at end of file