Skip to content

Commit

Permalink
Change Bitcoin::Descriptor::Expression#extract_pubkey return type to …
Browse files Browse the repository at this point in the history
…Bitcoin::Key from String
  • Loading branch information
azuchi committed Jul 6, 2024
1 parent 4bac156 commit 19a8b6d
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/bitcoin/descriptor/combo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def type
def to_scripts
candidates = [Pk.new(key), Pkh.new(key)]
pubkey = extract_pubkey(key)
if compressed_key?(pubkey)
candidates << Wpkh.new(pubkey)
if pubkey.compressed?
candidates << Wpkh.new(pubkey.pubkey)
candidates << Sh.new(candidates.last)
end
candidates.map(&:to_script)
Expand Down
4 changes: 2 additions & 2 deletions lib/bitcoin/descriptor/expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def compressed_key?(key)

# Extract public key from KEY format.
# @param [String] key KEY string.
# @return [String] public key.
# @return [Bitcoin::Key] public key.
def extract_pubkey(key)
if key.start_with?('[') # BIP32 fingerprint
raise ArgumentError, "Multiple ']' characters found for a single pubkey." if key.count('[') > 1 || key.count(']') > 1
Expand Down Expand Up @@ -83,7 +83,7 @@ def extract_pubkey(key)
end
key = key.is_a?(Bitcoin::Key) ? key : key.key
raise ArgumentError, Errors::Messages::INVALID_PUBLIC_KEY unless key.fully_valid_pubkey?
key.pubkey
key
end

# Derive key using +paths+.
Expand Down
2 changes: 1 addition & 1 deletion lib/bitcoin/descriptor/key_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class KeyExpression < Expression
# Constructor
# @raise [ArgumentError] If +key+ is invalid.
def initialize(key)
raise ArgumentError, "key must be string." unless key.is_a? String
raise ArgumentError, "Key must be string." unless key.is_a? String
extract_pubkey(key)
@key = key
end
Expand Down
4 changes: 2 additions & 2 deletions lib/bitcoin/descriptor/multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def type
end

def to_script
Script.to_multisig_script(threshold, keys.map{|key| extract_pubkey(key) }, sort: false)
Script.to_multisig_script(threshold, keys.map{|key| extract_pubkey(key).pubkey }, sort: false)
end

def to_hex
Expand All @@ -44,7 +44,7 @@ def validate!(threshold, keys)
raise ArgumentError, 'Multisig threshold cannot be 0, must be at least 1.' unless threshold > 0
raise ArgumentError, 'Multisig threshold cannot be larger than the number of keys.' if threshold > keys.size
raise ArgumentError, "Multisig must have between 1 and #{Bitcoin::MAX_PUBKEYS_PER_MULTISIG} keys, inclusive." if keys.size > Bitcoin::MAX_PUBKEYS_PER_MULTISIG
keys.map{|key| extract_pubkey(key) }
keys.each{|key| extract_pubkey(key) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bitcoin/descriptor/pk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def type
# Convert to bitcoin script.
# @return [Bitcoin::Script]
def to_script
Bitcoin::Script.new << extract_pubkey(key) << OP_CHECKSIG
Bitcoin::Script.new << extract_pubkey(key).pubkey << OP_CHECKSIG
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/bitcoin/descriptor/pkh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def type
end

def to_script
Script.to_p2pkh(Bitcoin.hash160(extract_pubkey(key)))
Script.to_p2pkh(extract_pubkey(key).hash160)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bitcoin/descriptor/sorted_multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def type
end

def to_script
Script.to_multisig_script(threshold, keys.map{|key| extract_pubkey(key) }, sort: true)
Script.to_multisig_script(threshold, keys.map{|key| extract_pubkey(key).pubkey }, sort: true)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/bitcoin/descriptor/wpkh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module Descriptor
class Wpkh < KeyExpression
def initialize(key)
super(key)
raise ArgumentError, "Uncompressed key are not allowed." unless compressed_key?(extract_pubkey(key))
raise ArgumentError, "Uncompressed key are not allowed." unless extract_pubkey(key).compressed?
end

def type
:wpkh
end

def to_script
Script.to_p2wpkh(Bitcoin.hash160(extract_pubkey(key)))
Script.to_p2wpkh(extract_pubkey(key).hash160)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/bitcoin/descriptor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
expect{wsh(sh(pk('L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1')))}.to raise_error(ArgumentError, 'Can only have sh() at top level.')
expect{sh(sh(pk('L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1')))}.to raise_error(ArgumentError, 'Can only have sh() at top level.')
expect{wsh(wsh(pk('L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1')))}.to raise_error(ArgumentError, 'Can only have wsh() at top level or inside sh().')
expect{combo(pkh('03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd'))}.to raise_error(ArgumentError, 'key must be string.') # BIP-384
expect{combo(pkh('03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd'))}.to raise_error(ArgumentError, 'Key must be string.') # BIP-384

# Checksums
expected = 'a91445a9a622a8b0a1269944be477640eedc447bbd8487'
Expand Down

0 comments on commit 19a8b6d

Please sign in to comment.