Skip to content

Commit fac6709

Browse files
committed
Simplify transaction signing in examples using 'build_and_sign' method
1 parent 91cd7a4 commit fac6709

File tree

5 files changed

+15
-65
lines changed

5 files changed

+15
-65
lines changed

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pygments==2.11.2; python_version >= "3.6" and python_full_version < "3.0.0" or p
3737
pynacl==1.5.0; python_version >= "3.6"
3838
pyparsing==3.0.7; python_version >= "3.7"
3939
pytest-cov==3.0.0; python_version >= "3.6"
40-
pytest==7.1.0; python_version >= "3.7"
40+
pytest==7.1.1; python_version >= "3.7"
4141
pytz==2021.3; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.4.0" and python_version >= "3.6"
4242
requests==2.27.1; python_version >= "3.7" and python_full_version < "3.0.0" and python_version < "4" or python_version >= "3.7" and python_version < "4" and python_full_version >= "3.6.0"
4343
retry==0.9.2

docs/source/guides/transaction.rst

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -142,35 +142,22 @@ Specify output amount::
142142

143143
Step 6
144144

145-
Create the transaction body using transaction builder::
145+
Create a signed transaction using transaction builder. Unlike building a raw transaction, where we need to manually
146+
sign a transaction and build a transaction witness set, transaction builder can build and sign a transaction directly
147+
with its `build_and_sign` method. The code below tells the builder to build a transaction and sign the transaction
148+
with a list of signing keys (in this case, we only need the signature from one signing key, `sk`) and send the change
149+
back to sender's address::
146150

147-
>>> tx_body = builder.build(change_address=address)
151+
>>> tx = builder.build_and_sign([sk], change_address=address)
148152

149-
Transaction ID could be obtained when we are done configuring the transaction body, because is essentially the hash
150-
of `TransactionBody`::
153+
Transaction ID could be obtained from the transaction obejct::
151154

152-
>>> tx_body.id
155+
>>> tx.id
153156
TransactionId(hex='1d40b950ded3a144fb4c100d1cf8b85719da91b06845530e34a0304427692ce4')
154157

155-
Step 7
156-
157-
Sign the transaction body hash and create a complete transaction (same as the step 4 in raw transaction example)::
158-
159-
>>> # Sign the transaction body hash
160-
>>> signature = sk.sign(tx_body.hash())
161-
>>> # Alternatively, we can sign the transaction ID as well
162-
>>> signature_alternative = sk.sign(tx_body.id.payload)
163-
>>> assert signature == signature_alternative
164-
165-
>>> # Add verification key and the signature to the witness set
166-
>>> vk_witnesses = [VerificationKeyWitness(vk, signature)]
167-
168-
>>> # Create final signed transaction
169-
>>> signed_tx = Transaction(tx_body, TransactionWitnessSet(vkey_witnesses=vk_witnesses))
170-
171158

172159
By using transaction builder, we no longer need to specify which UTxO to use as transaction input or calculate
173-
transaction fee, because they are taken care by the transaction builder.
160+
transaction fee, because they are taken care by the transaction builder. Also, the code becomes much more concise.
174161

175162
A more complex example of using transaction builder could be found
176163
in this `Github example <https://github.com/cffls/pycardano/blob/main/examples/tx_builder.py>`_.

examples/delegator_loyalty_rewards.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,15 @@
9191

9292
builder.auxiliary_data = auxiliary_data
9393

94-
tx_body = builder.build(change_address=Address.from_primitive(CHANGE_ADDRESS))
95-
96-
# Sign the transaction body hash using the payment signing key
97-
signature = psk.sign(tx_body.hash())
98-
99-
# Add verification key and the signature to the witness set
100-
vk_witnesses = [VerificationKeyWitness(pvk, signature)]
101-
10294
# Create final signed transaction
103-
signed_tx = Transaction(
104-
tx_body,
105-
TransactionWitnessSet(vkey_witnesses=vk_witnesses),
106-
auxiliary_data=auxiliary_data,
95+
signed_tx = builder.build_and_sign(
96+
[psk], change_address=Address.from_primitive(CHANGE_ADDRESS)
10797
)
10898

10999
# Submit signed transaction to the network
110100
print(signed_tx)
111101

112102
print("#### Transaction id ####")
113-
print(tx_body.hash().hex())
103+
print(signed_tx.id)
114104
context.submit_tx(signed_tx.to_cbor())
115105
print("Transaction successfully submitted!")

examples/native_token.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,8 @@ def load_or_create_key_pair(base_dir, base_name):
161161
# Build a finalized transaction body with the change returning to our own address
162162
tx_body = builder.build(change_address=address)
163163

164-
"""Sign transaction and add witnesses"""
165-
# Sign the transaction body hash using the payment signing key
166-
payment_signature = payment_skey.sign(tx_body.hash())
167-
168-
# Sign the transaction body hash using the policy signing key because we are minting new tokens
169-
policy_signature = policy_skey.sign(tx_body.hash())
170-
171-
# Add verification keys and their signatures to the witness set
172-
vk_witnesses = [
173-
VerificationKeyWitness(payment_vkey, payment_signature),
174-
VerificationKeyWitness(policy_vkey, policy_signature),
175-
]
176-
177164
# Create final signed transaction
178-
signed_tx = Transaction(
179-
tx_body,
180-
# Beside vk witnesses, We also need to add the policy script to witness set when we are minting new tokens.
181-
TransactionWitnessSet(vkey_witnesses=vk_witnesses, native_scripts=native_scripts),
182-
auxiliary_data=auxiliary_data,
183-
)
165+
signed_tx = builder.build_and_sign([payment_skey, policy_skey], change_address=address)
184166

185167
print("############### Transaction created ###############")
186168
print(signed_tx)

examples/tx_builder.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,8 @@
7575
)
7676
)
7777

78-
# Build a finalized transaction body with the change returning to the address we own
79-
tx_body = builder.build(change_address=address)
80-
81-
# Sign the transaction body hash using the payment signing key
82-
signature = psk.sign(tx_body.hash())
83-
84-
# Add verification key and the signature to the witness set
85-
vk_witnesses = [VerificationKeyWitness(pvk, signature)]
86-
8778
# Create final signed transaction
88-
signed_tx = Transaction(tx_body, TransactionWitnessSet(vkey_witnesses=vk_witnesses))
79+
signed_tx = builder.build_and_sign([psk], change_address=address)
8980

9081
# Submit signed transaction to the network
9182
context.submit_tx(signed_tx.to_cbor())

0 commit comments

Comments
 (0)