From 1164a77f10f759cde1460891eb74bf45df7c646b Mon Sep 17 00:00:00 2001 From: azuchi Date: Sun, 14 Jul 2024 14:10:49 +0900 Subject: [PATCH] Add Bitcoin::Descriptor::KeyExpression#extracted_key --- lib/bitcoin/descriptor/combo.rb | 2 +- lib/bitcoin/descriptor/key_expression.rb | 7 +++++++ lib/bitcoin/descriptor/pk.rb | 2 +- lib/bitcoin/descriptor/pkh.rb | 2 +- lib/bitcoin/descriptor/raw_tr.rb | 3 +-- lib/bitcoin/descriptor/wpkh.rb | 2 +- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/bitcoin/descriptor/combo.rb b/lib/bitcoin/descriptor/combo.rb index 7b45016..4edf2b8 100644 --- a/lib/bitcoin/descriptor/combo.rb +++ b/lib/bitcoin/descriptor/combo.rb @@ -9,7 +9,7 @@ def type def to_scripts candidates = [Pk.new(key), Pkh.new(key)] - pubkey = extract_pubkey(key) + pubkey = extracted_key if pubkey.compressed? candidates << Wpkh.new(pubkey.pubkey) candidates << Sh.new(candidates.last) diff --git a/lib/bitcoin/descriptor/key_expression.rb b/lib/bitcoin/descriptor/key_expression.rb index 67629fb..773c643 100644 --- a/lib/bitcoin/descriptor/key_expression.rb +++ b/lib/bitcoin/descriptor/key_expression.rb @@ -18,6 +18,13 @@ def args def top_level? false end + + # Get extracted key. + # @return [Bitcoin::Key] Extracted key. + def extracted_key + extract_pubkey(key) + end + end end end \ No newline at end of file diff --git a/lib/bitcoin/descriptor/pk.rb b/lib/bitcoin/descriptor/pk.rb index 9f42ebb..693b3c5 100644 --- a/lib/bitcoin/descriptor/pk.rb +++ b/lib/bitcoin/descriptor/pk.rb @@ -18,7 +18,7 @@ def type # Convert to bitcoin script. # @return [Bitcoin::Script] def to_script - k = extract_pubkey(key) + k = extracted_key target_key = xonly ? k.xonly_pubkey : k.pubkey Bitcoin::Script.new << target_key << OP_CHECKSIG end diff --git a/lib/bitcoin/descriptor/pkh.rb b/lib/bitcoin/descriptor/pkh.rb index be6216a..c23016f 100644 --- a/lib/bitcoin/descriptor/pkh.rb +++ b/lib/bitcoin/descriptor/pkh.rb @@ -8,7 +8,7 @@ def type end def to_script - Script.to_p2pkh(extract_pubkey(key).hash160) + Script.to_p2pkh(extracted_key.hash160) end end end diff --git a/lib/bitcoin/descriptor/raw_tr.rb b/lib/bitcoin/descriptor/raw_tr.rb index ee0e36f..cd95462 100644 --- a/lib/bitcoin/descriptor/raw_tr.rb +++ b/lib/bitcoin/descriptor/raw_tr.rb @@ -13,8 +13,7 @@ def top_level? end def to_script - k = extract_pubkey(key) - Bitcoin::Script.new << OP_1 << k.xonly_pubkey + Bitcoin::Script.new << OP_1 << extract_pubkey(key).xonly_pubkey end end end diff --git a/lib/bitcoin/descriptor/wpkh.rb b/lib/bitcoin/descriptor/wpkh.rb index d5a7e40..f86f108 100644 --- a/lib/bitcoin/descriptor/wpkh.rb +++ b/lib/bitcoin/descriptor/wpkh.rb @@ -12,7 +12,7 @@ def type end def to_script - Script.to_p2wpkh(extract_pubkey(key).hash160) + Script.to_p2wpkh(extracted_key.hash160) end end end