Deduplicate getP2SHScriptForHash

This commit is contained in:
tecnovert
2025-05-11 11:59:19 +02:00
parent 38302d2d79
commit 80915d9865
4 changed files with 9 additions and 19 deletions

View File

@@ -50,9 +50,6 @@ from .util import (
make_int,
ensure,
)
from .util.script import (
getP2SHScriptForHash,
)
from .util.address import (
toWIF,
decodeWif,
@@ -4536,7 +4533,7 @@ class BasicSwap(BaseApp):
prev_p2wsh = ci.getScriptDest(txn_script)
script_pub_key = prev_p2wsh.hex()
else:
script_pub_key = getP2SHScriptForHash(ci.pkh(txn_script)).hex()
script_pub_key = ci.get_p2sh_script_pubkey(txn_script).hex()
prevout = {
"txid": prev_txnid,

View File

@@ -1393,6 +1393,9 @@ class BTCInterface(Secp256k1Interface):
def getScriptDest(self, script):
return CScript([OP_0, sha256(script)])
def getP2WSHScriptDest(self, script):
return CScript([OP_0, sha256(script)])
def getScriptScriptSig(self, script: bytes) -> bytes:
return bytes()

View File

@@ -14,7 +14,6 @@ from basicswap.contrib.test_framework.messages import (
)
from basicswap.contrib.test_framework.script import (
CScript,
OP_0,
OP_DUP,
OP_HASH160,
OP_EQUALVERIFY,
@@ -25,7 +24,6 @@ from basicswap.util import (
TemporaryError,
)
from basicswap.util.script import (
getP2WSH,
getCompactSizeLen,
getWitnessElementLen,
)
@@ -256,7 +254,7 @@ class PARTInterfaceBlind(PARTInterface):
ephemeral_pubkey = self.getPubkey(ephemeral_key)
assert len(ephemeral_pubkey) == 33
nonce = self.getScriptLockTxNonce(vkbv)
p2wsh_addr = self.encode_p2wsh(getP2WSH(script))
p2wsh_addr = self.encode_p2wsh(self.getP2WSHScriptDest(script))
inputs = []
outputs = [
{
@@ -330,7 +328,7 @@ class PARTInterfaceBlind(PARTInterface):
locked_coin = input_blinded_info["amount"]
tx_lock_id = lock_tx_obj["txid"]
refund_script = self.genScriptLockRefundTxScript(Kal, Kaf, csv_val)
p2wsh_addr = self.encode_p2wsh(getP2WSH(refund_script))
p2wsh_addr = self.encode_p2wsh(self.getP2WSHScriptDest(refund_script))
inputs = [
{
@@ -495,7 +493,7 @@ class PARTInterfaceBlind(PARTInterface):
lock_txo_scriptpk = bytes.fromhex(
lock_tx_obj["vout"][lock_output_n]["scriptPubKey"]["hex"]
)
script_pk = CScript([OP_0, hashlib.sha256(script_out).digest()])
script_pk = self.getP2WSHScriptDest(script_out)
ensure(lock_txo_scriptpk == script_pk, "Bad output script")
A, B = extractScriptLockScriptValues(script_out)
ensure(A == Kal, "Bad script leader pubkey")
@@ -572,7 +570,7 @@ class PARTInterfaceBlind(PARTInterface):
lock_refund_txo_scriptpk = bytes.fromhex(
lock_refund_tx_obj["vout"][lock_refund_output_n]["scriptPubKey"]["hex"]
)
script_pk = CScript([OP_0, hashlib.sha256(script_out).digest()])
script_pk = self.getP2WSHScriptDest(script_out)
ensure(lock_refund_txo_scriptpk == script_pk, "Bad output script")
A, B, csv_val, C = extractScriptLockRefundScriptValues(script_out)
ensure(A == Kal, "Bad script pubkey")

View File

@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2022 tecnovert
# Copyright (c) 2025 The Basicswap developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import struct
import hashlib
from basicswap.contrib.test_framework.script import (
OP_PUSHDATA1,
OP_PUSHDATA2,
@@ -89,14 +89,6 @@ def decodePushData(script_bytes, o):
return (data, i + datasize - o)
def getP2SHScriptForHash(p2sh):
return bytes((OpCodes.OP_HASH160, 0x14)) + p2sh + bytes((OpCodes.OP_EQUAL,))
def getP2WSH(script):
return bytes((OpCodes.OP_0, 0x20)) + hashlib.sha256(script).digest()
def SerialiseNumCompact(v):
if v < 253:
return bytes((v,))