3

How can WIF be easily converted to WitnessPubKeyHash in Go?

We need to get the public key from WIF and than calculate witness program as ripemd160(sha256(compressed_pub_key)).

But is there a function in btcutil to convert the public key to witnessProg required by NewAddressWitnessPubKeyHash function:

func NewAddressWitnessPubKeyHash(witnessProg []byte, net *chaincfg.Params) (*AddressWitnessPubKeyHash, error)
0

There are 2 cases where the version byte in the witness program is either 0 or 1.
If the version byte is 0, you need a 20-byte HASH160 of the public key. Reference wiki.

witnessProg := btcutil.Hash160(w.SerializePubKey())
p2wpkh, err := btcutil.NewAddressWitnessPubKeyHash(witnessProg, chainParams)

But I am not really sure about the case where version byte is 1.

Edit: previous implementation was probably wrong, fixed after some more research.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.