From 54277ee542d64eefc251dab75d084c7c092a6c75 Mon Sep 17 00:00:00 2001 From: miaolz123 Date: Wed, 7 Nov 2018 20:30:40 +0800 Subject: [PATCH] feat: add p2wpkh in p2sh address --- README.md | 81 ++++++++++++++++++++++++++++--------------------------- key.go | 34 ++++++++++++++++++----- 2 files changed, 69 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index d8d584c..d01608e 100644 --- a/README.md +++ b/README.md @@ -28,51 +28,54 @@ go get -v -u github.com/foxnut/go-hdwallet package main import ( - "fmt" + "fmt" - "github.com/foxnut/go-hdwallet" + "github.com/foxnut/go-hdwallet" ) var ( - mnemonic = "range sheriff try enroll deer over ten level bring display stamp recycle" + mnemonic = "range sheriff try enroll deer over ten level bring display stamp recycle" ) func main() { - master, err := hdwallet.NewKey( - hdwallet.Mnemonic(mnemonic), - ) - if err != nil { - panic(err) - } - - // BTC: 1AwEPfoojHnKrhgt1vfuZAhrvPrmz7Rh4 - wallet, _ := master.GetWallet(hdwallet.CoinType(hdwallet.BTC)) - address, _ := wallet.GetAddress() - fmt.Println("BTC: ", address) - - // BCH: 1CSBT18sjcCwLCpmnnyN5iqLc46Qx7CC91 - wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.BCH)) - address, _ = wallet.GetAddress() - fmt.Println("BCH: ", address) - - // LTC: LLCaMFT8AKjDTvz1Ju8JoyYXxuug4PZZmS - wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.LTC)) - address, _ = wallet.GetAddress() - fmt.Println("LTC: ", address) - - // DOGE: DHLA3rJcCjG2tQwvnmoJzD5Ej7dBTQqhHK - wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.DOGE)) - address, _ = wallet.GetAddress() - fmt.Println("DOGE:", address) - - // ETH: 0x37039021cBA199663cBCb8e86bB63576991A28C1 - wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.ETH)) - address, _ = wallet.GetAddress() - fmt.Println("ETH: ", address) - - // ETC: 0x480C69E014C7f018dAbF17A98273e90f0b0680cf - wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.ETC)) - address, _ = wallet.GetAddress() - fmt.Println("ETC: ", address) + master, err := hdwallet.NewKey( + hdwallet.Mnemonic(mnemonic), + ) + if err != nil { + panic(err) + } + + // BTC: 1AwEPfoojHnKrhgt1vfuZAhrvPrmz7Rh4 + wallet, _ := master.GetWallet(hdwallet.CoinType(hdwallet.BTC), hdwallet.AddressIndex(1)) + address, _ := wallet.GetAddress() + addressP2WPKH, _ := wallet.GetKey().AddressP2WPKH() + addressP2WPKHInP2SH, _ := wallet.GetKey().AddressP2WPKHInP2SH() + fmt.Println("BTC: ", address, addressP2WPKH, addressP2WPKHInP2SH) + + // BCH: 1CSBT18sjcCwLCpmnnyN5iqLc46Qx7CC91 + wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.BCH)) + address, _ = wallet.GetAddress() + addressBCH, _ := wallet.GetKey().AddressBCH() + fmt.Println("BCH: ", address, addressBCH) + + // LTC: LLCaMFT8AKjDTvz1Ju8JoyYXxuug4PZZmS + wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.LTC)) + address, _ = wallet.GetAddress() + fmt.Println("LTC: ", address) + + // DOGE: DHLA3rJcCjG2tQwvnmoJzD5Ej7dBTQqhHK + wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.DOGE)) + address, _ = wallet.GetAddress() + fmt.Println("DOGE:", address) + + // ETH: 0x37039021cBA199663cBCb8e86bB63576991A28C1 + wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.ETH)) + address, _ = wallet.GetAddress() + fmt.Println("ETH: ", address) + + // ETC: 0x480C69E014C7f018dAbF17A98273e90f0b0680cf + wallet, _ = master.GetWallet(hdwallet.CoinType(hdwallet.ETC)) + address, _ = wallet.GetAddress() + fmt.Println("ETC: ", address) } ``` diff --git a/key.go b/key.go index 7a260b4..f734e9d 100644 --- a/key.go +++ b/key.go @@ -5,10 +5,10 @@ import ( "encoding/hex" "github.com/btcsuite/btcd/btcec" + "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil/hdkeychain" "github.com/cpacia/bchutil" - "golang.org/x/crypto/ripemd160" ) // Key struct @@ -158,12 +158,7 @@ func (k *Key) PublicHash() ([]byte, error) { return nil, err } - hash := make([]byte, 0, ripemd160.Size) - for _, b := range *address.Hash160() { - hash = append(hash, b) - } - - return hash, nil + return address.ScriptAddress(), nil } // AddressBTC generate public key to btc style address @@ -207,3 +202,28 @@ func (k *Key) AddressP2WPKH() (string, error) { return addr.EncodeAddress(), nil } + +// AddressP2WPKHInP2SH generate public key to p2wpkh nested within p2sh style address +func (k *Key) AddressP2WPKHInP2SH() (string, error) { + pubHash, err := k.PublicHash() + if err != nil { + return "", err + } + + addr, err := btcutil.NewAddressWitnessPubKeyHash(pubHash, k.opt.Params) + if err != nil { + return "", err + } + + script, err := txscript.PayToAddrScript(addr) + if err != nil { + return "", err + } + + addr1, err := btcutil.NewAddressScriptHash(script, k.opt.Params) + if err != nil { + return "", err + } + + return addr1.EncodeAddress(), nil +}