Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
Rename randomKey() to secret()
Browse files Browse the repository at this point in the history
...and add `secret.fromSeed()` for testing purposes.
  • Loading branch information
slowli committed Oct 1, 2017
1 parent 1c12670 commit 0fcdbd4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
40 changes: 36 additions & 4 deletions src/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,42 @@ export function verify (message, signature, pubkey) {
return nacl.sign.detached.verify(message, rawOrSelf(signature), rawOrSelf(pubkey))
}

export function randomKey () {
const secretKey = nacl.sign.keyPair().secretKey
secretKey.pub = function () { return PublicKey.from(fromSecretKey(this)) }
secretKey.rawPub = function () { return fromSecretKey(this) }
/**
* Randomly generates a key pair for signing/verification purposes.
*
* @returns {Uint8Array}
* Secret key with the following additional methods:
* - `pub(): PublicKey` returns Exonum-typed public key
* - `rawPub(): Uint8Array` returns 32-byte raw public key buffer
*/
export function secret () {
const { secretKey, publicKey } = nacl.sign.keyPair()
const exonumPub = PublicKey.from(publicKey)

secretKey.pub = function () { return exonumPub }
secretKey.rawPub = function () { return publicKey.slice(0) }

return secretKey
}

/**
* Generates a key pair for signing/verification purposes from a given 32-byte seed.
* You should not use this method unless for testing purposes; use `secret()` instead.
*
* @param {Uint8Array} seed
* 32-byte seed to generate the key from
* @returns {Uint8Array}
* Secret key with the following additional methods:
* - `pub(): PublicKey` returns Exonum-typed public key
* - `rawPub(): Uint8Array` returns 32-byte raw public key buffer
*/
secret.fromSeed = function (seed) {
const { secretKey, publicKey } = nacl.sign.keyPair.fromSeed(seed)
const exonumPub = PublicKey.from(publicKey)

secretKey.pub = function () { return exonumPub }
secretKey.rawPub = function () { return publicKey.slice(0) }

return secretKey
}

Expand Down
29 changes: 28 additions & 1 deletion test/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import nacl from 'tweetnacl'

import fixedBuffer from '../src/lowlevel/fixedBuffer'
import std from '../src/std'
import { hash, sign, verify } from '../src/crypto'
import { hash, sign, verify, secret } from '../src/crypto'
import { isExonumObject } from '../src/lowlevel/common'

const expect = chai
.use(chaiBytes)
Expand Down Expand Up @@ -117,3 +118,29 @@ describe('verify', () => {
expect(verify(exMessage, Signature.from(signature), PublicKey.from(pk))).to.be.true()
})
})

describe('secret', () => {
it('should generate a keypair', () => {
const key = secret()
expect(key).to.be.a('uint8array')
expect(key.pub()).to.satisfy(isExonumObject)
expect(key.rawPub()).to.be.a('uint8array')
})

it('should generate a new key each time', () => {
const [keyA, keyB] = [secret(), secret()]
expect(keyA).to.not.equalBytes(keyB)
expect(keyA.pub().equals(keyB.pub())).to.be.false()
})
})

describe('secret.fromSeed', () => {
it('should work for 32-byte seeds', () => {
const h = hash(std.Str.from('correct horse battery staple'))
const key = secret.fromSeed(h)
expect(key).to.be.a('uint8array')
expect(key.pub()).to.satisfy(isExonumObject)
expect(key.rawPub()).to.be.a('uint8array')
expect(key.subarray(0, 32)).to.equalBytes(h)
})
})
4 changes: 2 additions & 2 deletions test/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ describe('Message', () => {
}
})

const aliceKey = crypto.randomKey()
const bobKey = crypto.randomKey()
const aliceKey = crypto.secret()
const bobKey = crypto.secret()

describe('constructor', () => {
it('should construct a message', () => {
Expand Down

0 comments on commit 0fcdbd4

Please sign in to comment.