forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprng_transaction.py
More file actions
107 lines (73 loc) · 3.68 KB
/
Copy pathprng_transaction.py
File metadata and controls
107 lines (73 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
Example demonstrating PRNG (Pseudo-Random Number Generator) transaction functionality.
The PRNG transaction is a transaction that generates a pseudo-random number.
You can set the range for the PRNG transaction to generate a pseudo-random number within a range.
If no range is set, the PRNG transaction will generate a 48 byte unsigned pseudo-random number.
"""
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python import Client
from hiero_sdk_python.prng_transaction import PrngTransaction
from hiero_sdk_python.query.transaction_record_query import TransactionRecordQuery
from hiero_sdk_python.response_code import ResponseCode
load_dotenv()
network_name = os.getenv("NETWORK", "testnet").lower()
def setup_client() -> Client:
"""Setup Client."""
client = Client.from_env()
print(f"Network: {client.network.network}")
print(f"Client set up with operator id {client.operator_account_id}")
return client
def prng_with_range(client, range_value):
"""Generate a pseudo-random number within a specified range."""
receipt = PrngTransaction().set_range(range_value).execute(client)
if receipt.status != ResponseCode.SUCCESS:
print(f"PRNG transaction failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
# Get the transaction record to see the generated number
record = TransactionRecordQuery(receipt.transaction_id).execute(client)
print(f"Generated PRNG number: {record.prng_number}")
print(f"Number is within range: 0 to {range_value}")
def prng_without_range(client):
"""Generate pseudo-random bytes without specifying a range."""
receipt = PrngTransaction().execute(client)
if receipt.status != ResponseCode.SUCCESS:
print(f"PRNG transaction failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
# Get the transaction record to see the generated bytes
record = TransactionRecordQuery(receipt.transaction_id).execute(client)
print(f"Generated PRNG bytes length: {len(record.prng_bytes)} bytes")
print(f"PRNG number: {record.prng_number}")
def prng_transaction():
"""
Demonstrates PRNG transaction functionality by:
1. Setting up client with operator account
2. Generating a random number within a range
3. Generating random bytes without a range.
"""
client = setup_client()
# Generate a random number within a range (from 0 to 1000)
range_value = 1000
receipt = PrngTransaction().set_range(range_value).execute(client)
if receipt.status != ResponseCode.SUCCESS:
print(f"PRNG transaction failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
# Get the transaction record to see the generated number
record = TransactionRecordQuery(receipt.transaction_id).execute(client)
print(f"Generated PRNG number from 0 to {range_value}: {record.prng_number}")
# Generate random number (48 byte unsigned pseudo-random number) without range
receipt = PrngTransaction().execute(client)
# Alternative way of doing it:
# receipt = PrngTransaction().set_range(0).execute(client)
# receipt = PrngTransaction().set_range(None).execute(client)
if receipt.status != ResponseCode.SUCCESS:
print(f"PRNG transaction failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
# Get the transaction record to see the generated number in bytes
record = TransactionRecordQuery(receipt.transaction_id).execute(client)
print("\nGenerated PRNG number without setting range:")
print(f"PRNG number in hex: {record.prng_bytes.hex()}")
print(f"PRNG number length: {len(record.prng_bytes)} bytes")
if __name__ == "__main__":
prng_transaction()