1 | import { ed25519 } from '@noble/curves/ed25519';
|
2 | import { hasBigInt, u8aToU8a } from '@polkadot/util';
|
3 | import { ed25519Verify as wasmVerify, isReady } from '@polkadot/wasm-crypto';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export 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 | }
|