@withautonomi/autonomi 0.5.3 → 0.6.1

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.1"
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.3" }
12
+ autonomi = { path = "../autonomi", version = "0.6.1" }
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.3",
3
+ "version": "0.6.1",
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.3",
61
- "@withautonomi/autonomi-darwin-x64": "0.5.3",
62
- "@withautonomi/autonomi-linux-x64-gnu": "0.5.3",
63
- "@withautonomi/autonomi-darwin-arm64": "0.5.3"
60
+ "@withautonomi/autonomi-win32-x64-msvc": "0.6.1",
61
+ "@withautonomi/autonomi-darwin-x64": "0.6.1",
62
+ "@withautonomi/autonomi-linux-x64-gnu": "0.6.1",
63
+ "@withautonomi/autonomi-darwin-arm64": "0.6.1"
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,
@@ -732,9 +787,9 @@ impl Client {
732
787
 
733
788
  /// Get the user data from the vault
734
789
  #[napi]
735
- pub async fn get_user_data_from_vault(&self, secret_key: &VaultSecretKey) -> Result<UserData> {
790
+ pub async fn vault_get_user_data(&self, secret_key: &VaultSecretKey) -> Result<UserData> {
736
791
  self.0
737
- .get_user_data_from_vault(&secret_key.0)
792
+ .vault_get_user_data(&secret_key.0)
738
793
  .await
739
794
  .map(UserData)
740
795
  .map_err(map_error)
@@ -744,14 +799,14 @@ impl Client {
744
799
  ///
745
800
  /// Returns the total cost of the put operation
746
801
  #[napi]
747
- pub async fn put_user_data_to_vault(
802
+ pub async fn vault_put_user_data(
748
803
  &self,
749
804
  secret_key: &VaultSecretKey,
750
805
  payment_option: &PaymentOption,
751
806
  user_data: &UserData,
752
807
  ) -> Result</* AttoTokens */ String> {
753
808
  self.0
754
- .put_user_data_to_vault(&secret_key.0, payment_option.0.clone(), user_data.0.clone())
809
+ .vault_put_user_data(&secret_key.0, payment_option.0.clone(), user_data.0.clone())
755
810
  .await
756
811
  .map(|c| c.to_string())
757
812
  .map_err(map_error)
@@ -761,15 +816,11 @@ impl Client {
761
816
  ///
762
817
  /// Returns the content type of the bytes in the vault.
763
818
  #[napi]
764
- pub async fn fetch_and_decrypt_vault(
819
+ pub async fn vault_get(
765
820
  &self,
766
821
  secret_key: &VaultSecretKey,
767
822
  ) -> Result</* (Bytes, VaultContentType) */ tuple_result::FetchAndDecryptVault> {
768
- let (data, content_type) = self
769
- .0
770
- .fetch_and_decrypt_vault(&secret_key.0)
771
- .await
772
- .map_err(map_error)?;
823
+ let (data, content_type) = self.0.vault_get(&secret_key.0).await.map_err(map_error)?;
773
824
 
774
825
  Ok(tuple_result::FetchAndDecryptVault { data, content_type })
775
826
  }
@@ -800,7 +851,7 @@ impl Client {
800
851
  /// It is recommended to use the hash of the app name or unique identifier as the content type.
801
852
 
802
853
  #[napi]
803
- pub async fn write_bytes_to_vault(
854
+ pub async fn vault_put(
804
855
  &self,
805
856
  data: Buffer,
806
857
  payment_option: &PaymentOption,
@@ -810,7 +861,7 @@ impl Client {
810
861
  let data = Bytes::copy_from_slice(&data);
811
862
 
812
863
  self.0
813
- .write_bytes_to_vault(
864
+ .vault_put(
814
865
  data,
815
866
  payment_option.0.clone(),
816
867
  &secret_key.0,
@@ -821,6 +872,46 @@ impl Client {
821
872
  .map_err(map_error)
822
873
  }
823
874
 
875
+ /// @deprecated Use `vault_get_user_data` instead. This function will be removed in a future version.
876
+ #[napi]
877
+ pub async fn get_user_data_from_vault(&self, secret_key: &VaultSecretKey) -> Result<UserData> {
878
+ self.vault_get_user_data(secret_key).await
879
+ }
880
+
881
+ /// @deprecated Use `vault_put_user_data` instead. This function will be removed in a future version.
882
+ #[napi]
883
+ pub async fn put_user_data_to_vault(
884
+ &self,
885
+ secret_key: &VaultSecretKey,
886
+ payment_option: &PaymentOption,
887
+ user_data: &UserData,
888
+ ) -> Result</* AttoTokens */ String> {
889
+ self.vault_put_user_data(secret_key, payment_option, user_data)
890
+ .await
891
+ }
892
+
893
+ /// @deprecated Use `vault_get` instead. This function will be removed in a future version.
894
+ #[napi]
895
+ pub async fn fetch_and_decrypt_vault(
896
+ &self,
897
+ secret_key: &VaultSecretKey,
898
+ ) -> Result</* (Bytes, VaultContentType) */ tuple_result::FetchAndDecryptVault> {
899
+ self.vault_get(secret_key).await
900
+ }
901
+
902
+ /// @deprecated Use `vault_put` instead. This function will be removed in a future version.
903
+ #[napi]
904
+ pub async fn write_bytes_to_vault(
905
+ &self,
906
+ data: Buffer,
907
+ payment_option: &PaymentOption,
908
+ secret_key: &VaultSecretKey,
909
+ content_type: &VaultContentType,
910
+ ) -> Result</* AttoTokens */ String> {
911
+ self.vault_put(data, payment_option, secret_key, content_type)
912
+ .await
913
+ }
914
+
824
915
  // Registers
825
916
 
826
917
  /// Get the register history, starting from the root to the latest entry.
@@ -1541,6 +1632,14 @@ impl Network {
1541
1632
  let network = autonomi::Network::new(local).map_err(map_error)?;
1542
1633
  Ok(Self(network))
1543
1634
  }
1635
+
1636
+ #[napi]
1637
+ pub fn from_string(name: String) -> Result<Self> {
1638
+ let network = autonomi::Network::from_str(&name).map_err(|()| {
1639
+ napi::Error::new(Status::InvalidArg, format!("Invalid network name '{name}'"))
1640
+ })?;
1641
+ Ok(Self(network))
1642
+ }
1544
1643
  }
1545
1644
 
1546
1645
  #[napi]
@@ -2071,7 +2170,7 @@ impl PrivateArchive {
2071
2170
  .collect()
2072
2171
  }
2073
2172
 
2074
- /// List all data maps of the files in the archive
2173
+ /// List all datamaps of the files in the archive
2075
2174
  #[napi]
2076
2175
  pub fn data_maps(&self) -> Vec<DataMapChunk> {
2077
2176
  self.0.data_maps().into_iter().map(DataMapChunk).collect()
Binary file