-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathencrypt_message.py
More file actions
71 lines (57 loc) · 2.82 KB
/
encrypt_message.py
File metadata and controls
71 lines (57 loc) · 2.82 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
from ton_core import Address, NetworkGlobalID, TextCipher, to_nano
from tonutils.clients import ToncenterClient
from tonutils.contracts import WalletV4R2, get_public_key_get_method
# Mnemonic phrase — 24 words (TON-native) or 12/18/24 words (BIP-39 import)
# Used to derive the wallet's private key
MNEMONIC = "word1 word2 word3 ..."
# Destination address (recipient)
# Recipient must have deployed wallet contract with public key accessible via get_public_key
DESTINATION_ADDRESS = Address("UQ...")
async def main() -> None:
# Initialize HTTP client for TON blockchain interaction
# NetworkGlobalID.MAINNET (-239) for production
# NetworkGlobalID.TESTNET (-3) for testing
client = ToncenterClient(network=NetworkGlobalID.MAINNET)
await client.connect()
# Create wallet instance from mnemonic (full access mode)
# Returns: (wallet, public_key, private_key, mnemonic)
# Keep private_key for encryption as our_private_key
wallet, _, our_private_key, _ = WalletV4R2.from_mnemonic(client, MNEMONIC)
# Retrieve recipient's public key from blockchain
# Calls get_public_key() get-method on recipient's wallet contract
# Recipient wallet must be deployed (active state) to have accessible public key
# Raises error if wallet is uninit or doesn't support get_public_key method
their_public_key = await get_public_key_get_method(
client=client,
address=DESTINATION_ADDRESS,
)
# Encrypt message using end-to-end encryption
# Uses elliptic curve Diffie-Hellman (ECDH) for shared secret
# Only sender (with our_private_key) and recipient (with their_private_key) can decrypt
# payload: plaintext message to encrypt
# sender_address: included in encrypted payload for recipient verification
# our_private_key: sender's private key for ECDH
# their_public_key: recipient's public key for ECDH
body = TextCipher.encrypt(
payload="Hello from tonutils!",
sender_address=wallet.address,
our_private_key=our_private_key,
their_public_key=their_public_key,
)
# Send TON with encrypted message
# destination: recipient address
# amount: in nanotons (1 TON = 1,000,000,000 nanotons)
# body: encrypted message (only recipient can decrypt with their private key)
msg = await wallet.transfer(
destination=DESTINATION_ADDRESS,
amount=to_nano(0.01), # Convert 0.01 TON to nanotons (10,000,000)
body=body,
)
# Normalized hash of the signed external message (computed locally before sending)
# Not a blockchain transaction hash — use it to track whether the message
# was accepted on-chain (e.g. via explorers, API queries, or your own checks)
print(f"Transaction hash: {msg.normalized_hash}")
await client.close()
if __name__ == "__main__":
import asyncio
asyncio.run(main())