ods / bech32m

MIT License
1 stars 0 forks source link

Usage? #2

Closed allinbit closed 8 months ago

allinbit commented 8 months ago

Can you give usage of this lib?

Thank you.

ods commented 8 months ago

Hi @allinbit, Have you looked into the tests?

allinbit commented 8 months ago

Got it. Thx!

allinbit commented 8 months ago

How to convert a hexadecimal privatekey to taproot address use this lib?

I use bech32m but in result, it doesn't match to the correct address.

I'm not sure if it's my understanding that's wrong. Is taproot address use x-only pubkey?

I will appreciate if you explain it for me. Thank you.

ods commented 8 months ago

To calculate P2TR address you need a public key and a tweak. For degenerative case without tweak ("raw" taproot, rather not useful besides tests; you cat get it from deriveaddresses RPC call with rawtr(<key>) descriptor) it's as simple as bech32m.encode("bc", 1, x_only_public_key).

allinbit commented 8 months ago

How to get tweak value?

I use this script to calculate private key '1' address. Is it correct?

**import hashlib import ecdsa import bech32m

private_key = '0000000000000000000000000000000000000000000000000000000000000001'

sk = ecdsa.SigningKey.from_string(bytes.fromhex(private_key), curve=ecdsa.SECP256k1) public_key = sk.get_verifying_key().to_string().hex()

compressed_public_key = '02' + public_key[0:64] if int(public_key[-2:], 16) % 2 == 0 else '03' + public_key[0:64] compressed_public_key_bytes = bytes.fromhex(compressed_public_key)

ripemd160_hash_c = hashlib.new('ripemd160') ripemd160_hash_c.update(hashlib.sha256(compressed_public_key_bytes).digest()) hashed_compressed_public_key = ripemd160_hash_c.digest() hex_hashed_compressed_public_key = hashed_compressed_public_key.hex()

bech32_hrp = 'bc' witness_version = 0 witness_program = bytes.fromhex(hex_hashed_compressed_public_key) bech32_address = bech32m.encode(bech32_hrp, witness_version, witness_program)

witness_version_t = 1 witness_program_t = bytes.fromhex(compressed_public_key[2:]) taproot_address = bech32m.encode(bech32_hrp, witness_version_t, witness_program_t)

print("Bech32:", bech32_address) print("P2TR:", taproot_address)**

output: Bech32: bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 P2TR: bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0

The bech32 address is correct, but P2TR address is incorrect. Is this call 'raw' taproot? And how to get tweak value?

ods commented 8 months ago

Yes, bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0 is the raw taproot address for this key.

And how to get tweak value?

First link from Google search: https://bitcoin.stackexchange.com/q/110402/127317