-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,9 @@ metrics/prometheus/prometheus.yml | |
sn_node_manager/.vagrant | ||
.venv/ | ||
uv.lock | ||
*.so | ||
*.pyc | ||
|
||
*.pyc | ||
*.swp | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from autonomi_client import Client, Wallet, PaymentOption | ||
import sys | ||
|
||
def init_wallet(private_key: str) -> Wallet: | ||
try: | ||
wallet = Wallet(private_key) | ||
print(f"Initialized wallet with address: {wallet.address()}") | ||
|
||
balance = wallet.balance() | ||
print(f"Wallet balance: {balance}") | ||
|
||
return wallet | ||
except Exception as e: | ||
print(f"Failed to initialize wallet: {e}") | ||
sys.exit(1) | ||
|
||
def connect_to_network(peers: list[str]) -> Client: | ||
try: | ||
client = Client.connect(peers) | ||
print("Successfully connected to network") | ||
return client | ||
except Exception as e: | ||
print(f"Failed to connect to network: {e}") | ||
sys.exit(1) | ||
|
||
def upload_data(client: Client, data: bytes, payment: PaymentOption) -> str: | ||
try: | ||
addr = client.data_put(data, payment) | ||
print(f"Successfully uploaded data to: {addr}") | ||
return addr | ||
except Exception as e: | ||
print(f"Failed to upload data: {e}") | ||
sys.exit(1) | ||
|
||
def download_data(client: Client, addr: str) -> bytes: | ||
try: | ||
data = client.data_get(addr) | ||
print(f"Successfully downloaded {len(data)} bytes") | ||
return data | ||
except Exception as e: | ||
print(f"Failed to download data: {e}") | ||
sys.exit(1) | ||
|
||
def main(): | ||
# Configuration | ||
private_key = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" | ||
Check failure Code scanning / devskim A token or key was found in source code. If this represents a secret, it should be moved somewhere else. Error
Do not store tokens or keys in source code.
|
||
peers = ["/ip4/127.0.0.1/tcp/12000"] | ||
Check notice Code scanning / devskim Accessing localhost could indicate debug code, or could hinder scaling. Note
Do not leave debug code in production
|
||
|
||
# Initialize | ||
wallet = init_wallet(private_key) | ||
client = connect_to_network(peers) | ||
payment = PaymentOption.wallet(wallet) | ||
|
||
# Upload test data | ||
test_data = b"Hello, Safe Network!" | ||
addr = upload_data(client, test_data, payment) | ||
|
||
# Download and verify | ||
downloaded = download_data(client, addr) | ||
assert downloaded == test_data, "Data verification failed!" | ||
print("Data verification successful!") | ||
|
||
# Example file handling | ||
try: | ||
with open("example.txt", "rb") as f: | ||
file_data = f.read() | ||
file_addr = upload_data(client, file_data, payment) | ||
|
||
# Download and save to new file | ||
downloaded = download_data(client, file_addr) | ||
with open("example_downloaded.txt", "wb") as f_out: | ||
f_out.write(downloaded) | ||
print("File operations completed successfully!") | ||
except IOError as e: | ||
print(f"File operation failed: {e}") | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from autonomi_client import Client, Wallet, PaymentOption, RegisterSecretKey | ||
import hashlib | ||
|
||
def handle_data_operations(client: Client, payment: PaymentOption): | ||
"""Example of various data operations""" | ||
print("\n=== Data Operations ===") | ||
|
||
# Upload some text data | ||
text_data = b"Hello, Safe Network!" | ||
text_addr = client.data_put(text_data, payment) | ||
print(f"Text data uploaded to: {text_addr}") | ||
|
||
# Upload binary data (like an image) | ||
with open("example.jpg", "rb") as f: | ||
image_data = f.read() | ||
image_addr = client.data_put(image_data, payment) | ||
print(f"Image uploaded to: {image_addr}") | ||
|
||
# Download and verify data | ||
downloaded_text = client.data_get(text_addr) | ||
assert downloaded_text == text_data, "Text data verification failed!" | ||
print("Text data verified successfully") | ||
|
||
# Download and save image | ||
downloaded_image = client.data_get(image_addr) | ||
with open("downloaded_example.jpg", "wb") as f: | ||
f.write(downloaded_image) | ||
print("Image downloaded successfully") | ||
|
||
def handle_register_operations(client: Client, wallet: Wallet): | ||
"""Example of register operations""" | ||
print("\n=== Register Operations ===") | ||
|
||
# Create a register key | ||
register_key = client.register_generate_key() | ||
print(f"Generated register key") | ||
|
||
# Create a register with initial value | ||
register_name = "my_first_register" | ||
initial_value = b"Initial register value" | ||
register = client.register_create( | ||
initial_value, | ||
register_name, | ||
register_key, | ||
wallet | ||
) | ||
print(f"Created register at: {register.address()}") | ||
|
||
# Read current value | ||
values = register.values() | ||
print(f"Current register values: {[v.decode() for v in values]}") | ||
|
||
# Update register value | ||
new_value = b"Updated register value" | ||
client.register_update(register, new_value, register_key) | ||
print("Register updated") | ||
|
||
# Read updated value | ||
updated_register = client.register_get(register.address()) | ||
updated_values = updated_register.values() | ||
print(f"Updated register values: {[v.decode() for v in updated_values]}") | ||
|
||
def main(): | ||
# Initialize wallet and client | ||
private_key = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" | ||
Check failure Code scanning / devskim A token or key was found in source code. If this represents a secret, it should be moved somewhere else. Error
Do not store tokens or keys in source code.
|
||
peers = ["/ip4/127.0.0.1/tcp/12000"] | ||
Check notice Code scanning / devskim Accessing localhost could indicate debug code, or could hinder scaling. Note
Do not leave debug code in production
|
||
|
||
try: | ||
# Setup | ||
wallet = Wallet(private_key) | ||
print(f"Wallet address: {wallet.address()}") | ||
print(f"Wallet balance: {wallet.balance()}") | ||
|
||
client = Client.connect(peers) | ||
payment = PaymentOption.wallet(wallet) | ||
|
||
# Run examples | ||
handle_data_operations(client, payment) | ||
handle_register_operations(client, wallet) | ||
|
||
except Exception as e: | ||
print(f"Error: {e}") | ||
return 1 | ||
|
||
print("\nAll operations completed successfully!") | ||
return 0 | ||
|
||
if __name__ == "__main__": | ||
exit(main()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from autonomi_client import Client, Wallet, PaymentOption | ||
|
||
def main(): | ||
# Initialize a wallet with a private key | ||
# This should be a valid Ethereum private key (64 hex chars without '0x' prefix) | ||
private_key = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" | ||
Check failure Code scanning / devskim A token or key was found in source code. If this represents a secret, it should be moved somewhere else. Error
Do not store tokens or keys in source code.
|
||
wallet = Wallet(private_key) | ||
print(f"Wallet address: {wallet.address()}") | ||
print(f"Wallet balance: {wallet.balance()}") | ||
|
||
# Connect to the network | ||
# These should be valid multiaddresses of network nodes | ||
peers = [ | ||
"/ip4/127.0.0.1/tcp/12000", | ||
Check notice Code scanning / devskim Accessing localhost could indicate debug code, or could hinder scaling. Note
Do not leave debug code in production
|
||
"/ip4/127.0.0.1/tcp/12001" | ||
Check notice Code scanning / devskim Accessing localhost could indicate debug code, or could hinder scaling. Note
Do not leave debug code in production
|
||
] | ||
client = Client.connect(peers) | ||
|
||
# Create payment option using the wallet | ||
payment = PaymentOption.wallet(wallet) | ||
|
||
# Upload some data | ||
data = b"Hello, Safe Network!" | ||
addr = client.data_put(data, payment) | ||
print(f"Data uploaded to address: {addr}") | ||
|
||
# Download the data back | ||
downloaded = client.data_get(addr) | ||
print(f"Downloaded data: {downloaded.decode()}") | ||
|
||
# You can also upload files | ||
with open("example.txt", "rb") as f: | ||
file_data = f.read() | ||
file_addr = client.data_put(file_data, payment) | ||
print(f"File uploaded to address: {file_addr}") | ||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from autonomi_client import Client, Wallet, PaymentOption, RegisterSecretKey, RegisterPermissions | ||
from typing import List, Optional | ||
import json | ||
|
||
class DataManager: | ||
def __init__(self, client: Client, wallet: Wallet): | ||
self.client = client | ||
self.wallet = wallet | ||
self.payment = PaymentOption.wallet(wallet) | ||
|
||
def store_private_data(self, data: bytes) -> str: | ||
"""Store data privately and return its address""" | ||
addr = self.client.private_data_put(data, self.payment) | ||
return addr | ||
|
||
def retrieve_private_data(self, addr: str) -> bytes: | ||
"""Retrieve privately stored data""" | ||
return self.client.private_data_get(addr) | ||
|
||
def create_shared_register(self, name: str, initial_value: bytes, | ||
allowed_writers: List[str]) -> str: | ||
"""Create a register that multiple users can write to""" | ||
register_key = self.client.register_generate_key() | ||
|
||
# Create permissions for all writers | ||
permissions = RegisterPermissions.new_with(allowed_writers) | ||
|
||
register = self.client.register_create_with_permissions( | ||
initial_value, | ||
name, | ||
register_key, | ||
permissions, | ||
self.wallet | ||
) | ||
|
||
return register.address() | ||
|
||
def main(): | ||
# Initialize | ||
private_key = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" | ||
Check failure Code scanning / devskim A token or key was found in source code. If this represents a secret, it should be moved somewhere else. Error
Do not store tokens or keys in source code.
|
||
peers = ["/ip4/127.0.0.1/tcp/12000"] | ||
Check notice Code scanning / devskim Accessing localhost could indicate debug code, or could hinder scaling. Note
Do not leave debug code in production
|
||
|
||
try: | ||
wallet = Wallet(private_key) | ||
client = Client.connect(peers) | ||
manager = DataManager(client, wallet) | ||
|
||
# Store private data | ||
user_data = { | ||
"username": "alice", | ||
"preferences": { | ||
"theme": "dark", | ||
"notifications": True | ||
} | ||
} | ||
private_data = json.dumps(user_data).encode() | ||
private_addr = manager.store_private_data(private_data) | ||
print(f"Stored private data at: {private_addr}") | ||
|
||
# Retrieve and verify private data | ||
retrieved_data = manager.retrieve_private_data(private_addr) | ||
retrieved_json = json.loads(retrieved_data.decode()) | ||
print(f"Retrieved data: {retrieved_json}") | ||
|
||
# Create shared register | ||
allowed_writers = [ | ||
wallet.address(), # self | ||
"0x1234567890abcdef1234567890abcdef12345678" # another user | ||
] | ||
register_addr = manager.create_shared_register( | ||
"shared_config", | ||
b"initial shared data", | ||
allowed_writers | ||
) | ||
print(f"Created shared register at: {register_addr}") | ||
|
||
# Verify register | ||
register = client.register_get(register_addr) | ||
values = register.values() | ||
print(f"Register values: {[v.decode() for v in values]}") | ||
|
||
except Exception as e: | ||
print(f"Error: {e}") | ||
return 1 | ||
|
||
print("All operations completed successfully!") | ||
return 0 | ||
|
||
if __name__ == "__main__": | ||
exit(main()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from autonomi_client import ( | ||
Client, Wallet, PaymentOption, PrivateDataAccess, | ||
encrypt, hash_to_short_string | ||
) | ||
import json | ||
|
||
def demonstrate_private_data(client: Client, payment: PaymentOption): | ||
"""Show private data handling""" | ||
print("\n=== Private Data Operations ===") | ||
|
||
# Create some private data | ||
secret_data = { | ||
"password": "very_secret", | ||
"api_key": "super_secret_key" | ||
} | ||
data_bytes = json.dumps(secret_data).encode() | ||
|
||
# Store it privately | ||
access = client.private_data_put(data_bytes, payment) | ||
print(f"Stored private data, access token: {access.to_hex()}") | ||
print(f"Short reference: {access.address()}") | ||
|
||
# Retrieve it | ||
retrieved_bytes = client.private_data_get(access) | ||
retrieved_data = json.loads(retrieved_bytes.decode()) | ||
print(f"Retrieved private data: {retrieved_data}") | ||
|
||
return access.to_hex() | ||
|
||
def demonstrate_encryption(): | ||
"""Show self-encryption functionality""" | ||
print("\n=== Self-Encryption Operations ===") | ||
|
||
# Create test data | ||
test_data = b"This is some test data for encryption" | ||
|
||
# Encrypt it | ||
data_map, chunks = encrypt(test_data) | ||
print(f"Original data size: {len(test_data)} bytes") | ||
print(f"Data map size: {len(data_map)} bytes") | ||
print(f"Number of chunks: {len(chunks)}") | ||
print(f"Total chunks size: {sum(len(c) for c in chunks)} bytes") | ||
|
||
def main(): | ||
# Initialize | ||
private_key = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" | ||
Check failure Code scanning / devskim A token or key was found in source code. If this represents a secret, it should be moved somewhere else. Error
Do not store tokens or keys in source code.
|
||
peers = ["/ip4/127.0.0.1/tcp/12000"] | ||
Check notice Code scanning / devskim Accessing localhost could indicate debug code, or could hinder scaling. Note
Do not leave debug code in production
|
||
|
||
try: | ||
# Setup | ||
wallet = Wallet(private_key) | ||
print(f"Wallet address: {wallet.address()}") | ||
print(f"Wallet balance: {wallet.balance()}") | ||
|
||
client = Client.connect(peers) | ||
payment = PaymentOption.wallet(wallet) | ||
|
||
# Run demonstrations | ||
access_token = demonstrate_private_data(client, payment) | ||
demonstrate_encryption() | ||
|
||
# Show utility function | ||
print("\n=== Utility Functions ===") | ||
short_hash = hash_to_short_string(access_token) | ||
print(f"Short hash of access token: {short_hash}") | ||
|
||
except Exception as e: | ||
print(f"Error: {e}") | ||
return 1 | ||
|
||
print("\nAll operations completed successfully!") | ||
return 0 | ||
|
||
if __name__ == "__main__": | ||
exit(main()) |