Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@
"bigi": "1.1.0",
"crypto-js": "3.1.2-3",
"ecurve": "0.10.0",
"secure-random": "0.2.1"
"secure-random": "1.0.0"
}
}
6 changes: 4 additions & 2 deletions src/eckey.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ ECKey.fromWIF = function(string) {
}

ECKey.makeRandom = function(compressed, rng) {
rng = rng || secureRandom
rng = rng || secureRandom.randomBuffer

var buffer = rng(32)
assert(Buffer.isBuffer(buffer), 'Expected Buffer, got ' + buffer)

var buffer = new Buffer(rng(32))
var d = BigInteger.fromBuffer(buffer)
d = d.mod(curve.n)

Expand Down
39 changes: 39 additions & 0 deletions test/eckey.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ var assert = require('assert')
var crypto = require('../src/crypto')
var networks = require('../src/networks')

var secureRandom = require('secure-random')
var sinon = require('sinon')

var BigInteger = require('bigi')
var ECKey = require('../src/eckey')

Expand Down Expand Up @@ -76,6 +79,42 @@ describe('ECKey', function() {
})
})

describe('makeRandom', function() {
var exWIF = 'KwMWvwRJeFqxYyhZgNwYuYjbQENDAPAudQx5VEmKJrUZcq6aL2pv'
var exPrivKey = ECKey.fromWIF(exWIF)
var exBuffer = exPrivKey.d.toBuffer(32)

describe('using default RNG', function() {
beforeEach(function() {
sinon.stub(secureRandom, 'randomBuffer').returns(exBuffer)
})

afterEach(function() {
secureRandom.randomBuffer.restore()
})

it('generates a ECKey', function() {
var privKey = ECKey.makeRandom()

assert.equal(privKey.toWIF(), exWIF)
})

it('supports compression', function() {
assert.equal(ECKey.makeRandom(true).pub.compressed, true)
assert.equal(ECKey.makeRandom(false).pub.compressed, false)
})
})

it('allows a custom RNG to be used', function() {
function rng(size) {
return exBuffer.slice(0, size)
}

var privKey = ECKey.makeRandom(undefined, rng)
assert.equal(privKey.toWIF(), exWIF)
})
})

describe('signing', function() {
var hash = crypto.sha256('Vires in numeris')
var priv = ECKey.makeRandom()
Expand Down