bip32

package
v0.0.0-...-3606b63 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2019 License: BSD-3-Clause, MIT Imports: 14 Imported by: 0

README

go-bip32

A fully compliant implementation of the BIP0032 spec for Hierarchical Deterministic Bitcoin addresses

Example

package main

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

// Example address creation for a ficticious 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 skpping 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)
  }
}

Documentation

Overview

Package bip32 A fully compliant implementation of the BIP0032 spec for Hierarchical Deterministic Bitcoin addresses

Index

Constants

View Source
const (
	// FirstHardenedChild FirstHardenedChild
	FirstHardenedChild = uint32(0x80000000)
	// PublicKeyCompressedLength 公钥压缩长度
	PublicKeyCompressedLength = 33
)

Variables

View Source
var (
	// PrivateWalletVersion 私钥钱包版本
	PrivateWalletVersion, _ = hex.DecodeString("0488ADE4")
	// PublicWalletVersion 公钥钱包版本
	PublicWalletVersion, _ = hex.DecodeString("0488B21E")
)

Functions

func NewSeed

func NewSeed() ([]byte, error)

NewSeed Cryptographically secure seed

Types

type Key

type Key struct {
	Version     []byte // 4 bytes
	Depth       byte   // 1 bytes
	ChildNumber []byte // 4 bytes
	FingerPrint []byte // 4 bytes
	ChainCode   []byte // 32 bytes
	Key         []byte // 33 bytes
	IsPrivate   bool   // unserialized
}

Key Represents a bip32 extended key containing key data, chain code, parent information, and other meta data

func NewMasterKey

func NewMasterKey(seed []byte) (*Key, error)

NewMasterKey Creates a new master extended key from a seed

func (*Key) NewChildKey

func (key *Key) NewChildKey(childIdx uint32) (*Key, error)

NewChildKey Derives a child key from a given parent as outlined by bip32

func (*Key) PublicKey

func (key *Key) PublicKey() *Key

PublicKey Create public version of key or return a copy; 'Neuter' function from the bip32 spec

func (*Key) Serialize

func (key *Key) Serialize() []byte

Serialize Serialized an Key to a 78 byte byte slice

func (*Key) String

func (key *Key) String() string

String Encode the Key in the standard Bitcoin base58 encoding

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL