UNPKG

901 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util-crypto authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import nacl from 'tweetnacl';
4import { u8aToU8a } from '@polkadot/util';
5import { ed25519Sign as wasmSign, isReady } from '@polkadot/wasm-crypto';
6/**
7 * @name ed25519Sign
8 * @summary Signs a message using the supplied secretKey
9 * @description
10 * Returns message signature of `message`, using the `secretKey`.
11 * @example
12 * <BR>
13 *
14 * ```javascript
15 * import { ed25519Sign } from '@polkadot/util-crypto';
16 *
17 * ed25519Sign([...], [...]); // => [...]
18 * ```
19 */
20
21export function ed25519Sign(message, {
22 publicKey,
23 secretKey
24}, onlyJs) {
25 if (!secretKey) {
26 throw new Error('Expected a valid secretKey');
27 }
28
29 const messageU8a = u8aToU8a(message);
30 return !onlyJs && isReady() ? wasmSign(publicKey, secretKey.subarray(0, 32), messageU8a) : nacl.sign.detached(messageU8a, secretKey);
31}
\No newline at end of file