Skip to content

Commit

Permalink
feat: add p2wpkh in p2sh address
Browse files Browse the repository at this point in the history
  • Loading branch information
miaolz123 committed Nov 7, 2018
1 parent f005b89 commit 54277ee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 46 deletions.
81 changes: 42 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```
34 changes: 27 additions & 7 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 54277ee

Please sign in to comment.