fortesp / bitcoinaddress

Bitcoin Wallet Address Generator
https://pypi.org/project/bitcoinaddress/
MIT License
146 stars 54 forks source link

p2wsh addresses #13

Open s256s9 opened 3 years ago

s256s9 commented 3 years ago

I'm looking for to double check some of my python code outputs and find that your outputs for p2wsh doesnt match this sample which I was guided:

https://bc-2.jp/tools/bech32demo/index.html

Could You check your metod for this and explain how there is difference.

Thanks in advance.

fortesp commented 3 years ago

Could you provide an actual practical example with private keys? Thanks.

s256s9 commented 3 years ago

Sure, For sample private key:

6b88c087247aa2f07ee1c5956b8e1a9f4c7f892a70e324f1bb3d161e05ca107b

your code get me:

Private Key HEX: 6b88c087247aa2f07ee1c5956b8e1a9f4c7f892a70e324f1bb3d161e05ca107b

Private Key WIF: 5JdeQ39z8NUkNVvB37tt74Cu2WSNVj7qb9PdY651UoQnqyCm937

Private Key WIF compressed: Kzpk4gWf9XoURtdhJMRzZj9WdevysnuBTi2tGDNh7hr1LpJcgvXd

Public Key: 04218ad6cdc632e7ae7d04472374311cebbbbf0ab540d2d08c3400bb844c654231b385c9781def7eabb9900860621d6f1ba5ac3858265459ef5c217feb9568c37c

Public Key compressed: 02218ad6cdc632e7ae7d04472374311cebbbbf0ab540d2d08c3400bb844c654231

Public Address 1: 1E984zyYbNmeuumzEdqT8VSL8QGJi3byAD

Public Address 1 compressed: 18VkRiDhFu2Z17AvtpU3vL2LbTXDzCvDVo

Public Address 3: 33Hfmw1a28beXWp3EYibo7MaDfdUPoqTCn

Public Address bc1 P2WPKH: bc1q2guvw9zcu3jdnlus9xdtef9p67uuka4tze5k6y

Public Address bc1 P2WSH: bc1qaft3757t82vxt57uw3e4urqkvs7nr8r2mq0pnxuussyvaj7wc7asvlwrp0

From my point all is correct but p2wsh part

In bech32 demo output (which I linked above) for p2wsh is:

bc1q42z3npmypmj57k9yv9spskzszg0m4dmh2yyu3tq6xddjcp8pyx0q80uswh

I look into your code and find in address.py line 132:

redeem_script_P2WSH = hashlib.sha256(p).digest()

In method which I have redeem_script_P2WSH looks like hashlib.sha256(p_red).digest()

where is p_red = 0x21 + p + 0xac

I try to correct this for myself but because rest of code is too complex for my level of py knowledge and I only get errors.

Thanks for your time anyway.

If someone know more py or js code for double check this p2wsh part it will be welcome.

s256s9 commented 3 years ago

I finally succeed to get method which I have with no errors.

I put this part in that section:

pr1 = bytes.fromhex('21') po1 = bytes.fromhex('ac') p_red = pr1 + p + po1 redeem_script_P2WSH = hashlib.sha256(p_red).digest()

thanks

fortesp commented 3 years ago

Would you do a pull request? Thank you for your help!

Jolly-Pirate commented 3 years ago

I'm not sure what's accurate anymore... I think this needs further investigation. In the example private key 6b88c087247aa2f07ee1c5956b8e1a9f4c7f892a70e324f1bb3d161e05ca107b, we have 3 different results:

bc1qaft3757t82vxt57uw3e4urqkvs7nr8r2mq0pnxuussyvaj7wc7asvlwrp0 original bitcoinadress output bc1q3muwly6pm3u4tgf9xqr44t6d490d6e4j7syrsf2xj2tx0rnzaw9s49kpk0 website output https://bc-2.jp/tools/bech32demo/index.html bc1q42z3npmypmj57k9yv9spskzszg0m4dmh2yyu3tq6xddjcp8pyx0q80uswh bitcoinadress after the proposed patch

fortesp commented 3 years ago

I tested this somewhere else and i got your result. That why i was confident about your solution. I need check it again.

fazlearefin commented 2 years ago

It seems your implementation of P2WSH is not correct. If you look up this page https://iancoleman.io/bip39/ , select BIP141 in the Derivation Path and choose P2WSH (1-of-1 multisig), the addresses generated for the corresponding private keys does not match your implementation.

The website uses open source JS code but my JS knowledge is limited to figure out how they are generating the P2WSH.