@withautonomi/autonomi 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Cargo.toml CHANGED
@@ -1,7 +1,7 @@
1
1
  [package]
2
- edition = "2021"
2
+ edition = "2024"
3
3
  name = "autonomi-nodejs"
4
- version = "0.1.1"
4
+ version = "0.2.0"
5
5
  description = "NodeJS bindings for the autonomi client"
6
6
  license = "GPL-3.0"
7
7
 
@@ -9,7 +9,7 @@ license = "GPL-3.0"
9
9
  crate-type = ["cdylib"]
10
10
 
11
11
  [dependencies]
12
- autonomi = { path = "../autonomi", version = "0.5.2" }
12
+ autonomi = { path = "../autonomi", version = "0.6.0" }
13
13
  bytes = { version = "1.0.1", features = ["serde"] }
14
14
  eyre = "0.6.12"
15
15
  futures = "0.3"
@@ -20,5 +20,8 @@ serde = { version = "1.0.133", features = ["derive", "rc"] }
20
20
  serde_json = "1.0"
21
21
  tokio = { version = "1", features = ["full"] }
22
22
 
23
+ [lints]
24
+ workspace = true
25
+
23
26
  [build-dependencies]
24
27
  napi-build = "2.0.1"
package/README.md CHANGED
@@ -31,23 +31,23 @@ For example usage, see the [`__test__`](./__test__) directory. Replace `import {
31
31
 
32
32
  # Contributing, compilation and publishing
33
33
 
34
- To contribute or develop on the source code directly, we need a few requirements.
34
+ To contribute or develop on the source code directly, Node.js must be installed (installation instructions [here](https://nodejs.org/en/download)).
35
35
 
36
- - Yarn
37
- - `npm install --global yarn`
38
- - We need the NAPI RS CLI tool
39
- - `yarn global add @napi-rs/cli`
36
+ With Node.js installed, change the working directory to `autonomi-nodejs/`:
37
+ ```console
38
+ $ cd ./autonomi-nodejs/
39
+ ```
40
40
 
41
- Install the dependencies for the project:
41
+ Then install the dependencies for the project:
42
42
  ```console
43
- $ yarn install
43
+ $ npm install
44
44
  ```
45
45
 
46
46
  ## Build
47
47
 
48
- Then build using the `napi` CLI:
48
+ Then build using the build script (which calls the `napi` CLI):
49
49
  ```console
50
- $ npx napi build
50
+ $ npm run build
51
51
  ```
52
52
 
53
53
  ## Running tests
@@ -55,9 +55,9 @@ $ npx napi build
55
55
  Run the `test` script:
56
56
 
57
57
  ```console
58
- yarn test
58
+ npm test
59
59
  # Or run a specific test
60
- yarn test __test__/register.spec.mjs -m 'registers errors'
60
+ npm test __test__/register.spec.mjs -m 'registers errors'
61
61
  ```
62
62
 
63
63
  ## Publishing
@@ -73,4 +73,4 @@ It's a good practice to have an unreleased version number ready to go. So if `0.
73
73
 
74
74
  ### Workflow
75
75
 
76
- Use the 'JS publish to NPM' workflow (`nodejs-publish.yml`) to publish the package from `main` or a tag. This workflow has to be manually dispatched through GitHub.
76
+ Use the 'Node.js (release)' workflow (`nodejs-publish.yml`) to publish the package from `main` or a tag. This workflow has to be manually dispatched through GitHub.
package/build.rs CHANGED
@@ -1,5 +1,3 @@
1
- extern crate napi_build;
2
-
3
1
  fn main() {
4
2
  napi_build::setup();
5
3
  }
@@ -0,0 +1,64 @@
1
+ // Copyright 2025 MaidSafe.net limited.
2
+ //
3
+ // This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4
+ // Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5
+ // under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6
+ // KIND, either express or implied. Please review the Licences for the specific language governing
7
+ // permissions and limitations relating to use of the SAFE Network Software.
8
+
9
+ import { Client, SecretKey, Wallet, Network, PaymentOption } from '@withautonomi/autonomi';
10
+
11
+ async function scratchpadExample() {
12
+ try {
13
+ // Initialize client and wallet
14
+ const client = await Client.initLocal();
15
+ const network = new Network(true); // Use testnet for local development
16
+ // For mainnet use: new Network(false)
17
+ const privateKey = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
18
+ const wallet = Wallet.newFromPrivateKey(network, privateKey);
19
+ const payment = PaymentOption.fromWallet(wallet);
20
+
21
+ // Create secret key for scratchpad
22
+ const key = SecretKey.random();
23
+ const publicKey = key.publicKey();
24
+
25
+ // Check cost
26
+ const estimatedCost = await client.scratchpadCost(publicKey);
27
+ console.log(`Estimated scratchpad cost: ${estimatedCost}`);
28
+
29
+ // Create scratchpad
30
+ const contentType = 42n;
31
+ const initialData = Buffer.from("Hello, Autonomi!");
32
+ const { cost: actualCost, addr } = await client.scratchpadCreate(key, contentType, initialData, payment);
33
+ console.log(`Created at ${addr.toHex()}`);
34
+ console.log(`Actual cost: ${actualCost}`);
35
+
36
+ // Get scratchpad
37
+ const scratchpad = await client.scratchpadGet(addr);
38
+ console.assert(scratchpad.counter() === 0n);
39
+ console.log(`Retrieved scratchpad with counter: ${scratchpad.counter()}`);
40
+
41
+ // Decrypt content
42
+ const decrypted = scratchpad.decryptData(key);
43
+ console.assert(Buffer.compare(decrypted, initialData) === 0);
44
+ console.log("✓ Decrypted content matches initial data");
45
+
46
+ // Update scratchpad (free)
47
+ const newData = Buffer.from("Updated content!");
48
+ await client.scratchpadUpdate(key, contentType, newData);
49
+ console.log("✓ Scratchpad updated successfully");
50
+
51
+ // Get updated scratchpad
52
+ const updated = await client.scratchpadGet(addr);
53
+ console.assert(updated.counter() === 1n);
54
+ const updatedContent = updated.decryptData(key);
55
+ console.assert(Buffer.compare(updatedContent, newData) === 0);
56
+ console.log(`✓ Updated scratchpad verified with counter: ${updated.counter()}`);
57
+ console.log("✓ All scratchpad operations completed successfully!");
58
+
59
+ } catch (error) {
60
+ console.error('Error:', error.message);
61
+ }
62
+ }
63
+
64
+ scratchpadExample();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@withautonomi/autonomi",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "NodeJS bindings for Autonomi client",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -57,9 +57,9 @@
57
57
  "includeVersion": true
58
58
  },
59
59
  "optionalDependencies": {
60
- "@withautonomi/autonomi-win32-x64-msvc": "0.5.2",
61
- "@withautonomi/autonomi-darwin-x64": "0.5.2",
62
- "@withautonomi/autonomi-linux-x64-gnu": "0.5.2",
63
- "@withautonomi/autonomi-darwin-arm64": "0.5.2"
60
+ "@withautonomi/autonomi-win32-x64-msvc": "0.6.0",
61
+ "@withautonomi/autonomi-darwin-x64": "0.6.0",
62
+ "@withautonomi/autonomi-linux-x64-gnu": "0.6.0",
63
+ "@withautonomi/autonomi-darwin-arm64": "0.6.0"
64
64
  }
65
65
  }
package/src/lib.rs CHANGED
@@ -1,3 +1,6 @@
1
+ // Allow unused_async as NAPI bindings may require async signatures
2
+ #![allow(clippy::unused_async)]
3
+
1
4
  use std::{path::PathBuf, str::FromStr};
2
5
 
3
6
  use autonomi::{AttoTokens, Bytes, Chunk, Multiaddr, Signature};
@@ -279,6 +282,28 @@ impl Client {
279
282
  .map_err(map_error)
280
283
  }
281
284
 
285
+ /// Update an existing pointer from a specific pointer to point to a new target on the network.
286
+ ///
287
+ /// This will increment the counter of the pointer and update the target.
288
+ /// This function is used internally by `pointer_update` after the pointer has been retrieved from the network.
289
+ /// To skip the retrieval step if you already have the pointer, use this function directly.
290
+ /// This function will return the new pointer after it has been updated.
291
+ #[napi]
292
+ pub async fn pointer_update_from(
293
+ &self,
294
+ current: &Pointer,
295
+ owner: &SecretKey,
296
+ target: &PointerTarget,
297
+ ) -> Result<Pointer> {
298
+ let new_pointer = self
299
+ .0
300
+ .pointer_update_from(&current.0, &owner.0, target.0.clone())
301
+ .await
302
+ .map_err(map_error)?;
303
+
304
+ Ok(Pointer(new_pointer))
305
+ }
306
+
282
307
  /// Calculate the cost of storing a pointer
283
308
  #[napi]
284
309
  pub async fn pointer_cost(&self, key: &PublicKey) -> Result</* AttoTokens */ String> {
@@ -392,6 +417,36 @@ impl Client {
392
417
  .map_err(map_error)
393
418
  }
394
419
 
420
+ /// Update an existing scratchpad from a specific scratchpad to the network.
421
+ ///
422
+ /// This will increment the counter of the scratchpad and update the content.
423
+ /// This function is used internally by `scratchpad_update` after the scratchpad has been retrieved from the network.
424
+ /// To skip the retrieval step if you already have the scratchpad, use this function directly.
425
+ /// This function will return the new scratchpad after it has been updated.
426
+ #[napi]
427
+ pub async fn scratchpad_update_from(
428
+ &self,
429
+ current: &Scratchpad,
430
+ owner: &SecretKey,
431
+ content_type: BigInt, // `u64`
432
+ data: Buffer,
433
+ ) -> Result<Scratchpad> {
434
+ let content_type = big_int_to_u64(content_type, "content_type")?;
435
+
436
+ let new_scratchpad = self
437
+ .0
438
+ .scratchpad_update_from(
439
+ &current.0,
440
+ &owner.0,
441
+ content_type,
442
+ &Bytes::copy_from_slice(&data),
443
+ )
444
+ .await
445
+ .map_err(map_error)?;
446
+
447
+ Ok(Scratchpad(new_scratchpad))
448
+ }
449
+
395
450
  /// Get the cost of creating a new Scratchpad
396
451
  #[napi]
397
452
  pub async fn scratchpad_cost(&self, owner: &PublicKey) -> Result</* AttoTokens */ String> {
@@ -574,7 +629,7 @@ impl Client {
574
629
  /// Upload the content of all files in a directory to the network.
575
630
  /// The directory is recursively walked and each file is uploaded to the network.
576
631
  ///
577
- /// The data maps of these (private) files are not uploaded but returned within
632
+ /// The datamaps of these (private) files are not uploaded but returned within
578
633
  /// the PrivateArchive return type.
579
634
 
580
635
  #[napi]
@@ -665,9 +720,9 @@ impl Client {
665
720
 
666
721
  /// Upload the content of all files in a directory to the network. The directory is recursively walked and each file is uploaded to the network.
667
722
  ///
668
- /// The data maps of these files are uploaded on the network, making the individual files publicly available.
723
+ /// The datamaps of these files are uploaded on the network, making the individual files publicly available.
669
724
  ///
670
- /// This returns, but does not upload (!),the PublicArchive containing the data maps of the uploaded files.
725
+ /// This returns, but does not upload (!),the PublicArchive containing the datamaps of the uploaded files.
671
726
  #[napi]
672
727
  pub async fn dir_content_upload_public(
673
728
  &self,
@@ -1541,6 +1596,14 @@ impl Network {
1541
1596
  let network = autonomi::Network::new(local).map_err(map_error)?;
1542
1597
  Ok(Self(network))
1543
1598
  }
1599
+
1600
+ #[napi]
1601
+ pub fn from_string(name: String) -> Result<Self> {
1602
+ let network = autonomi::Network::from_str(&name).map_err(|()| {
1603
+ napi::Error::new(Status::InvalidArg, format!("Invalid network name '{name}'"))
1604
+ })?;
1605
+ Ok(Self(network))
1606
+ }
1544
1607
  }
1545
1608
 
1546
1609
  #[napi]
@@ -2071,7 +2134,7 @@ impl PrivateArchive {
2071
2134
  .collect()
2072
2135
  }
2073
2136
 
2074
- /// List all data maps of the files in the archive
2137
+ /// List all datamaps of the files in the archive
2075
2138
  #[napi]
2076
2139
  pub fn data_maps(&self) -> Vec<DataMapChunk> {
2077
2140
  self.0.data_maps().into_iter().map(DataMapChunk).collect()
Binary file