• About
  • FAQ
  • Earn Bitcoin while Surfing the net
  • Buy & Sell Crypto on Paxful
Newsletter
Approx Foundation
  • Home
    • Home – Layout 1
  • Bitcoin
  • Ethereum
  • Regulation
  • Market
  • Blockchain
  • Business
  • Guide
  • Contact Us
No Result
View All Result
  • Home
    • Home – Layout 1
  • Bitcoin
  • Ethereum
  • Regulation
  • Market
  • Blockchain
  • Business
  • Guide
  • Contact Us
No Result
View All Result
Approx Foundation
No Result
View All Result
Home Bitcoin

segregated witness – Unable to get the signatures right for P2SH-P2WSH nested 2 on 2 multisig script

Moussa by Moussa
February 21, 2025
in Bitcoin
0
peer discovery – how to obtain the IP addresses of nodes for mining pools?
189
SHARES
1.5k
VIEWS
Share on FacebookShare on Twitter


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 the signatures are somehow coming out same each time ? I have checked multiple times at different sources but nothing answers it

I am trying to construct a P2SH-P2WSH transaction from scratch can someone help me debug the above implementation and what is wrong with it?



Source link

Related articles

ICO Era Whale Buys ETH at $2,794 as Ethereum Price Now Sits At $2,720

ICO Era Whale Buys ETH at $2,794 as Ethereum Price Now Sits At $2,720

September 22, 2026

Stacking BTC with MIca platform?

September 22, 2026
Share76Tweet47

Related Posts

ICO Era Whale Buys ETH at $2,794 as Ethereum Price Now Sits At $2,720

ICO Era Whale Buys ETH at $2,794 as Ethereum Price Now Sits At $2,720

by Moussa
September 22, 2026
0

Key TakeawaysThe ICO-era wallet bought 8,492.8 ETH through four Cow Swap orders on Sept. 21 at about $2,794 each.Hours later...

Stacking BTC with MIca platform?

by Moussa
September 22, 2026
0

i'm looking for plateform where i can stake BTC with the best interest. Any suggestion ? I'm european i need...

Pi Network Clears 417,000 KYC Flags as PI Crypto Price Surges +5%

by Moussa
September 22, 2026
0

The Pi crypto Network says it has resolved two long-standing bottlenecks affecting more than 914,000 Pioneers stuck in its KYC...

Over 38 Million USDT in Risk-Related Funds Intercepted, Futures Insurance Fund Reaches 792 Million USDT

Bitget Wallet Adds 1700 Tokenized Stocks Through Reality Integration

by Moussa
September 22, 2026
0

Trusted Editorial content, reviewed by leading industry experts and seasoned editors. Ad Disclosure TL;DR Bitget Wallet is adding more than...

Coinbase Investor Class Action Can Move Forward, Federal Judge Rules

Coinbase Opens IPO Allocations To US Retail Traders Starting With Oura

by Moussa
September 22, 2026
0

TL;DR Eligible US retail Coinbase customers can now request allocations in IPOs through the Coinbase app. The service is offered...

Load More

youssufi.com

sephina.com

[vc_row full_width="stretch_row" parallax="content-moving" vc_row_background="" background_repeat="no-repeat" background_position="center center" footer_scheme="dark" css=".vc_custom_1517813231908{padding-top: 60px !important;padding-bottom: 30px !important;background-color: #191818 !important;background-position: center;background-repeat: no-repeat !important;background-size: cover !important;}" footer_widget_title_color="#fcbf46" footer_button_bg="#fcb11e"][vc_column width="1/4"]

We bring you the latest in Crypto News

[/vc_column][vc_column width="1/4"][vc_wp_categories]
[/vc_column][vc_column width="1/4"][vc_wp_tagcloud taxonomy="post_tag"][/vc_column][vc_column width="1/4"]

Newsletter

[vc_raw_html]JTND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hYmVsJTNFJTNDaW5wdXQlMjB0eXBlJTNEJTIyY2hlY2tib3glMjIlMjBuYW1lJTNEJTIybnklMjIlMjByZXF1aXJlZCUyMGNsYXNzJTNEJTIydG5wLXByaXZhY3klMjIlM0UlQzIlQTBCeSUyMGNvbnRpbnVpbmclMkMlMjB5b3UlMjBhY2NlcHQlMjB0aGUlMjBwcml2YWN5JTIwcG9saWN5JTNDJTJGbGFiZWwlM0UlM0MlMkZkaXYlM0UlM0NkaXYlMjBjbGFzcyUzRCUyMnRucC1maWVsZCUyMHRucC1maWVsZC1idXR0b24lMjIlM0UlM0NpbnB1dCUyMGNsYXNzJTNEJTIydG5wLXN1Ym1pdCUyMiUyMHR5cGUlM0QlMjJzdWJtaXQlMjIlMjB2YWx1ZSUzRCUyMlN1YnNjcmliZSUyMiUyMCUzRSUwQSUzQyUyRmRpdiUzRSUwQSUzQyUyRmZvcm0lM0UlMEElM0MlMkZkaXYlM0UlM0NiciUyRiUzRSUzQyUyRnAlM0U=[/vc_raw_html][/vc_column][/vc_row]
No Result
View All Result
  • Contact Us
  • Homepages
  • Business
  • Guide

© 2024 APPROX FOUNDATION - The Crypto Currency News