-
Notifications
You must be signed in to change notification settings - Fork 139
/
encode.ts
29 lines (21 loc) · 892 Bytes
/
encode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Copyright 2017-2019 @polkadot/keyring authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { PairInfo } from './types';
import { stringToU8a, u8aConcat, u8aFixLength, assert } from '@polkadot/util';
import { naclEncrypt } from '@polkadot/util-crypto';
import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
export default function encode ({ publicKey, secretKey }: PairInfo, passphrase?: string): Uint8Array {
assert(secretKey, 'Expected a valid secretKey to be passed to encode');
const encoded = u8aConcat(
PKCS8_HEADER,
secretKey,
PKCS8_DIVIDER,
publicKey
);
if (!passphrase) {
return encoded;
}
const { encrypted, nonce } = naclEncrypt(encoded, u8aFixLength(stringToU8a(passphrase), 256, true));
return u8aConcat(nonce, encrypted);
}