github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/go-bip32/README.md (about)

     1  # go-bip32
     2  
     3  A fully compliant implementation of the BIP0032 spec for Hierarchical Deterministic Bitcoin addresses
     4  
     5  
     6  ## Example
     7  
     8  ```go
     9  package main
    10  
    11  import (
    12    "github.com/tyler-smith/go-bip32"
    13    "fmt"
    14    "log"
    15  )
    16  
    17  // Example address creation for a ficticious company ComputerVoice Inc. where
    18  // each department has their own wallet to manage
    19  func main(){
    20    // Generate a seed to determine all keys from.
    21    // This should be persisted, backed up, and secured
    22    seed, err := bip32.NewSeed()
    23    if err != nil {
    24      log.Fatalln("Error generating seed:", err)
    25    }
    26  
    27    // Create master private key from seed
    28    computerVoiceMasterKey, _ := bip32.NewMasterKey(seed)
    29  
    30    // Map departments to keys
    31    // There is a very small chance a given child index is invalid
    32    // If so your real program should handle this by skpping the index
    33    departmentKeys := map[string]*bip32.Key{}
    34    departmentKeys["Sales"], _ = computerVoiceMasterKey.NewChildKey(0)
    35    departmentKeys["Marketing"], _ = computerVoiceMasterKey.NewChildKey(1)
    36    departmentKeys["Engineering"], _ = computerVoiceMasterKey.NewChildKey(2)
    37    departmentKeys["Customer Support"], _ = computerVoiceMasterKey.NewChildKey(3)
    38  
    39    // Create public keys for record keeping, auditors, payroll, etc
    40    departmentAuditKeys := map[string]*bip32.Key{}
    41    departmentAuditKeys["Sales"] = departmentKeys["Sales"].PublicKey()
    42    departmentAuditKeys["Marketing"] = departmentKeys["Marketing"].PublicKey()
    43    departmentAuditKeys["Engineering"] = departmentKeys["Engineering"].PublicKey()
    44    departmentAuditKeys["Customer Support"] = departmentKeys["Customer Support"].PublicKey()
    45  
    46    // Print public keys
    47    for department, pubKey := range departmentAuditKeys {
    48      fmt.Println(department, pubKey)
    49    }
    50  }
    51  ```