From 6ce0efea3f124fb1ddad340df85266818ed206a9 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 11 Apr 2018 15:22:36 +0200 Subject: [PATCH] Use detached sign (no splitting) (#114) * Use detached sign (no splitting) * Add pair meta get/set * Types --- packages/util-crypto/src/nacl/sign.js | 3 +-- packages/util-keyring/src/pair/decode.js | 8 ++++++- packages/util-keyring/src/pair/getMeta.js | 10 +++++++++ packages/util-keyring/src/pair/index.js | 27 ++++++++++------------- packages/util-keyring/src/pair/setMeta.js | 10 +++++++++ packages/util-keyring/src/pair/types.js | 12 ++++++++++ 6 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 packages/util-keyring/src/pair/getMeta.js create mode 100644 packages/util-keyring/src/pair/setMeta.js create mode 100644 packages/util-keyring/src/pair/types.js diff --git a/packages/util-crypto/src/nacl/sign.js b/packages/util-crypto/src/nacl/sign.js index e3154cd69..c58e9be23 100644 --- a/packages/util-crypto/src/nacl/sign.js +++ b/packages/util-crypto/src/nacl/sign.js @@ -17,6 +17,5 @@ const nacl = require('tweetnacl'); naclSign([...], [...]) // => [...] */ module.exports = function naclSign (message: Uint8Array, secretKey: Uint8Array): Uint8Array { - // NOTE: Original message appended to the signature, slice result only - return nacl.sign(message, secretKey).subarray(0, 64); + return nacl.sign.detached(message, secretKey); }; diff --git a/packages/util-keyring/src/pair/decode.js b/packages/util-keyring/src/pair/decode.js index faea8528d..adbd15671 100644 --- a/packages/util-keyring/src/pair/decode.js +++ b/packages/util-keyring/src/pair/decode.js @@ -4,6 +4,8 @@ // @flow const u8aConcat = require('@polkadot/util/u8a/concat'); +const assert = require('@polkadot/util/assert'); +const naclFromSeed = require('@polkadot/util-crypto/nacl/keypair/fromSeed'); const { PKCS8_DIVIDER, PKCS8_HEADER } = require('./defaults'); @@ -15,9 +17,13 @@ module.exports = function decode (encoded: Uint8Array, passphrase?: Uint8Array | // TODO: decrypt using passphrase const publicKey = encoded.subarray(PUBLIC_OFFSET, PUBLIC_OFFSET + KEY_LENGTH); const seed = encoded.subarray(SEED_OFFSET, SEED_OFFSET + KEY_LENGTH); + const secretKey = u8aConcat(seed, publicKey); + const validate = naclFromSeed(seed); + + assert(validate.publicKey.toString() === publicKey.toString(), 'Pkcs8 decoded keys are not matching'); return { publicKey, - secretKey: u8aConcat(seed, publicKey) + secretKey }; }; diff --git a/packages/util-keyring/src/pair/getMeta.js b/packages/util-keyring/src/pair/getMeta.js new file mode 100644 index 000000000..9c55d16f7 --- /dev/null +++ b/packages/util-keyring/src/pair/getMeta.js @@ -0,0 +1,10 @@ +// 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 { PairState, PairState$Meta } from './types'; + +module.exports = function getMeta (state: PairState): PairState$Meta { + return state.meta; +}; diff --git a/packages/util-keyring/src/pair/index.js b/packages/util-keyring/src/pair/index.js index 50169d2cd..9cafb7b9e 100644 --- a/packages/util-keyring/src/pair/index.js +++ b/packages/util-keyring/src/pair/index.js @@ -5,39 +5,36 @@ import type { KeypairType } from '@polkadot/util-crypto/types'; import type { KeyringPair } from '../types'; +import type { PairState, PairState$Meta } 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 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 - }; -} +const getMeta = require('./getMeta'); +const setMeta = require('./setMeta'); module.exports = function pair ({ publicKey, secretKey }: KeypairType): KeyringPair { + const state: PairState = { + meta: {} + }; + return { decodePkcs8: (encoded: Uint8Array, passphrase?: Uint8Array | string): void => { - const decoded = decodePkcs8(encoded, passphrase); + const decoded = decode(encoded, passphrase); publicKey = decoded.publicKey; secretKey = decoded.secretKey; }, encodePkcs8: (passphrase?: Uint8Array | string): Uint8Array => encode(secretKey, passphrase), + getMeta: (): PairState$Meta => + getMeta(state), publicKey: (): Uint8Array => publicKey, + setMeta: (meta: PairState$Meta): void => + setMeta(state, meta), sign: (message: Uint8Array): Uint8Array => naclSign(message, secretKey), verify: (message: Uint8Array, signature: Uint8Array): boolean => diff --git a/packages/util-keyring/src/pair/setMeta.js b/packages/util-keyring/src/pair/setMeta.js new file mode 100644 index 000000000..51f130819 --- /dev/null +++ b/packages/util-keyring/src/pair/setMeta.js @@ -0,0 +1,10 @@ +// 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 { PairState, PairState$Meta } from './types'; + +module.exports = function setMeta (state: PairState, meta: PairState$Meta): void { + state.meta = meta; +}; diff --git a/packages/util-keyring/src/pair/types.js b/packages/util-keyring/src/pair/types.js new file mode 100644 index 000000000..72b67c7db --- /dev/null +++ b/packages/util-keyring/src/pair/types.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 + +export type PairState$Meta = { + [string]: string +}; + +export type PairState = { + meta: PairState$Meta +};