create-xpub
Advanced tools
Comparing version 0.0.1 to 1.0.0
{ | ||
"name": "create-xpub", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "Create a BIP32 extended public key", | ||
@@ -37,4 +37,5 @@ "main": "src/index.js", | ||
"bs58check": "^2.1.2", | ||
"hash.js": "^1.1.7" | ||
"hash.js": "^1.1.7", | ||
"ow": "^0.8.0" | ||
} | ||
} |
@@ -9,2 +9,4 @@ # create-xpub | ||
Creates a Base58 encoded extended public key (xpub) for use in a [BIP32 hierarchical deterministic wallet](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki). | ||
## Install | ||
@@ -16,4 +18,85 @@ | ||
## Usage | ||
You should familiarise yourself with [BIP32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) to understand what these arguments represent. | ||
```js | ||
const createXpub = require('create-xpub'); | ||
const xpub = createXpub({ | ||
depth: 3, | ||
childnum: 2147483648, | ||
publicKey: '048bcdcf59f046b13f1eb35b608d1211265fde8cc44fc7a5a7f7107c5cf238095328a0e0d7be17c7d3e48490e8c6433af6d2c3dacc687f3fecaa98a3d05f17de97', | ||
chainCode: '84cf7d9029cdd9fcadbb3717fd92ec0db7d7d9787c57c13c08fc887c389b566b' | ||
}); | ||
// => 'xpub6CgMcBZk66ayM9ESh7QtBmRKJbsa6rBeBH2k4aQZQJGossryP5r2N2nQS4hBMG1wb8igPoH53bxtzTBaeMqJkbu8bxsih1gGkoAn23Nr8VP' | ||
``` | ||
Pass in version bytes for a different network: | ||
```js | ||
const createXpub = require('create-xpub'); | ||
const TESTNET = 0x043587CF; | ||
const xpub = createXpub({ | ||
network: TESTNET, | ||
depth: 3, | ||
childnum: 2147483648, | ||
publicKey: '048bcdcf59f046b13f1eb35b608d1211265fde8cc44fc7a5a7f7107c5cf238095328a0e0d7be17c7d3e48490e8c6433af6d2c3dacc687f3fecaa98a3d05f17de97', | ||
chainCode: '84cf7d9029cdd9fcadbb3717fd92ec0db7d7d9787c57c13c08fc887c389b566b' | ||
}); | ||
// => tpubDD3z8RPRoNYRcwRJ9JPyPgkgdiyE6Eghiud3R8ThkD2hdAXgTJh7WUTEg6mxskyBP3Fb1NnwahnwgdgC3DgYe3MRfZd2NYLWLkmBn7UWZXk' | ||
``` | ||
## API | ||
### createXpub(options) | ||
Returns a Base58 encoded extended public key. | ||
#### options | ||
Type: `Object` | ||
An object containing the following properties of the derivation path. | ||
##### network | ||
Type: `Number`<br> | ||
Default: `0x0488B21E` | ||
Network version bytes. | ||
Default is `0x0488B21E` for mainnet. | ||
##### depth | ||
Type: `Number`<br> | ||
Default: `undefined` | ||
The depth of the derived key. | ||
##### childnum | ||
Type: `Number`<br> | ||
Default: `undefined` | ||
The child number | ||
##### chainCode | ||
Type: `String`<br> | ||
Default: `undefined` | ||
The chain code | ||
##### publicKey | ||
Type: `String`<br> | ||
Default: `undefined` | ||
The public key in compressed or uncompressed form. | ||
## License | ||
MIT © Luke Childs |
@@ -0,1 +1,2 @@ | ||
const ow = require('ow'); | ||
const bs58check = require('bs58check'); | ||
@@ -35,2 +36,8 @@ const {sha256, ripemd160} = require('hash.js'); | ||
const createXpub = ({network = XPUB, depth, childnum, chainCode, publicKey}) => { | ||
ow(network, ow.number.label('network')); | ||
ow(depth, ow.number.label('depth')); | ||
ow(childnum, ow.number.label('childnum')); | ||
ow(chainCode, ow.string.label('chainCode')); | ||
ow(publicKey, ow.string.label('publicKey')); | ||
publicKey = compressPublicKey(publicKey); | ||
@@ -37,0 +44,0 @@ const fingerprint = getPublicKeyFingerprint(publicKey); |
import test from 'ava'; | ||
import createXpub from '..'; | ||
const TPUB = 0x043587CF; | ||
const xpubTestParams = { | ||
depth: 3, | ||
childnum: 2147483648, | ||
chainCode: '84cf7d9029cdd9fcadbb3717fd92ec0db7d7d9787c57c13c08fc887c389b566b', | ||
publicKey: '048bcdcf59f046b13f1eb35b608d1211265fde8cc44fc7a5a7f7107c5cf238095328a0e0d7be17c7d3e48490e8c6433af6d2c3dacc687f3fecaa98a3d05f17de97' | ||
}; | ||
const expectedXpub = 'xpub6CgMcBZk66ayM9ESh7QtBmRKJbsa6rBeBH2k4aQZQJGossryP5r2N2nQS4hBMG1wb8igPoH53bxtzTBaeMqJkbu8bxsih1gGkoAn23Nr8VP'; | ||
const expectedTpub = 'tpubDD3z8RPRoNYRcwRJ9JPyPgkgdiyE6Eghiud3R8ThkD2hdAXgTJh7WUTEg6mxskyBP3Fb1NnwahnwgdgC3DgYe3MRfZd2NYLWLkmBn7UWZXk'; | ||
test('createXpub is exported', t => { | ||
t.not(createXpub, undefined); | ||
}); | ||
test('createXpub is serialised correctly', t => { | ||
const xpub = createXpub(xpubTestParams); | ||
t.is(xpub, expectedXpub); | ||
}); | ||
test('Different networks can be passed in', t => { | ||
const tpub = createXpub({ | ||
...xpubTestParams, | ||
network: TPUB | ||
}); | ||
t.is(tpub, expectedTpub); | ||
}); | ||
test('Handles compressed or uncompressed public key', t => { | ||
const xpubUncompressedKey = createXpub(xpubTestParams); | ||
const xpubCompressedKey = createXpub({ | ||
...xpubTestParams, | ||
publicKey: '038bcdcf59f046b13f1eb35b608d1211265fde8cc44fc7a5a7f7107c5cf2380953' | ||
}); | ||
t.is(xpubUncompressedKey, xpubCompressedKey); | ||
}); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7449
77
0
101
3