Skip to content

Add functions to automatically add required signers and validity range #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pycardano/txbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,8 @@ def build(
change_address: Optional[Address] = None,
merge_change: Optional[bool] = False,
collateral_change_address: Optional[Address] = None,
auto_validity: Optional[bool] = True,
auto_required_signers: Optional[bool] = True,
) -> TransactionBody:
"""Build a transaction body from all constraints set through the builder.

Expand All @@ -878,11 +880,34 @@ def build(
merge_change (Optional[bool]): If the change address match one of the transaction output, the change amount
will be directly added to that transaction output, instead of being added as a separate output.
collateral_change_address (Optional[Address]): Address to which collateral changes will be returned.
auto_validity (Optional[bool]): Automatically set the validity interval of the transaction to a
reasonable value
auto_required_signers (Optional[bool]): Automatically add all pubkeyhashes of transaction inputs
to required signatories

Returns:
TransactionBody: A transaction body.
"""
self._ensure_no_input_exclusion_conflict()

if auto_validity:
# Automatically set the validity range to a tight value around transaction creation
last_slot = self.context.last_block_slot
if self.validity_start is None:
self.validity_start = max(0, last_slot - 1000)
if self.ttl is None:
self.ttl = last_slot + 10000

if auto_required_signers:
# collect all signatories from explicitly defined transaction inputs and collateral inputs
required_signers = set(
i.output.address.payment_part
for i in self.inputs + self.collaterals
if not isinstance(i.output.address.payment_part, ScriptHash)
and i.output.address.payment_part is not None
)
self.required_signers = list(required_signers)

selected_utxos = []
selected_amount = Value()
for i in self.inputs:
Expand Down Expand Up @@ -1165,6 +1190,8 @@ def build_and_sign(
change_address: Optional[Address] = None,
merge_change: Optional[bool] = False,
collateral_change_address: Optional[Address] = None,
auto_validity: Optional[bool] = True,
auto_required_signers: Optional[bool] = True,
) -> Transaction:
"""Build a transaction body from all constraints set through the builder and sign the transaction with
provided signing keys.
Expand All @@ -1177,6 +1204,10 @@ def build_and_sign(
merge_change (Optional[bool]): If the change address match one of the transaction output, the change amount
will be directly added to that transaction output, instead of being added as a separate output.
collateral_change_address (Optional[Address]): Address to which collateral changes will be returned.
auto_validity (Optional[bool]): Automatically set the validity interval of the transaction to
a reasonable value
auto_required_signers (Optional[bool]): Automatically add all pubkeyhashes of transaction inputs
to required signatories

Returns:
Transaction: A signed transaction.
Expand All @@ -1186,6 +1217,8 @@ def build_and_sign(
change_address=change_address,
merge_change=merge_change,
collateral_change_address=collateral_change_address,
auto_validity=auto_validity,
auto_required_signers=auto_required_signers,
)
witness_set = self.build_witness_set()
witness_set.vkey_witnesses = []
Expand Down
98 changes: 78 additions & 20 deletions test/pycardano/test_txbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def test_tx_builder(chain_context):
TransactionOutput.from_primitive([sender, 500000])
)

tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

expected = {
0: [[b"11111111111111111111111111111111", 0]],
Expand Down Expand Up @@ -100,7 +102,9 @@ def test_tx_builder_with_certain_input(chain_context):
TransactionOutput.from_primitive([sender, 500000])
)

tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

expected = {
0: [[b"22222222222222222222222222222222", 1]],
Expand Down Expand Up @@ -136,7 +140,9 @@ def test_tx_builder_multi_asset(chain_context):
)
)

tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

expected = {
0: [
Expand Down Expand Up @@ -178,7 +184,11 @@ def test_tx_builder_raises_utxo_selection(chain_context):
)

with pytest.raises(UTxOSelectionException) as e:
tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address,
auto_validity=False,
auto_required_signers=False,
)

# The unfulfilled amount includes requested (991000000) and estimated fees (161277)
assert "Unfulfilled amount:\n {\n 'coin': 991161277" in e.value.args[0]
Expand Down Expand Up @@ -214,7 +224,9 @@ def test_tx_small_utxo_precise_fee(chain_context):

# This will not fail as we replace max fee constraint with more precise fee calculation
# And remainder is greater than minimum ada required for change
tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

expect = {
0: [
Expand Down Expand Up @@ -266,7 +278,9 @@ def test_tx_small_utxo_balance_pass(chain_context):

# Balance is smaller than minimum ada required in change
# Additional UTxOs are selected from the input address
tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

expected = {
0: [
Expand Down Expand Up @@ -312,7 +326,9 @@ def test_tx_builder_mint_multi_asset(chain_context):
tx_builder.native_scripts = [script]
tx_builder.ttl = 123456789

tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

expected = {
0: [
Expand Down Expand Up @@ -354,7 +370,9 @@ def test_tx_add_change_split_nfts(chain_context):
TransactionOutput.from_primitive([sender, 7000000])
)

tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

expected = {
0: [
Expand Down Expand Up @@ -786,7 +804,9 @@ def test_excluded_input(chain_context):

tx_builder.excluded_inputs.append(chain_context.utxos(sender)[0])

tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

expected = {
0: [[b"22222222222222222222222222222222", 1]],
Expand Down Expand Up @@ -819,15 +839,22 @@ def test_build_and_sign(chain_context):
TransactionOutput.from_primitive([sender, 500000])
)

tx_body = tx_builder1.build(change_address=sender_address)
tx_body = tx_builder1.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

tx_builder2 = TransactionBuilder(
chain_context, [RandomImproveMultiAsset([0, 0, 0, 0, 0])]
)
tx_builder2.add_input_address(sender).add_output(
TransactionOutput.from_primitive([sender, 500000])
)
tx = tx_builder2.build_and_sign([SK], change_address=sender_address)
tx = tx_builder2.build_and_sign(
[SK],
change_address=sender_address,
auto_validity=False,
auto_required_signers=False,
)

assert tx.transaction_witness_set.vkey_witnesses == [
VerificationKeyWitness(SK.to_verification_key(), SK.sign(tx_body.hash()))
Expand Down Expand Up @@ -883,7 +910,7 @@ def test_tx_builder_exact_fee_no_change(chain_context):

tx_builder.add_output(TransactionOutput.from_primitive([sender, 5000000]))

tx_body = tx_builder.build()
tx_body = tx_builder.build(auto_validity=False, auto_required_signers=False)

tx_builder = TransactionBuilder(chain_context)

Expand All @@ -897,7 +924,9 @@ def test_tx_builder_exact_fee_no_change(chain_context):
TransactionOutput.from_primitive([sender, input_amount - tx_body.fee])
)

tx = tx_builder.build_and_sign([SK])
tx = tx_builder.build_and_sign(
[SK], auto_validity=False, auto_required_signers=False
)

expected = {
0: [[b"11111111111111111111111111111111", 3]],
Expand Down Expand Up @@ -933,7 +962,9 @@ def test_tx_builder_certificates(chain_context):

tx_builder.certificates = [stake_registration, stake_delegation]

tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

expected = {
0: [[b"11111111111111111111111111111111", 0]],
Expand Down Expand Up @@ -970,7 +1001,9 @@ def test_tx_builder_withdrawal(chain_context):
withdrawals = Withdrawals({bytes(stake_address): 10000})
tx_builder.withdrawals = withdrawals

tx_body = tx_builder.build(change_address=sender_address)
tx_body = tx_builder.build(
change_address=sender_address, auto_validity=False, auto_required_signers=False
)

expected = {
0: [[b"11111111111111111111111111111111", 0]],
Expand Down Expand Up @@ -1002,7 +1035,12 @@ def test_tx_builder_no_output(chain_context):

tx_builder.add_input(utxo1)

tx_body = tx_builder.build(change_address=sender_address, merge_change=True)
tx_body = tx_builder.build(
change_address=sender_address,
merge_change=True,
auto_validity=False,
auto_required_signers=False,
)

expected = {
0: [[b"11111111111111111111111111111111", 3]],
Expand All @@ -1029,7 +1067,12 @@ def test_tx_builder_merge_change_to_output(chain_context):
tx_builder.add_input(utxo1)
tx_builder.add_output(TransactionOutput.from_primitive([sender, 10000]))

tx_body = tx_builder.build(change_address=sender_address, merge_change=True)
tx_body = tx_builder.build(
change_address=sender_address,
merge_change=True,
auto_validity=False,
auto_required_signers=False,
)

expected = {
0: [[b"11111111111111111111111111111111", 3]],
Expand Down Expand Up @@ -1060,7 +1103,12 @@ def test_tx_builder_merge_change_to_output_2(chain_context):
tx_builder.add_output(TransactionOutput.from_primitive([receiver, 10000]))
tx_builder.add_output(TransactionOutput.from_primitive([sender, 0]))

tx_body = tx_builder.build(change_address=sender_address, merge_change=True)
tx_body = tx_builder.build(
change_address=sender_address,
merge_change=True,
auto_validity=False,
auto_required_signers=False,
)

expected = {
0: [[b"11111111111111111111111111111111", 3]],
Expand Down Expand Up @@ -1089,7 +1137,12 @@ def test_tx_builder_merge_change_to_zero_amount_output(chain_context):
tx_builder.add_input(utxo1)
tx_builder.add_output(TransactionOutput.from_primitive([sender, 0]))

tx_body = tx_builder.build(change_address=sender_address, merge_change=True)
tx_body = tx_builder.build(
change_address=sender_address,
merge_change=True,
auto_validity=False,
auto_required_signers=False,
)

expected = {
0: [[b"11111111111111111111111111111111", 3]],
Expand All @@ -1116,7 +1169,12 @@ def test_tx_builder_merge_change_smaller_than_min_utxo(chain_context):
tx_builder.add_input(utxo1)
tx_builder.add_output(TransactionOutput.from_primitive([sender, 9800000]))

tx_body = tx_builder.build(change_address=sender_address, merge_change=True)
tx_body = tx_builder.build(
change_address=sender_address,
merge_change=True,
auto_validity=False,
auto_required_signers=False,
)

expected = {
0: [[b"11111111111111111111111111111111", 3]],
Expand Down
2 changes: 1 addition & 1 deletion test/pycardano/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def epoch(self) -> int:
return 300

@property
def slot(self) -> int:
def last_block_slot(self) -> int:
"""Current slot number"""
return 2000

Expand Down