What are the steps to create a Bech32 address from an ECDSA public key? Bitcoin wiki has a nice step-by-step guide to create an address from a public key but what about Bech32 addresses? I've read BIP173 but I don't understand what to give as an input.
-
Maybe this can help you: github.com/sipa/bech32/tree/master/ref/python– porsCommented Feb 9, 2018 at 18:42
-
1@pors I have seen this repo but I do not know what to input. I understand I am to use the bech32_encode function but what do I put in there? The public key? The hash160? the P2WSH? How do I create that?– Mike DCommented Feb 9, 2018 at 22:04
-
Welcome to Bitcoin.SE! For @pors, you can help by posting your expanded comments as an answer and possibly including some 'how to use' information.– WilltechCommented Feb 11, 2018 at 5:43
-
nullius has created a tool in december, to play with bech32 addresses in C. Easy usable, and code is documented as well. See here: bitcointalk.org/index.php?topic=2664728.msg27178628#msg27178628– pebwindkraftCommented Feb 20, 2018 at 11:03
Add a comment
|
1 Answer
Ok, I figured out how to do it.
You need 3 pieces of information to construct the bech32 address.
hrp
: the human-readable part. This isbc
for mainnet andtb
for testnetwitver
: the witness version. This is0
at the moment represented by the byte0x00
but it can go up to16
when they add more versions.witprog
: the witness programm. In case you want a Pay-to-witness-public-key (P2WPK) address which is the most common ones, this is the 20-byte hash160 of the compressed public key i.eripemd160(sha256(compressed_pub_key))
. In case you want a Pay-to-witness-script-hash (P2WSH) address, this is the 32-byte sha256 of the scriptPubKey which is the script that will need to evaluate to True for someone to be able to spend the output. More on this in BIP141
Once you have those 3 pieces, you can use the encode function in one of the reference implementations
I have put together a small pure Python library for anyone interested how all this stuff works behind the hood.
-
Can you add some text vectors here or point to your test vectors?– Jus12Commented May 29, 2022 at 11:36
-
1