Skip to content
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
92 changes: 92 additions & 0 deletions .github/instructions/*.instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import hashlib
import struct
import binascii

def decode_varint(s):
i = s.read(1)[0]
if i < 0xfd:
return i
elif i == 0xfd:
return struct.unpack("<H", s.read(2))[0]
elif i == 0xfe:
return struct.unpack("<I", s.read(4))[0]
else:
return struct.unpack("<Q", s.read(8))[0]

def decode_tx(hex_str):
# Clean the string just in case
hex_str = hex_str.strip()
# Remove any non-hex suffix if present (the user text)
# The valid hex seems to end at ...00000000
# Let's try to parse and stop when done
try:
b = binascii.unhexlify(hex_str)
except:
# If unhexlify fails, try to find the valid prefix
# The string provided ends with ...6a00000000
# Let's take the hex part known from visual inspection or try to cut off
# We know the user appended "d3ad..." which is not hex
# Let's look for the last valid hex char? No, "d" is valid.
# Let's just take the hex string provided in the thought block
valid_hex = "0200000000010142c65fa705f326604c6e10cfbc3b553d3ab5a695fc3874d50b87535cb45df1590000000000fdffffff012c6d0100000000001600140ef43d2a1b31bc5a968edb6fe84d2f303291e69102463043022069b74eb2afb03d4a7c9d31138d629f0f95f63d2a6f2b6895d8af5c25336c4b76021f1748f96644a08cb02d406915f2dcf2ad87dc253fe0b49b2f431462f21bade5012103bc78d9e2f73cb46fb1c33469619ab9de8ed1dc909874f0c0907787e81fdd426a00000000"
b = binascii.unhexlify(valid_hex)

# Parsing logic would go here but standard libraries are better if available.
# Since I don't have a bitcoin lib, I'll do a basic parse or double hash for TxID.

# Double SHA256 for TxID (legacy format)
# For Segwit, we need to strip witness data for TxID, keep it for wTxID.

# This is a raw segwit tx (marker 00 flag 01)
return valid_hex

hex_val = "0200000000010142c65fa705f326604c6e10cfbc3b553d3ab5a695fc3874d50b87535cb45df1590000000000fdffffff012c6d0100000000001600140ef43d2a1b31bc5a968edb6fe84d2f303291e69102463043022069b74eb2afb03d4a7c9d31138d629f0f95f63d2a6f2b6895d8af5c25336c4b76021f1748f96644a08cb02d406915f2dcf2ad87dc253fe0b49b2f431462f21bade5012103bc78d9e2f73cb46fb1c33469619ab9de8ed1dc909874f0c0907787e81fdd426a00000000"
print(f"Hex length: {len(hex_val)}")

# Let's try to hash it to get a TxID?
# Wait, for Segwit, TxID is hash of legacy serialization (no witness).
# I need to reconstruct legacy serialization to get TxID.

# Structure:
# Version: 02000000
# Marker: 00
# Flag: 01
# Input Count: 01
# Input:
# PrevHash: 42c65fa705f326604c6e10cfbc3b553d3ab5a695fc3874d50b87535cb45df159 (needs reversing)
# Index: 00000000
# ScriptLen: 00
# Sequence: fdffffff
# Output Count: 01
# Output:
# Amount: 2c6d010000000000 (0x016d2c -> 93484 sats)
# ScriptLen: 16 (22 bytes)
# Script: 00140ef43d2a1b31bc5a968edb6fe84d2f303291e691 (P2WPKH)
# Witness:
# Item Count: 02
# Item 1 Size: 46 (70 bytes?) -> 0x46 is 70.
# Item 1: 3043022069b74eb2afb03d4a7c9d31138d629f0f95f63d2a6f2b6895d8af5c25336c4b76021f1748f96644a08cb02d406915f2dcf2ad87dc253fe0b49b2f431462f21bade
# Item 2 Size: 21 (33 bytes)
# Item 2: 03bc78d9e2f73cb46fb1c33469619ab9de8ed1dc909874f0c0907787e81fdd426a
# Locktime: 00000000

# Legacy Serialization (for TxID):
# Version (4) + Input Count (varint) + Inputs (PrevHash+Index+ScriptLen+Script+Seq) + Output Count (varint) + Outputs (Amount+ScriptLen+Script) + Locktime (4)
# Version: 02000000
# InCnt: 01
# Input: 59f1...42 (Rev PrevHash) + 00000000 + 00 + fdffffff
# OutCnt: 01
# Output: 2c6d010000000000 + 16 + 00140ef43d2a1b31bc5a968edb6fe84d2f303291e691
# Locktime: 00000000

legacy_hex = "02000000" + "01" + "42c65fa705f326604c6e10cfbc3b553d3ab5a695fc3874d50b87535cb45df159" + "00000000" + "00" + "fdffffff" + "01" + "2c6d010000000000" + "16" + "00140ef43d2a1b31bc5a968edb6fe84d2f303291e691" + "00000000"

def double_sha256(h):
b = binascii.unhexlify(h)
return hashlib.sha256(hashlib.sha256(b).digest()).hexdigest()

txid = double_sha256(legacy_hex)
# TxID is little endian in display usually, so reverse it
txid_display = "".join(reversed([txid[i:i+2] for i in range(0, len(txid), 2)]))

print(f"{txid_display=}")