Skip to content

Commit ad30d68

Browse files
committed
Add a simple way to generate taproot from a public key
1 parent 1481983 commit ad30d68

3 files changed

Lines changed: 8 additions & 6 deletions

File tree

example/regtest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@
2525
pabtc.rpc.generate_to_address(10, pabtc.core.Address.p2sh_p2ms(2, [pub1, pub2]))
2626
pabtc.rpc.generate_to_address(10, pabtc.core.Address.p2sh_p2wpkh(pub1.hash()))
2727
pabtc.rpc.generate_to_address(10, pabtc.core.Address.p2wpkh(pub1.hash()))
28-
p2tr_pubkey = bytearray(pabtc.taproot.pubkey_tweak(pub1.pt(), bytearray()).x.n.to_bytes(32))
29-
pabtc.rpc.generate_to_address(10, pabtc.core.Address.p2tr(p2tr_pubkey))
28+
pabtc.rpc.generate_to_address(10, pabtc.core.Address.p2tr(pub1.tr(bytearray())))
3029
pabtc.rpc.generate_to_address(99, pabtc.core.Address.p2pkh(pub1.hash()))

example/taproot.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
class Signerp2trp2pk(pabtc.wallet.Signer):
1515
def __init__(self, pubkey: pabtc.core.PubKey) -> None:
1616
self.pubkey = pubkey
17-
p2tr_pubkey = bytearray(pabtc.taproot.pubkey_tweak(pubkey.pt(), mast.hash).x.n.to_bytes(32))
18-
self.addr = pabtc.core.Address.p2tr(p2tr_pubkey)
17+
self.addr = pabtc.core.Address.p2tr(pubkey.tr(mast.hash))
1918
self.script = pabtc.core.ScriptPubKey.address(self.addr)
2019
output_pubkey_byte = bytearray(
2120
[0x02]) + pabtc.bech32.decode_segwit_addr(pabtc.config.current.prefix.bech32, 1, self.addr)
@@ -41,8 +40,7 @@ def sign(self, tx: pabtc.core.Transaction) -> None:
4140
class Signerp2trp2ms(pabtc.wallet.Signer):
4241
def __init__(self, pubkey: pabtc.core.PubKey) -> None:
4342
self.pubkey = pubkey
44-
p2tr_pubkey = bytearray(pabtc.taproot.pubkey_tweak(pubkey.pt(), mast.hash).x.n.to_bytes(32))
45-
self.addr = pabtc.core.Address.p2tr(p2tr_pubkey)
43+
self.addr = pabtc.core.Address.p2tr(pubkey.tr(mast.hash))
4644
self.script = pabtc.core.ScriptPubKey.address(self.addr)
4745
output_pubkey_byte = bytearray(
4846
[0x02]) + pabtc.bech32.decode_segwit_addr(pabtc.config.current.prefix.bech32, 1, self.addr)

pabtc/core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ def sec_decode(cls, data: bytearray) -> PubKey:
199199
y = -y % pabtc.secp256k1.P
200200
return PubKey(x, y)
201201

202+
def tr(self, merkle: bytearray) -> bytearray:
203+
# Get the taproot output key corresponding to the internal public key and the taproot tree merkle root.
204+
assert len(merkle) == 32
205+
return bytearray(pabtc.taproot.pubkey_tweak(self.pt(), merkle).x.n.to_bytes(32))
206+
202207

203208
class ScriptPubKey:
204209
# The Script pubkey is the locking code for an output. It's made up of script, which is a mini programming language

0 commit comments

Comments
 (0)