Creating a dlc contract

I am trying to create a dlc contract in bitcoin. I found few libraries in multiple programming languages by p2pderivatives

Instead of using the libraries, I find it much easier to execute the contract on a bitcoin regtest using bitcoin-cli.

Setting up the environment

brew install bitcoin # start bitcoind in regtest mode as a daemon bitcoind -regtest -daemon -fallbackfee=0.0002 # To create a new address for alice bitcoin-cli -regtest createwallet alice bitcoin-cli -regtest createwallet bob # use loadwallet to load the wallet (if you restart bitcoind) bitcoin-cli -regtest loadwallet alice bitcoin-cli -regtest loadwallet bob bitcoin-cli -regtest -rpcwallet=alice getwalletinfo bitcoin-cli -regtest -rpcwallet=bob getwalletinfo # Generate some blocks to get initial coins bitcoin-cli -regtest generatetoaddress 101 $(bitcoin-cli -regtest -rpcwallet=alice getnewaddress) # Send some coins from Alice to Bob bitcoin-cli -regtest -rpcwallet=alice sendtoaddress $(bitcoin-cli -regtest -rpcwallet=bob getnewaddress) 10 # Check the wallet balances bitcoin-cli -regtest -rpcwallet=alice getbalance bitcoin-cli -regtest -rpcwallet=bob getbalance # To delete a wallet (if you want to start fresh) bitcoin-cli -regtest unloadwallet alice

Both parties alice and bob need to agree to the contract terms

Oracle

To create a new address for the oracle:

# Create a wallet for the oracle (to be able to sign messages) bitcoin-cli -regtest createwallet oracle false false "" # Load the oracle wallet bitcoin-cli -regtest loadwallet oracle # Get a new address for the oracle oracle_address=$(bitcoin-cli -regtest -rpcwallet=oracle getnewaddress -addresstype legacy)

To sign an outcome with the oracle:

bitcoin-cli -regtest -rpcwallet=oracle signmessage $oracle_address "oracle outcome 1" # Verify the signature bitcoin-cli -regtest -rpcwallet=alice verifymessage $oracle_address "signature" "oracle outcome 1"

Creating a Contract and CETs Based on Oracle Outcome

To create a contract between Alice and Bob:

# Alice and Bob create multisig addresses alice_address=$(bitcoin-cli -regtest -rpcwallet=alice getnewaddress) bob_address=$(bitcoin-cli -regtest -rpcwallet=bob getnewaddress) alice_pubkey=$(bitcoin-cli -regtest -rpcwallet=alice getaddressinfo $alice_address | jq -r '.pubkey') bob_pubkey=$(bitcoin-cli -regtest -rpcwallet=bob getaddressinfo $bob_address | jq -r '.pubkey') multisig_address=$(bitcoin-cli -regtest createmultisig 2 "[\"$alice_pubkey\", \"$bob_pubkey\"]" | jq -r '.address') # Alice funds the multisig address alice_fund_txn=$(bitcoin-cli -regtest -rpcwallet=alice sendtoaddress $multisig_address 5) # Bob funds the multisig address bob_fund_txn=$(bitcoin-cli -regtest -rpcwallet=bob sendtoaddress $multisig_address 5) # alice creates cet for when the outcome is 1 cet_alice_1=$(bitcoin-cli -regtest -rpcwallet=alice createrawtransaction "[{\"txid\":\"$alice_fund_txn\",\"vout\":0}]" "{\"$alice_address\":7.5}") # todo: we need to create adaptor signatures signed_cet_alice_1=$(bitcoin-cli -regtest -rpcwallet=alice signrawtransactionwithwallet $cet_alice_1 | jq -r '.hex') cet_alice_0=$(bitcoin-cli -regtest -rpcwallet=alice createrawtransaction "[{\"txid\":\"$alice_fund_txn\",\"vout\":0}]" "{\"$alice_address\":2.5}") # bob creates cet for when the outcome is 0 cet_bob_0=$(bitcoin-cli -regtest -rpcwallet=bob createrawtransaction "[{\"txid\":\"$bob_fund_txn\",\"vout\":0}]" "{\"$bob_address\":7.5}") cet_bob_1=$(bitcoin-cli -regtest -rpcwallet=bob createrawtransaction "[{\"txid\":\"$bob_fund_txn\",\"vout\":0}]" "{\"$bob_address\":2.5}") # todo: we need to create adaptor signatures signed_cet_bob_1=$(bitcoin-cli -regtest -rpcwallet=bob signrawtransactionwithwallet $cet_bob_1 | jq -r '.hex') # Oracle signs the outcome oracle_outcome=1 oracle_signature=$(bitcoin-cli -regtest -rpcwallet=oracle signmessage $oracle_address "$oracle_outcome") # Alice verify the oracle signature bitcoin-cli -regtest -rpcwallet=alice verifymessage $oracle_address "$oracle_signature" "$oracle_outcome" # Assuming the oracle signature is valid, they can broadcast the appropriate CET # todo: bitcoin-cli -regtest sendrawtransaction $signed_cet_alice_1 bitcoin-cli -regtest sendrawtransaction $signed_cet_bob_0

Creating adaptor signature

Challenges

References