refactor: Remove encodeSegwitP2WSH and getScriptAddress
This commit is contained in:
parent
1d932cdd01
commit
edddf9158a
@ -17,7 +17,6 @@ from .rpc import (
|
|||||||
)
|
)
|
||||||
from .util import (
|
from .util import (
|
||||||
TemporaryError,
|
TemporaryError,
|
||||||
pubkeyToAddress,
|
|
||||||
)
|
)
|
||||||
from .chainparams import (
|
from .chainparams import (
|
||||||
Coins,
|
Coins,
|
||||||
@ -95,18 +94,12 @@ class BaseApp:
|
|||||||
return c
|
return c
|
||||||
raise ValueError('Unknown coin: {}'.format(coin_name))
|
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):
|
def encodeSegwit(self, coin_type, raw):
|
||||||
return segwit_addr.encode(chainparams[coin_type][self.chain]['hrp'], 0, raw)
|
return segwit_addr.encode(chainparams[coin_type][self.chain]['hrp'], 0, raw)
|
||||||
|
|
||||||
def decodeSegwit(self, coin_type, addr):
|
def decodeSegwit(self, coin_type, addr):
|
||||||
return bytes(segwit_addr.decode(chainparams[coin_type][self.chain]['hrp'], addr)[1])
|
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):
|
def callrpc(self, method, params=[], wallet=None):
|
||||||
return callrpc(self.coin_clients[Coins.PART]['rpcport'], self.coin_clients[Coins.PART]['rpcauth'], method, params, wallet)
|
return callrpc(self.coin_clients[Coins.PART]['rpcport'], self.coin_clients[Coins.PART]['rpcauth'], method, params, wallet)
|
||||||
|
|
||||||
|
@ -2189,9 +2189,9 @@ class BasicSwap(BaseApp):
|
|||||||
ci = self.ci(coin_type)
|
ci = self.ci(coin_type)
|
||||||
|
|
||||||
if self.coin_clients[coin_type]['use_segwit']:
|
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:
|
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())
|
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)}])
|
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']:
|
if self.coin_clients[coin_to]['use_segwit']:
|
||||||
p2wsh = getP2WSH(participate_script)
|
p2wsh = getP2WSH(participate_script)
|
||||||
addr_to = self.encodeSegwitP2WSH(coin_to, p2wsh)
|
addr_to = ci.encode_p2wsh(p2wsh)
|
||||||
else:
|
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)}])
|
txn = self.callcoinrpc(coin_to, 'createrawtransaction', [[], {addr_to: ci.format_amount(amount_to)}])
|
||||||
options = {
|
options = {
|
||||||
@ -2429,7 +2429,7 @@ class BasicSwap(BaseApp):
|
|||||||
p2wsh = getP2WSH(txn_script)
|
p2wsh = getP2WSH(txn_script)
|
||||||
vout = getVoutByP2WSH(txjs, p2wsh.hex())
|
vout = getVoutByP2WSH(txjs, p2wsh.hex())
|
||||||
else:
|
else:
|
||||||
addr_to = self.getScriptAddress(Coins.PART, txn_script)
|
addr_to = self.ci(Coins.PART).encode_p2sh(txn_script)
|
||||||
vout = getVoutByAddress(txjs, addr_to)
|
vout = getVoutByAddress(txjs, addr_to)
|
||||||
|
|
||||||
bid_date = dt.datetime.fromtimestamp(bid.created_at).date()
|
bid_date = dt.datetime.fromtimestamp(bid.created_at).date()
|
||||||
@ -2930,6 +2930,9 @@ class BasicSwap(BaseApp):
|
|||||||
save_bid = False
|
save_bid = False
|
||||||
coin_from = Coins(offer.coin_from)
|
coin_from = Coins(offer.coin_from)
|
||||||
coin_to = Coins(offer.coin_to)
|
coin_to = Coins(offer.coin_to)
|
||||||
|
ci_from = self.ci(coin_from)
|
||||||
|
ci_to = self.ci(coin_to)
|
||||||
|
|
||||||
# TODO: Batch calls to scantxoutset
|
# TODO: Batch calls to scantxoutset
|
||||||
# TODO: timeouts
|
# TODO: timeouts
|
||||||
if state == BidStates.BID_ABANDONED:
|
if state == BidStates.BID_ABANDONED:
|
||||||
@ -2938,7 +2941,7 @@ class BasicSwap(BaseApp):
|
|||||||
if state == BidStates.BID_ACCEPTED:
|
if state == BidStates.BID_ACCEPTED:
|
||||||
# Waiting for initiate txn to be confirmed in 'from' chain
|
# Waiting for initiate txn to be confirmed in 'from' chain
|
||||||
initiate_txnid_hex = bid.initiate_tx.txid.hex()
|
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
|
index = None
|
||||||
tx_height = None
|
tx_height = None
|
||||||
last_initiate_txn_conf = bid.initiate_tx.conf
|
last_initiate_txn_conf = bid.initiate_tx.conf
|
||||||
@ -2961,7 +2964,7 @@ class BasicSwap(BaseApp):
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if self.coin_clients[coin_from]['use_segwit']:
|
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:
|
else:
|
||||||
addr = p2sh
|
addr = p2sh
|
||||||
|
|
||||||
@ -3001,9 +3004,9 @@ class BasicSwap(BaseApp):
|
|||||||
elif state == BidStates.SWAP_INITIATED:
|
elif state == BidStates.SWAP_INITIATED:
|
||||||
# Waiting for participate txn to be confirmed in 'to' chain
|
# Waiting for participate txn to be confirmed in 'to' chain
|
||||||
if self.coin_clients[coin_to]['use_segwit']:
|
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:
|
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)
|
ci_to = self.ci(coin_to)
|
||||||
found = ci_to.getLockTxHeight(None, addr, bid.amount_to, bid.chain_b_height_start, find_index=True)
|
found = ci_to.getLockTxHeight(None, addr, bid.amount_to, bid.chain_b_height_start, find_index=True)
|
||||||
|
@ -20,6 +20,7 @@ from .util import (
|
|||||||
b58encode,
|
b58encode,
|
||||||
decodeAddress,
|
decodeAddress,
|
||||||
decodeScriptNum,
|
decodeScriptNum,
|
||||||
|
pubkeyToAddress,
|
||||||
getCompactSizeLen,
|
getCompactSizeLen,
|
||||||
SerialiseNumCompact,
|
SerialiseNumCompact,
|
||||||
getWitnessElementLen)
|
getWitnessElementLen)
|
||||||
@ -93,7 +94,7 @@ def find_vout_for_address_from_txobj(tx_obj, addr):
|
|||||||
for i in range(len(tx_obj["vout"])):
|
for i in range(len(tx_obj["vout"])):
|
||||||
if any([addr == a for a in tx_obj["vout"][i]["scriptPubKey"]["addresses"]]):
|
if any([addr == a for a in tx_obj["vout"][i]["scriptPubKey"]["addresses"]]):
|
||||||
return i
|
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):
|
class BTCInterface(CoinInterface):
|
||||||
@ -279,6 +280,9 @@ class BTCInterface(CoinInterface):
|
|||||||
program = script[2:] # strip version and length
|
program = script[2:] # strip version and length
|
||||||
return segwit_addr.encode(bech32_prefix, version, program)
|
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):
|
def pubkey_to_address(self, pk):
|
||||||
assert(len(pk) == 33)
|
assert(len(pk) == 33)
|
||||||
return self.pkh_to_address(hash160(pk))
|
return self.pkh_to_address(hash160(pk))
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import basicswap.contrib.segwit_addr as segwit_addr
|
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
|
|
||||||
from .contrib.test_framework.messages import (
|
from .contrib.test_framework.messages import (
|
||||||
@ -119,9 +118,6 @@ class PARTInterfaceBlind(PARTInterface):
|
|||||||
def coin_name(self):
|
def coin_name(self):
|
||||||
return super().coin_name() + ' Blind'
|
return super().coin_name() + ' Blind'
|
||||||
|
|
||||||
def encodeSegwitP2WSH(self, p2wsh):
|
|
||||||
return segwit_addr.encode(self.chainparams_network()['hrp'], 0, p2wsh[2:])
|
|
||||||
|
|
||||||
def getScriptLockTxNonce(self, data):
|
def getScriptLockTxNonce(self, data):
|
||||||
return hashlib.sha256(data + bytes('locktx', 'utf-8')).digest()
|
return hashlib.sha256(data + bytes('locktx', 'utf-8')).digest()
|
||||||
|
|
||||||
@ -156,7 +152,7 @@ class PARTInterfaceBlind(PARTInterface):
|
|||||||
ephemeral_pubkey = self.getPubkey(ephemeral_key)
|
ephemeral_pubkey = self.getPubkey(ephemeral_key)
|
||||||
assert(len(ephemeral_pubkey) == 33)
|
assert(len(ephemeral_pubkey) == 33)
|
||||||
nonce = self.getScriptLockTxNonce(vkbv)
|
nonce = self.getScriptLockTxNonce(vkbv)
|
||||||
p2wsh_addr = self.encodeSegwitP2WSH(getP2WSH(script))
|
p2wsh_addr = self.encode_p2wsh(getP2WSH(script))
|
||||||
inputs = []
|
inputs = []
|
||||||
outputs = [{'type': 'blind', 'amount': self.format_amount(value), 'address': p2wsh_addr, 'nonce': nonce.hex(), 'data': ephemeral_pubkey.hex()}]
|
outputs = [{'type': 'blind', 'amount': self.format_amount(value), 'address': p2wsh_addr, 'nonce': nonce.hex(), 'data': ephemeral_pubkey.hex()}]
|
||||||
params = [inputs, outputs]
|
params = [inputs, outputs]
|
||||||
@ -204,7 +200,7 @@ class PARTInterfaceBlind(PARTInterface):
|
|||||||
locked_coin = input_blinded_info['amount']
|
locked_coin = input_blinded_info['amount']
|
||||||
tx_lock_id = lock_tx_obj['txid']
|
tx_lock_id = lock_tx_obj['txid']
|
||||||
refund_script = self.genScriptLockRefundTxScript(Kal, Kaf, csv_val)
|
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']}]
|
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()}]
|
outputs = [{'type': 'blind', 'amount': locked_coin, 'address': p2wsh_addr, 'nonce': output_nonce.hex(), 'data': ephemeral_pubkey.hex()}]
|
||||||
|
Loading…
Reference in New Issue
Block a user