37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
// Copyright 2017-2021 @polkadot/util-crypto authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { HexString } from '@polkadot/util/types';
|
|
|
|
import nacl from 'tweetnacl';
|
|
|
|
import { assert, u8aToU8a } from '@polkadot/util';
|
|
import { ed25519Verify as wasmVerify, isReady } from '@polkadot/wasm-crypto';
|
|
|
|
/**
|
|
* @name ed25519Sign
|
|
* @summary Verifies the signature on the supplied message.
|
|
* @description
|
|
* Verifies the `signature` on `message` with the supplied `publicKey`. Returns `true` on sucess, `false` otherwise.
|
|
* @example
|
|
* <BR>
|
|
*
|
|
* ```javascript
|
|
* import { ed25519Verify } from '@polkadot/util-crypto';
|
|
*
|
|
* ed25519Verify([...], [...], [...]); // => true/false
|
|
* ```
|
|
*/
|
|
export function ed25519Verify (message: HexString | Uint8Array | string, signature: HexString | Uint8Array | string, publicKey: HexString | Uint8Array | string, onlyJs?: boolean): boolean {
|
|
const messageU8a = u8aToU8a(message);
|
|
const publicKeyU8a = u8aToU8a(publicKey);
|
|
const signatureU8a = u8aToU8a(signature);
|
|
|
|
assert(publicKeyU8a.length === 32, () => `Invalid publicKey, received ${publicKeyU8a.length}, expected 32`);
|
|
assert(signatureU8a.length === 64, () => `Invalid signature, received ${signatureU8a.length} bytes, expected 64`);
|
|
|
|
return !onlyJs && isReady()
|
|
? wasmVerify(signatureU8a, messageU8a, publicKeyU8a)
|
|
: nacl.sign.detached.verify(messageU8a, signatureU8a, publicKeyU8a);
|
|
}
|