Skip to content

Commit

Permalink
Implement rawtr() descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
azuchi committed Jul 11, 2024
1 parent 02f3ae6 commit 6f72a45
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/bitcoin/descriptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Descriptor
autoload :Tr, 'bitcoin/descriptor/tr'
autoload :MultiA, 'bitcoin/descriptor/multi_a'
autoload :SortedMultiA, 'bitcoin/descriptor/sorted_multi_a'
autoload :RawTr, 'bitcoin/descriptor/raw_tr'
autoload :Checksum, 'bitcoin/descriptor/checksum'

module_function
Expand Down Expand Up @@ -104,6 +105,13 @@ def tr(key, tree = nil)
Tr.new(key, tree)
end

# Generate taproot output script descriptor.
# @param [String] key
# @return [Bitcoin::Descriptor::RawTr]
def rawtr(key)
RawTr.new(key)
end

# Generate tapscript multisig output for given keys.
# @param [Integer] threshold the threshold of multisig.
# @param [Array[String]] keys an array of keys.
Expand Down Expand Up @@ -169,6 +177,8 @@ def parse(string, top_level = true)
else
tr(key, parse(rest, false))
end
when 'rawtr'
rawtr(args_str)
else
raise ArgumentError, "Parse failed: #{string}"
end
Expand Down
21 changes: 21 additions & 0 deletions lib/bitcoin/descriptor/raw_tr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Bitcoin
module Descriptor
# rawtr() expression
class RawTr < KeyExpression
include Bitcoin::Opcodes

def type
:rawtr
end

def top_level?
true
end

def to_script
k = extract_pubkey(key)
Bitcoin::Script.new << OP_1 << k.xonly_pubkey
end
end
end
end
13 changes: 13 additions & 0 deletions spec/bitcoin/descriptor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,17 @@
.to raise_error(ArgumentError, "Multisig threshold cannot be larger than the number of keys.")
end
end

describe 'rawtr()' do
it do
expect(rawtr("xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt/86'/1'/0'/1/0").to_hex).
to eq('51205172af752f057d543ce8e4a6f8dcf15548ec6be44041bfa93b72e191cfc8c1ee')
desc_rawtr = "rawtr(xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt/86'/1'/0'/1/0)#q8dqvgc8"
rawtr = described_class.parse(desc_rawtr)
expect(rawtr.to_s(checksum: true)).to eq(desc_rawtr)
expect(rawtr('L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1').to_hex).to eq('5120a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd')
desc_rawtr = "rawtr(xpub68FQ9imX6mCWacw6eNRjaa8q8ynnHmUd5i7MVR51ZMPP5JycyfVHSLQVFPHMYiTybWJnSBL2tCBpy6aJTR2DYrshWYfwAxs8SosGXd66d8/*, xpub69Mvq3QMipdvnd9hAyeTnT5jrkcBuLErV212nsGf3qr7JPWysc9HnNhCsazdzj1etSx28hPSE8D7DnceFbNdw4Kg8SyRfjE2HFLv1P8TSGc/*)"
expect{described_class.parse(desc_rawtr)}.to raise_error(ArgumentError)
end
end
end

0 comments on commit 6f72a45

Please sign in to comment.