Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix script-level locktime encoding #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions chapter5-timelocks/script-level-timelocks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"path_to_bitcoin_functional_test = \"/Users/dariuscognac/bitcoin/test/functional\"\n",
"path_to_bitcoin_tx_tutorial = \"/Users/dariuscognac/Documents/Github/bitcoin-tx-tutorial\"\n",
"\n",
"import sys\n",
"import sys, math\n",
"\n",
"# Add the functional test framework to our PATH\n",
"sys.path.insert(0, path_to_bitcoin_functional_test)\n",
Expand Down Expand Up @@ -116,15 +116,20 @@
"\n",
"# let's suppose Peter's 18th birthday is estimated to happen at block 500\n",
"locktime_int = 500\n",
"\n",
"# minimal encoding of locktime\n",
"if 1 <= locktime_int <= 16:\n",
" locktime_script = (80 + locktime_int).to_bytes(1, byteorder=\"little\", signed=False) # OP_1 to OP_16\n",
"else:\n",
" locktime_script = pushbytes(locktime_int.to_bytes(math.ceil(locktime_int.bit_length() / 8), byteorder=\"little\", signed=False))\n",
" \n",
"locktime = locktime_int.to_bytes(4, byteorder=\"little\", signed=False)\n",
"locktime_val = locktime_int.to_bytes(2, byteorder=\"little\", signed=False)\n",
"\n",
"sequence = bytes.fromhex(\"ffff fffe\")\n",
"print(\"sequence: \", sequence.hex())\n",
"\n",
"redeemScript = (\n",
" varint_len(locktime_val)\n",
" + locktime_val\n",
" locktime_script\n",
" + bytes.fromhex(\"b1\") # OP_CLTV\n",
" + bytes.fromhex(\"75\") # OP_DROP\n",
" + pushbytes(receiver_pubkey)\n",
Expand Down Expand Up @@ -564,7 +569,7 @@
"\n",
"sequence_int = 398\n",
"sequence = sequence_int.to_bytes(4, byteorder=\"little\", signed=False)\n",
"sequence_val = sequence_int.to_bytes(2, byteorder=\"little\", signed=False)\n",
"sequence_val = sequence_int.to_bytes(2, byteorder=\"little\", signed=False) # 2 bytes is minimal for 398\n",
"\n",
"redeemScript = (\n",
" varint_len(sequence_val)\n",
Expand Down