Luke Childs
6 years ago
commit
231fc7add9
7 changed files with 146 additions and 0 deletions
@ -0,0 +1,5 @@ |
|||
node_modules |
|||
.nyc_output |
|||
npm-debug.log |
|||
package-lock.json |
|||
yarn.lock |
@ -0,0 +1,8 @@ |
|||
language: node_js |
|||
node_js: |
|||
- '8' |
|||
script: npm test |
|||
after_success: npm run coverage |
|||
notifications: |
|||
email: |
|||
on_success: never |
@ -0,0 +1,21 @@ |
|||
MIT License |
|||
|
|||
Copyright (c) 2018 Luke Childs |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in all |
|||
copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
SOFTWARE. |
@ -0,0 +1,17 @@ |
|||
# create-xpub |
|||
|
|||
> Create a BIP32 extended public key |
|||
|
|||
[![Build Status](https://travis-ci.com/lukechilds/create-xpub.svg?branch=master)](https://travis-ci.com/lukechilds/create-xpub) |
|||
[![Coverage Status](https://coveralls.io/repos/github/lukechilds/create-xpub/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/create-xpub?branch=master) |
|||
[![npm](https://img.shields.io/npm/v/create-xpub.svg)](https://www.npmjs.com/package/create-xpub) |
|||
|
|||
## Install |
|||
|
|||
```shell |
|||
npm install create-xpub |
|||
``` |
|||
|
|||
## License |
|||
|
|||
MIT © Luke Childs |
@ -0,0 +1,39 @@ |
|||
{ |
|||
"name": "create-xpub", |
|||
"version": "0.0.0", |
|||
"description": "Create a BIP32 extended public key", |
|||
"main": "src/index.js", |
|||
"engines": { |
|||
"node": ">=8" |
|||
}, |
|||
"scripts": { |
|||
"test": "xo && nyc ava", |
|||
"coverage": "nyc report --reporter=text-lcov | coveralls" |
|||
}, |
|||
"repository": { |
|||
"type": "git", |
|||
"url": "git+https://github.com/lukechilds/create-xpub.git" |
|||
}, |
|||
"keywords": [ |
|||
"xpub", |
|||
"bip32", |
|||
"bitcoin", |
|||
"cryptocurrency" |
|||
], |
|||
"author": "Luke Childs <lukechilds123@gmail.com> (http://lukechilds.co.uk)", |
|||
"license": "MIT", |
|||
"bugs": { |
|||
"url": "https://github.com/lukechilds/create-xpub/issues" |
|||
}, |
|||
"homepage": "https://github.com/lukechilds/create-xpub#readme", |
|||
"devDependencies": { |
|||
"ava": "^0.25.0", |
|||
"coveralls": "^3.0.2", |
|||
"nyc": "^13.1.0", |
|||
"xo": "^0.23.0" |
|||
}, |
|||
"dependencies": { |
|||
"bs58check": "^2.1.2", |
|||
"hash.js": "^1.1.7" |
|||
} |
|||
} |
@ -0,0 +1,50 @@ |
|||
const bs58check = require('bs58check'); |
|||
const {sha256, ripemd160} = require('hash.js'); |
|||
|
|||
const XPUB = 0x0488B21E; |
|||
|
|||
const compressPublicKey = publicKey => { |
|||
if (publicKey.startsWith('02') || publicKey.startsWith('03')) { |
|||
return publicKey; |
|||
} |
|||
|
|||
const yIsEven = (parseInt(publicKey.slice(-2), 16) % 2 === 0); |
|||
|
|||
return (yIsEven ? '02' : '03') + publicKey.slice(2, 66); |
|||
}; |
|||
|
|||
const hash160 = buf => ripemd160().update( |
|||
sha256().update(buf).digest() |
|||
).digest(); |
|||
|
|||
const getPublicKeyFingerprint = publicKey => { |
|||
publicKey = Buffer.from(publicKey, 'hex'); |
|||
|
|||
const publicKeyHash = hash160(publicKey); |
|||
|
|||
return ( |
|||
((publicKeyHash[0] << 24) | |
|||
(publicKeyHash[1] << 16) | |
|||
(publicKeyHash[2] << 8) | |
|||
publicKeyHash[3]) >>> |
|||
0 |
|||
); |
|||
}; |
|||
|
|||
const createXpub = ({network = XPUB, depth, childnum, chainCode, publicKey}) => { |
|||
publicKey = compressPublicKey(publicKey); |
|||
const fingerprint = getPublicKeyFingerprint(publicKey); |
|||
|
|||
const xpub = Buffer.from([ |
|||
network.toString(16).padStart(8, '0'), |
|||
depth.toString(16).padStart(2, '0'), |
|||
fingerprint.toString(16).padStart(8, '0'), |
|||
childnum.toString(16).padStart(8, '0'), |
|||
chainCode, |
|||
publicKey |
|||
].join(''), 'hex'); |
|||
|
|||
return bs58check.encode(xpub); |
|||
}; |
|||
|
|||
module.exports = createXpub; |
@ -0,0 +1,6 @@ |
|||
import test from 'ava'; |
|||
import createXpub from '..'; |
|||
|
|||
test('createXpub is exported', t => { |
|||
t.not(createXpub, undefined); |
|||
}); |
Loading…
Reference in new issue