UNPKG

786 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util-crypto authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { u8aToU8a } from '@polkadot/util';
4import { sr25519Agree } from '@polkadot/wasm-crypto';
5/**
6 * @name sr25519Agreement
7 * @description Key agreement between other's public key and self secret key
8 */
9
10export function sr25519Agreement(secretKey, publicKey) {
11 const secretKeyU8a = u8aToU8a(secretKey);
12 const publicKeyU8a = u8aToU8a(publicKey);
13
14 if (publicKeyU8a.length !== 32) {
15 throw new Error(`Invalid publicKey, received ${publicKeyU8a.length} bytes, expected 32`);
16 } else if (secretKeyU8a.length !== 64) {
17 throw new Error(`Invalid secretKey, received ${secretKeyU8a.length} bytes, expected 64`);
18 }
19
20 return sr25519Agree(publicKeyU8a, secretKeyU8a);
21}
\No newline at end of file