Closed jhdscript closed 6 years ago
package main
import (
"fmt"
"os"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
)
var (
btcaddr = "1EWVYro91c1rnCBb8ZVKXVasfWGW45VMKT"
btcnet = &chaincfg.MainNetParams
)
func main() {
decAddr, err := btcutil.DecodeAddress(btcaddr, btcnet)
if err != nil {
fmt.Printf("DecodeAddress: %v\n", err)
os.Exit(1)
}
pkh, ok := decAddr.(*btcutil.AddressPubKeyHash)
if !ok {
fmt.Printf("invalid type: %T\n", pkh)
os.Exit(1)
}
if !pkh.IsForNet(btcnet) {
fmt.Printf("address not for %s\n", btcnet.Name)
os.Exit(1)
}
fmt.Printf("hash160: %x\n", *pkh.Hash160())
fmt.Printf("script address: %x\n", pkh.ScriptAddress())
}
Finally i use something like this
deco := base58.Decode("198zMGToZqZh5mFHgfsc2ekMtBVJZQ9ARQ") fmt.Printf("%x",deco[1:21]) return
Instead of asserting it is a concrete type you can also type assert it to an interface that has the Hash160 method, as there are other address types that support it (such as P2SH):
type hash160er interface {
Hash160() *[20]byte
}
addrh, ok := addr.(hash160er)
if !ok {
// address can not return a hash160. it could be a pubkey, in which case you could
// convert the pubkey to a pubkey hash address, but that is no longer the same address
}
fmt.Printf("%x\n", addrh.Hash160()[:])
I would not just straight up slice the bytes out of a decoded base58 string.
Hello, i Have a question. I m trying to convert a public address (like "1EWVYro91c1rnCBb8ZVKXVasfWGW45VMKT") to hash160 using btcutil.
But I don't finder any helper to do this. Coul you help ?