GithubHelp home page GithubHelp logo

bip32-utils's Introduction

bip32-utils

TRAVIS NPM

js-standard-style

A set of utilities for working with BIP32. Compatible with bitcoinjs-lib ^2.0.0 and ^3.0.0.

Example

BIP32 Account

const bip39 = require('bip39')
const bitcoin = require('bitcoinjs-lib')
const bip32utils = require('bip32-utils')

let mnemonic = bip39.generateMnemonic()
let seed = bip39.mnemonicToSeedSync(mnemonic)

let hdNode = bitcoin.bip32.fromSeed(seed)

let childNode = hdNode.deriveHardened(0)
let external = childNode.derive(0)
let internal = childNode.derive(1)
let account = new bip32utils.Account([
  new bip32utils.Chain(external.neutered()),
  new bip32utils.Chain(internal.neutered())
])

console.log(account.getChainAddress(0))
// => 1QEj2WQD9vxTzsGEvnmLpvzeLVrpzyKkGt

account.nextChainAddress(0)

console.log(account.getChainAddress(1))
// => 1DAi282VN7Ack9o5BqWYkiEsS8Vgx1rLn

console.log(account.nextChainAddress(1))
// => 1CXKM323V3kkrHmZQYPUTftGh9VrAWuAYX

console.log(account.derive('1QEj2WQD9vxTzsGEvnmLpvzeLVrpzyKkGt'))
// => xpub6A5Fz4JZg4kd8pLTTaMBKsvVgzRBrvai6ChoxWNTtYQ3UDVG1VyAWQqi6SNqkpsfsx9F8pRqwtKUbU4j4gqpuN2gpgQs4DiJxsJQvTjdzfA

// NOTE: passing in the parent nodes allows for private key escalation (see xprv vs xpub)

console.log(account.derive('1QEj2WQD9vxTzsGEvnmLpvzeLVrpzyKkGt', [external, internal]))
// => xprv9vodQPEygdPGUWeKUVNd6M2N533PvEYP21tYxznauyhrYBBCmdKxRJzmnsTsSNqfTJPrDF98GbLCm6xRnjceZ238Qkf5GQGHk79CrFqtG4d

BIP32 Chains

const bip39 = require('bip39')
const bitcoin = require('bitcoinjs-lib')
const bip32utils = require('bip32-utils')

let mnemonic = bip39.generateMnemonic()
let seed = bip39.mnemonicToSeedSync(mnemonic)

let hdNode = bitcoin.bip32.fromSeed(seed)

let chain = new bip32utils.Chain(hdNode)

let address = chain.get()
console.log(chain.find(address))
// => 0

for (let k = 0; k < 10; ++k) chain.next()

address = chain.get()

console.log(chain.find(address))
// => 10

console.log(chain.pop())
// => pops the 11th address

BIP32 Discovery (manual)

let bip32utils = require('bip32-utils')
let bitcoin = require('bitcoinjs-lib')
let Blockchain = require('cb-blockr')

// ...

let blockchain = new Blockchain('testnet')
let hdNode = bitcoin.HDNode.fromSeedHex(seedHex)
let chain = bip32utils.Chain(hdNode)
let GAP_LIMIT = 20

bip32utils.discovery(chain, GAP_LIMIT, function(addresses, callback) {
  blockchain.addresses.summary(addresses, function(err, results) {
    if (err) return callback(err)

    let areUsed = results.map(function(result) {
      return result.totalReceived > 0
    })

    callback(undefined, areUsed)
  })
}, function(err, used, checked) {
  if (err) throw err

  console.log('Discovered at most ' + used + ' used addresses')
  console.log('Checked ' + checked + ' addresses')
  console.log('With at least ' + (checked - used) + ' unused addresses')

  // throw away ALL unused addresses AFTER the last unused address
  let unused = checked - used
  for (let i = 1; i < unused; ++i) chain.pop()

  // remember used !== total, chain may have started at a k-index > 0
  console.log('Total number of addresses (after prune): ', chain.addresses.length)
})

LICENSE MIT

bip32-utils's People

Contributors

adoliin avatar bitspill avatar cloneearth avatar dcousens avatar emilkarl avatar fanatid avatar junderw avatar ostlerdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bip32-utils's Issues

Unable to derivate xPub from Account

Hello :) I am trying to derive the xPub having the account, if I use the code in readme:
account.getChildrenMap(account.getAllAddresses())
what I return is not the xpub but the entire hdnode like this :

mn.....gKR {
  chainCode:Uint8Array[32]
  depth:5
  index:0
  keyPair:ECPair
  parentFingerprint:61...83
}

Then If I use the .toBase58 method on the resulting keyPair I obtain the xprv...
What I am doing wrong?

Documentation?

I recently started learning about HD wallets and found this library. I was wondering whether there was any documentation available on the API.

Also, compared to the BIP32 repo, does bip32-utils provide a low(er) level interface -- but the same interface in high-level?

Unexpected token: new

I'm relatively new to bip32-utils and it's the reason I'm finally learning Node.js. So far all good but I've encountered this problem and I don't know how to solve that, and I've took it from your examples. Thanks

var account = new bip32utils.Account([
     new bip32utils.Chain(external.neutered())
    new bip32utils.Chain(internal.neutered())
])

Error message:
^^^
new bip32utils.Chain(internal.neutered())

SyntaxError: Unexpected token new

at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)

Process finished with exit code 1
^^^

Instantiate a BIP32 class from a private key?

the methods .fromEntropy and .fromExtendedKey allow instantiating a class from a BIP39 phrase or an extended private/public key, but how can I do it using the hex (ascii) representation of a private key alone?

CKD: Public parent key → public child key

Hello!

I was wondering how I could implement the described process in the BIP32 documentation using this library?

The function CKDpub((Kpar, cpar), i) → (Ki, ci) computes a child extended public key from the parent extended public key. It is only defined for non-hardened child keys.

Check whether i ≥ 231 (whether the child is a hardened key).
If so (hardened child): return failure
If not (normal child): let I = HMAC-SHA512(Key = cpar, Data = serP(Kpar) || ser32(i)).
Split I into two 32-byte sequences, IL and IR.
The returned child key Ki is point(parse256(IL)) + Kpar.
The returned chain code ci is IR.
In case parse256(IL) ≥ n or Ki is the point at infinity, the resulting key is invalid, and one should proceed with the next value for i.

The main idea behind this is that I'd like to generate a BIP39 mnemonic phrase key, obtain a public address linked from it, and then only be able to generate public addresses -without the ability to recover privatekeys- and without having to keep any sensible information on the machine.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.