Skip to content

Commit

Permalink
Fix var names in silent payments example
Browse files Browse the repository at this point in the history
  • Loading branch information
pool2win committed Apr 24, 2024
1 parent b110c62 commit 91c8561
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
5 changes: 5 additions & 0 deletions docs/_examples/silent_payments.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ include::{sourcedir}/lib/contracts/silent_payments.rb[tags=silent-payment]
<3> Generate a tweaked key using receiver's address and sender's input key
<4> Generate a tweaked private key for receiver using the sender's input key
<5> Spend the received payment using the receiver's tweaked key

We have a jupyter notebook that walks through the above
example. link:https://github.com/pool2win/bitcoin-dsl/blob/main/notebooks/silent-payments.ipynb[Silent Payments
Jupyter Notebook]

8 changes: 3 additions & 5 deletions lib/contracts/silent_payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

# frozen_string_literal: false

node :reset

# tag::silent-payment[]
@sender_input_key = key :new # <1>
@sender = key :new
Expand All @@ -28,9 +26,9 @@

@sender_coinbase = spendable_coinbase_for @sender_input_key # <2>

@sender_dh_key = multiply point: @receiver, scalar: @sender_input_key # <3>
@sender_dh_share = multiply point: @receiver, scalar: @sender_input_key # <3>

@taproot_output_key = tweak_public_key @receiver, with: hash160(@sender_dh_key) # <3>
@taproot_output_key = tweak_public_key @receiver, with: hash160(@sender_dh_share) # <3>

@taproot_output_tx = transaction inputs: [{ tx: @sender_coinbase,
vout: 0,
Expand All @@ -56,5 +54,5 @@

assert_mempool_accept @spend_received_payment_tx
broadcast @spend_received_payment_tx
confirm transaction @spend_received_payment_tx
confirm transaction: @spend_received_payment_tx
# end::silent-payment[]
76 changes: 41 additions & 35 deletions notebooks/silent-payments.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ac612b0-1925-48af-93c9-397d3cbbd389",
"cell_type": "markdown",
"id": "1494f763-79c9-48f2-8015-3f279f4a98df",
"metadata": {},
"outputs": [],
"source": [
"node :reset"
"#### Create Keys\n",
"\n",
"The sender_input_key is the key from the input that the sender spends to create the silent payment."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82a6a946-c6f3-4291-bfaa-21dd3c2a6021",
"id": "ea04c5c6-f268-48da-84c9-5e13ed0900ab",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -42,87 +42,93 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e715fc9d-cde5-4d61-bf9f-bb28dee1cb33",
"cell_type": "markdown",
"id": "f1f9c9ac-a4fd-4325-b04e-e83956ef5ea3",
"metadata": {},
"outputs": [],
"source": [
"extend_chain to: @sender_input_key, num_blocks: 101"
"#### Create Coinbases for Sender"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b6df634b-f118-48e9-9e42-1b1c77d07d59",
"id": "d595ffb2-78d3-4e5a-b39e-273ab30fd2a8",
"metadata": {},
"outputs": [],
"source": [
"extend_chain to: @sender_input_key, num_blocks: 101\n",
"\n",
"@sender_coinbase = spendable_coinbase_for @sender_input_key"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "48bb6323-6efd-4eaf-bbbd-0db9e737074d",
"metadata": {},
"outputs": [],
"source": [
"@sender_dh_key = multiply point: @receiver, scalar: @sender_input_key"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e952ad11-56cd-451a-8f54-3888a506e594",
"cell_type": "markdown",
"id": "98e48fd7-8b3e-444f-ab42-ebefaa85f65d",
"metadata": {},
"outputs": [],
"source": [
"@taproot_output_key = tweak_public_key @receiver, with: hash160(@sender_dh_key)"
"#### Generate Silent Payment\n",
"\n",
"First we create the DH shared secret.\n",
"\n",
"We use that to tweak the receiver's public key to get the internal key to be used for the taproot transaction."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b9fe805-9c8e-4e0e-a581-6c5297633bdf",
"id": "be0e5aa5-ec73-423c-a5d2-008652c5969e",
"metadata": {},
"outputs": [],
"source": [
"@sender_dh_share = multiply point: @receiver, scalar: @sender_input_key\n",
"\n",
"@taproot_output_key = tweak_public_key @receiver, with: hash160(@sender_dh_share)\n",
"\n",
"@taproot_output_tx = transaction inputs: [{ tx: @sender_coinbase, vout: 0, script_sig: 'sig:wpkh(@sender_input_key)' }],\n",
" outputs: [{ amount: 49.999.sats, taproot: { internal_key: @taproot_output_key } }]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13985d37-d286-439f-81f7-d597062d5b06",
"cell_type": "markdown",
"id": "c7eb1a9e-ebab-4a1d-b556-8e33bd954417",
"metadata": {},
"outputs": [],
"source": [
"broadcast @taproot_output_tx"
"#### Broadcast and Confirm the Silent Payment "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e489e724-e77e-496d-a874-7d59401408e4",
"id": "f2880a4e-51e3-4639-889c-58468412f302",
"metadata": {},
"outputs": [],
"source": [
"broadcast @taproot_output_tx\n",
"\n",
"confirm transaction: @taproot_output_tx\n",
"extend_chain num_blocks: 100"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68316f7d-27f1-4ed0-a0dd-936e764a3f11",
"id": "be0c2d8c-cc23-4b22-b2b3-7cc1e61d3682",
"metadata": {},
"outputs": [],
"source": [
"assert_equal @taproot_output_tx.inputs[0].script_witness.stack[1].bth, @sender_input_key.pubkey"
]
},
{
"cell_type": "markdown",
"id": "3aaf7157-5bf9-4195-8585-0e2e4e122c44",
"metadata": {},
"source": [
"#### Tweak the Receiver's Private Key\n",
"\n",
"The receiver is watching the chain for a taproot transaction where the internal key is the receiver's public key tweaked with a DH secret. When a match is found, the receiver can spend that output whenever they want."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 91c8561

Please sign in to comment.