From f5123832bf4d1c80eb4983b573e7d8df38b4eaca Mon Sep 17 00:00:00 2001 From: diminator Date: Mon, 3 Apr 2017 21:35:19 +0200 Subject: [PATCH] seed from hash of secret --- dist/node/index.js | 8 ++++---- src/Ed25519Keypair.js | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/dist/node/index.js b/dist/node/index.js index 585c7b8..0802e13 100644 --- a/dist/node/index.js +++ b/dist/node/index.js @@ -3,11 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true }); -exports.Connection = exports.Transaction = exports.Ed25519KeyPair = undefined; +exports.Connection = exports.Transaction = exports.Ed25519Keypair = undefined; -var _Ed25519Keypair = require('./Ed25519Keypair'); +var _Ed25519Keypair2 = require('./Ed25519Keypair'); -var _Ed25519Keypair2 = _interopRequireDefault(_Ed25519Keypair); +var _Ed25519Keypair3 = _interopRequireDefault(_Ed25519Keypair2); var _transaction = require('./transaction'); @@ -21,6 +21,6 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -exports.Ed25519KeyPair = _Ed25519Keypair2.default; +exports.Ed25519Keypair = _Ed25519Keypair3.default; exports.Transaction = _Transaction; exports.Connection = _Connection; \ No newline at end of file diff --git a/src/Ed25519Keypair.js b/src/Ed25519Keypair.js index 7781f6a..0bbe407 100644 --- a/src/Ed25519Keypair.js +++ b/src/Ed25519Keypair.js @@ -1,14 +1,26 @@ import base58 from 'bs58'; import nacl from 'tweetnacl'; +import sha3 from 'js-sha3'; /** * @class Keypair Ed25519 keypair in base58 (as BigchainDB expects base58 keys) * @type {Object} + * @param {number} secret A seed that will be used as a key derivation function * @property {string} publicKey * @property {string} privateKey */ -export default function Ed25519Keypair() { - const keyPair = nacl.sign.keyPair(); +export default function Ed25519Keypair(secret) { + let keyPair; + if (secret) { + // Quick and dirty: use key derivation function instead + const secretHash = sha3.sha3_256 + .create() + .update(secret) + .array(); + keyPair = nacl.sign.keyPair.fromSeed(new Uint8Array(secretHash)) + } else { + keyPair = nacl.sign.keyPair(); + } this.publicKey = base58.encode(keyPair.publicKey); // tweetnacl's generated secret key is the secret key + public key (resulting in a 64-byte buffer) this.privateKey = base58.encode(keyPair.secretKey.slice(0, 32));