From 35d2f69b75d6c3b91b8030255f20abadf389bb5b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 20 Mar 2018 15:42:39 +0100 Subject: [PATCH] Pkcs8 bytes for pairs (non-encrypted) (#111) * Upgrade dependencies * Pkcs8 bytes for pairs (non-encrypted) * Bump minor * Pass only secretKey around for encoding * Cleanup seed interface --- lerna.json | 2 +- packages/util-crypto/package.json | 2 +- .../util-crypto/src/nacl/keypair/fromSeed.js | 28 +++------------ .../src/nacl/keypair/fromSeed.spec.js | 23 ++----------- packages/util-crypto/src/nacl/sign.spec.js | 4 ++- packages/util-crypto/src/nacl/verify.spec.js | 4 ++- packages/util-keyring/package.json | 6 ++-- packages/util-keyring/src/decrypt.js | 10 +++--- packages/util-keyring/src/decrypt.spec.js | 10 +++--- packages/util-keyring/src/encrypt.js | 6 ++-- packages/util-keyring/src/encrypt.spec.js | 15 ++++++-- packages/util-keyring/src/index.js | 14 ++++---- packages/util-keyring/src/index.spec.js | 19 +++++------ packages/util-keyring/src/pair/decode.js | 23 +++++++++++++ packages/util-keyring/src/pair/decrypt.js | 20 ----------- packages/util-keyring/src/pair/defaults.js | 12 +++++++ packages/util-keyring/src/pair/encode.js | 19 +++++++++++ packages/util-keyring/src/pair/encrypt.js | 28 --------------- packages/util-keyring/src/pair/index.js | 34 ++++++++++++++----- packages/util-keyring/src/pair/index.spec.js | 2 +- packages/util-keyring/src/pairs.js | 2 +- packages/util-keyring/src/testing.js | 17 ++++++---- packages/util-keyring/src/types.js | 29 ++++------------ packages/util-rlp/package.json | 4 +-- packages/util-triehash/package.json | 8 ++--- packages/util/package.json | 4 +-- yarn.lock | 6 ++-- 27 files changed, 168 insertions(+), 183 deletions(-) create mode 100644 packages/util-keyring/src/pair/decode.js delete mode 100644 packages/util-keyring/src/pair/decrypt.js create mode 100644 packages/util-keyring/src/pair/defaults.js create mode 100644 packages/util-keyring/src/pair/encode.js delete mode 100644 packages/util-keyring/src/pair/encrypt.js diff --git a/lerna.json b/lerna.json index de15802a4..f4b84fb57 100644 --- a/lerna.json +++ b/lerna.json @@ -10,5 +10,5 @@ "packages": [ "packages/*" ], - "version": "0.17.4" + "version": "0.18.0" } diff --git a/packages/util-crypto/package.json b/packages/util-crypto/package.json index 07d505495..45d9b59a4 100644 --- a/packages/util-crypto/package.json +++ b/packages/util-crypto/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-crypto", - "version": "0.17.4", + "version": "0.18.0", "description": "A collection of useful crypto utilities for @polkadot", "main": "index.js", "keywords": [ diff --git a/packages/util-crypto/src/nacl/keypair/fromSeed.js b/packages/util-crypto/src/nacl/keypair/fromSeed.js index a4055fe97..73f4538ea 100644 --- a/packages/util-crypto/src/nacl/keypair/fromSeed.js +++ b/packages/util-crypto/src/nacl/keypair/fromSeed.js @@ -6,38 +6,18 @@ import type { KeypairType } from '../../types'; const nacl = require('tweetnacl'); -const hexToU8a = require('@polkadot/util/hex/toU8a'); -const isHex = require('@polkadot/util/is/hex'); -const isU8a = require('@polkadot/util/is/u8a'); -const u8aFromString = require('@polkadot/util/u8a/fromString'); /** @name naclKeypairFromSeed - @signature naclKeypairFromSeed (seed: Uint8Array | string): { secretKey: Uint8Array, publicKey: Uint8Array } + @signature naclKeypairFromSeed (seed: Uint8Array): { secretKey: Uint8Array, publicKey: Uint8Array } @summary Creates a new public/secret keypair from a seed. @description - Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed. Seed can be a `Uint8Array`, a hex string or a string of bytes. + Returns a object containing a `publicKey` & `secretKey` generated from the supplied seed. @example import { naclKeypairFromSeed } from '@polkadot/util-crypto'; naclKeypairFromSeed(...) // => { secretKey: [...], publicKey: [...] } */ -module.exports = function naclKeypairFromSeed (seed: Uint8Array | string): KeypairType { - if (isU8a(seed)) { - // $FlowFixMe type has been determined - return nacl.sign.keyPair.fromSeed(seed); - } - - // $FlowFixMe type has been determined - if (isHex(seed)) { - return nacl.sign.keyPair.fromSeed( - // $FlowFixMe type has been determined - hexToU8a(seed) - ); - } - - return nacl.sign.keyPair.fromSeed( - // $FlowFixMe type has been determined - u8aFromString(seed) - ); +module.exports = function naclKeypairFromSeed (seed: Uint8Array): KeypairType { + return nacl.sign.keyPair.fromSeed(seed); }; diff --git a/packages/util-crypto/src/nacl/keypair/fromSeed.spec.js b/packages/util-crypto/src/nacl/keypair/fromSeed.spec.js index 4f6f48039..780b64ea5 100644 --- a/packages/util-crypto/src/nacl/keypair/fromSeed.spec.js +++ b/packages/util-crypto/src/nacl/keypair/fromSeed.spec.js @@ -3,13 +3,12 @@ // of the ISC license. See the LICENSE file for details. const u8aFromString = require('@polkadot/util/u8a/fromString'); -const u8aToHex = require('@polkadot/util/u8a/toHex'); const { naclKeypairFromSeed } = require('../index'); describe('naclKeypairFromSeed', () => { // NOTE: Aligned with Rust test, b"12345678901234567890123456789012" - const TEST = '12345678901234567890123456789012'; + const TEST = u8aFromString('12345678901234567890123456789012'); const RESULT = { publicKey: new Uint8Array([ 0x2f, 0x8c, 0x61, 0x29, 0xd8, 0x16, 0xcf, 0x51, @@ -27,27 +26,9 @@ describe('naclKeypairFromSeed', () => { ]) }; - it('generates a valid publicKey/secretKey pair (string)', () => { + it('generates a valid publicKey/secretKey pair (u8a)', () => { expect( naclKeypairFromSeed(TEST) ).toEqual(RESULT); }); - - it('generates a valid publicKey/secretKey pair (u8a)', () => { - expect( - naclKeypairFromSeed( - u8aFromString(TEST) - ) - ).toEqual(RESULT); - }); - - it('generates a valid publicKey/secretKey pair (hex)', () => { - expect( - naclKeypairFromSeed( - u8aToHex( - u8aFromString(TEST) - ) - ) - ).toEqual(RESULT); - }); }); diff --git a/packages/util-crypto/src/nacl/sign.spec.js b/packages/util-crypto/src/nacl/sign.spec.js index 60260fcdd..8233068cc 100644 --- a/packages/util-crypto/src/nacl/sign.spec.js +++ b/packages/util-crypto/src/nacl/sign.spec.js @@ -2,6 +2,8 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. +const u8aFromString = require('@polkadot/util/u8a/fromString'); + const { naclSign, naclKeypairFromSeed } = require('./index'); describe('naclSign', () => { @@ -9,7 +11,7 @@ describe('naclSign', () => { beforeEach(() => { secretKey = naclKeypairFromSeed( - '12345678901234567890123456789012' + u8aFromString('12345678901234567890123456789012') ).secretKey; }); diff --git a/packages/util-crypto/src/nacl/verify.spec.js b/packages/util-crypto/src/nacl/verify.spec.js index 6149da07e..34425fd39 100644 --- a/packages/util-crypto/src/nacl/verify.spec.js +++ b/packages/util-crypto/src/nacl/verify.spec.js @@ -2,6 +2,8 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. +const u8aFromString = require('@polkadot/util/u8a/fromString'); + const { naclVerify, naclKeypairFromSeed } = require('./index'); describe('naclSign', () => { @@ -10,7 +12,7 @@ describe('naclSign', () => { beforeEach(() => { publicKey = naclKeypairFromSeed( - '12345678901234567890123456789012' + u8aFromString('12345678901234567890123456789012') ).publicKey; signature = new Uint8Array([28, 58, 206, 239, 249, 70, 59, 191, 166, 40, 219, 218, 235, 170, 25, 79, 10, 94, 9, 197, 34, 126, 1, 150, 246, 68, 28, 238, 36, 26, 172, 163, 168, 90, 202, 211, 126, 246, 57, 212, 43, 24, 88, 197, 240, 113, 118, 76, 37, 81, 91, 110, 236, 50, 144, 134, 100, 223, 220, 238, 34, 185, 211, 7]); }); diff --git a/packages/util-keyring/package.json b/packages/util-keyring/package.json index 3d0f0b2a4..c0659659d 100644 --- a/packages/util-keyring/package.json +++ b/packages/util-keyring/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-keyring", - "version": "0.17.4", + "version": "0.18.0", "description": "Keyring management", "main": "index.js", "engines": { @@ -31,8 +31,8 @@ "test": "echo \"Tests only available from root wrapper\"" }, "dependencies": { - "@polkadot/util": "^0.17.4", - "@polkadot/util-crypto": "^0.17.4", + "@polkadot/util": "^0.18.0", + "@polkadot/util-crypto": "^0.18.0", "babel-runtime": "^6.26.0" } } diff --git a/packages/util-keyring/src/decrypt.js b/packages/util-keyring/src/decrypt.js index c64b6116f..c7683b8a4 100644 --- a/packages/util-keyring/src/decrypt.js +++ b/packages/util-keyring/src/decrypt.js @@ -3,17 +3,17 @@ // of the ISC license. See the LICENSE file for details. // @flow -import type { KeyringPairEncrypted, KeyringPair } from './types'; +import type { KeyringPair } from './types'; const createPair = require('./pair'); -module.exports = function decrypt (box: KeyringPairEncrypted, secret: Uint8Array | string): KeyringPair { +module.exports = function decrypt (encoded: Uint8Array, passphrase: Uint8Array | string): KeyringPair { const pair = createPair({ - publicKey: box.publicKey, - secretKey: new Uint8Array([]) + publicKey: new Uint8Array(32), + secretKey: new Uint8Array(64) }); - pair.decryptSelf(box, secret); + pair.decodePkcs8(encoded, passphrase); return pair; }; diff --git a/packages/util-keyring/src/decrypt.spec.js b/packages/util-keyring/src/decrypt.spec.js index 5e6ca948d..b792aa721 100644 --- a/packages/util-keyring/src/decrypt.spec.js +++ b/packages/util-keyring/src/decrypt.spec.js @@ -2,11 +2,13 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. +const u8aFromString = require('@polkadot/util/u8a/fromString'); + const index = require('./index'); describe('decrypt', () => { const publicKey = new Uint8Array([47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238]); - const seedOne = '12345678901234567890123456789012'; + const seedOne = u8aFromString('12345678901234567890123456789012'); let keypair; let encrypted; @@ -21,9 +23,7 @@ describe('decrypt', () => { it('decrypts a previously encrypted', () => { expect( - keypair.decrypt(encrypted, 'test') - ).toMatchObject({ - publicKey - }); + keypair.decrypt(encrypted, 'test').publicKey() + ).toEqual(publicKey); }); }); diff --git a/packages/util-keyring/src/encrypt.js b/packages/util-keyring/src/encrypt.js index ceba75d36..faa7be851 100644 --- a/packages/util-keyring/src/encrypt.js +++ b/packages/util-keyring/src/encrypt.js @@ -3,12 +3,12 @@ // of the ISC license. See the LICENSE file for details. // @flow -import type { KeyringPairEncrypted, KeyringPair } from './types'; +import type { KeyringPair } from './types'; -module.exports = function encrypt (pair: ?KeyringPair, secret: Uint8Array | string): ?KeyringPairEncrypted { +module.exports = function encrypt (pair: ?KeyringPair, passphrase: Uint8Array | string): ?Uint8Array { if (!pair) { return null; } - return pair.encryptSelf(secret); + return pair.encodePkcs8(passphrase); }; diff --git a/packages/util-keyring/src/encrypt.spec.js b/packages/util-keyring/src/encrypt.spec.js index 99792dd6c..75250895c 100644 --- a/packages/util-keyring/src/encrypt.spec.js +++ b/packages/util-keyring/src/encrypt.spec.js @@ -2,11 +2,16 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. +const u8aFromString = require('@polkadot/util/u8a/fromString'); +const u8aConcat = require('@polkadot/util/u8a/concat'); + const index = require('./index'); +const { PKCS8_DIVIDER, PKCS8_HEADER } = require('./pair/defaults'); + describe('encrypt', () => { const publicKey = new Uint8Array([47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238]); - const seedOne = '12345678901234567890123456789012'; + const seedOne = u8aFromString('12345678901234567890123456789012'); let keypair; beforeEach(() => { @@ -17,8 +22,12 @@ describe('encrypt', () => { it('encrypts the key', () => { expect( - keypair.encrypt(publicKey, 'test').publicKey - ).toEqual(publicKey); + keypair.encrypt(publicKey, 'test') + ).toEqual( + u8aConcat( + PKCS8_HEADER, seedOne, PKCS8_DIVIDER, publicKey + ) + ); }); it('returns null for non-existing', () => { diff --git a/packages/util-keyring/src/index.js b/packages/util-keyring/src/index.js index cf7de8464..7c0263e60 100644 --- a/packages/util-keyring/src/index.js +++ b/packages/util-keyring/src/index.js @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. // @flow -import type { KeyringPairEncrypted, KeyringInstance, KeyringPair } from './types'; +import type { KeyringInstance, KeyringPair } from './types'; const naclKeypairFromSeed = require('@polkadot/util-crypto/nacl/keypair/fromSeed'); @@ -16,16 +16,16 @@ module.exports = function keyring (): KeyringInstance { const pairs = createPairs(); return { - addFromSeed: (seed: Uint8Array | string): KeyringPair => + addFromSeed: (seed: Uint8Array): KeyringPair => pairs.add(createPair(naclKeypairFromSeed(seed))), - decrypt: (encrypted: KeyringPairEncrypted, secret: Uint8Array | string): KeyringPair => - pairs.add(decrypt(encrypted, secret)), - encrypt: (publicKey: Uint8Array, secret: Uint8Array | string): ?KeyringPairEncrypted => - encrypt(pairs.get(publicKey), secret), + decrypt: (encrypted: Uint8Array, passphrase: Uint8Array | string): KeyringPair => + pairs.add(decrypt(encrypted, passphrase)), + encrypt: (publicKey: Uint8Array, passphrase: Uint8Array | string): ?Uint8Array => + encrypt(pairs.get(publicKey), passphrase), getPair: (publicKey: Uint8Array): ?KeyringPair => pairs.get(publicKey), getPairs: pairs.all, getPublicKeys: (): Array => - pairs.all().map(({ publicKey }) => publicKey) + pairs.all().map(({ publicKey }) => publicKey()) }; }; diff --git a/packages/util-keyring/src/index.spec.js b/packages/util-keyring/src/index.spec.js index 41ad997ae..664993562 100644 --- a/packages/util-keyring/src/index.spec.js +++ b/packages/util-keyring/src/index.spec.js @@ -2,13 +2,16 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. +const hexToU8a = require('@polkadot/util/hex/toU8a'); +const u8aFromString = require('@polkadot/util/u8a/fromString'); + const index = require('./index'); describe('keypair', () => { const publicKeyOne = new Uint8Array([47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238]); const publicKeyTwo = new Uint8Array([215, 90, 152, 1, 130, 177, 10, 183, 213, 75, 254, 211, 201, 100, 7, 58, 14, 225, 114, 243, 218, 166, 35, 37, 175, 2, 26, 104, 247, 7, 81, 26]); - const seedOne = '12345678901234567890123456789012'; - const seedTwo = '0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60'; + const seedOne = u8aFromString('12345678901234567890123456789012'); + const seedTwo = hexToU8a('0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60'); let keypair; beforeEach(() => { @@ -19,10 +22,8 @@ describe('keypair', () => { it('adds the pair', () => { expect( - keypair.addFromSeed(seedTwo) - ).toMatchObject({ - publicKey: publicKeyTwo - }); + keypair.addFromSeed(seedTwo).publicKey() + ).toEqual(publicKeyTwo); }); it('allows publicKeys retrieval', () => { @@ -35,9 +36,7 @@ describe('keypair', () => { it('allows retrieval of a specific item', () => { expect( - keypair.getPair(publicKeyOne) - ).toMatchObject({ - publicKey: publicKeyOne - }); + keypair.getPair(publicKeyOne).publicKey() + ).toEqual(publicKeyOne); }); }); diff --git a/packages/util-keyring/src/pair/decode.js b/packages/util-keyring/src/pair/decode.js new file mode 100644 index 000000000..faea8528d --- /dev/null +++ b/packages/util-keyring/src/pair/decode.js @@ -0,0 +1,23 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +const u8aConcat = require('@polkadot/util/u8a/concat'); + +const { PKCS8_DIVIDER, PKCS8_HEADER } = require('./defaults'); + +const KEY_LENGTH = 32; +const SEED_OFFSET = PKCS8_HEADER.length; +const PUBLIC_OFFSET = SEED_OFFSET + KEY_LENGTH + PKCS8_DIVIDER.length; + +module.exports = function decode (encoded: Uint8Array, passphrase?: Uint8Array | string) { + // TODO: decrypt using passphrase + const publicKey = encoded.subarray(PUBLIC_OFFSET, PUBLIC_OFFSET + KEY_LENGTH); + const seed = encoded.subarray(SEED_OFFSET, SEED_OFFSET + KEY_LENGTH); + + return { + publicKey, + secretKey: u8aConcat(seed, publicKey) + }; +}; diff --git a/packages/util-keyring/src/pair/decrypt.js b/packages/util-keyring/src/pair/decrypt.js deleted file mode 100644 index d8635e62c..000000000 --- a/packages/util-keyring/src/pair/decrypt.js +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2017-2018 Jaco Greeff -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. -// @flow - -import type { KeyringPairEncrypted } from '../types'; - -const kdf = require('@polkadot/util-crypto/kdf/asU8a'); -const naclDecrypt = require('@polkadot/util-crypto/nacl/decrypt'); -const assert = require('@polkadot/util/assert'); - -module.exports = function decrypt ({ crypto: { params: { nonce }, kdf: { rounds, salt }, text } }: KeyringPairEncrypted, secret: Uint8Array | string): Uint8Array { - const { key } = kdf(secret, rounds, salt); - const secretKey = naclDecrypt(text, key, nonce); - - assert(secretKey, 'Unable to decrypt pair using secret'); - - // $FlowFixMe checking for type just above - return ((secretKey: any): Uint8Array); -}; diff --git a/packages/util-keyring/src/pair/defaults.js b/packages/util-keyring/src/pair/defaults.js new file mode 100644 index 000000000..24dadae0e --- /dev/null +++ b/packages/util-keyring/src/pair/defaults.js @@ -0,0 +1,12 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +const PKCS8_DIVIDER = new Uint8Array([161, 35, 3, 33, 0]); +const PKCS8_HEADER = new Uint8Array([48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32]); + +module.exports = { + PKCS8_DIVIDER, + PKCS8_HEADER +}; diff --git a/packages/util-keyring/src/pair/encode.js b/packages/util-keyring/src/pair/encode.js new file mode 100644 index 000000000..83f0eff8a --- /dev/null +++ b/packages/util-keyring/src/pair/encode.js @@ -0,0 +1,19 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +const u8aConcat = require('@polkadot/util/u8a/concat'); + +const { PKCS8_DIVIDER, PKCS8_HEADER } = require('./defaults'); + +module.exports = function encode (secretKey: Uint8Array, passphrase?: Uint8Array | string): Uint8Array { + // TODO: encrypt using passphrase + + return u8aConcat( + PKCS8_HEADER, + secretKey.subarray(0, 32), + PKCS8_DIVIDER, + secretKey.subarray(32, 64) + ); +}; diff --git a/packages/util-keyring/src/pair/encrypt.js b/packages/util-keyring/src/pair/encrypt.js deleted file mode 100644 index b5ba52d36..000000000 --- a/packages/util-keyring/src/pair/encrypt.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2017-2018 Jaco Greeff -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. -// @flow - -import type { KeyringPairEncrypted } from '../types'; - -const kdf = require('@polkadot/util-crypto/kdf/asU8a'); -const naclEncrypt = require('@polkadot/util-crypto/nacl/encrypt'); - -module.exports = function encrypt (message: Uint8Array, secret: Uint8Array | string, publicKey: Uint8Array): KeyringPairEncrypted { - const { key, rounds, salt } = kdf(secret); - const { encrypted, nonce } = naclEncrypt(message, key); - - return { - crypto: { - params: { - nonce - }, - kdf: { - rounds, - salt - }, - text: encrypted - }, - publicKey - }; -}; diff --git a/packages/util-keyring/src/pair/index.js b/packages/util-keyring/src/pair/index.js index 66c811713..50169d2cd 100644 --- a/packages/util-keyring/src/pair/index.js +++ b/packages/util-keyring/src/pair/index.js @@ -4,22 +4,40 @@ // @flow import type { KeypairType } from '@polkadot/util-crypto/types'; -import type { KeyringPairEncrypted, KeyringPair } from '../types'; +import type { KeyringPair } from '../types'; +const naclFromSeed = require('@polkadot/util-crypto/nacl/keypair/fromSeed'); const naclSign = require('@polkadot/util-crypto/nacl/sign'); const naclVerify = require('@polkadot/util-crypto/nacl/verify'); +const assert = require('@polkadot/util/assert'); -const decrypt = require('./decrypt'); -const encrypt = require('./encrypt'); +const decode = require('./decode'); +const encode = require('./encode'); + +function decodePkcs8 (encoded: Uint8Array, passphrase?: Uint8Array | string) { + const { publicKey, secretKey } = decode(encoded, passphrase); + const validate = naclFromSeed(secretKey.subarray(0, 32)); + + assert(validate.publicKey.toString() === publicKey.toString(), 'Pkcs8 decoded keys are not matching'); + + return { + publicKey, + secretKey + }; +} module.exports = function pair ({ publicKey, secretKey }: KeypairType): KeyringPair { return { - publicKey, - decryptSelf: (encrypted: KeyringPairEncrypted, secret: Uint8Array | string): void => { - secretKey = decrypt(encrypted, secret); + decodePkcs8: (encoded: Uint8Array, passphrase?: Uint8Array | string): void => { + const decoded = decodePkcs8(encoded, passphrase); + + publicKey = decoded.publicKey; + secretKey = decoded.secretKey; }, - encryptSelf: (secret: Uint8Array | string): KeyringPairEncrypted => - encrypt(secretKey, secret, publicKey), + encodePkcs8: (passphrase?: Uint8Array | string): Uint8Array => + encode(secretKey, passphrase), + publicKey: (): Uint8Array => + publicKey, sign: (message: Uint8Array): Uint8Array => naclSign(message, secretKey), verify: (message: Uint8Array, signature: Uint8Array): boolean => diff --git a/packages/util-keyring/src/pair/index.spec.js b/packages/util-keyring/src/pair/index.spec.js index 14ee0c35c..61c596426 100644 --- a/packages/util-keyring/src/pair/index.spec.js +++ b/packages/util-keyring/src/pair/index.spec.js @@ -10,7 +10,7 @@ describe('pair', () => { it('has a publicKey', () => { expect( - keyring.one.publicKey + keyring.one.publicKey() ).toEqual( new Uint8Array([47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238]) ); diff --git a/packages/util-keyring/src/pairs.js b/packages/util-keyring/src/pairs.js index 46a0323a1..b11372adb 100644 --- a/packages/util-keyring/src/pairs.js +++ b/packages/util-keyring/src/pairs.js @@ -14,7 +14,7 @@ module.exports = function pairs (): KeyringPairs { return { add: (pair: KeyringPair): KeyringPair => { - self[pair.publicKey] = pair; + self[pair.publicKey()] = pair; return pair; }, diff --git a/packages/util-keyring/src/testing.js b/packages/util-keyring/src/testing.js index 65d83dc5d..fd1e92923 100644 --- a/packages/util-keyring/src/testing.js +++ b/packages/util-keyring/src/testing.js @@ -9,12 +9,17 @@ type TestKeyring = { [string]: KeyringPair }; +const hexToU8a = require('@polkadot/util/hex/toU8a'); +const u8aFromString = require('@polkadot/util/u8a/fromString'); + const createKeyring = require('./index'); -function padSeed (seed: string): string { - return seed.length < 32 - ? padSeed(`${seed} `) - : seed; +function padSeed (seed: string): Uint8Array { + while (seed.length < 32) { + seed += ' '; + } + + return u8aFromString(seed); } const SEEDS = { @@ -31,9 +36,9 @@ const SEEDS = { ferdie: padSeed('Ferdie'), one: - '12345678901234567890123456789012', + padSeed('12345678901234567890123456789012'), two: - '0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60' + hexToU8a('0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60') }; module.exports = function testKeyring (): TestKeyring { diff --git a/packages/util-keyring/src/types.js b/packages/util-keyring/src/types.js index 5213c8e2f..d49d1b984 100644 --- a/packages/util-keyring/src/types.js +++ b/packages/util-keyring/src/types.js @@ -3,27 +3,10 @@ // of the ISC license. See the LICENSE file for details. // @flow -export type KeyringPairEncrypted$Cipher = 'xsalsa20-poly1305'; - -export type KeyringPairEncrypted = { - crypto: { - params: { - nonce: Uint8Array - }, - kdf: { - rounds: number, - salt: Uint8Array - }, - text: Uint8Array - }, - publicKey: Uint8Array -}; - export type KeyringPair = { - publicKey: Uint8Array, - - decryptSelf: (box: KeyringPairEncrypted, secret: Uint8Array | string) => void, - encryptSelf: (secret: Uint8Array | string) => KeyringPairEncrypted, + decodePkcs8: (encoded: Uint8Array, passphrase: Uint8Array | string) => void, + encodePkcs8: (passphrase: Uint8Array | string) => Uint8Array, + publicKey: () => Uint8Array, sign (message: Uint8Array): Uint8Array, verify (message: Uint8Array, signature: Uint8Array): boolean }; @@ -35,9 +18,9 @@ export type KeyringPairs = { }; export type KeyringInstance = { - addFromSeed (seed: Uint8Array | string): KeyringPair, - decrypt (encrypted: KeyringPairEncrypted, secret: Uint8Array | string): KeyringPair, - encrypt (publicKey: Uint8Array, secret: Uint8Array | string): ?KeyringPairEncrypted, + addFromSeed (seed: Uint8Array): KeyringPair, + decrypt (encrypted: Uint8Array, secret: Uint8Array | string): KeyringPair, + encrypt (publicKey: Uint8Array, secret: Uint8Array | string): ?Uint8Array, getPair (publicKey: Uint8Array): ?KeyringPair, getPairs (): Array, getPublicKeys (): Array diff --git a/packages/util-rlp/package.json b/packages/util-rlp/package.json index c27b05e2c..56caba6bf 100644 --- a/packages/util-rlp/package.json +++ b/packages/util-rlp/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-rlp", - "version": "0.17.4", + "version": "0.18.0", "description": "RLP encoding and decoding", "main": "index.js", "keywords": [ @@ -29,7 +29,7 @@ "test": "echo \"Tests only available from root wrapper\"" }, "dependencies": { - "@polkadot/util": "^0.17.4", + "@polkadot/util": "^0.18.0", "babel-runtime": "^6.26.0" }, "devDependencies": { diff --git a/packages/util-triehash/package.json b/packages/util-triehash/package.json index 762f903b3..ae66f5630 100644 --- a/packages/util-triehash/package.json +++ b/packages/util-triehash/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util-triehash", - "version": "0.17.4", + "version": "0.18.0", "description": "Creation of triehash values", "main": "index.js", "keywords": [ @@ -29,9 +29,9 @@ "test": "echo \"Tests only available from root wrapper\"" }, "dependencies": { - "@polkadot/util": "^0.17.4", - "@polkadot/util-crypto": "^0.17.4", - "@polkadot/util-rlp": "^0.17.4", + "@polkadot/util": "^0.18.0", + "@polkadot/util-crypto": "^0.18.0", + "@polkadot/util-rlp": "^0.18.0", "babel-runtime": "^6.26.0" } } diff --git a/packages/util/package.json b/packages/util/package.json index d3f90343f..541bc9f6d 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util", - "version": "0.17.4", + "version": "0.18.0", "description": "A collection of useful utilities for @polkadot", "main": "index.js", "keywords": [ @@ -29,7 +29,7 @@ "test": "echo \"Tests only available from root wrapper\"" }, "devDependencies": { - "@polkadot/api-jsonrpc": "^0.8.4" + "@polkadot/api-jsonrpc": "^0.8.5" }, "dependencies": { "babel-runtime": "^6.26.0", diff --git a/yarn.lock b/yarn.lock index f82fda264..1beba0c76 100644 --- a/yarn.lock +++ b/yarn.lock @@ -79,9 +79,9 @@ lodash "^4.2.0" to-fast-properties "^2.0.0" -"@polkadot/api-jsonrpc@^0.8.4": - version "0.8.4" - resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.8.4.tgz#e4f34523a42e1772abf6c25ac604fb9b387ad10e" +"@polkadot/api-jsonrpc@^0.8.5": + version "0.8.5" + resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.8.5.tgz#f27afc6304dcf94b0e95f62b33b2091ba6b690e5" dependencies: babel-runtime "^6.26.0"