Comparing version 0.0.2 to 0.0.3
244
index.js
@@ -1,131 +0,139 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const HDKey = require("hdkey"); | ||
const bitcoin = require("bitcoinjs-lib"); | ||
const bip39 = require('bip39'); | ||
const assert = require('bsert'); | ||
/** | ||
* Generate a BIP39 menemonic | ||
*/ | ||
function randomMenemonic() { | ||
return bip39.generateMnemonic(); | ||
} | ||
exports.randomMenemonic = randomMenemonic; | ||
/** | ||
* Generate a pseudo-random seed from BIP39 menemonic | ||
* @param menemonic | ||
*/ | ||
function seedFromMenemonic(menemonic) { | ||
assert(bip39.validateMnemonic(menemonic)); | ||
return bip39.mnemonicToSeedSync(menemonic).toString('hex'); | ||
} | ||
exports.seedFromMenemonic = seedFromMenemonic; | ||
/** | ||
* Derive an extended keypair from seed | ||
* @param seed The seed-string to use for generating the HDKey | ||
* @param net Network to use ("testnet", "mainnet") | ||
* @param n n-th derived keypair default:0 | ||
*/ | ||
function deriveAccount(seed, net = "mainnet", n = 0) { | ||
assert(n >= 0); | ||
let version; | ||
if (net === 'testnet') { | ||
version = { private: 0x04358394, public: 0x043587CF }; // HDKey does not support testnet keys out of the box | ||
System.register([], function (exports_1, context_1) { | ||
"use strict"; | ||
var HDKey, bitcoin, bip39, assert; | ||
var __moduleName = context_1 && context_1.id; | ||
/** | ||
* Generate a BIP39 menemonic | ||
*/ | ||
function randomMenemonic() { | ||
return bip39.generateMnemonic(); | ||
} | ||
let rootKey = HDKey.fromMasterSeed(seed, version); | ||
let childKey = rootKey.derive('m/' + n + '\''); | ||
childKey.network = version; | ||
return childKey; | ||
} | ||
exports.deriveAccount = deriveAccount; | ||
//doc https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki | ||
//lib https://github.com/cryptocoinjs/hdkey | ||
//gen http://bip32.org/ | ||
// enter bip32 key of the following formats: | ||
// - extended public mainnet key (xpub prefix) | ||
// - extended private mainnet key (xprv prefix) (not reccomended) | ||
// - extended public testnet key (tpub prefix) | ||
// - extended private testnet key (tprv prefix) (not reccomended) | ||
/** | ||
* | ||
* @param extendedKey Extended key to generate address from | ||
* @param networkName Network to use ("testnet", "mainnet") | ||
* @param n n-th derived address default:0 | ||
*/ | ||
function generateAddress(extendedKey, n = 0) { | ||
assert(n >= 0); | ||
let version; | ||
let t = checkHDKey(extendedKey); | ||
if (!t) { | ||
throw new Error("Not an extended key: " + extendedKey); | ||
exports_1("randomMenemonic", randomMenemonic); | ||
/** | ||
* Generate a pseudo-random seed from BIP39 menemonic | ||
* @param menemonic | ||
*/ | ||
function seedFromMenemonic(menemonic) { | ||
assert(bip39.validateMnemonic(menemonic)); | ||
return bip39.mnemonicToSeedSync(menemonic).toString('hex'); | ||
} | ||
if (t.net == "testnet") { | ||
version = { private: 0x04358394, public: 0x043587CF }; //HDKey does not support testnet keys out of the box | ||
exports_1("seedFromMenemonic", seedFromMenemonic); | ||
/** | ||
* Derive an extended keypair from seed | ||
* @param seed The seed-string to use for generating the HDKey | ||
* @param net Network to use ("testnet", "mainnet") | ||
* @param n n-th derived keypair default:0 | ||
*/ | ||
function deriveAccount(seed, net = "mainnet", n = 0) { | ||
assert(n >= 0); | ||
let version; | ||
if (net === 'testnet') { | ||
version = { private: 0x04358394, public: 0x043587CF }; // HDKey does not support testnet keys out of the box | ||
} | ||
let rootKey = HDKey.fromMasterSeed(seed, version); | ||
let childKey = rootKey.derive('m/' + n + '\''); | ||
childKey.network = version; | ||
return childKey; | ||
} | ||
const hdkey = HDKey.fromExtendedKey(extendedKey, version); | ||
const childkey = hdkey.derive('m/0/' + n); | ||
const address = bitcoin.payments.p2pkh({ pubkey: childkey._publicKey, network: bitcoin.networks[t.net] }).address; | ||
return address; | ||
} | ||
exports.generateAddress = generateAddress; | ||
function HDKeyFromExtendedKey(extendedKeyB58) { | ||
let hdkey; | ||
let version; | ||
let t = checkHDKey(extendedKeyB58); | ||
if (!t) { | ||
return false; | ||
exports_1("deriveAccount", deriveAccount); | ||
//doc https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki | ||
//lib https://github.com/cryptocoinjs/hdkey | ||
//gen http://bip32.org/ | ||
// enter bip32 key of the following formats: | ||
// - extended public mainnet key (xpub prefix) | ||
// - extended private mainnet key (xprv prefix) (not reccomended) | ||
// - extended public testnet key (tpub prefix) | ||
// - extended private testnet key (tprv prefix) (not reccomended) | ||
/** | ||
* | ||
* @param extendedKey Extended key to generate address from | ||
* @param networkName Network to use ("testnet", "mainnet") | ||
* @param n n-th derived address default:0 | ||
*/ | ||
function generateAddress(extendedKey, n = 0) { | ||
assert(n >= 0); | ||
let version; | ||
let t = checkHDKey(extendedKey); | ||
if (!t) { | ||
throw new Error("Not an extended key: " + extendedKey); | ||
} | ||
if (t.net == "testnet") { | ||
version = { private: 0x04358394, public: 0x043587CF }; //HDKey does not support testnet keys out of the box | ||
} | ||
const hdkey = HDKey.fromExtendedKey(extendedKey, version); | ||
const childkey = hdkey.derive('m/0/' + n); | ||
const address = bitcoin.payments.p2pkh({ pubkey: childkey._publicKey, network: bitcoin.networks[t.net] }).address; | ||
return address; | ||
} | ||
if (t.net === 'testnet') { | ||
version = { private: 0x04358394, public: 0x043587CF }; // HDKey does not support testnet keys out of the box | ||
} | ||
if (t.type === 'public') { | ||
hdkey = HDKey.fromExtendedKey(extendedKeyB58, version); | ||
} | ||
else { | ||
hdkey = HDKey.fromExtendedKey(extendedKeyB58, version); | ||
} | ||
return hdkey; | ||
} | ||
exports.HDKeyFromExtendedKey = HDKeyFromExtendedKey; | ||
function checkHDKey(key) { | ||
if (key.startsWith('x')) { | ||
if (key.startsWith('xpub')) { | ||
return { net: 'mainnet', type: 'public' }; | ||
exports_1("generateAddress", generateAddress); | ||
function HDKeyFromExtendedKey(extendedKeyB58) { | ||
let hdkey; | ||
let version; | ||
let t = checkHDKey(extendedKeyB58); | ||
if (!t) { | ||
return false; | ||
} | ||
if (t.net === 'testnet') { | ||
version = { private: 0x04358394, public: 0x043587CF }; // HDKey does not support testnet keys out of the box | ||
} | ||
if (t.type === 'public') { | ||
hdkey = HDKey.fromExtendedKey(extendedKeyB58, version); | ||
} | ||
else { | ||
return { net: 'mainnet', type: 'private' }; | ||
hdkey = HDKey.fromExtendedKey(extendedKeyB58, version); | ||
} | ||
return hdkey; | ||
} | ||
if (key.startsWith('t')) { | ||
if (key.startsWith('tpub')) { | ||
return { net: 'testnet', type: 'public' }; | ||
exports_1("HDKeyFromExtendedKey", HDKeyFromExtendedKey); | ||
function checkHDKey(key) { | ||
if (key.startsWith('x')) { | ||
if (key.startsWith('xpub')) { | ||
return { net: 'mainnet', type: 'public' }; | ||
} | ||
else { | ||
return { net: 'mainnet', type: 'private' }; | ||
} | ||
} | ||
else { | ||
return { net: 'testnet', type: 'private' }; | ||
if (key.startsWith('t')) { | ||
if (key.startsWith('tpub')) { | ||
return { net: 'testnet', type: 'public' }; | ||
} | ||
else { | ||
return { net: 'testnet', type: 'private' }; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
exports.checkHDKey = checkHDKey; | ||
function deriveAddress(extendedKey, n, derivationPrefix = 'm/0/') { | ||
let t = checkHDKey(extendedKey); | ||
let childkey = deriveKey(extendedKey, n, derivationPrefix); | ||
if (!t || !childkey) { | ||
return false; | ||
} | ||
return addressFromHDKey(childkey._publicKey, t.net); | ||
} | ||
exports.deriveAddress = deriveAddress; | ||
function deriveKey(extendedKey, n, derivationPrefix = 'm/0/') { | ||
let t = checkHDKey(extendedKey); | ||
let hdkey = HDKeyFromExtendedKey(extendedKey); | ||
if (!t || !hdkey) { | ||
return false; | ||
exports_1("checkHDKey", checkHDKey); | ||
function deriveAddress(extendedKey, n, derivationPrefix = 'm/0/') { | ||
let t = checkHDKey(extendedKey); | ||
let childkey = deriveKey(extendedKey, n, derivationPrefix); | ||
if (!t || !childkey) { | ||
return false; | ||
} | ||
return addressFromHDKey(childkey._publicKey, t.net); | ||
} | ||
return hdkey.derive(derivationPrefix + n); | ||
} | ||
exports.deriveKey = deriveKey; | ||
function addressFromHDKey(hdkey, net = "mainnet") { | ||
return bitcoin.payments.p2pkh({ pubkey: Buffer.from(hdkey), network: bitcoin.networks[net] }).address; | ||
} | ||
exports.addressFromHDKey = addressFromHDKey; | ||
exports_1("deriveAddress", deriveAddress); | ||
function deriveKey(extendedKey, n, derivationPrefix = 'm/0/') { | ||
let t = checkHDKey(extendedKey); | ||
let hdkey = HDKeyFromExtendedKey(extendedKey); | ||
if (!t || !hdkey) { | ||
return false; | ||
} | ||
return hdkey.derive(derivationPrefix + n); | ||
} | ||
exports_1("deriveKey", deriveKey); | ||
function addressFromHDKey(hdkey, net = "mainnet") { | ||
return bitcoin.payments.p2pkh({ pubkey: Buffer.from(hdkey), network: bitcoin.networks[net] }).address; | ||
} | ||
exports_1("addressFromHDKey", addressFromHDKey); | ||
return { | ||
setters: [], | ||
execute: function () { | ||
HDKey = require('hdkey'); | ||
bitcoin = require('bitcoinjs-lib'); | ||
bip39 = require('bip39'); | ||
assert = require('bsert'); | ||
} | ||
}; | ||
}); |
{ | ||
"name": "btc-hdkey", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Hierarchical Deterministic Wallet support for BTC", | ||
@@ -26,3 +26,2 @@ "main": "index.js", | ||
"index.js", | ||
"index.ts", | ||
"index.d.ts" | ||
@@ -29,0 +28,0 @@ ], |
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
7273
3
175