Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Autonomi API Registers and Integration in CLI #2208

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions autonomi/WASM_docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
## JavaScript Autonomi API Documentation

Note that this is a first version and will be subject to change.

### **Client**

The `Client` object allows interaction with the network to store and retrieve data. Below are the available methods for the `Client` class.

#### **Constructor**

```javascript
let client = await new Client([multiaddress]);
```

- **multiaddress** (Array of Strings): A list of network addresses for the client to connect to.

Example:
```javascript
let client = await new Client(["/ip4/127.0.0.1/tcp/36075/ws/p2p/12D3KooWALb...BhDAfJY"]);
```

#### **Methods**

##### **put(data, wallet)**

Uploads a piece of encrypted data to the network.

```javascript
let result = await client.put(data, wallet);
```

- **data** (Uint8Array): The data to be stored.
- **wallet** (Wallet): The wallet used to pay for the storage.

Returns:
- **result** (XorName): The XOR address of the stored data.

Example:
```javascript
let wallet = getFundedWallet();
let data = new Uint8Array([1, 2, 3]);
let result = await client.put(data, wallet);
```

##### **get(data_map_addr)**

Fetches encrypted data from the network using its XOR address.

```javascript
let data = await client.get(data_map_addr);
```

- **data_map_addr** (XorName): The XOR address of the data to fetch.

Returns:
- **data** (Uint8Array): The fetched data.

Example:
```javascript
let data = await client.get(result);
```

##### **cost(data)**

Gets the cost of storing the provided data on the network.

```javascript
let cost = await client.cost(data);
```

- **data** (Uint8Array): The data whose storage cost you want to calculate.

Returns:
- **cost** (AttoTokens): The calculated cost for storing the data.

Example:
```javascript
let cost = await client.cost(new Uint8Array([1, 2, 3]));
```

---

### **Wallet**

The `Wallet` object represents an Ethereum wallet used for data payments.

#### **Methods**

##### **new_from_private_key(network, private_key)**

Creates a new wallet using the given private key.

```javascript
let wallet = Wallet.new_from_private_key(network, private_key);
```

- **network** (EvmNetwork): The network to which the wallet connects.
- **private_key** (String): The private key of the wallet.

Returns:
- **wallet** (Wallet): The created wallet.

Example:
```javascript
let wallet = Wallet.new_from_private_key(EvmNetwork.default(), "your_private_key_here");
```

##### **address()**

Gets the wallet’s address.

```javascript
let address = wallet.address();
```

Returns:
- **address** (Address): The wallet's address.

Example:
```javascript
let wallet = Wallet.new_from_private_key(EvmNetwork.default(), "your_private_key_here");
let address = wallet.address();
```

---

### **EvmNetwork**

The `EvmNetwork` object represents the blockchain network.

#### **Methods**

##### **default()**

Connects to the default network.

```javascript
let network = EvmNetwork.default();
```

Returns:
- **network** (EvmNetwork): The default network.

Example:
```javascript
let network = EvmNetwork.default();
```

---

### Example Usage:

```javascript
let client = await new Client(["/ip4/127.0.0.1/tcp/36075/ws/p2p/12D3KooWALb...BhDAfJY"]);
console.log("connected");

let wallet = Wallet.new_from_private_key(EvmNetwork.default(), "your_private_key_here");
console.log("wallet retrieved");

let data = new Uint8Array([1, 2, 3]);
let result = await client.put(data, wallet);
console.log("Data stored at:", result);

let fetchedData = await client.get(result);
console.log("Data retrieved:", fetchedData);
```

---

This documentation covers the basic usage of `Client`, `Wallet`, and `EvmNetwork` types in the JavaScript API.
6 changes: 4 additions & 2 deletions autonomi/src/client/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ impl Client {
Ok(map_xor_name)
}

pub(crate) async fn cost(&self, data: Bytes) -> Result<AttoTokens, PayError> {
/// Get the cost of storing a piece of data.
pub async fn cost(&self, data: Bytes) -> Result<AttoTokens, PayError> {
let now = std::time::Instant::now();
let (data_map_chunk, chunks) = encrypt(data)?;

Expand All @@ -209,6 +210,7 @@ impl Client {
Ok(total_cost)
}

/// Pay for the chunks and get the proof of payment.
pub(crate) async fn pay(
&self,
content_addrs: impl Iterator<Item = XorName>,
Expand Down Expand Up @@ -236,7 +238,7 @@ impl Client {
Ok((proofs, skipped_chunks))
}

async fn get_store_quotes(
pub(crate) async fn get_store_quotes(
&self,
content_addrs: impl Iterator<Item = XorName>,
) -> Result<HashMap<XorName, PayeeQuote>, PayError> {
Expand Down
Loading
Loading