1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-25 20:00:19 +01:00
js-bigchaindb-driver/dist/node/Ed25519Keypair.js
2017-05-10 09:36:07 +02:00

43 lines
1.4 KiB
JavaScript

'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = Ed25519Keypair;
var _bs = require('bs58');
var _bs2 = _interopRequireDefault(_bs);
var _tweetnacl = require('tweetnacl');
var _tweetnacl2 = _interopRequireDefault(_tweetnacl);
var _jsSha = require('js-sha3');
var _jsSha2 = _interopRequireDefault(_jsSha);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @public
* @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
*/
function Ed25519Keypair(secret) {
var keyPair = void 0;
if (secret) {
// Quick and dirty: use key derivation function instead
var secretHash = _jsSha2.default.sha3_256.create().update(secret).array();
keyPair = _tweetnacl2.default.sign.keyPair.fromSeed(new Uint8Array(secretHash));
} else {
keyPair = _tweetnacl2.default.sign.keyPair();
}
this.publicKey = _bs2.default.encode(keyPair.publicKey);
// tweetnacl's generated secret key is the secret key + public key (resulting in a 64-byte buffer)
this.privateKey = _bs2.default.encode(keyPair.secretKey.slice(0, 32));
}
module.exports = exports['default'];