Use detached sign (no splitting) (#114)
* Use detached sign (no splitting) * Add pair meta get/set * Types
This commit is contained in:
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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 =>
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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
|
||||
};
|
||||
Reference in New Issue
Block a user