Skip to content

Commit

Permalink
Add taproot script path spend contract
Browse files Browse the repository at this point in the history
  • Loading branch information
pool2win committed Apr 22, 2024
1 parent aa08be0 commit 61e57b7
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
93 changes: 93 additions & 0 deletions lib/contracts/taproot/scriptpath_spend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Copyright 2024 Kulpreet Singh
#
# This file is part of Bitcoin-DSL
#
# Bitcoin-DSL is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bitcoin-DSL is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bitcoin-DSL. If not, see <https://www.gnu.org/licenses/>.

# frozen_string_literal: false

# Generate new keys
Bitcoin::Node::Configuration.new(network: :regtest)
@alice = key wif: 'cNtZwU6mYnUXDJtPqfyEaRsZuNy6C5PCZHY16xbps85HvRSn9KqE'
@bob = key wif: 'cMyDpdQkC1qRfWYNQHXawUwzEmXyFjv7PYw2EqYFRnhXhBs4bXt9'
@carol = key wif: 'cRCuYhzDcPPCjfVZPzSiuRAsXUgivChpz5xEfeXPRAi2EDnuHymz'

transition :create_input_tx do
# Seed alice with some coins and make coinbase spendable
extend_chain num_blocks: 101, to: @alice

# Get coinbase to spend using tr
@coinbase_tx = get_coinbase_at 2

@taproot_keypath_tx = transaction inputs: [
{ tx: @coinbase_tx,
vout: 0,
script_sig: 'sig:wpkh(@alice)' }
],
outputs: [
{
taproot: { internal_key: @bob,
leaves: ['pk(@carol)', 'pk(@alice)'] },
amount: 49.999.sats
}
]

broadcast @taproot_keypath_tx
confirm transaction: @taproot_keypath_tx

log 'Transaction with taproot output confirmed'
end

transition :spend_first_leaf do
@spend_taproot_output_tx = transaction inputs: [
{ tx: @taproot_keypath_tx,
vout: 0,
script_sig: { leaf_index: 0, sig: 'sig:@carol' },
sighash: :all }
],
outputs: [
{ descriptor: 'wpkh(@carol)',
amount: 49.998.sats }
]

assert_mempool_accept @spend_taproot_output_tx

broadcast @spend_taproot_output_tx
confirm transaction: @spend_taproot_output_tx

log 'Taproot script path transaction spent using first leaf'
end

transition :spend_second_leaf do
@spend_taproot_output_tx = transaction inputs: [
{ tx: @taproot_keypath_tx,
vout: 0,
script_sig: { leaf_index: 1, sig: 'sig:@alice' },
sighash: :all }
],
outputs: [
{ descriptor: 'wpkh(@carol)',
amount: 49.998.sats }
]

assert_mempool_accept @spend_taproot_output_tx

broadcast @spend_taproot_output_tx
confirm transaction: @spend_taproot_output_tx

log 'Taproot script path transaction spent using second leaf'
end

run_transitions :create_input_tx, :spend_first_leaf
run_transitions :reset, :create_input_tx, :spend_second_leaf
1 change: 1 addition & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@

describe 'taproot transactions' do
it_behaves_like 'script evaluation', './lib/contracts/taproot/keypath_spend.rb'
it_behaves_like 'script evaluation', './lib/contracts/taproot/scriptpath_spend.rb'
end
end

0 comments on commit 61e57b7

Please sign in to comment.