Giter VIP home page Giter VIP logo

go-bip32's Introduction

go-bip32

PkgGoDev GitHub release MIT License Contributors

Build check Go Report Card Coverage Status

An implementation of the BIP32 spec for Hierarchical Deterministic Bitcoin addresses as a simple Go library. The semantics of derived keys are up to the user. BIP43 and BIP44 are good schemes to implement with this library. An additional library for either or both of those on top of this library should be developed.

Example

It's very unlikely, but possible, that a given index does not produce a valid private key. Error checking is skipped in this example for brevity but should be handled in real code. In such a case, a ErrInvalidPrivateKey is returned.

ErrInvalidPrivateKey should be handled by trying the next index for a child key.

Any valid private key will have a valid public key so that Key.PublicKey() method never returns an error.

package main

import (
  "github.com/tyler-smith/go-bip32"
  "fmt"
  "log"
)

// Example address creation for a fictitious company ComputerVoice Inc. where
// each department has their own wallet to manage
func main(){
  // Generate a seed to determine all keys from.
  // This should be persisted, backed up, and secured
  seed, err := bip32.NewSeed()
  if err != nil {
    log.Fatalln("Error generating seed:", err)
  }

  // Create master private key from seed
  computerVoiceMasterKey, _ := bip32.NewMasterKey(seed)

  // Map departments to keys
  // There is a very small chance a given child index is invalid
  // If so your real program should handle this by skipping the index
  departmentKeys := map[string]*bip32.Key{}
  departmentKeys["Sales"], _ = computerVoiceMasterKey.NewChildKey(0)
  departmentKeys["Marketing"], _ = computerVoiceMasterKey.NewChildKey(1)
  departmentKeys["Engineering"], _ = computerVoiceMasterKey.NewChildKey(2)
  departmentKeys["Customer Support"], _ = computerVoiceMasterKey.NewChildKey(3)

  // Create public keys for record keeping, auditors, payroll, etc
  departmentAuditKeys := map[string]*bip32.Key{}
  departmentAuditKeys["Sales"] = departmentKeys["Sales"].PublicKey()
  departmentAuditKeys["Marketing"] = departmentKeys["Marketing"].PublicKey()
  departmentAuditKeys["Engineering"] = departmentKeys["Engineering"].PublicKey()
  departmentAuditKeys["Customer Support"] = departmentKeys["Customer Support"].PublicKey()

  // Print public keys
  for department, pubKey := range departmentAuditKeys {
    fmt.Println(department, pubKey)
  }
}

Thanks

The developers at Factom have contributed a lot to this library and have made many great improvements to it. Please check out their project(s) and give them a thanks if you use this library.

Thanks to bartekn from Stellar for some important bug catches.

go-bip32's People

Contributors

bartekn avatar cpacia avatar thepiachu avatar tiero avatar tyler-smith 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  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

go-bip32's Issues

Key-length error

Recently during testing we noticed a weird error with the package - sometimes the length of the returned Key was shorter than 32. Here is an example of how to get such a key: FactomProject/go-bip44@fd672f4

It turns out the way private keys were added didn't take into account that bigInt.Bytes() truncates leading zeroes. After fixing that, it also turned out that the code checking whether a private key is valid was wrong - it tried to decode the key as a varint, meaning any leading zero was considered invalid. I assumed the code was supposed to remove any private key that was ALL zeroes. A key length checker was also implemented: FactomProject@4eb3239

Feel free to pull in the changes.

TestPublicDerivation failed

Hi,

In go-bip32, run the TestPublicDerivation tests failed, cases are here:
https://github.com/btcsuite/btcutil/blob/master/hdkeychain/extendedkey_test.go#L402

It means that the public key derivation maybe something wrong here, but I am not sure.
Please check.

Tests results:

--- FAIL: TestPublicDerivation (0.38s)
	bip32_test.go:430: Child #1 (test vector 1 chain m/0): mismatched serialized public extended key -- got: xpub69eaVGhVzLjMdmrydH5W1sgbxLZpKWDFAWd5MeqVYtudJo6QgQPRt6SgTvXc1vqY1rBi4T2sWH8kMNayqzD8m3ZrzJwueQn1knPfTtzmZrd, want: xpub68Gmy5EVb2BdFbj2LpWrk1M7obNuaPTpT5oh9QCCo5sRfqSHVYWex97WpDZzszdzHzxXDAzPLVSwybe4uPYkSk4G3gnrPqqkV9RyNzAcNJ1
	bip32_test.go:430: Child #2 (test vector 1 chain m/0/1): mismatched serialized public extended key -- got: xpub6Ar94arVeUsZYt9Fc6Rsmg5SeumUForKiVPeauNGEMZjSFf3GRjvV5FTaHKAEd1MUa1TfvRbzNDEMm7PhPH9BAESxKgumbmVqPw5YZfbCcu, want: xpub6AvUGrnEpfvJBbfx7sQ89Q8hEMPM65UteqEX4yUbUiES2jHfjexmfJoxCGSwFMZiPBaKQT1RiKWrKfuDV4vpgVs4Xn8PpPTR2i79rwHd4Zr
	bip32_test.go:430: Child #3 (test vector 1 chain m/0/1/2): mismatched serialized public extended key -- got: xpub6BnegVYDnrUEwCvWXYh5Tubs2W1viNH6UDRM3VRKF7JijycXHbZBDJiexQysSy6nDB4JwJFF6z1wfGWf6ZfSQDEHusEhQNX1FiSSzixFXvF, want: xpub6BqyndF6rhZqmgktFCBcapkwubGxPqoAZtQaYewJHXVKZcLdnqBVC8N6f6FSHWUghjuTLeubWyQWfJdk2G3tGgvgj3qngo4vLTnnSjAZckv
	bip32_test.go:430: Child #4 (test vector 1 chain m/0/1/2/2): mismatched serialized public extended key -- got: xpub6Dz8W5TGArNC82P3twDn26GnMns89jgY1h6CHKiHKCd2FhrnNYwcpdn4zBVRuE1GWhSBRMWq6bWWFepXSVAQ7L4V6mjFYxquin4mpuZPeg9, want: xpub6FHUhLbYYkgFQiFrDiXRfQFXBB2msCxKTsNyAExi6keFxQ8sHfwpogY3p3s1ePSpUqLNYks5T6a3JqpCGszt4kxbyq7tUoFP5c8KWyiDtPp
	bip32_test.go:430: Child #5 (test vector 1 chain m/0/1/2/2/1000000000): mismatched serialized public extended key -- got: xpub6GPKhRLbEUDjuGiBhPufvWadsVajCvgH9NDsqot23uumnrv4c8CVcfhEZcG5XJqt68G48aQeMbhm4z16VHj1bwg9EYZdcweUBxsFjCkgyUD, want: xpub6GX3zWVgSgPc5tgjE6ogT9nfwSADD3tdsxpzd7jJoJMqSY12Be6VQEFwDCp6wAQoZsH2iq5nNocHEaVDxBcobPrkZCjYW3QUmoDYzMFBDu9
	bip32_test.go:430: Child #7 (test vector 2 chain m/0): mismatched serialized public extended key -- got: xpub67u47hXvNiVAEfs3JAuU8KLGajbst9yEDqhR3pqoMUA9bANDeagoL8Jfr3meyXHirhT3jAbm6cy714k4SsPTeDohfEVzRAnyAVGabsDgYnA, want: xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH
	bip32_test.go:430: Child #8 (test vector 2 chain m/0/2147483647): mismatched serialized public extended key -- got: xpub6AmHX79iTJnmEfyp3FULENW9UN8pcEMJ6NpH79THhebLu1mMfD5vftv8pWJ1MVEnAYSwzNXvxUsQFJG7ww8U4tAN6q8pZbcRbiyjFbRakcy, want: xpub6ASAVgeWMg4pmutghzHG3BohahjwNwPmy2DgM6W9wGegtPrvNgjBwuZRD7hSDFhYfunq8vDgwG4ah1gVzZysgp3UsKz7VNjCnSUJJ5T4fdD
	bip32_test.go:430: Child #9 (test vector 2 chain m/0/2147483647/1): mismatched serialized public extended key -- got: xpub6Bs6WJe8Pf5D3JaaFVyJ3fFZzKVcQ1k4htAyfAS4kQTFuDixgRcxoqKNEvxVGZvekGYSQ5j5nkQagJ3gkKbBgxFpm5S7YKEKv9xrn1QXmWr, want: xpub6CrnV7NzJy4VdgP5niTpqWJiFXMAca6qBm5Hfsry77SQmN1HGYHnjsZSujoHzdxf7ZNK5UVrmDXFPiEW2ecwHGWMFGUxPC9ARipss9rXd4b
	bip32_test.go:430: Child #10 (test vector 2 chain m/0/2147483647/1/2147483646): mismatched serialized public extended key -- got: xpub6Es4peWS7qGjCpkxytmwNmqjYHnwLEtouTNZ6XrYZUJSz3Ec7gmniGAUuDyzuvG5oNbzRSTbTqptM6XvyNTMx49m3MmhdbW6F7qcXBNKDX1, want: xpub6FL2423qFaWzHCvBndkN9cbkn5cysiUeFq4eb9t9kE88jcmY63tNuLNRzpHPdAM4dUpLhZ7aUm2cJ5zF7KYonf4jAPfRqTMTRBNkQL3Tfta
	bip32_test.go:430: Child #11 (test vector 2 chain m/0/2147483647/1/2147483646/2): mismatched serialized public extended key -- got: xpub6G4y2AV7wCUkoE8MgW5o9wSPgnf3oZP6uoyJBHExD2D1x1u7EGtBQjugJPT3TTPL7cJmyoxnwk6sXjp9W8pyXVngzD3mdSnJFbS2nctyRQn, want: xpub6H7WkJf547AiSwAbX6xsm8Bmq9M9P1Gjequ5SipsjipWmtXSyp4C3uwzewedGEgAMsDy4jEvNTWtxLyqqHY9C12gaBmgUdk2CGmwachwnWK

BitcoinBase58

@tyler-smith
I've tried using bip32.BitcoinBase58Encoding.EncodeToString for address encoding.

It seems like current encoding is not handling properly leading zero.

What is more, I've cloned repository and swapped basen to btcutil/base58, unit tests passed.

package main

import (
	"fmt"

	"github.com/btcsuite/btcutil/base58"
	"github.com/tyler-smith/go-bip32"
)

func main() {
	b := []byte{0, 1}
	fmt.Println(base58.Encode(b))
	fmt.Println(bip32.BitcoinBase58Encoding.EncodeToString(b))
}
12
2

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.