1 | import { u8aConcat } from '@polkadot/util';
|
2 | import { naclEncrypt, scryptEncode, scryptToU8a } from '@polkadot/util-crypto';
|
3 | import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults.js';
|
4 | export function encodePair({ publicKey, secretKey }, passphrase) {
|
5 | if (!secretKey) {
|
6 | throw new Error('Expected a valid secretKey to be passed to encode');
|
7 | }
|
8 | const encoded = u8aConcat(PKCS8_HEADER, secretKey, PKCS8_DIVIDER, publicKey);
|
9 | if (!passphrase) {
|
10 | return encoded;
|
11 | }
|
12 | const { params, password, salt } = scryptEncode(passphrase);
|
13 | const { encrypted, nonce } = naclEncrypt(encoded, password.subarray(0, 32));
|
14 | return u8aConcat(scryptToU8a(salt, params), nonce, encrypted);
|
15 | }
|