Autonomi client API
Project description
autonomi - Autonomi client API
Connect to and build on the Autonomi network.
Usage
Add the autonomi crate to your Cargo.toml:
[dependencies]
autonomi = { path = "../autonomi", version = "0.1.0" }
Running tests
Using a local EVM testnet
- If you haven't, install Foundry, to be able to run Anvil nodes: https://book.getfoundry.sh/getting-started/installation
- Run a local EVM node:
cargo run --bin evm_testnet
- Run a local network with the
localfeature and use the local evm node.
cargo run --bin=safenode-manager --features=local -- local run --build --clean --rewards-address <ETHEREUM_ADDRESS> evm-local
- Then run the tests with the
localfeature and pass the EVM params again:
EVM_NETWORK=local cargo test --package=autonomi --features=local
# Or with logs
RUST_LOG=autonomi EVM_NETWORK=local cargo test --package=autonomi --features=local -- --nocapture
Using a live testnet or mainnet
Using the hardcoded Arbitrum One option as an example, but you can also use the command flags of the steps above and
point it to a live network.
- Run a local network with the
localfeature:
cargo run --bin=safenode-manager --features=local -- local run --build --clean --rewards-address <ETHEREUM_ADDRESS> evm-arbitrum-one
- Then run the tests with the
localfeature. Make sure that the wallet of the private key you pass has enough gas and payment tokens on the network (in this case Arbitrum One):
EVM_NETWORK=arbitrum-one EVM_PRIVATE_KEY=<PRIVATE_KEY> cargo test --package=autonomi --features=local
# Or with logs
RUST_LOG=autonomi EVM_NETWORK=arbitrum-one EVM_PRIVATE_KEY=<PRIVATE_KEY> cargo test --package=autonomi --features=local -- --nocapture
WebAssembly
To run a WASM test
- Install
wasm-pack - Make sure your Rust supports the
wasm32-unknown-unknowntarget. (If you haverustup:rustup target add wasm32-unknown-unknown.) - Pass a bootstrap peer via
SAFE_PEERS. This has to be the websocket address, e.g./ip4/<ip>/tcp/<port>/ws/p2p/<peer ID>.- As well as the other environment variables needed for EVM payments (e.g.
RPC_URL).
- As well as the other environment variables needed for EVM payments (e.g.
- Optionally specify the specific test, e.g.
-- putto runput()inwasm.rsonly.
Example:
SAFE_PEERS=/ip4/<ip>/tcp/<port>/ws/p2p/<peer ID> wasm-pack test --release --firefox autonomi --features=data,files --test wasm -- put
Test from JS in the browser
wasm-pack test does not execute JavaScript, but runs mostly WebAssembly. Again make sure the environment variables are
set and build the JS package:
wasm-pack build --dev --target=web autonomi --features=vault
Then cd into autonomi/tests-js, and use npm to install and serve the test html file.
cd autonomi/tests-js
npm install
npm run serve
Then go to http://127.0.0.1:8080/tests-js in the browser. Here, enter a ws multiaddr of a local node and press '
run'.
MetaMask example
There is a MetaMask example for doing a simple put operation.
Build the package with the external-signer feature (and again with the env variables) and run a webserver, e.g. with
Python:
wasm-pack build --dev --target=web autonomi --features=external-signer
python -m http.server --directory=autonomi 8000
Then visit http://127.0.0.1:8000/examples/metamask in your (modern) browser.
Here, enter a ws multiaddr of a local node and press 'run'.
Faucet (local)
There is no faucet server, but instead you can use the Deployer wallet private key printed in the EVM node output to
initialise a wallet from with almost infinite gas and payment tokens. Example:
let rpc_url = "http://localhost:54370/";
let payment_token_address = "0x5FbDB2315678afecb367f032d93F642f64180aa3";
let data_payments_address = "0x8464135c8F25Da09e49BC8782676a84730C318bC";
let private_key = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
let network = Network::Custom(CustomNetwork::new(
rpc_url,
payment_token_address,
data_payments_address,
));
let deployer_wallet = Wallet::new_from_private_key(network, private_key).unwrap();
let receiving_wallet = Wallet::new_with_random_wallet(network);
// Send 10 payment tokens (atto)
let _ = deployer_wallet
.transfer_tokens(receiving_wallet.address(), Amount::from(10))
.await;
Alternatively, you can provide the wallet address that should own all the gas and payment tokens to the EVM testnet
startup command using the --genesis-wallet flag:
cargo run --bin evm_testnet -- --genesis-wallet <ETHEREUM_ADDRESS>
*************************
* Ethereum node started *
*************************
RPC URL: http://localhost:60093/
Payment token address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Chunk payments address: 0x8464135c8F25Da09e49BC8782676a84730C318bC
Deployer wallet private key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
Genesis wallet balance: (tokens: 20000000000000000000000000, gas: 9998998011366954730202)
Python Bindings
The Autonomi client library provides Python bindings for easy integration with Python applications.
Installation
pip install autonomi-client
Quick Start
from autonomi_client import Client, Wallet, PaymentOption
# Initialize wallet with private key
wallet = Wallet("your_private_key_here")
print(f"Wallet address: {wallet.address()}")
print(f"Balance: {wallet.balance()}")
# Connect to network
client = Client.connect(["/ip4/127.0.0.1/tcp/12000"])
# Create payment option
payment = PaymentOption.wallet(wallet)
# Upload data
data = b"Hello, Safe Network!"
addr = client.data_put(data, payment)
print(f"Data uploaded to: {addr}")
# Download data
retrieved = client.data_get(addr)
print(f"Retrieved: {retrieved.decode()}")
Available Modules
Core Components
-
Client: Main interface to the Autonomi networkconnect(peers: List[str]): Connect to network nodesdata_put(data: bytes, payment: PaymentOption): Upload datadata_get(addr: str): Download dataprivate_data_put(data: bytes, payment: PaymentOption): Store private dataprivate_data_get(access: PrivateDataAccess): Retrieve private dataregister_generate_key(): Generate register key
-
Wallet: Ethereum wallet managementnew(private_key: str): Create wallet from private keyaddress(): Get wallet addressbalance(): Get current balance
-
PaymentOption: Payment configurationwallet(wallet: Wallet): Create payment option from wallet
Private Data
PrivateDataAccess: Handle private data storagefrom_hex(hex: str): Create from hex stringto_hex(): Convert to hex stringaddress(): Get short reference address
# Private data example
access = client.private_data_put(secret_data, payment)
print(f"Private data stored at: {access.to_hex()}")
retrieved = client.private_data_get(access)
Registers
- Register operations for mutable data
register_create(value: bytes, name: str, key: RegisterSecretKey, wallet: Wallet)register_get(address: str)register_update(register: Register, value: bytes, key: RegisterSecretKey)
# Register example
key = client.register_generate_key()
register = client.register_create(b"Initial value", "my_register", key, wallet)
client.register_update(register, b"New value", key)
Vaults
-
VaultSecretKey: Manage vault accessnew(): Generate new keyfrom_hex(hex: str): Create from hex stringto_hex(): Convert to hex string
-
UserData: User data managementnew(): Create new user dataadd_file_archive(archive: str): Add file archiveadd_private_file_archive(archive: str): Add private archivefile_archives(): List archivesprivate_file_archives(): List private archives
# Vault example
vault_key = VaultSecretKey.new()
cost = client.vault_cost(vault_key)
client.write_bytes_to_vault(data, payment, vault_key, content_type=1)
data, content_type = client.fetch_and_decrypt_vault(vault_key)
Utility Functions
encrypt(data: bytes): Self-encrypt datahash_to_short_string(input: str): Generate short reference
Complete Examples
Data Management
def handle_data_operations(client, payment):
# Upload text
text_data = b"Hello, Safe Network!"
text_addr = client.data_put(text_data, payment)
# Upload binary data
with open("image.jpg", "rb") as f:
image_data = f.read()
image_addr = client.data_put(image_data, payment)
# Download and verify
downloaded = client.data_get(text_addr)
assert downloaded == text_data
Private Data and Encryption
def handle_private_data(client, payment):
# Create and encrypt private data
secret = {"api_key": "secret_key"}
data = json.dumps(secret).encode()
# Store privately
access = client.private_data_put(data, payment)
print(f"Access token: {access.to_hex()}")
# Retrieve
retrieved = client.private_data_get(access)
secret = json.loads(retrieved.decode())
Vault Management
def handle_vault(client, payment):
# Create vault
vault_key = VaultSecretKey.new()
# Store user data
user_data = UserData()
user_data.add_file_archive("archive_address")
# Save to vault
cost = client.put_user_data_to_vault(vault_key, payment, user_data)
# Retrieve
retrieved = client.get_user_data_from_vault(vault_key)
archives = retrieved.file_archives()
Error Handling
All operations can raise exceptions. It's recommended to use try-except blocks:
try:
client = Client.connect(peers)
# ... operations ...
except Exception as e:
print(f"Error: {e}")
Best Practices
- Always keep private keys secure
- Use error handling for all network operations
- Clean up resources when done
- Monitor wallet balance for payments
- Use appropriate content types for vault storage
For more examples, see the examples/ directory in the repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autonomi_client-0.2.3.tar.gz.
File metadata
- Download URL: autonomi_client-0.2.3.tar.gz
- Upload date:
- Size: 411.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f531660486d7b4e24e8227a20e4a3063a0b011bde7479944598394f4d04c1c94
|
|
| MD5 |
70361d3e700def853b1367b2dc52de2c
|
|
| BLAKE2b-256 |
44315555db4292c6fe0840e409d3adc74cbf457b146bde2e516d055ff9494419
|
Provenance
The following attestation bundles were made for autonomi_client-0.2.3.tar.gz:
Publisher:
python-publish-client.yml on dirvine/safe_network
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autonomi_client-0.2.3.tar.gz -
Subject digest:
f531660486d7b4e24e8227a20e4a3063a0b011bde7479944598394f4d04c1c94 - Sigstore transparency entry: 147945491
- Sigstore integration time:
-
Permalink:
dirvine/safe_network@1348cf0fe6c331ac4de4bccc3fa239abc865f34a -
Branch / Tag:
refs/tags/v0.3.5 - Owner: https://github.com/dirvine
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-client.yml@1348cf0fe6c331ac4de4bccc3fa239abc865f34a -
Trigger Event:
push
-
Statement type:
File details
Details for the file autonomi_client-0.2.3-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: autonomi_client-0.2.3-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 6.2 MB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6b296d02b12c6532f4b77a54d5275ea54c45cf26f1ac61fd8834bbf2b9e5dac
|
|
| MD5 |
424ff502b463b2b6d6ca91d646dcc982
|
|
| BLAKE2b-256 |
0b8be442f53d56ab26fb6131f202c39f6886b822cfaf8daae293bf3ad00c412f
|
Provenance
The following attestation bundles were made for autonomi_client-0.2.3-cp38-abi3-win_amd64.whl:
Publisher:
python-publish-client.yml on dirvine/safe_network
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autonomi_client-0.2.3-cp38-abi3-win_amd64.whl -
Subject digest:
e6b296d02b12c6532f4b77a54d5275ea54c45cf26f1ac61fd8834bbf2b9e5dac - Sigstore transparency entry: 147945496
- Sigstore integration time:
-
Permalink:
dirvine/safe_network@1348cf0fe6c331ac4de4bccc3fa239abc865f34a -
Branch / Tag:
refs/tags/v0.3.5 - Owner: https://github.com/dirvine
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-client.yml@1348cf0fe6c331ac4de4bccc3fa239abc865f34a -
Trigger Event:
push
-
Statement type:
File details
Details for the file autonomi_client-0.2.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: autonomi_client-0.2.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 67.7 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4f0ab2180617473e1e45de51328b4906dbc45b7970b0f22e482be9a8603b4a6
|
|
| MD5 |
2bb689da2e48297bd884f031d1a04c19
|
|
| BLAKE2b-256 |
724ee052b14a48c6f475b41ce2dfb8f6cea1a63bc2d74072881f2cd74c468579
|
Provenance
The following attestation bundles were made for autonomi_client-0.2.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
python-publish-client.yml on dirvine/safe_network
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autonomi_client-0.2.3-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl -
Subject digest:
b4f0ab2180617473e1e45de51328b4906dbc45b7970b0f22e482be9a8603b4a6 - Sigstore transparency entry: 147945492
- Sigstore integration time:
-
Permalink:
dirvine/safe_network@1348cf0fe6c331ac4de4bccc3fa239abc865f34a -
Branch / Tag:
refs/tags/v0.3.5 - Owner: https://github.com/dirvine
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-client.yml@1348cf0fe6c331ac4de4bccc3fa239abc865f34a -
Trigger Event:
push
-
Statement type:
File details
Details for the file autonomi_client-0.2.3-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: autonomi_client-0.2.3-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 7.0 MB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
081b6990a5ecfc133ab8919277876e1038a956bd3c018390f91914a6ed551a55
|
|
| MD5 |
8b7175e46ae2561d2bb52e83f29ab203
|
|
| BLAKE2b-256 |
ee10e7cbb8f9bc6ed8c23ee6a07a71a3782247613bb056ba7ed4689f71d353da
|
Provenance
The following attestation bundles were made for autonomi_client-0.2.3-cp38-abi3-macosx_11_0_arm64.whl:
Publisher:
python-publish-client.yml on dirvine/safe_network
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autonomi_client-0.2.3-cp38-abi3-macosx_11_0_arm64.whl -
Subject digest:
081b6990a5ecfc133ab8919277876e1038a956bd3c018390f91914a6ed551a55 - Sigstore transparency entry: 147945495
- Sigstore integration time:
-
Permalink:
dirvine/safe_network@1348cf0fe6c331ac4de4bccc3fa239abc865f34a -
Branch / Tag:
refs/tags/v0.3.5 - Owner: https://github.com/dirvine
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-client.yml@1348cf0fe6c331ac4de4bccc3fa239abc865f34a -
Trigger Event:
push
-
Statement type:
File details
Details for the file autonomi_client-0.2.3-cp38-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: autonomi_client-0.2.3-cp38-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 7.2 MB
- Tags: CPython 3.8+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2da2ff890c25f242d094be3b669b2b5db83b56dc8c0cb1b669fcae31dc0e076
|
|
| MD5 |
7fa56fd6f3508e45eded94c7962cfd54
|
|
| BLAKE2b-256 |
1127198fa52e52071e4c34dd386fd364f829b538d9c527cde35f2bc0e7463f22
|
Provenance
The following attestation bundles were made for autonomi_client-0.2.3-cp38-abi3-macosx_10_12_x86_64.whl:
Publisher:
python-publish-client.yml on dirvine/safe_network
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
autonomi_client-0.2.3-cp38-abi3-macosx_10_12_x86_64.whl -
Subject digest:
c2da2ff890c25f242d094be3b669b2b5db83b56dc8c0cb1b669fcae31dc0e076 - Sigstore transparency entry: 147945493
- Sigstore integration time:
-
Permalink:
dirvine/safe_network@1348cf0fe6c331ac4de4bccc3fa239abc865f34a -
Branch / Tag:
refs/tags/v0.3.5 - Owner: https://github.com/dirvine
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-client.yml@1348cf0fe6c331ac4de4bccc3fa239abc865f34a -
Trigger Event:
push
-
Statement type: