This repository was archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Bip32fix #115
Closed
Closed
Bip32fix #115
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e1eefb1
Fix PrivateKey.toBuffer
fanatid 89b4697
Add note to README for 1.0.0 staging/development
5f1bcb0
Remove Node.js 0.10, 0.12 and iojs 2.0.0 from travis ci
5a7069d
Fix implementation of hd derivation to be bip32 compliant
f42bbd3
Handle edge case that invalid private key is derived
1c8fe1e
Handle invalid public key derivation rather than throw error
d0e8627
Add toBufferNoPadding method to private key
6e311fe
defaults to nonCompliant key derivation
matiu 7083da2
update docs to `getChild`
matiu 218cf31
better warn message
matiu 40ab42d
rm warning
matiu 183a09c
update doc
matiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,6 @@ | ||
| language: node_js | ||
| sudo: false | ||
| node_js: | ||
| - '0.10' | ||
| - '0.12' | ||
| - '2.0.0' | ||
| - '4' | ||
| before_install: | ||
| - npm install -g bower | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| **Note**: This is a development/staging branch for 1.0.0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be removed. |
||
|
|
||
| Bitcore Library | ||
| ======= | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,6 +127,31 @@ HDPrivateKey._getDerivationIndexes = function(path) { | |
| return _.any(indexes, isNaN) ? null : indexes; | ||
| }; | ||
|
|
||
|
|
||
| /** | ||
| * | ||
| * Get a derived child based on a string or number. This method should not be called directly. Please use: `getChild` or `getNonCompliantChild'. | ||
| * | ||
| */ | ||
| HDPrivateKey.prototype.derive = function(arg, hardened, nonCompliant) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are adding |
||
|
|
||
| // Defaults to the nonCompliant derivation, for backwards compatibility | ||
| if (_.isUndefined(nonCompliant)) | ||
| nonCompliant = true; | ||
|
|
||
| if (_.isNumber(arg)) { | ||
| return this._deriveWithNumber(arg, hardened, nonCompliant); | ||
| } else if (_.isString(arg)) { | ||
| return this._deriveFromString(arg, nonCompliant); | ||
| } else { | ||
| throw new hdErrors.InvalidDerivationArgument(arg); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Get a derived child based on a string or number. | ||
| * | ||
|
|
@@ -142,25 +167,40 @@ HDPrivateKey._getDerivationIndexes = function(path) { | |
| * @example | ||
| * ```javascript | ||
| * var parent = new HDPrivateKey('xprv...'); | ||
| * var child_0_1_2h = parent.derive(0).derive(1).derive(2, true); | ||
| * var copy_of_child_0_1_2h = parent.derive("m/0/1/2'"); | ||
| * var child_0_1_2h = parent.getChild(0).getChild(1).getChild(2, true); | ||
| * var copy_of_child_0_1_2h = parent.getChild("m/0/1/2'"); | ||
| * assert(child_0_1_2h.xprivkey === copy_of_child_0_1_2h); | ||
| * ``` | ||
| * | ||
| * @param {string|number} arg | ||
| * @param {boolean?} hardened | ||
| * | ||
| */ | ||
| HDPrivateKey.prototype.derive = function(arg, hardened) { | ||
| if (_.isNumber(arg)) { | ||
| return this._deriveWithNumber(arg, hardened); | ||
| } else if (_.isString(arg)) { | ||
| return this._deriveFromString(arg); | ||
| } else { | ||
| throw new hdErrors.InvalidDerivationArgument(arg); | ||
| } | ||
|
|
||
| HDPrivateKey.prototype.getChild = function(arg, hardened) { | ||
| var derived = this.derive(arg, hardened, false); | ||
| return derived; | ||
| }; | ||
|
|
||
| /** | ||
| * WARNING: If this is a new implementation you should NOT use this method, you should be using | ||
| * `getChild` instead. | ||
| * | ||
| * This method is explicitly for use and compatibility with an implementation that | ||
| * was not compliant with BIP32 regarding the derivation algorithm. The private key | ||
| * must be 32 bytes hashing, and this implementation will use the non-zero padded | ||
| * serialization of a private key, such that it's still possible to derive the privateKey | ||
| * to recover those funds. | ||
| * | ||
| * @param {string|number} arg | ||
| * @param {boolean?} hardened | ||
| */ | ||
| HDPrivateKey.prototype.getNonCompliantChild = function(arg, hardened) { | ||
| var derived = this.derive(arg, hardened, true); | ||
| return derived; | ||
| }; | ||
|
|
||
| HDPrivateKey.prototype._deriveWithNumber = function(index, hardened) { | ||
| HDPrivateKey.prototype._deriveWithNumber = function(index, hardened, nonCompliant) { | ||
| /* jshint maxstatements: 20 */ | ||
| /* jshint maxcomplexity: 10 */ | ||
| if (!HDPrivateKey.isValidPath(index, hardened)) { | ||
|
|
@@ -172,15 +212,25 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened) { | |
| index += HDPrivateKey.Hardened; | ||
| } | ||
|
|
||
| var cached = HDKeyCache.get(this.xprivkey, index, hardened); | ||
| var cached = HDKeyCache.get(this.xprivkey, index, hardened, nonCompliant); | ||
| if (cached) { | ||
| return cached; | ||
| } | ||
|
|
||
| var indexBuffer = BufferUtil.integerAsBuffer(index); | ||
| var data; | ||
| if (hardened) { | ||
| data = BufferUtil.concat([new buffer.Buffer([0]), this.privateKey.toBuffer(), indexBuffer]); | ||
| if (hardened && nonCompliant) { | ||
| // The private key serialization in this case will not be exactly 32 bytes and can be | ||
| // any value less, and the value is not zero-padded. | ||
| var nonZeroPadded = this.privateKey.bn.toBuffer(); | ||
| data = BufferUtil.concat([new buffer.Buffer([0]), nonZeroPadded, indexBuffer]); | ||
| } else if (hardened) { | ||
| // This will use a 32 byte zero padded serialization of the private key | ||
| var privateKeyBuffer = this.privateKey.bn.toBuffer({ | ||
| size: 32 | ||
| }); | ||
| assert(privateKeyBuffer.length === 32, 'length of private key buffer is expected to be 32 bytes'); | ||
| data = BufferUtil.concat([new buffer.Buffer([0]), privateKeyBuffer, indexBuffer]); | ||
| } else { | ||
| data = BufferUtil.concat([this.publicKey.toBuffer(), indexBuffer]); | ||
| } | ||
|
|
@@ -194,6 +244,11 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened) { | |
| size: 32 | ||
| }); | ||
|
|
||
| if (!PrivateKey.isValid(privateKey)) { | ||
| // Index at this point is already hardened, we can pass null as the hardened arg | ||
| return this._deriveWithNumber(index + 1, null, nonCompliant); | ||
| } | ||
|
|
||
| var derived = new HDPrivateKey({ | ||
| network: this.network, | ||
| depth: this.depth + 1, | ||
|
|
@@ -202,18 +257,18 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened) { | |
| chainCode: chainCode, | ||
| privateKey: privateKey | ||
| }); | ||
| HDKeyCache.set(this.xprivkey, index, hardened, derived); | ||
| HDKeyCache.set(this.xprivkey, index, hardened, derived, nonCompliant); | ||
| return derived; | ||
| }; | ||
|
|
||
| HDPrivateKey.prototype._deriveFromString = function(path) { | ||
| HDPrivateKey.prototype._deriveFromString = function(path, nonCompliant) { | ||
| if (!HDPrivateKey.isValidPath(path)) { | ||
| throw new hdErrors.InvalidPath(path); | ||
| } | ||
|
|
||
| var indexes = HDPrivateKey._getDerivationIndexes(path); | ||
| var derived = indexes.reduce(function(prev, index) { | ||
| return prev._deriveWithNumber(index); | ||
| return prev._deriveWithNumber(index, null, nonCompliant); | ||
| }, this); | ||
|
|
||
| return derived; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add v6 to the testing matrix? I don't where this project stands in terms of node versions >= 4
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a PR that does this at #79 there are several changes needed in the build to fix this, primarily because of how npm@3 installs dependencies (more flat).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we aren't going to make this PR as a breaking change, it would make sense to keep these versions here.