P2SHP2WSH – Earlybirds Invest https://earlybirdsinvest.com Latest Crypto News Sun, 23 Feb 2025 09:21:59 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.7 https://i0.wp.com/earlybirdsinvest.com/wp-content/uploads/2024/12/cropped-New-Project-2024-12-17T235703.455.png?fit=32%2C32&ssl=1 P2SHP2WSH – Earlybirds Invest https://earlybirdsinvest.com 32 32 240146708 Cannot generate correct P2SH-P2WSH address from specified redemption script https://earlybirdsinvest.com/cannot-generate-correct-p2sh-p2wsh-address-from-specified-redemption-script/ https://earlybirdsinvest.com/cannot-generate-correct-p2sh-p2wsh-address-from-specified-redemption-script/#respond Sun, 23 Feb 2025 09:21:58 +0000 https://earlybirdsinvest.com/cannot-generate-correct-p2sh-p2wsh-address-from-specified-redemption-script/

Could you provide the generated code? I’m struggling to generate the P2SH-P2WSH address from the specified redemption script. In reality, there is no key to the witness program. …In this case I just wanted to double check if the generated address and the specified address were the same

Transaction Spec
Private Key 1: 39dc0a9f0b185a2ee56349691f34716e6e0cda06a7f9707742ac113c4e2317bf
Private Key 2: 5077ccd9c558b7d04a81920d38aa11b4a9f9de3b23fab45c3ef28039920fdd6d
Redeem Script (ASM): OP_2 032ff8c5df0bc00fe1ac2319c3b8070d6d1e04cfbf4fedda499ae7b775185ad53b 039bbc8d24f89e5bc44c5b0d1980d6658316a6b2440023117c3c03a4975b04dd56 OP_2 OP_CHECKMULTISIG
Redeem Script (HEX): 5221032ff8c5df0bc00fe1ac2319c3b8070d6d1e04cfbf4fedda499ae7b775185ad53b21039bbc8d24f89e5bc44c5b0d1980d6658316a6b2440023117c3c03a4975b04dd5652ae
Transaction should contain exactly 1 input with:
Outpoint:
Hash: 0000000000000000000000000000000000000000000000000000000000000000
Index: 0
Sequence: 0xffffffff
Transaction should contain exactly 1 output with:
Value: 0.001
Address: 325UUecEQuyrTd28Xs2hvAxdAjHM7XzqVF
Locktime: 0

I’m doing this in Python

# Given Redeem Script (HEX)
redeem_script_hex = "5221032ff8c5df0bc00fe1ac2319c3b8070d6d1e04cfbf4fedda499ae7b775185ad53b21039bbc8d24f89e5bc44c5b0d1980d6658316a6b2440023117c3c03a4975b04dd5652ae"
script_bytes = bytes.fromhex(redeem_script_hex)
sha256_hash = hashlib.sha256(script_bytes).digest()
redeem_script_hash = hashlib.new("ripemd160", sha256_hash).digest()
prefixed_hash = b'\x05' + redeem_script_hash
checksum = hashlib.sha256(hashlib.sha256(prefixed_hash).digest()).digest()(:4)
final_data = prefixed_hash + checksum
address = base58.b58encode(final_data).decode()
print(address)

Putput appears

h-multisig-tx-jatin-2708/python$ python main.py
39C5GaHDocqYVgyCq2TrMfknTL45LPPcKF

But it should come

Address: 325UUecEQuyrTd28Xs2hvAxdAjHM7XzqVF (as given in ques)

Can anyone explain why the addresses are different, as the witness appears to be missing…?

]]>
https://earlybirdsinvest.com/cannot-generate-correct-p2sh-p2wsh-address-from-specified-redemption-script/feed/ 0 21340
Can’t get the signature of P2SH-P2WSH nested 2 correctly in two Multisig scripts https://earlybirdsinvest.com/cant-get-the-signature-of-p2sh-p2wsh-nested-2-correctly-in-two-multisig-scripts/ https://earlybirdsinvest.com/cant-get-the-signature-of-p2sh-p2wsh-nested-2-correctly-in-two-multisig-scripts/#respond Fri, 21 Feb 2025 12:11:56 +0000 https://earlybirdsinvest.com/cant-get-the-signature-of-p2sh-p2wsh-nested-2-correctly-in-two-multisig-scripts/
def BIP_143_raw_transaction(prev_tx_id: str, amount_to_be_sent: int | float, signatureScript: str | None, pubKeyScript: str | None):
    # Version
    version = 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.extend(version)
    raw_hash_pre_images.extend(HashPrev_out)
    raw_hash_pre_images.extend(HashSequence)
    raw_hash_pre_images.extend(bytes.fromhex(
        "000000000000000000000000000000000000000000000000000000000000000000000000"))
    # CORRECTION 2 scriptcode
    #ONLY ERROR IF THIS
    raw_hash_pre_images.extend(bytes.fromhex(
        "475221032ff8c5df0bc00fe1ac2319c3b8070d6d1e04cfbf4fedda499ae7b775185ad53b21039bbc8d24f89e5bc44c5b0d1980d6658316a6b2440023117c3c03a4975b04dd5652ae"))
    raw_hash_pre_images.extend(bytes.fromhex("a086010000000000"))
    raw_hash_pre_images.extend(bytes.fromhex("ffffffff"))
    raw_hash_pre_images.extend(HashOutputs)
    raw_hash_pre_images.extend(bytes.fromhex("00000000"))
    raw_hash_pre_images.extend(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):
    try:
        witness_stack = bytearray()
        # WITNESS STACK SIZE
        witness_stack.extend(bytes.fromhex("04"))
        witness_stack.extend(bytes.fromhex("00"))
        for sig in signatures:
            witness_stack.extend(struct.pack("<256HashofRedeemScript/witnessScript>
        sigScriptlen = struct.pack('


def P2SH_P2WSH_PubKeyScript(redeem_script_hash: str):
    try:
        pubKeyScript = CScript((
            OP_HASH160,
            bytes.fromhex(redeem_script_hash),
            OP_EQUAL
        ))
        return pubKeyScript.hex()

    except Exception as error:
        print("An error ocurred while generating 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 Address from the Script Hash
    scr_add = generate_token_address(scr_hash)
    print(scr_add, " Address after checksum and base58 decode")
    # Generating 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")
    # Generating raw unsigned transaction for P2SH_P2WSH as The signScript will remain empty in case of witness program
    raw_tx_P2SH_P2WSH = createRawTransaction(
        "0000000000000000000000000000000000000000000000000000000000000000", 100000, "", pub_key_P2SH_P2WSH)
    print(raw_tx_P2SH_P2WSH, " Raw unsigned transaction for P2SH P2WSH")
    # breakpoint()
    bip_143_raw = BIP_143_raw_transaction("", 1, "", P2SH_P2WSH_PubKeyScript)
    print(bip_143_raw, " Raw BIP 143 raw 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 raw 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 don’t understand, but do signatures come out the same way in any way every time? I checked multiple times with different sources, but that doesn’t answer anything

I’m trying to build a P2SH-P2WSH transaction from scratch. Could someone help me debug the above implementation?

]]>
https://earlybirdsinvest.com/cant-get-the-signature-of-p2sh-p2wsh-nested-2-correctly-in-two-multisig-scripts/feed/ 0 20923