Browse Source

hdkey: added toJSON()/fromJSON()

master
JP Richardson 9 years ago
parent
commit
0088681bbf
  1. 11
      lib/hdkey.js
  2. 18
      test/hdkey.test.js

11
lib/hdkey.js

@ -158,6 +158,13 @@ HDKey.prototype.deriveChild = function (index) {
return hd
}
HDKey.prototype.toJSON = function () {
return {
privateExtendedKey: this.privateExtendedKey,
publicExtendedKey: this.publicExtendedKey
}
}
HDKey.fromMasterSeed = function (seedBuffer, versions) {
var I = crypto.createHmac('sha512', MASTER_SECRET).update(seedBuffer).digest()
var IL = I.slice(0, 32)
@ -197,6 +204,10 @@ HDKey.fromExtendedKey = function (base58key, versions) {
return hdkey
}
HDKey.fromJSON = function (obj) {
return HDKey.fromExtendedKey(obj.privateExtendedKey)
}
function serialize (hdkey, version, key) {
// => version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33)
var buffer = new Buffer(LEN)

18
test/hdkey.test.js

@ -18,6 +18,24 @@ describe('hdkey', function () {
assert.equal(childkey.privateExtendedKey, f.private)
assert.equal(childkey.publicExtendedKey, f.public)
})
describe('> ' + f.path + ' toJSON() / fromJSON()', function () {
it('should return an object read for JSON serialization', function () {
var hdkey = HDKey.fromMasterSeed(new Buffer(f.seed, 'hex'))
var childkey = hdkey.derive(f.path)
var obj = {
privateExtendedKey: f.private,
publicExtendedKey: f.public
}
assert.deepEqual(childkey.toJSON(), obj)
var newKey = HDKey.fromJSON(obj)
assert.strictEqual(newKey.privateExtendedKey, f.private)
assert.strictEqual(newKey.publicExtendedKey, f.public)
})
})
})
})

Loading…
Cancel
Save