2017-04-26 15:58:19 +02:00
|
|
|
import base58 from 'bs58';
|
|
|
|
import nacl from 'tweetnacl';
|
|
|
|
import sha3 from 'js-sha3';
|
|
|
|
|
|
|
|
/**
|
2017-05-03 01:02:50 +02:00
|
|
|
* @public
|
2017-04-26 15:58:19 +02:00
|
|
|
* @class Keypair Ed25519 keypair in base58 (as BigchainDB expects base58 keys)
|
|
|
|
* @type {Object}
|
2017-05-03 01:02:50 +02:00
|
|
|
* @param {number} [secret] A seed that will be used as a key derivation function
|
2017-04-26 15:58:19 +02:00
|
|
|
* @property {string} publicKey
|
|
|
|
* @property {string} privateKey
|
|
|
|
*/
|
|
|
|
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));
|
|
|
|
}
|