Browse Source

added tests for privateKey/publicKey

master
JP Richardson 11 years ago
parent
commit
87c873491f
  1. 2
      lib/hdkey.js
  2. 3
      package.json
  3. 40
      test/hdkey.test.js

2
lib/hdkey.js

@ -45,7 +45,7 @@ Object.defineProperty(HDKey.prototype, 'publicKey', {
},
set: function(value) {
assert(value.length === 33 || value.length === 65, 'Public key must be 33 or 65 bytes.')
var pt = Point.decodeFrom(ecparams.curve, value)
var pt = Point.decodeFrom(ecparams, value)
this._publicKey = pt.getEncoded(true) //force compressed point
this._privateKey = null
this._privateKeyInteger = null

3
package.json

@ -25,7 +25,8 @@
"bs58": "^1.0.0",
"coveralls": "^2.10.0",
"mocha-lcov-reporter": "0.0.1",
"istanbul": "^0.2.10"
"istanbul": "^0.2.10",
"secure-random": "^1.0.0"
},
"dependencies": {
"sha512": "0.0.1",

40
test/hdkey.test.js

@ -1,8 +1,12 @@
var assert = require('assert')
var crypto = require('crypto')
var BigInteger = require('bigi')
var bs58 = require('bs58')
var HDKey = require('../')
var ecurve = require('ecurve')
var secureRandom = require('secure-random')
var ecparams = ecurve.getCurveByName('secp256k1')
var HDKey = require('../')
var fixtures = require('./fixtures/hdkey')
function encode(buf) {
@ -24,4 +28,38 @@ describe('hdkey', function() {
})
})
})
describe('- privateKey', function() {
it('should throw an error if incorrect key size', function() {
var hdkey = new HDKey()
assert.throws(function() {
hdkey.privateKey = new Buffer([1,2,3,4])
},/key must be 32/)
})
})
describe('- publicKey', function() {
it('should throw an error if incorrect key size', function() {
assert.throws(function() {
var hdkey = new HDKey()
hdkey.publicKey = new Buffer([1,2,3,4])
},/key must be 33 or 65/)
})
it('should not throw if key is 33 bytes (compressed)', function() {
var priv = secureRandom.randomBuffer(32)
var pub = ecparams.params.G.multiply(BigInteger.fromBuffer(priv)).getEncoded(true)
assert.equal(pub.length, 33)
var hdkey = new HDKey()
hdkey.publicKey = pub
})
it('should not throw if key is 65 bytes (not compressed)', function() {
var priv = secureRandom.randomBuffer(32)
var pub = ecparams.params.G.multiply(BigInteger.fromBuffer(priv)).getEncoded(false)
assert.equal(pub.length, 65)
var hdkey = new HDKey()
hdkey.publicKey = pub
})
})
})
Loading…
Cancel
Save