Comparing version 0.2.0 to 0.3.0
41
index.js
@@ -6,9 +6,22 @@ /* | ||
const HARDENED = 0x80000000 | ||
var BIPPath = function (path) { | ||
if (!Array.isArray(path)) { | ||
throw new Error('Input must be an Array') | ||
} | ||
if (path.length === 0) { | ||
throw new Error('Path must contain at least one level') | ||
} | ||
for (var i = 0; i < path.length; i++) { | ||
if (typeof path[i] !== 'number') { | ||
throw new Error('Path element is not a number') | ||
} | ||
} | ||
this.path = path | ||
} | ||
BIPPath.validateString = function (text) { | ||
BIPPath.validatePathArray = function (path) { | ||
try { | ||
BIPPath.fromString(text) | ||
BIPPath.fromPathArray(path) | ||
return true | ||
@@ -20,11 +33,21 @@ } catch (e) { | ||
BIPPath.fromTrezor = function (path) { | ||
// FIXME: check input? | ||
BIPPath.validateString = function (text, reqRoot) { | ||
try { | ||
BIPPath.fromString(text, reqRoot) | ||
return true | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
BIPPath.fromPathArray = function (path) { | ||
return new BIPPath(path) | ||
} | ||
BIPPath.fromString = function (text) { | ||
BIPPath.fromString = function (text, reqRoot) { | ||
// skip the root | ||
if (text.startsWith('m/')) { | ||
text = text.slice(2) | ||
} else if (reqRoot) { | ||
throw new Error('Root element is required') | ||
} | ||
@@ -41,3 +64,3 @@ | ||
if (tmp[2] === 'h' || tmp[2] === 'H' || tmp[2] === '\'') { | ||
ret[i] |= 0x80000000 | ||
ret[i] |= HARDENED | ||
} else if (tmp[2].length != 0) { | ||
@@ -50,3 +73,3 @@ throw new Error('Invalid modifier') | ||
BIPPath.prototype.toTrezor = function () { | ||
BIPPath.prototype.toPathArray = function () { | ||
return this.path | ||
@@ -59,4 +82,4 @@ } | ||
var tmp = this.path[i] | ||
if (tmp & 0x80000000) { | ||
ret[i] = (tmp & ~0x80000000) + (oldStyle ? 'h' : '\'') | ||
if (tmp & HARDENED) { | ||
ret[i] = (tmp & ~HARDENED) + (oldStyle ? 'h' : '\'') | ||
} else { | ||
@@ -63,0 +86,0 @@ ret[i] = tmp |
{ | ||
"name": "bippath", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Bitcoin BIP32 ('HD Wallet') path helpers.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ # BIPPath | ||
- `m/44h/0h/0h/0/0` where the letter `h` means hardened key | ||
- and a binary representation predominantly used by Trezor and compatible wallets | ||
- and a binary representation predominantly used by Trezor & compatible wallets and some software tools, such as [bitcoinjs-lib](https://github.com/bitcoinjs/bitcoinjs-lib) | ||
@@ -20,10 +20,11 @@ Some useful links: | ||
- `BIPPath.fromString(path)` - creates an instance with a path written as text | ||
- `BIPPath.fromTrezor(path)` - creates an instance with a path as an array (Trezor-style) | ||
- `new BIPPath(path)` - alias for `BIPPath.fromTrezor(path)` | ||
- `<bippath>.toTrezor()` - returns a Trezor-style array | ||
- `BIPPath.fromString(path, reqRoot)` - creates an instance from a path written as text. Set `reqRoot` to true if the `m/` prefix is mandatory. | ||
- `BIPPath.fromPathArray(path)` - creates an instance from a binary path array | ||
- `new BIPPath(path)` - alias for `BIPPath.fromPathArray(path)` | ||
- `<bippath>.toString(noRoot, oldStyle)` - returns a text encoded path. Set to `noRoot` to true to omit the `m/` prefix. Set `oldStyle` true to use `h` instead of `'` for marking hardened nodes. | ||
- `BIPPath.validateString(path)` - returns true if the input is a valid path | ||
- `<bippath>.toPathArray()` - returns a binary path array | ||
- `BIPPath.validateString(path, reqRoot)` - returns true if the input is a valid path string | ||
- `BIPPath.validatePathArray(path)` - returns true if the input is a valid binary path array | ||
Trezor-style arrays contain each node as a separate number, where hardened nodes are marked by setting the 32th bit: `m/44'/1/1/0` corresponds to `[ 44 | 0x80000000, 1, 1, 0 ]` | ||
Binary path arrays contain each node as a separate number, where hardened nodes are marked by setting the 32th bit: `m/44'/1/1/0` corresponds to `[ 44 | 0x80000000, 1, 1, 0 ]` | ||
@@ -36,3 +37,3 @@ | ||
bippath.fromTrezor([44 | 0x80000000, 1, 1, 0]).toString() // m/44'/1/1/0 | ||
bippath.fromPathArray([44 | 0x80000000, 1, 1, 0]).toString() // m/44'/1/1/0 | ||
@@ -43,3 +44,3 @@ bippath.fromString("m/44'/0'/0'").toString(false, true) // m/44h/0h/0h | ||
bippath.fromString("m/44'/0'/0'").toTrezor() // [ 0x80000044, 0x80000000, 0x80000000 ] | ||
bippath.fromString("m/44'/0'/0'").toPathArray() // [ 0x80000044, 0x80000000, 0x80000000 ] | ||
``` |
@@ -6,11 +6,36 @@ /* global describe, it */ | ||
describe('fromTrezor()', function () { | ||
describe('fromPathArray()', function () { | ||
it('should work with proper input', function () { | ||
assert.equal(bippath.fromTrezor([44 | 0x80000000, 1, 1, 0]).toString(), 'm/44\'/1/1/0') | ||
assert.equal(bippath.fromPathArray([44 | 0x80000000, 1, 1, 0]).toString(), 'm/44\'/1/1/0') | ||
}) | ||
it('should fail for no parameter', function () { | ||
assert.throws(function () { | ||
bippath.fromPathArray() | ||
}) | ||
}) | ||
it('should fail for number', function () { | ||
assert.throws(function () { | ||
bippath.fromPathArray(1) | ||
}) | ||
}) | ||
it('should fail for string', function () { | ||
assert.throws(function () { | ||
bippath.fromPathArray('wrong') | ||
}) | ||
}) | ||
it('should fail for empty array', function () { | ||
assert.throws(function () { | ||
bippath.fromPathArray([]) | ||
}) | ||
}) | ||
it('should fail for non-number array', function () { | ||
assert.throws(function () { | ||
bippath.fromPathArray([ 1, 'wrong' ]) | ||
}) | ||
}) | ||
}) | ||
describe('toTrezor()', function () { | ||
describe('toPathArray()', function () { | ||
it('should work with proper input', function () { | ||
assert.deepEqual(bippath.fromTrezor([44 | 0x80000000, 1, 1, 0]).toTrezor(), [ 44 | 0x80000000, 1, 1, 0 ]) | ||
assert.deepEqual(bippath.fromPathArray([44 | 0x80000000, 1, 1, 0]).toPathArray(), [ 44 | 0x80000000, 1, 1, 0 ]) | ||
}) | ||
@@ -26,5 +51,13 @@ }) | ||
}) | ||
it('should work without m/ prefixt', function () { | ||
it('should work without m/ prefix', function () { | ||
assert.equal(bippath.fromString('44\'/0\'/0\'').toString(), 'm/44\'/0\'/0\'') | ||
}) | ||
it('should require the m/ prefix', function () { | ||
assert.equal(bippath.fromString('m/44\'/0\'/0\'', true).toString(), 'm/44\'/0\'/0\'') | ||
}) | ||
it('should require the m/ prefix (and fail)', function () { | ||
assert.throws(function () { | ||
bippath.fromString('44\'/0\'/0\'', true) | ||
}) | ||
}) | ||
}) | ||
@@ -34,12 +67,12 @@ | ||
it('should work with new style ouput', function () { | ||
assert.equal(bippath.fromTrezor([44 | 0x80000000, 1, 1, 0]).toString(), 'm/44\'/1/1/0') | ||
assert.equal(bippath.fromPathArray([44 | 0x80000000, 1, 1, 0]).toString(), 'm/44\'/1/1/0') | ||
}) | ||
it('should work with old style ouput', function () { | ||
assert.equal(bippath.fromTrezor([44 | 0x80000000, 1, 1, 0]).toString(false, true), 'm/44h/1/1/0') | ||
assert.equal(bippath.fromPathArray([44 | 0x80000000, 1, 1, 0]).toString(false, true), 'm/44h/1/1/0') | ||
}) | ||
it('should work with new style ouput (without m/ prefix)', function () { | ||
assert.equal(bippath.fromTrezor([44 | 0x80000000, 1, 1, 0]).toString(true), '44\'/1/1/0') | ||
assert.equal(bippath.fromPathArray([44 | 0x80000000, 1, 1, 0]).toString(true), '44\'/1/1/0') | ||
}) | ||
it('should work with old style ouput (without m/ prefix)', function () { | ||
assert.equal(bippath.fromTrezor([44 | 0x80000000, 1, 1, 0]).toString(true, true), '44h/1/1/0') | ||
assert.equal(bippath.fromPathArray([44 | 0x80000000, 1, 1, 0]).toString(true, true), '44h/1/1/0') | ||
}) | ||
@@ -54,2 +87,3 @@ }) | ||
assert.equal(bippath.validateString('44\'/1'), true); | ||
assert.equal(bippath.validateString('m/44/1', true), true); | ||
}) | ||
@@ -60,3 +94,18 @@ it('should fail', function () { | ||
assert.equal(bippath.validateString(''), false); | ||
assert.equal(bippath.validateString('44/1', true), false); | ||
}) | ||
}) | ||
describe('validatePathArray()', function () { | ||
it('should work', function () { | ||
assert.equal(bippath.validatePathArray([ 44 ]), true); | ||
assert.equal(bippath.validatePathArray([ 44 | 0x80000000, 1 ]), true); | ||
}) | ||
it('should fail', function () { | ||
assert.equal(bippath.validatePathArray(), false); | ||
assert.equal(bippath.validatePathArray(1), false); | ||
assert.equal(bippath.validatePathArray('wrong'), false); | ||
assert.equal(bippath.validatePathArray([]), false); | ||
assert.equal(bippath.validatePathArray([ 'wrong' ]), false); | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10108
179
43