mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-07-20 13:25:42 +00:00
31 lines
978 B
JavaScript
31 lines
978 B
JavaScript
import { ed25519 } from '@noble/curves/ed25519';
|
|
import { hasBigInt, u8aToU8a } from '@pezkuwi/util';
|
|
import { ed25519Sign as wasmSign, isReady } from '@pezkuwi/wasm-crypto';
|
|
/**
|
|
* @name ed25519Sign
|
|
* @summary Signs a message using the supplied secretKey
|
|
* @description
|
|
* Returns message signature of `message`, using the `secretKey`.
|
|
* @example
|
|
* <BR>
|
|
*
|
|
* ```javascript
|
|
* import { ed25519Sign } from '@pezkuwi/util-crypto';
|
|
*
|
|
* ed25519Sign([...], [...]); // => [...]
|
|
* ```
|
|
*/
|
|
export function ed25519Sign(message, { publicKey, secretKey }, onlyJs) {
|
|
if (!secretKey) {
|
|
throw new Error('Expected a valid secretKey');
|
|
}
|
|
else if (!publicKey) {
|
|
throw new Error('Expected a valid publicKey');
|
|
}
|
|
const messageU8a = u8aToU8a(message);
|
|
const privateU8a = secretKey.subarray(0, 32);
|
|
return !hasBigInt || (!onlyJs && isReady())
|
|
? wasmSign(publicKey, privateU8a, messageU8a)
|
|
: ed25519.sign(messageU8a, privateU8a);
|
|
}
|