All files / src Ed25519Keypair.js

100% Statements 5/5
50% Branches 1/2
100% Functions 1/1
100% Lines 5/5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 184x 4x                     9x 9x   9x    
import base58 from 'bs58'
import nacl from 'tweetnacl'
 
/**
 * @public
 * @class Keypair Ed25519 keypair in base58 (as BigchainDB expects base58 keys)
 * @type {Object}
 * @param {Buffer} [seed] A seed that will be used as a key derivation function
 * @property {string} publicKey
 * @property {string} privateKey
 */
export default function Ed25519Keypair(seed) {
    const keyPair = seed ? nacl.sign.keyPair.fromSeed(seed) : 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))
}