|
2 | 2 | import pytest |
3 | 3 | from hiero_sdk_python.account.account_create_transaction import AccountCreateTransaction |
4 | 4 | from hiero_sdk_python.crypto.private_key import PrivateKey |
| 5 | +from hiero_sdk_python.crypto.public_key import PublicKey |
5 | 6 | from hiero_sdk_python.file.file_id import FileId |
6 | 7 | from hiero_sdk_python.query.account_info_query import AccountInfoQuery |
7 | 8 | from hiero_sdk_python.query.transaction_get_receipt_query import TransactionGetReceiptQuery |
@@ -352,3 +353,154 @@ def test_batch_transaction_with_inner_schedule_transaction(env): |
352 | 353 |
|
353 | 354 | batch_receipt = batch_tx.execute(env.client) |
354 | 355 | assert batch_receipt.status == ResponseCode.BATCH_TRANSACTION_IN_BLACKLIST |
| 356 | + |
| 357 | + |
| 358 | +def test_batch_transaction_with_public_key_as_batch_key(env): |
| 359 | + """Test batch transaction can use PublicKey as batch_key.""" |
| 360 | + # Generate a key pair - we'll use the PublicKey as batch_key |
| 361 | + batch_private_key = PrivateKey.generate() |
| 362 | + batch_public_key = batch_private_key.public_key() |
| 363 | + |
| 364 | + receiver_id = create_account_tx(PrivateKey.generate().public_key(), env.client) |
| 365 | + |
| 366 | + # Use PublicKey in batchify |
| 367 | + transfer_tx = ( |
| 368 | + TransferTransaction() |
| 369 | + .add_hbar_transfer(account_id=env.operator_id, amount=-1) |
| 370 | + .add_hbar_transfer(account_id=receiver_id, amount=1) |
| 371 | + .batchify(env.client, batch_public_key) # Using PublicKey! |
| 372 | + ) |
| 373 | + |
| 374 | + # Verify batch_key was set to PublicKey |
| 375 | + assert isinstance(transfer_tx.batch_key, PublicKey) |
| 376 | + assert transfer_tx.batch_key == batch_public_key |
| 377 | + |
| 378 | + # Sign and execute with PrivateKey |
| 379 | + batch_tx = ( |
| 380 | + BatchTransaction() |
| 381 | + .add_inner_transaction(transfer_tx) |
| 382 | + .freeze_with(env.client) |
| 383 | + .sign(batch_private_key) # Sign with corresponding PrivateKey |
| 384 | + ) |
| 385 | + |
| 386 | + batch_receipt = batch_tx.execute(env.client) |
| 387 | + assert batch_receipt.status == ResponseCode.SUCCESS |
| 388 | + |
| 389 | + # Inner Transaction Receipt |
| 390 | + transfer_tx_id = batch_tx.get_inner_transaction_ids()[0] |
| 391 | + transfer_tx_receipt = ( |
| 392 | + TransactionGetReceiptQuery() |
| 393 | + .set_transaction_id(transfer_tx_id) |
| 394 | + .execute(env.client) |
| 395 | + ) |
| 396 | + assert transfer_tx_receipt.status == ResponseCode.SUCCESS |
| 397 | + |
| 398 | + |
| 399 | +def test_batch_transaction_with_mixed_public_and_private_keys(env): |
| 400 | + """Test batch transaction can handle inner transactions with mixed PrivateKey and PublicKey.""" |
| 401 | + # Generate different keys |
| 402 | + batch_key1_private = PrivateKey.generate() |
| 403 | + batch_key2_private = PrivateKey.generate() |
| 404 | + batch_key2_public = batch_key2_private.public_key() |
| 405 | + batch_key3_private = PrivateKey.generate() |
| 406 | + batch_key3_public = batch_key3_private.public_key() |
| 407 | + |
| 408 | + # Create receivers |
| 409 | + receiver_id1 = create_account_tx(PrivateKey.generate().public_key(), env.client) |
| 410 | + receiver_id2 = create_account_tx(PrivateKey.generate().public_key(), env.client) |
| 411 | + receiver_id3 = create_account_tx(PrivateKey.generate().public_key(), env.client) |
| 412 | + |
| 413 | + # First inner transaction uses PrivateKey |
| 414 | + transfer_tx1 = ( |
| 415 | + TransferTransaction() |
| 416 | + .add_hbar_transfer(account_id=env.operator_id, amount=-1) |
| 417 | + .add_hbar_transfer(account_id=receiver_id1, amount=1) |
| 418 | + .batchify(env.client, batch_key1_private) # PrivateKey |
| 419 | + ) |
| 420 | + |
| 421 | + # Second inner transaction uses PublicKey |
| 422 | + transfer_tx2 = ( |
| 423 | + TransferTransaction() |
| 424 | + .add_hbar_transfer(account_id=env.operator_id, amount=-1) |
| 425 | + .add_hbar_transfer(account_id=receiver_id2, amount=1) |
| 426 | + .batchify(env.client, batch_key2_public) # PublicKey |
| 427 | + ) |
| 428 | + |
| 429 | + # Third inner transaction uses PublicKey |
| 430 | + transfer_tx3 = ( |
| 431 | + TransferTransaction() |
| 432 | + .add_hbar_transfer(account_id=env.operator_id, amount=-1) |
| 433 | + .add_hbar_transfer(account_id=receiver_id3, amount=1) |
| 434 | + .batchify(env.client, batch_key3_public) # PublicKey |
| 435 | + ) |
| 436 | + |
| 437 | + # Verify key types |
| 438 | + assert isinstance(transfer_tx1.batch_key, PrivateKey) |
| 439 | + assert isinstance(transfer_tx2.batch_key, PublicKey) |
| 440 | + assert isinstance(transfer_tx3.batch_key, PublicKey) |
| 441 | + |
| 442 | + # Assemble and sign batch transaction |
| 443 | + batch_tx = ( |
| 444 | + BatchTransaction() |
| 445 | + .add_inner_transaction(transfer_tx1) |
| 446 | + .add_inner_transaction(transfer_tx2) |
| 447 | + .add_inner_transaction(transfer_tx3) |
| 448 | + .freeze_with(env.client) |
| 449 | + .sign(batch_key1_private) |
| 450 | + .sign(batch_key2_private) # Sign with PrivateKey for PublicKey batch_key |
| 451 | + .sign(batch_key3_private) # Sign with PrivateKey for PublicKey batch_key |
| 452 | + ) |
| 453 | + |
| 454 | + batch_receipt = batch_tx.execute(env.client) |
| 455 | + assert batch_receipt.status == ResponseCode.SUCCESS |
| 456 | + |
| 457 | + # Verify all inner transactions succeeded |
| 458 | + for transfer_tx_id in batch_tx.get_inner_transaction_ids(): |
| 459 | + transfer_tx_receipt = ( |
| 460 | + TransactionGetReceiptQuery() |
| 461 | + .set_transaction_id(transfer_tx_id) |
| 462 | + .execute(env.client) |
| 463 | + ) |
| 464 | + assert transfer_tx_receipt.status == ResponseCode.SUCCESS |
| 465 | + |
| 466 | + |
| 467 | +def test_batch_transaction_set_batch_key_with_public_key(env): |
| 468 | + """Test batch transaction inner transaction can use set_batch_key with PublicKey.""" |
| 469 | + # Generate a key pair |
| 470 | + batch_private_key = PrivateKey.generate() |
| 471 | + batch_public_key = batch_private_key.public_key() |
| 472 | + |
| 473 | + receiver_id = create_account_tx(PrivateKey.generate().public_key(), env.client) |
| 474 | + |
| 475 | + # Use set_batch_key with PublicKey instead of batchify |
| 476 | + transfer_tx = ( |
| 477 | + TransferTransaction() |
| 478 | + .add_hbar_transfer(account_id=env.operator_id, amount=-1) |
| 479 | + .add_hbar_transfer(account_id=receiver_id, amount=1) |
| 480 | + .set_batch_key(batch_public_key) # Using set_batch_key with PublicKey |
| 481 | + .freeze_with(env.client) |
| 482 | + .sign(env.operator_key) # Sign inner transaction with operator key |
| 483 | + ) |
| 484 | + |
| 485 | + # Verify batch_key was set correctly |
| 486 | + assert transfer_tx.batch_key == batch_public_key |
| 487 | + assert isinstance(transfer_tx.batch_key, PublicKey) |
| 488 | + |
| 489 | + batch_tx = ( |
| 490 | + BatchTransaction() |
| 491 | + .add_inner_transaction(transfer_tx) |
| 492 | + .freeze_with(env.client) |
| 493 | + .sign(batch_private_key) # Sign batch transaction with batch key |
| 494 | + ) |
| 495 | + |
| 496 | + batch_receipt = batch_tx.execute(env.client) |
| 497 | + assert batch_receipt.status == ResponseCode.SUCCESS |
| 498 | + |
| 499 | + # Inner Transaction Receipt |
| 500 | + transfer_tx_id = batch_tx.get_inner_transaction_ids()[0] |
| 501 | + transfer_tx_receipt = ( |
| 502 | + TransactionGetReceiptQuery() |
| 503 | + .set_transaction_id(transfer_tx_id) |
| 504 | + .execute(env.client) |
| 505 | + ) |
| 506 | + assert transfer_tx_receipt.status == ResponseCode.SUCCESS |
0 commit comments