|
| 1 | +""" |
| 2 | +This module provides functionality for TimeLock Encryption (TLE), a mechanism that encrypts data such that it can |
| 3 | +only be decrypted after a specific amount of time (expressed in the form of Drand rounds). It includes functions |
| 4 | +for encryption, decryption, and handling the decryption process by waiting for the reveal round. The logic is based on |
| 5 | +Drand QuickNet. |
| 6 | +
|
| 7 | +Main Functions: |
| 8 | + - encrypt: Encrypts data and returns the encrypted data along with the reveal round. |
| 9 | + - decrypt: Decrypts the provided encrypted data when the reveal round is reached. |
| 10 | + - wait_reveal_and_decrypt: Waits for the reveal round and decrypts the encrypted data. |
| 11 | +
|
| 12 | +Usage Example: |
| 13 | + ```python |
| 14 | + from bittensor import timelock |
| 15 | + data = "From Cortex to Bittensor" |
| 16 | + encrypted_data, reveal_round = timelock.encrypt(data, n_blocks=5) |
| 17 | + decrypted_data = timelock.wait_reveal_and_decrypt(encrypted_data) |
| 18 | + ``` |
| 19 | +
|
| 20 | +Usage Example with custom data: |
| 21 | + ```python |
| 22 | + import pickle |
| 23 | + from dataclasses import dataclass |
| 24 | +
|
| 25 | + from bittensor import timelock |
| 26 | +
|
| 27 | +
|
| 28 | + @dataclass |
| 29 | + class Person: |
| 30 | + name: str |
| 31 | + age: int |
| 32 | +
|
| 33 | + # get instance of your data |
| 34 | + x_person = Person("X Lynch", 123) |
| 35 | +
|
| 36 | + # get bytes of your instance |
| 37 | + byte_data = pickle.dumps(x_person) |
| 38 | +
|
| 39 | + # get TLE encoded bytes |
| 40 | + encrypted, reveal_round = timelock.encrypt(byte_data, 1) |
| 41 | +
|
| 42 | + # wait when reveal round appears in Drand QuickNet and get decrypted data |
| 43 | + decrypted = timelock.wait_reveal_and_decrypt(encrypted_data=encrypted) |
| 44 | +
|
| 45 | + # convert bytes into your instance back |
| 46 | + x_person_2 = pickle.loads(decrypted) |
| 47 | +
|
| 48 | + # make sure initial and decoded instances are the same |
| 49 | + assert x_person == x_person_2 |
| 50 | + ``` |
| 51 | +
|
| 52 | +Note: |
| 53 | +For handling fast-block nodes, set the `block_time` parameter to 0.25 seconds during encryption. |
| 54 | +""" |
| 55 | + |
1 | 56 | import struct
|
2 | 57 | import time
|
3 | 58 | from typing import Optional, Union
|
|
0 commit comments