Skip to content

Commit

Permalink
fix p2sh signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Oct 2, 2024
1 parent 3eb888b commit 23a35de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def check_transaction_sanity(db, source, tx_info, unsigned_tx_hex, encoding, inp
if encoding == "p2sh":
# make_canonical can't determine the address, so we blindly change the desired to the parsed
desired_source = parsed_source
# desired_destination = parsed_destination
desired_destination = parsed_destination
except exceptions.BTCOnlyError:
# Skip BTC‐only transactions.
return
Expand Down
23 changes: 18 additions & 5 deletions counterparty-core/counterpartycore/test/regtest/testp2sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ def get_new_address(seed: str):
return address, secret_key


def sign_rawtransaction(rawtransaction, address, secret_key, amount=None):
def sign_p2wpkh_rawtransaction(rawtransaction, address, secret_key, amount=None):
tx = CMutableTransaction.deserialize(bytes.fromhex(rawtransaction))
txin_index = 0
redeem_script = address.to_redeemScript()
prev_txid = b2lx(tx.vin[txin_index].prevout.hash)
if not amount:
amount = get_tx_out_amount(prev_txid, tx.vin[txin_index].prevout.n)
amount = int(amount * 10e8)
# amount = int(10 * 10e8)
print("AMount", amount)
sighash = SignatureHash(
redeem_script, tx, txin_index, SIGHASH_ALL, amount=amount, sigversion=SIGVERSION_WITNESS_V0
)
Expand All @@ -90,6 +88,18 @@ def sign_rawtransaction(rawtransaction, address, secret_key, amount=None):
return signed_tx.serialize().hex()


def sign_p2sh_rawtransaction(rawtransaction, secret_key):
tx = CMutableTransaction.deserialize(bytes.fromhex(rawtransaction))
txin_index = 0
txin_redeemScript = tx.vin[txin_index].scriptSig
sighash = SignatureHash(txin_redeemScript, tx, txin_index, SIGHASH_ALL)
sig = secret_key.sign(sighash) + bytes([SIGHASH_ALL])
# set scriptSig
vins = [CTxIn(tx.vin[0].prevout, CScript([sig, txin_redeemScript]))]
signed_tx = CMutableTransaction(vins, tx.vout)
return signed_tx.serialize().hex()


def send_funds_to_address(address):
source_with_xcp = api_call("assets/XCP/balances", {"limit": 1})["result"][0]["address"]
sh.python3(
Expand Down Expand Up @@ -148,7 +158,9 @@ def send_funds_to_address(address):
print("unsigned_pretx_hex:", unsigned_pretx_hex)

# sign pretx
signed_pretx_hex = sign_rawtransaction(unsigned_pretx_hex, address, secret_key, int(10 * 10e8))
signed_pretx_hex = sign_p2wpkh_rawtransaction(
unsigned_pretx_hex, address, secret_key, int(10 * 10e8)
)
print("signed_pretx_hex:", signed_pretx_hex)

# broadcast pretx and get pretx_txid
Expand All @@ -174,6 +186,7 @@ def send_funds_to_address(address):
print("unsigned_finaltx_hex:", unsigned_finaltx_hex)

# sign and broadcast final tx
signed_finaltx_hex = sign_rawtransaction(unsigned_finaltx_hex, address, secret_key)
signed_finaltx_hex = sign_p2sh_rawtransaction(unsigned_finaltx_hex, secret_key)
print("signed_finaltx_hex:", signed_finaltx_hex)
txid = bitcoin_cli("sendrawtransaction", signed_finaltx_hex).strip()
bitcoin_cli("generatetoaddress", 1, destination_address)

0 comments on commit 23a35de

Please sign in to comment.