bitcoin.py/transaction.py: API changes: rm most hex usage
Instead of some functions operating with hex strings, and others using bytes, this consolidates most things to use bytes. This mainly focuses on bitcoin.py and transaction.py, and then adapts the API usages in other files. Notably, - scripts, - pubkeys, - signatures should be bytes in almost all places now.
This commit is contained in:
@@ -24,7 +24,7 @@ from .transaction import (Transaction, PartialTransaction, PartialTxInput, TxOut
|
||||
from .ecc import CURVE_ORDER, ecdsa_sig64_from_der_sig, ECPubkey, string_to_number
|
||||
from . import ecc, bitcoin, crypto, transaction
|
||||
from . import descriptor
|
||||
from .bitcoin import (push_script, redeem_script_to_address, address_to_script,
|
||||
from .bitcoin import (redeem_script_to_address, address_to_script,
|
||||
construct_witness, construct_script)
|
||||
from . import segwit_addr
|
||||
from .i18n import _
|
||||
@@ -591,7 +591,9 @@ def derive_blinded_privkey(basepoint_secret: bytes, per_commitment_secret: bytes
|
||||
return int.to_bytes(sum, length=32, byteorder='big', signed=False)
|
||||
|
||||
|
||||
def make_htlc_tx_output(amount_msat, local_feerate, revocationpubkey, local_delayedpubkey, success, to_self_delay):
|
||||
def make_htlc_tx_output(
|
||||
amount_msat, local_feerate, revocationpubkey, local_delayedpubkey, success, to_self_delay,
|
||||
) -> Tuple[bytes, PartialTxOutput]:
|
||||
assert type(amount_msat) is int
|
||||
assert type(local_feerate) is int
|
||||
script = make_commitment_output_to_local_witness_script(
|
||||
@@ -600,7 +602,7 @@ def make_htlc_tx_output(amount_msat, local_feerate, revocationpubkey, local_dela
|
||||
delayed_pubkey=local_delayedpubkey,
|
||||
)
|
||||
|
||||
p2wsh = bitcoin.redeem_script_to_address('p2wsh', script.hex())
|
||||
p2wsh = bitcoin.redeem_script_to_address('p2wsh', script)
|
||||
weight = HTLC_SUCCESS_WEIGHT if success else HTLC_TIMEOUT_WEIGHT
|
||||
fee = local_feerate * weight
|
||||
fee = fee // 1000 * 1000
|
||||
@@ -615,7 +617,7 @@ def make_htlc_tx_witness(remotehtlcsig: bytes, localhtlcsig: bytes,
|
||||
assert type(localhtlcsig) is bytes
|
||||
assert type(payment_preimage) is bytes
|
||||
assert type(witness_script) is bytes
|
||||
return bfh(construct_witness([0, remotehtlcsig, localhtlcsig, payment_preimage, witness_script]))
|
||||
return construct_witness([0, remotehtlcsig, localhtlcsig, payment_preimage, witness_script])
|
||||
|
||||
def make_htlc_tx_inputs(htlc_output_txid: str, htlc_output_index: int,
|
||||
amount_msat: int, witness_script: str) -> List[PartialTxInput]:
|
||||
@@ -648,7 +650,7 @@ def make_offered_htlc(
|
||||
assert type(remote_htlcpubkey) is bytes
|
||||
assert type(local_htlcpubkey) is bytes
|
||||
assert type(payment_hash) is bytes
|
||||
script = bfh(construct_script([
|
||||
script = construct_script([
|
||||
opcodes.OP_DUP,
|
||||
opcodes.OP_HASH160,
|
||||
bitcoin.hash_160(revocation_pubkey),
|
||||
@@ -675,7 +677,7 @@ def make_offered_htlc(
|
||||
opcodes.OP_CHECKSIG,
|
||||
opcodes.OP_ENDIF,
|
||||
opcodes.OP_ENDIF,
|
||||
]))
|
||||
])
|
||||
return script
|
||||
|
||||
def make_received_htlc(
|
||||
@@ -690,7 +692,7 @@ def make_received_htlc(
|
||||
assert type(i) is bytes
|
||||
assert type(cltv_abs) is int
|
||||
|
||||
script = bfh(construct_script([
|
||||
script = construct_script([
|
||||
opcodes.OP_DUP,
|
||||
opcodes.OP_HASH160,
|
||||
bitcoin.hash_160(revocation_pubkey),
|
||||
@@ -720,7 +722,7 @@ def make_received_htlc(
|
||||
opcodes.OP_CHECKSIG,
|
||||
opcodes.OP_ENDIF,
|
||||
opcodes.OP_ENDIF,
|
||||
]))
|
||||
])
|
||||
return script
|
||||
|
||||
WITNESS_TEMPLATE_OFFERED_HTLC = [
|
||||
@@ -830,7 +832,7 @@ def possible_output_idxs_of_htlc_in_ctx(*, chan: 'Channel', pcp: bytes, subject:
|
||||
local_htlc_pubkey=htlc_pubkey,
|
||||
payment_hash=payment_hash,
|
||||
cltv_abs=cltv_abs)
|
||||
htlc_address = redeem_script_to_address('p2wsh', preimage_script.hex())
|
||||
htlc_address = redeem_script_to_address('p2wsh', preimage_script)
|
||||
candidates = ctx.get_output_idxs_from_address(htlc_address)
|
||||
return {output_idx for output_idx in candidates
|
||||
if ctx.outputs()[output_idx].value == htlc.amount_msat // 1000}
|
||||
@@ -938,20 +940,20 @@ LOCAL = HTLCOwner.LOCAL
|
||||
REMOTE = HTLCOwner.REMOTE
|
||||
|
||||
def make_commitment_outputs(*, fees_per_participant: Mapping[HTLCOwner, int], local_amount_msat: int, remote_amount_msat: int,
|
||||
local_script: str, remote_script: str, htlcs: List[ScriptHtlc], dust_limit_sat: int) -> Tuple[List[PartialTxOutput], List[PartialTxOutput]]:
|
||||
local_script: bytes, remote_script: bytes, htlcs: List[ScriptHtlc], dust_limit_sat: int) -> Tuple[List[PartialTxOutput], List[PartialTxOutput]]:
|
||||
# BOLT-03: "Base commitment transaction fees are extracted from the funder's amount;
|
||||
# if that amount is insufficient, the entire amount of the funder's output is used."
|
||||
# -> if funder cannot afford feerate, their output might go negative, so take max(0, x) here:
|
||||
to_local_amt = max(0, local_amount_msat - fees_per_participant[LOCAL])
|
||||
to_local = PartialTxOutput(scriptpubkey=bfh(local_script), value=to_local_amt // 1000)
|
||||
to_local = PartialTxOutput(scriptpubkey=local_script, value=to_local_amt // 1000)
|
||||
to_remote_amt = max(0, remote_amount_msat - fees_per_participant[REMOTE])
|
||||
to_remote = PartialTxOutput(scriptpubkey=bfh(remote_script), value=to_remote_amt // 1000)
|
||||
to_remote = PartialTxOutput(scriptpubkey=remote_script, value=to_remote_amt // 1000)
|
||||
|
||||
non_htlc_outputs = [to_local, to_remote]
|
||||
htlc_outputs = []
|
||||
for script, htlc in htlcs:
|
||||
addr = bitcoin.redeem_script_to_address('p2wsh', script.hex())
|
||||
htlc_outputs.append(PartialTxOutput(scriptpubkey=bfh(address_to_script(addr)),
|
||||
addr = bitcoin.redeem_script_to_address('p2wsh', script)
|
||||
htlc_outputs.append(PartialTxOutput(scriptpubkey=address_to_script(addr),
|
||||
value=htlc.amount_msat // 1000))
|
||||
|
||||
# trim outputs
|
||||
@@ -1060,7 +1062,7 @@ def make_commitment_output_to_local_witness_script(
|
||||
assert type(revocation_pubkey) is bytes
|
||||
assert type(to_self_delay) is int
|
||||
assert type(delayed_pubkey) is bytes
|
||||
script = bfh(construct_script([
|
||||
script = construct_script([
|
||||
opcodes.OP_IF,
|
||||
revocation_pubkey,
|
||||
opcodes.OP_ELSE,
|
||||
@@ -1070,13 +1072,13 @@ def make_commitment_output_to_local_witness_script(
|
||||
delayed_pubkey,
|
||||
opcodes.OP_ENDIF,
|
||||
opcodes.OP_CHECKSIG,
|
||||
]))
|
||||
])
|
||||
return script
|
||||
|
||||
def make_commitment_output_to_local_address(
|
||||
revocation_pubkey: bytes, to_self_delay: int, delayed_pubkey: bytes) -> str:
|
||||
local_script = make_commitment_output_to_local_witness_script(revocation_pubkey, to_self_delay, delayed_pubkey)
|
||||
return bitcoin.redeem_script_to_address('p2wsh', local_script.hex())
|
||||
return bitcoin.redeem_script_to_address('p2wsh', local_script)
|
||||
|
||||
def make_commitment_output_to_remote_address(remote_payment_pubkey: bytes) -> str:
|
||||
return bitcoin.pubkey_to_address('p2wpkh', remote_payment_pubkey.hex())
|
||||
@@ -1087,10 +1089,10 @@ def sign_and_get_sig_string(tx: PartialTransaction, local_config, remote_config)
|
||||
sig_64 = ecdsa_sig64_from_der_sig(sig[:-1])
|
||||
return sig_64
|
||||
|
||||
def funding_output_script(local_config, remote_config) -> str:
|
||||
def funding_output_script(local_config: 'LocalConfig', remote_config: 'RemoteConfig') -> bytes:
|
||||
return funding_output_script_from_keys(local_config.multisig_key.pubkey, remote_config.multisig_key.pubkey)
|
||||
|
||||
def funding_output_script_from_keys(pubkey1: bytes, pubkey2: bytes) -> str:
|
||||
def funding_output_script_from_keys(pubkey1: bytes, pubkey2: bytes) -> bytes:
|
||||
pubkeys = sorted([pubkey1.hex(), pubkey2.hex()])
|
||||
return transaction.multisig_script(pubkeys, 2)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user