Skip to content

Commit

Permalink
Add opreturn example
Browse files Browse the repository at this point in the history
  • Loading branch information
pool2win committed Apr 4, 2024
1 parent de2086d commit ed5fcb5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
68 changes: 68 additions & 0 deletions lib/contracts/using_opreturn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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
@alice = key :new
@bob = key :new

@alice = key wif: 'L4bEFrQrfXH9mZBbq2SoTEx8FkyrG4aP2TZerN8DwSTaGoUcohJd'
@bob = key wif: 'KyWBUpCZF5n9WifoBcDDtCGYmX13SkubfQp3zCiDA4r2vozL5jE8'

# Seed alice with some coins
extend_chain to: @alice

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

@alice_coinbase = spendable_coinbase_for @alice

@data = 'ABCD'

@opcodes_tx = transaction inputs: [
{ tx: @alice_coinbase, vout: 0, script_sig: 'sig:@alice @alice' }
],
outputs: [
{
script: 'OP_DUP OP_HASH160 hash160(@bob) OP_EQUALVERIFY OP_CHECKSIG',
amount: 49.999.sats
},
{
script: 'OP_RETURN "ABCD"',
amount: 0
}
]

verify_signature for_transaction: @opcodes_tx,
at_index: 0,
with_prevout: [@alice_coinbase, 0]

broadcast @opcodes_tx

confirm transaction: @opcodes_tx, to: @alice

log 'Opcodes transaction confirmed'

@spend_opcodes_tx = transaction inputs: [
{ tx: @opcodes_tx, vout: 0, script_sig: 'sig:wpkh(@bob)' }
],
outputs: [
{ descriptor: 'wpkh(@bob)', amount: 49.998.sats }
]

broadcast @spend_opcodes_tx
1 change: 1 addition & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
RSpec.describe Runner.instance do
describe 'Running DSL scripts' do
it_behaves_like 'script evaluation', './lib/contracts/using_bitcoin_script.rb'
it_behaves_like 'script evaluation', './lib/contracts/using_opreturn.rb'

it_behaves_like 'script evaluation', './lib/contracts/anchor_transactions.rb'
it_behaves_like 'script evaluation', './lib/contracts/fold_transactions.rb'
Expand Down

0 comments on commit ed5fcb5

Please sign in to comment.