Browse Source

changed behavior of fromExtendedKey, now accepts base 58 string instead of Buffer bytes

master
JP Richardson 11 years ago
parent
commit
fa63146a48
  1. 1
      CHANGELOG.md
  2. 4
      lib/hdkey.js
  3. 9
      test/hdkey.test.js

1
CHANGELOG.md

@ -2,6 +2,7 @@
------------------
- bugfix: if private key was less than 32 bytes, pad out to 32 bytes with leading zeros (this happens in derive)
- changed behavior of `privateExtendedKey()` and `publicExtendedKey()` to return base 58 encoded `string` instead of `Buffer`
- changed behavior of `fromExtendedKey()` from accepting a type of `Buffer` bytes to base58 `string`
0.2.0 / 2014-06-25
------------------

4
lib/hdkey.js

@ -175,11 +175,13 @@ HDKey.fromMasterSeed = function(seedBuffer, versions) {
return hdkey
}
HDKey.fromExtendedKey = function(keyBuffer, versions) {
HDKey.fromExtendedKey = function(base58key, versions) {
// => version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33)
versions = versions || BITCOIN_VERSIONS
var hdkey = new HDKey(versions)
var keyBuffer = cs.decode(base58key)
var version = keyBuffer.readUInt32BE(0)
assert(version === versions.private || version === versions.public, 'Version mismatch: does not match private or public')

9
test/hdkey.test.js

@ -62,8 +62,7 @@ describe('hdkey', function() {
it('should parse it', function() {
//m/0/2147483647'/1/2147483646'/2
var key = "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j"
var keyBuffer = cs.decode(key)
var hdkey = HDKey.fromExtendedKey(keyBuffer)
var hdkey = HDKey.fromExtendedKey(key)
assert.equal(hdkey.versions.private, 0x0488ade4)
assert.equal(hdkey.versions.public, 0x0488b21e)
assert.equal(hdkey.depth, 5)
@ -80,8 +79,7 @@ describe('hdkey', function() {
it('should parse it', function() {
//m/0/2147483647'/1/2147483646'/2
var key = "xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt"
var keyBuffer = cs.decode(key)
var hdkey = HDKey.fromExtendedKey(keyBuffer)
var hdkey = HDKey.fromExtendedKey(key)
assert.equal(hdkey.versions.private, 0x0488ade4)
assert.equal(hdkey.versions.public, 0x0488b21e)
assert.equal(hdkey.depth, 5)
@ -98,8 +96,7 @@ describe('hdkey', function() {
describe('> when deriving public key', function() {
it('should work', function() {
var key = "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"
var keyBuffer = cs.decode(key)
var hdkey = HDKey.fromExtendedKey(keyBuffer)
var hdkey = HDKey.fromExtendedKey(key)
var path = "m/3353535/2223/0/99424/4/33"
var derivedHDKey = hdkey.derive(path)

Loading…
Cancel
Save