def BIP_143_raw_transaction(prev_tx_id: str, amount_to_be_sent: int | float, signatureScript: str | None, pubKeyScript: str | None):
# Model
model = bytes.fromhex("02000000")
# HashPrevOuts = prev_tx + vout
HashPrev_out = bytes.fromhex(double_sha256(
"0"*72))
# HashSequence
HashSequence = bytes.fromhex(double_sha256("f"*8))
# HashOutputs for ALL signHash
HashOutputs = bytes.fromhex(double_sha256(
"a08601000000000017a914043f512301b66ffa8d73e71907e2b0b80989521587"))
# Hash preimage for all
raw_hash_pre_images = bytearray()
raw_hash_pre_images.prolong(model)
raw_hash_pre_images.prolong(HashPrev_out)
raw_hash_pre_images.prolong(HashSequence)
raw_hash_pre_images.prolong(bytes.fromhex(
"000000000000000000000000000000000000000000000000000000000000000000000000"))
# CORRECTION 2 scriptcode
#ONLY ERROR IF THIS
raw_hash_pre_images.prolong(bytes.fromhex(
"475221032ff8c5df0bc00fe1ac2319c3b8070d6d1e04cfbf4fedda499ae7b775185ad53b21039bbc8d24f89e5bc44c5b0d1980d6658316a6b2440023117c3c03a4975b04dd5652ae"))
raw_hash_pre_images.prolong(bytes.fromhex("a086010000000000"))
raw_hash_pre_images.prolong(bytes.fromhex("ffffffff"))
raw_hash_pre_images.prolong(HashOutputs)
raw_hash_pre_images.prolong(bytes.fromhex("00000000"))
raw_hash_pre_images.prolong(bytes.fromhex("01000000"))
return raw_hash_pre_images.hex()
def double_sha256(hex_string):
binary_data = binascii.unhexlify(hex_string)
# return hashlib.sha256(hashlib.sha256(binary_data).digest()).digest()[::-1].hex()
return hashlib.sha256(hashlib.sha256(binary_data).digest()).hexdigest()
def finalize_signed_transaction(raw_tx, signatures, redeem_script, signatureScript: str, prev_tx_id: str, amount_to_be_sent: int, pubKeyScript: str):
attempt:
witness_stack = bytearray()
# WITNESS STACK SIZE
witness_stack.prolong(bytes.fromhex("04"))
witness_stack.prolong(bytes.fromhex("00"))
for sig in signatures:
witness_stack.prolong(struct.pack("<256HashofRedeemScript/witnessScript>
sigScriptlen = struct.pack('
def P2SH_P2WSH_PubKeyScript(redeem_script_hash: str):
attempt:
pubKeyScript = CScript([
OP_HASH160,
bytes.fromhex(redeem_script_hash),
OP_EQUAL
])
return pubKeyScript.hex()
besides Exception as error:
print("An error ocurred whereas producing the PubKey Script for P2SH-P2WSH Multi-sig transaction - :", error)
def util_main():
# IT WORKS
private_key_arr = ["39dc0a9f0b185a2ee56349691f34716e6e0cda06a7f9707742ac113c4e2317bf",
"5077ccd9c558b7d04a81920d38aa11b4a9f9de3b23fab45c3ef28039920fdd6d"]
# SHA 256 on redeem Script
hash_redeem_P2WSH = hashlib.sha256(bytes.fromhex(
"5221032ff8c5df0bc00fe1ac2319c3b8070d6d1e04cfbf4fedda499ae7b775185ad53b21039bbc8d24f89e5bc44c5b0d1980d6658316a6b2440023117c3c03a4975b04dd5652ae")).hexdigest()
print(hash_redeem_P2WSH, " HASHING THE REDEEM SCRIPT")
# Witness Program
scr = CScript([
OP_0,
bytes.fromhex(hash_redeem_P2WSH)
])
# Hashing the Witness program
scr_hash = hash_redeem_script(bytes.fromhex(scr.hex()))
print(scr_hash, " HASHING THE WITNESS PROGRAM")
# Recepient Tackle from the Script Hash
scr_add = generate_token_address(scr_hash)
print(scr_add, " Tackle after checksum and base58 decode")
# Producing the output lock script or the pubKeyScript
pub_key_P2SH_P2WSH = P2SH_P2WSH_PubKeyScript(scr_hash)
print(pub_key_P2SH_P2WSH, " PubKeyScript Hex for P2SH P2WSH transaction")
# Producing uncooked unsigned transaction for P2SH_P2WSH as The signScript will stay empty in case of witness program
raw_tx_P2SH_P2WSH = createRawTransaction(
"0000000000000000000000000000000000000000000000000000000000000000", 100000, "", pub_key_P2SH_P2WSH)
print(raw_tx_P2SH_P2WSH, " Uncooked unsigned transaction for P2SH P2WSH")
# breakpoint()
bip_143_raw = BIP_143_raw_transaction("", 1, "", P2SH_P2WSH_PubKeyScript)
print(bip_143_raw, " Uncooked BIP 143 uncooked transaction for ALL SigHash")
# CORRECTION 3
# s256 = hashlib.sha256(hashlib.sha256(
# bytes.fromhex(bip_143_raw)).digest()).digest()
# s256 = hashlib.sha256(bytes.fromhex(bip_143_raw)).hexdigest()
s256 = double_sha256(bip_143_raw)
print(s256, " Hex for BIP - 143")
signatures = signRawtransaction_P2SH_P2WSH(s256, private_key_arr)
print(signatures[0], " Signatures from uncooked BIP-143")
sigScript = signScript_P2SH_P2WSH(hash_redeem_P2WSH)
print(sigScript, " Signature Script for P2SH_P2WSH")
signed_transaction = finalize_signed_transaction(
raw_tx_P2SH_P2WSH, signatures[0], "5221032ff8c5df0bc00fe1ac2319c3b8070d6d1e04cfbf4fedda499ae7b775185ad53b21039bbc8d24f89e5bc44c5b0d1980d6658316a6b2440023117c3c03a4975b04dd5652ae", sigScript, "0000000000000000000000000000000000000000000000000000000000000000", 100000, pub_key_P2SH_P2WSH)
print(signed_transaction)
# signature script is 256sha hash of witness script
return signed_transaction
I do not perceive however the signatures are by some means popping out identical every time ? I’ve checked a number of instances at totally different sources however nothing solutions it
I’m attempting to assemble a P2SH-P2WSH transaction from scratch can somebody assist me debug the above implementation and what’s flawed with it?