refactor: Remove encodeSegwitP2WSH and getScriptAddress

This commit is contained in:
tecnovert 2021-11-05 10:55:18 +02:00
parent 1d932cdd01
commit edddf9158a
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
4 changed files with 19 additions and 23 deletions

View file

@ -17,7 +17,6 @@ from .rpc import (
)
from .util import (
TemporaryError,
pubkeyToAddress,
)
from .chainparams import (
Coins,
@ -95,18 +94,12 @@ class BaseApp:
return c
raise ValueError('Unknown coin: {}'.format(coin_name))
def encodeSegwitP2WSH(self, coin_type, p2wsh):
return segwit_addr.encode(chainparams[coin_type][self.chain]['hrp'], 0, p2wsh[2:])
def encodeSegwit(self, coin_type, raw):
return segwit_addr.encode(chainparams[coin_type][self.chain]['hrp'], 0, raw)
def decodeSegwit(self, coin_type, addr):
return bytes(segwit_addr.decode(chainparams[coin_type][self.chain]['hrp'], addr)[1])
def getScriptAddress(self, coin_type, script):
return pubkeyToAddress(chainparams[coin_type][self.chain]['script_address'], script)
def callrpc(self, method, params=[], wallet=None):
return callrpc(self.coin_clients[Coins.PART]['rpcport'], self.coin_clients[Coins.PART]['rpcauth'], method, params, wallet)

View file

@ -2189,9 +2189,9 @@ class BasicSwap(BaseApp):
ci = self.ci(coin_type)
if self.coin_clients[coin_type]['use_segwit']:
addr_to = self.encodeSegwitP2WSH(coin_type, getP2WSH(initiate_script))
addr_to = ci.encode_p2wsh(getP2WSH(initiate_script))
else:
addr_to = self.getScriptAddress(coin_type, initiate_script)
addr_to = ci.encode_p2sh(initiate_script)
self.log.debug('Create initiate txn for coin %s to %s for bid %s', str(coin_type), addr_to, bid_id.hex())
txn = self.callcoinrpc(coin_type, 'createrawtransaction', [[], {addr_to: ci.format_amount(bid.amount)}])
@ -2273,9 +2273,9 @@ class BasicSwap(BaseApp):
if self.coin_clients[coin_to]['use_segwit']:
p2wsh = getP2WSH(participate_script)
addr_to = self.encodeSegwitP2WSH(coin_to, p2wsh)
addr_to = ci.encode_p2wsh(p2wsh)
else:
addr_to = self.getScriptAddress(coin_to, participate_script)
addr_to = ci.encode_p2sh(participate_script)
txn = self.callcoinrpc(coin_to, 'createrawtransaction', [[], {addr_to: ci.format_amount(amount_to)}])
options = {
@ -2429,7 +2429,7 @@ class BasicSwap(BaseApp):
p2wsh = getP2WSH(txn_script)
vout = getVoutByP2WSH(txjs, p2wsh.hex())
else:
addr_to = self.getScriptAddress(Coins.PART, txn_script)
addr_to = self.ci(Coins.PART).encode_p2sh(txn_script)
vout = getVoutByAddress(txjs, addr_to)
bid_date = dt.datetime.fromtimestamp(bid.created_at).date()
@ -2930,6 +2930,9 @@ class BasicSwap(BaseApp):
save_bid = False
coin_from = Coins(offer.coin_from)
coin_to = Coins(offer.coin_to)
ci_from = self.ci(coin_from)
ci_to = self.ci(coin_to)
# TODO: Batch calls to scantxoutset
# TODO: timeouts
if state == BidStates.BID_ABANDONED:
@ -2938,7 +2941,7 @@ class BasicSwap(BaseApp):
if state == BidStates.BID_ACCEPTED:
# Waiting for initiate txn to be confirmed in 'from' chain
initiate_txnid_hex = bid.initiate_tx.txid.hex()
p2sh = self.getScriptAddress(coin_from, bid.initiate_tx.script)
p2sh = ci_from.encode_p2sh(bid.initiate_tx.script)
index = None
tx_height = None
last_initiate_txn_conf = bid.initiate_tx.conf
@ -2961,7 +2964,7 @@ class BasicSwap(BaseApp):
pass
else:
if self.coin_clients[coin_from]['use_segwit']:
addr = self.encodeSegwitP2WSH(coin_from, getP2WSH(bid.initiate_tx.script))
addr = ci_from.encode_p2wsh(getP2WSH(bid.initiate_tx.script))
else:
addr = p2sh
@ -3001,9 +3004,9 @@ class BasicSwap(BaseApp):
elif state == BidStates.SWAP_INITIATED:
# Waiting for participate txn to be confirmed in 'to' chain
if self.coin_clients[coin_to]['use_segwit']:
addr = self.encodeSegwitP2WSH(coin_to, getP2WSH(bid.participate_tx.script))
addr = ci_to.encode_p2wsh(getP2WSH(bid.participate_tx.script))
else:
addr = self.getScriptAddress(coin_to, bid.participate_tx.script)
addr = ci_to.encode_p2sh(bid.participate_tx.script)
ci_to = self.ci(coin_to)
found = ci_to.getLockTxHeight(None, addr, bid.amount_to, bid.chain_b_height_start, find_index=True)

View file

@ -20,6 +20,7 @@ from .util import (
b58encode,
decodeAddress,
decodeScriptNum,
pubkeyToAddress,
getCompactSizeLen,
SerialiseNumCompact,
getWitnessElementLen)
@ -93,7 +94,7 @@ def find_vout_for_address_from_txobj(tx_obj, addr):
for i in range(len(tx_obj["vout"])):
if any([addr == a for a in tx_obj["vout"][i]["scriptPubKey"]["addresses"]]):
return i
raise RuntimeError("Vout not found for address: txid=%s, addr=%s" % (txid, addr))
raise RuntimeError("Vout not found for address: txid={}, addr={}".format(tx_obj['txid'], addr))
class BTCInterface(CoinInterface):
@ -279,6 +280,9 @@ class BTCInterface(CoinInterface):
program = script[2:] # strip version and length
return segwit_addr.encode(bech32_prefix, version, program)
def encode_p2sh(self, script):
return pubkeyToAddress(self.chainparams_network()['script_address'], script)
def pubkey_to_address(self, pk):
assert(len(pk) == 33)
return self.pkh_to_address(hash160(pk))

View file

@ -6,7 +6,6 @@
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
import hashlib
import basicswap.contrib.segwit_addr as segwit_addr
from enum import IntEnum
from .contrib.test_framework.messages import (
@ -119,9 +118,6 @@ class PARTInterfaceBlind(PARTInterface):
def coin_name(self):
return super().coin_name() + ' Blind'
def encodeSegwitP2WSH(self, p2wsh):
return segwit_addr.encode(self.chainparams_network()['hrp'], 0, p2wsh[2:])
def getScriptLockTxNonce(self, data):
return hashlib.sha256(data + bytes('locktx', 'utf-8')).digest()
@ -156,7 +152,7 @@ class PARTInterfaceBlind(PARTInterface):
ephemeral_pubkey = self.getPubkey(ephemeral_key)
assert(len(ephemeral_pubkey) == 33)
nonce = self.getScriptLockTxNonce(vkbv)
p2wsh_addr = self.encodeSegwitP2WSH(getP2WSH(script))
p2wsh_addr = self.encode_p2wsh(getP2WSH(script))
inputs = []
outputs = [{'type': 'blind', 'amount': self.format_amount(value), 'address': p2wsh_addr, 'nonce': nonce.hex(), 'data': ephemeral_pubkey.hex()}]
params = [inputs, outputs]
@ -204,7 +200,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.encodeSegwitP2WSH(getP2WSH(refund_script))
p2wsh_addr = self.encode_p2wsh(getP2WSH(refund_script))
inputs = [{'txid': tx_lock_id, 'vout': spend_n, 'sequence': lock1_value, 'blindingfactor': input_blinded_info['blind']}]
outputs = [{'type': 'blind', 'amount': locked_coin, 'address': p2wsh_addr, 'nonce': output_nonce.hex(), 'data': ephemeral_pubkey.hex()}]