Skip to content

Commit

Permalink
transaction body layer
Browse files Browse the repository at this point in the history
  • Loading branch information
exploreriii committed Dec 15, 2024
1 parent 5110f14 commit a455c4b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
13 changes: 1 addition & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,4 @@ def mock_account_ids():
node_account_id = AccountId(0, 0, 3)
token_id_1 = TokenId(1, 1, 1)
token_id_2 = TokenId(2, 2, 2)
return account_id_sender, account_id_recipient, node_account_id, token_id_1, token_id_2

@pytest.fixture
def admin_key():
admin_key = PrivateKey.generate()

admin_public_key_bytes = admin_key.public_key().public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw
)

return admin_key, admin_public_key_bytes
return account_id_sender, account_id_recipient, node_account_id, token_id_1, token_id_2
33 changes: 18 additions & 15 deletions tests/test_token_create_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ def test_build_transaction_body_without_admin_key(mock_account_ids):
assert transaction_body.tokenCreation.initialSupply == 1000
assert not transaction_body.tokenCreation.HasField("adminKey")

def test_build_transaction_body(mock_account_ids, admin_key):
def test_build_transaction_body(mock_account_ids):
"""Test building a token creation transaction body with valid values."""
treasury_account, _, node_account_id, _, _ = mock_account_ids

private_key_admin = MagicMock()
private_key_admin.sign.return_value = b'admin_signature'
private_key_admin.public_key().public_bytes.return_value = b'admin_public_key'

token_tx = TokenCreateTransaction()
token_tx.set_token_name("MyToken")
token_tx.set_token_symbol("MTK")
token_tx.set_decimals(2)
token_tx.set_initial_supply(1000)
token_tx.set_treasury_account_id(treasury_account)
token_tx.transaction_id = generate_transaction_id(treasury_account)
token_tx.set_admin_key(admin_key[0])
token_tx.set_admin_key(private_key_admin)
token_tx.node_account_id = node_account_id

transaction_body = token_tx.build_transaction_body()
Expand All @@ -62,7 +66,7 @@ def test_build_transaction_body(mock_account_ids, admin_key):
assert transaction_body.tokenCreation.symbol == "MTK"
assert transaction_body.tokenCreation.decimals == 2
assert transaction_body.tokenCreation.initialSupply == 1000
assert transaction_body.tokenCreation.adminKey.ed25519 == admin_key[1]
assert transaction_body.tokenCreation.adminKey.ed25519 == b'admin_public_key'

def test_missing_fields():
"""Test that building a transaction without required fields raises a ValueError."""
Expand Down Expand Up @@ -131,35 +135,34 @@ def test_to_proto_without_admin_key(mock_account_ids):

assert not transaction_body.tokenCreation.HasField("adminKey")

def test_to_proto(mock_account_ids, admin_key):
def test_to_proto(mock_account_ids):
"""Test converting the token creation transaction to protobuf format after signing."""
treasury_account, _, node_account_id, _, _ = mock_account_ids

private_key = MagicMock()
private_key.sign.return_value = b'signature'
private_key.public_key().public_bytes.return_value = b'public_key'

private_key_admin = MagicMock()
private_key_admin.sign.return_value = b'admin_signature'
private_key_admin.public_key().public_bytes.return_value = b'admin_public_key'

token_tx = TokenCreateTransaction()
token_tx.set_token_name("MyToken")
token_tx.set_token_symbol("MTK")
token_tx.set_decimals(2)
token_tx.set_initial_supply(1000)
token_tx.set_treasury_account_id(treasury_account)
token_tx.set_admin_key(admin_key[0])
token_tx.set_admin_key(private_key_admin)
token_tx.transaction_id = generate_transaction_id(treasury_account)
token_tx.node_account_id = node_account_id

private_key = MagicMock()
private_key.sign.return_value = b'signature'
private_key.public_key().public_bytes.return_value = b'public_key'

private_key_admin = MagicMock()
private_key_admin.sign.return_value = b'admin_signature'
private_key_admin.public_key().public_bytes.return_value = b'admin_public_key'

token_tx.sign(private_key)
token_tx.sign(private_key_admin)
proto = token_tx.to_proto()

assert len(proto.signedTransactionBytes) > 0

transaction = transaction_pb2.Transaction.FromString(proto.signedTransactionBytes)
transaction_body = transaction_body_pb2.TransactionBody.FromString(transaction.bodyBytes)

assert transaction_body.tokenCreation.adminKey.ed25519 == admin_key[1]
assert transaction.body.tokenCreation.adminKey.ed25519 == b'admin_public_key'

0 comments on commit a455c4b

Please sign in to comment.