Decred CSV test.

This commit is contained in:
tecnovert
2024-04-29 00:40:14 +02:00
parent ab472c04be
commit 74c7072926
6 changed files with 130 additions and 8 deletions

View File

@@ -21,8 +21,8 @@ from basicswap.util.crypto import (
from basicswap.util.extkey import ExtKeyPair
from basicswap.util.integer import encode_varint
from basicswap.interface.dcr.rpc import make_rpc_func
from .messages import CTransaction, SigHashType, TxSerializeType
from .script import push_script_data
from .messages import CTransaction, CTxOut, SigHashType, TxSerializeType
from .script import push_script_data, OP_HASH160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_CHECKSIG
from coincurve.keys import (
PrivateKey
@@ -128,6 +128,14 @@ class DCRInterface(Secp256k1Interface):
def nbK() -> int: # No. of bytes requires to encode a public key
return 33
@staticmethod
def txVersion() -> int:
return 2
@staticmethod
def txoType():
return CTxOut
def __init__(self, coin_settings, network, swap_client=None):
super().__init__(network)
self._rpc_host = coin_settings.get('rpchost', '127.0.0.1')
@@ -169,6 +177,9 @@ class DCRInterface(Secp256k1Interface):
else:
self.rpc('getblockchaininfo')
def getChainHeight(self) -> int:
return self.rpc('getblockcount')
def checkWallets(self) -> int:
# Only one wallet possible?
return 1
@@ -239,3 +250,16 @@ class DCRInterface(Secp256k1Interface):
def stripTxSignature(self, tx_bytes) -> bytes:
tx = self.loadTx(tx_bytes)
return tx.serialize(TxSerializeType.NoWitness)
def getScriptDest(self, script: bytes) -> bytes:
# P2SH
script_hash = self.pkh(script)
assert len(script_hash) == 20
return OP_HASH160.to_bytes(1) + len(script_hash).to_bytes(1) + script_hash + OP_EQUAL.to_bytes(1)
def getPubkeyHashDest(self, pkh: bytes) -> bytes:
# P2PKH
assert len(pkh) == 20
return OP_DUP.to_bytes(1) + OP_HASH160.to_bytes(1) + len(pkh).to_bytes(1) + pkh + OP_EQUALVERIFY.to_bytes(1) + OP_CHECKSIG.to_bytes(1)

View File

@@ -32,9 +32,14 @@ class SignatureType(IntEnum):
STSchnorrSecp256k1 = 2
class COutpoint:
class COutPoint:
__slots__ = ('hash', 'n', 'tree')
def __init__(self, hash=0, n=0, tree=0):
self.hash = hash
self.n = n
self.tree = tree
def get_hash(self) -> bytes:
return self.hash.to_bytes(32, 'big')
@@ -43,15 +48,23 @@ class CTxIn:
__slots__ = ('prevout', 'sequence',
'value_in', 'block_height', 'block_index', 'signature_script') # Witness
def __init__(self, tx=None):
def __init__(self, prevout=COutPoint(), sequence=0):
self.prevout = prevout
self.sequence = sequence
self.value_in = -1
self.block_height = 0
self.block_index = 0xffffffff
self.signature_script = bytes()
class CTxOut:
__slots__ = ('value', 'version', 'script_pubkey')
def __init__(self, value=0, script_pubkey=bytes()):
self.value = value
self.version = 0
self.script_pubkey = script_pubkey
class CTransaction:
__slots__ = ('hash', 'version', 'vin', 'vout', 'locktime', 'expiry')
@@ -83,7 +96,7 @@ class CTransaction:
for i in range(num_txin):
txi = CTxIn()
txi.prevout = COutpoint()
txi.prevout = COutPoint()
txi.prevout.hash = int.from_bytes(data[o:o + 32], 'little')
o += 32
txi.prevout.n = int.from_bytes(data[o:o + 4], 'little')

View File

@@ -9,9 +9,15 @@ OP_0 = 0x00
OP_DATA_1 = 0x01
OP_1NEGATE = 0x4f
OP_1 = 0x51
OP_EQUAL = 0x87
OP_PUSHDATA1 = 0x4c
OP_PUSHDATA2 = 0x4d
OP_PUSHDATA4 = 0x4e
OP_DUP = 0x76
OP_EQUALVERIFY = 0x88
OP_HASH160 = 0xa9
OP_CHECKSIG = 0xac
OP_CHECKSEQUENCEVERIFY = 0xb2
def push_script_data(data_array: bytearray, data: bytes) -> None:
@@ -28,7 +34,7 @@ def push_script_data(data_array: bytearray, data: bytes) -> None:
return
if len_data < OP_PUSHDATA1:
data_array += bytes(((OP_DATA_1 - 1) + len_data,))
data_array += len_data.to_bytes(1)
elif len_data <= 0xff:
data_array += bytes((OP_PUSHDATA1, len_data))
elif len_data <= 0xffff:
@@ -36,4 +42,5 @@ def push_script_data(data_array: bytearray, data: bytes) -> None:
else:
data_array += bytes((OP_PUSHDATA4,)) + len_data.to_bytes(4, 'little')
print('[rm] data_array', (data_array + data).hex())
data_array += data

View File

@@ -488,7 +488,7 @@ class NAVInterface(BTCInterface):
return block_rv
def getScriptScriptSig(self, script: bytes) -> bytearray:
def getScriptScriptSig(self, script: bytes) -> bytes:
return self.getP2SHP2WSHScriptSig(script)
def getScriptDest(self, script):