Skip to content

Commit

Permalink
Merge pull request #2381 from mickvandijke/wasm-rename-methods
Browse files Browse the repository at this point in the history
refactor!: rename wasm methods
  • Loading branch information
b-zee authored Nov 6, 2024
2 parents 5f45d8c + 139d37a commit fd26255
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
8 changes: 4 additions & 4 deletions autonomi/examples/metamask/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function externalSignerPrivateDataPutToVault(peerAddr) {
await new Promise(resolve => setTimeout(resolve, 5000));

// Upload the data
const privateDataAccess = await client.privateDataPutWithReceipt(data, receipt);
const privateDataAccess = await client.putPrivateDataWithReceipt(data, receipt);

// Create a private archive
const privateArchive = new autonomi.PrivateArchive();
Expand All @@ -58,7 +58,7 @@ export async function externalSignerPrivateDataPutToVault(peerAddr) {
await new Promise(resolve => setTimeout(resolve, 5000));

// Upload the private archive
const privateArchiveAccess = await client.privateArchivePutWithReceipt(privateArchive, paReceipt);
const privateArchiveAccess = await client.putPrivateArchiveWithReceipt(privateArchive, paReceipt);

// Generate a random vault key (should normally be derived from a constant signature)
const vaultKey = autonomi.genSecretKey();
Expand Down Expand Up @@ -102,7 +102,7 @@ export async function externalSignerPrivateDataPutToVault(peerAddr) {
let fetchedPrivateArchiveAccess = fetchedUserData.privateFileArchives().keys().next().value;

// Get private archive
let fetchedPrivateArchive = await client.privateArchiveGet(fetchedPrivateArchiveAccess);
let fetchedPrivateArchive = await client.getPrivateArchive(fetchedPrivateArchiveAccess);

// Select first file in private archive
let [fetchedFilePath, [fetchedPrivateFileAccess, fetchedFileMetadata]] = fetchedPrivateArchive.map().entries().next().value;
Expand All @@ -112,7 +112,7 @@ export async function externalSignerPrivateDataPutToVault(peerAddr) {
console.log(fetchedFileMetadata);

// Fetch private file/data
let fetchedPrivateFile = await client.privateDataGet(fetchedPrivateFileAccess);
let fetchedPrivateFile = await client.getPrivateData(fetchedPrivateFileAccess);

// Compare to original data
console.log("Comparing fetched data to original data..");
Expand Down
65 changes: 32 additions & 33 deletions autonomi/src/client/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use super::vault::UserData;
use crate::client::data_private::PrivateDataAccess;
use crate::client::payment::Receipt;
use libp2p::Multiaddr;
use serde_wasm_bindgen::Serializer;
use sn_protocol::storage::Chunk;
use wasm_bindgen::prelude::*;

Expand All @@ -16,13 +15,13 @@ use wasm_bindgen::prelude::*;
///
/// ```js
/// let client = await Client.connect(["/ip4/127.0.0.1/tcp/36075/ws/p2p/12D3KooWALb...BhDAfJY"]);
/// const dataAddr = await client.dataPut(new Uint8Array([0, 1, 2, 3]), wallet);
/// const dataAddr = await client.putData(new Uint8Array([0, 1, 2, 3]), wallet);
///
/// const archive = new Archive();
/// archive.addNewFile("foo", dataAddr);
///
/// const archiveAddr = await client.archivePut(archive, wallet);
/// const archiveFetched = await client.archiveGet(archiveAddr);
/// const archiveAddr = await client.putArchive(archive, wallet);
/// const archiveFetched = await client.getArchive(archiveAddr);
/// ```
#[wasm_bindgen(js_name = Client)]
pub struct JsClient(super::Client);
Expand Down Expand Up @@ -81,14 +80,14 @@ impl JsClient {
/// Returns the hex encoded address of the chunk.
///
/// This is not yet implemented.
#[wasm_bindgen(js_name = chunkPut)]
pub async fn chunk_put(&self, _data: Vec<u8>, _wallet: &JsWallet) -> Result<String, JsError> {
#[wasm_bindgen(js_name = putChunk)]
pub async fn put_chunk(&self, _data: Vec<u8>, _wallet: &JsWallet) -> Result<String, JsError> {
async { unimplemented!() }.await
}

/// Fetch the chunk from the network.
#[wasm_bindgen(js_name = chunkGet)]
pub async fn chunk_get(&self, addr: String) -> Result<Vec<u8>, JsError> {
#[wasm_bindgen(js_name = getChunk)]
pub async fn get_chunk(&self, addr: String) -> Result<Vec<u8>, JsError> {
let addr = str_to_addr(&addr)?;
let chunk = self.0.chunk_get(addr).await?;

Expand All @@ -98,8 +97,8 @@ impl JsClient {
/// Upload data to the network.
///
/// Returns the hex encoded address of the data.
#[wasm_bindgen(js_name = dataPut)]
pub async fn data_put(&self, data: Vec<u8>, wallet: &JsWallet) -> Result<String, JsError> {
#[wasm_bindgen(js_name = putData)]
pub async fn put_data(&self, data: Vec<u8>, wallet: &JsWallet) -> Result<String, JsError> {
let data = crate::Bytes::from(data);
let xorname = self.0.data_put(data, (&wallet.0).into()).await?;

Expand All @@ -109,8 +108,8 @@ impl JsClient {
/// Upload private data to the network.
///
/// Returns the `PrivateDataAccess` chunk of the data.
#[wasm_bindgen(js_name = privateDataPut)]
pub async fn private_data_put(
#[wasm_bindgen(js_name = putPrivateData)]
pub async fn put_private_data(
&self,
data: Vec<u8>,
wallet: &JsWallet,
Expand All @@ -126,8 +125,8 @@ impl JsClient {
/// Uses a `Receipt` as payment.
///
/// Returns the `PrivateDataAccess` chunk of the data.
#[wasm_bindgen(js_name = privateDataPutWithReceipt)]
pub async fn private_data_put_with_receipt(
#[wasm_bindgen(js_name = putPrivateDataWithReceipt)]
pub async fn put_private_data_with_receipt(
&self,
data: Vec<u8>,
receipt: JsValue,
Expand All @@ -141,17 +140,17 @@ impl JsClient {
}

/// Fetch the data from the network.
#[wasm_bindgen(js_name = dataGet)]
pub async fn data_get(&self, addr: String) -> Result<Vec<u8>, JsError> {
#[wasm_bindgen(js_name = getData)]
pub async fn get_data(&self, addr: String) -> Result<Vec<u8>, JsError> {
let addr = str_to_addr(&addr)?;
let data = self.0.data_get(addr).await?;

Ok(data.to_vec())
}

/// Fetch the data from the network.
#[wasm_bindgen(js_name = privateDataGet)]
pub async fn private_data_get(&self, private_data_access: JsValue) -> Result<Vec<u8>, JsError> {
#[wasm_bindgen(js_name = getPrivateData)]
pub async fn get_private_data(&self, private_data_access: JsValue) -> Result<Vec<u8>, JsError> {
let private_data_access: PrivateDataAccess =
serde_wasm_bindgen::from_value(private_data_access)?;
let data = self.0.private_data_get(private_data_access).await?;
Expand All @@ -160,8 +159,8 @@ impl JsClient {
}

/// Get the cost of uploading data to the network.
#[wasm_bindgen(js_name = dataCost)]
pub async fn data_cost(&self, data: Vec<u8>) -> Result<AttoTokens, JsValue> {
#[wasm_bindgen(js_name = getDataCost)]
pub async fn get_data_cost(&self, data: Vec<u8>) -> Result<AttoTokens, JsValue> {
let data = crate::Bytes::from(data);
let cost = self.0.data_cost(data).await.map_err(JsError::from)?;

Expand Down Expand Up @@ -223,8 +222,8 @@ mod archive {
#[wasm_bindgen(js_class = Client)]
impl JsClient {
/// Fetch an archive from the network.
#[wasm_bindgen(js_name = archiveGet)]
pub async fn archive_get(&self, addr: String) -> Result<JsArchive, JsError> {
#[wasm_bindgen(js_name = getArchive)]
pub async fn get_archive(&self, addr: String) -> Result<JsArchive, JsError> {
let addr = str_to_addr(&addr)?;
let archive = self.0.archive_get(addr).await?;
let archive = JsArchive(archive);
Expand All @@ -235,8 +234,8 @@ mod archive {
/// Upload an archive to the network.
///
/// Returns the hex encoded address of the archive.
#[wasm_bindgen(js_name = archivePut)]
pub async fn archive_put(
#[wasm_bindgen(js_name = putArchive)]
pub async fn put_archive(
&self,
archive: &JsArchive,
wallet: &JsWallet,
Expand Down Expand Up @@ -295,8 +294,8 @@ mod archive_private {
#[wasm_bindgen(js_class = Client)]
impl JsClient {
/// Fetch a private archive from the network.
#[wasm_bindgen(js_name = privateArchiveGet)]
pub async fn private_archive_get(
#[wasm_bindgen(js_name = getPrivateArchive)]
pub async fn get_private_archive(
&self,
private_archive_access: JsValue,
) -> Result<JsPrivateArchive, JsError> {
Expand All @@ -311,8 +310,8 @@ mod archive_private {
/// Upload a private archive to the network.
///
/// Returns the `PrivateArchiveAccess` chunk of the archive.
#[wasm_bindgen(js_name = privateArchivePut)]
pub async fn private_archive_put(
#[wasm_bindgen(js_name = putPrivateArchive)]
pub async fn put_private_archive(
&self,
archive: &JsPrivateArchive,
wallet: &JsWallet,
Expand All @@ -331,8 +330,8 @@ mod archive_private {
/// Uses a `Receipt` as payment.
///
/// Returns the `PrivateArchiveAccess` chunk of the archive.
#[wasm_bindgen(js_name = privateArchivePutWithReceipt)]
pub async fn private_archive_put_with_receipt(
#[wasm_bindgen(js_name = putPrivateArchiveWithReceipt)]
pub async fn put_private_archive_with_receipt(
&self,
archive: &JsPrivateArchive,
receipt: JsValue,
Expand Down Expand Up @@ -642,10 +641,10 @@ mod external_signer {
///
/// ```js
/// const receipt = getReceiptFromQuotesAndPayments(quotes, payments);
/// const addr = await client.dataPutWithReceipt(data, receipt);
/// const addr = await client.putDataWithReceipt(data, receipt);
/// ```
#[wasm_bindgen(js_name = dataPutWithReceipt)]
pub async fn data_put_with_receipt(
#[wasm_bindgen(js_name = putDataWithReceipt)]
pub async fn put_data_with_receipt(
&self,
data: Vec<u8>,
receipt: JsValue,
Expand Down
22 changes: 11 additions & 11 deletions autonomi/tests-js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import init, * as atnm from '../pkg/autonomi.js';
import { assert } from './node_modules/chai/chai.js';
import {assert} from './node_modules/chai/chai.js';

function randomData(len) {
const array = new Uint8Array(len);
Expand All @@ -21,34 +21,34 @@ describe('autonomi', function () {

it('calculates cost', async () => {
const data = randomData(32);
const cost = await client.dataCost(data);
const cost = await client.getDataCost(data);

assert.typeOf(Number.parseFloat(cost.toString()), 'number');
});

it('puts data (32 bytes)', async () => {
const data = randomData(32);
const addr = await client.dataPut(data, wallet);
const addr = await client.putData(data, wallet);

assert.typeOf(addr, 'string');
});

it('puts data and gets it (32 bytes)', async () => {
const data = randomData(32);
const addr = await client.dataPut(data, wallet);
const fetchedData = await client.dataGet(addr);
const addr = await client.putData(data, wallet);
const fetchedData = await client.getData(addr);

assert.deepEqual(Array.from(data), Array.from(fetchedData));
});

it('puts data, creates archive and retrieves it', async () => {
const data = randomData(32);
const addr = await client.dataPut(data, wallet);
const addr = await client.putData(data, wallet);
const archive = new atnm.Archive();
archive.addNewFile("foo", addr);
const archiveAddr = await client.archivePut(archive, wallet);
const archiveAddr = await client.putArchive(archive, wallet);

const archiveFetched = await client.archiveGet(archiveAddr);
const archiveFetched = await client.getArchive(archiveAddr);

assert.deepEqual(archive, archiveFetched);
});
Expand All @@ -60,14 +60,14 @@ describe('autonomi', function () {

const archive = new atnm.Archive();
archive.addNewFile('foo', addr);
const archiveAddr = await client.archivePut(archive, wallet);
const archiveAddr = await client.putArchive(archive, wallet);

const userData = new atnm.UserData();
userData.addFileArchive(archiveAddr, 'foo');

await client.putUserDataToVault(userData, wallet, secretKey);
const userDataFetched = await client.getUserDataFromVault(secretKey);

assert.deepEqual(userDataFetched.fileArchives(), userData.fileArchives());
});
});

0 comments on commit fd26255

Please sign in to comment.