2018-08-10 12:49:26 +02:00
|
|
|
// Copyright BigchainDB GmbH and BigchainDB contributors
|
|
|
|
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
|
|
|
|
// Code is Apache-2.0 and docs are CC-BY-4.0
|
|
|
|
|
2017-06-12 16:57:29 +02:00
|
|
|
import base58 from 'bs58'
|
2021-03-09 07:51:36 +01:00
|
|
|
import { sign } from 'tweetnacl'
|
2017-04-26 15:58:19 +02:00
|
|
|
|
|
|
|
/**
|
2017-05-03 01:02:50 +02:00
|
|
|
* @public
|
2018-05-14 17:14:40 +02:00
|
|
|
* Ed25519 keypair in base58 (as BigchainDB expects base58 keys)
|
2017-04-26 15:58:19 +02:00
|
|
|
* @type {Object}
|
2017-05-14 14:41:04 +02:00
|
|
|
* @param {Buffer} [seed] 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
|
|
|
|
*/
|
2017-05-14 14:41:04 +02:00
|
|
|
export default function Ed25519Keypair(seed) {
|
2021-03-09 07:51:36 +01:00
|
|
|
const keyPair = seed ? sign.keyPair.fromSeed(seed) : sign.keyPair()
|
2018-11-06 11:00:35 +01:00
|
|
|
this.publicKey = base58.encode(Buffer.from(keyPair.publicKey))
|
2017-04-26 15:58:19 +02:00
|
|
|
// tweetnacl's generated secret key is the secret key + public key (resulting in a 64-byte buffer)
|
2018-11-06 11:00:35 +01:00
|
|
|
this.privateKey = base58.encode(Buffer.from(keyPair.secretKey.slice(0, 32)))
|
2017-04-26 15:58:19 +02:00
|
|
|
}
|