@autonomys/auto-utils 0.1.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/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # Autonomys SDK - TypeScript
2
+
3
+ Welcome to the Autonomys SDK! This README provides an overview of the SDK's functionalities, including wallet setup, network management, data storage, and cryptographic operations. Each section provides a brief description of the included functions and their usage.
4
+
5
+ ## Table of Contents
6
+
7
+ 1. Wallet Management
8
+ 2. Network Management
9
+ 3. Data Storage
10
+ 4. Cryptographic Functions
11
+ 5. API Activation
12
+ 6. Constants
13
+ 7. Types
14
+
15
+ ## Wallet Management
16
+
17
+ ### setupWallet
18
+
19
+ Sets up a wallet using a mnemonic or URI.
20
+
21
+ ```typescript
22
+ export const setupWallet = async (input: MnemonicOrURI): Promise<KeyringPair>
23
+ ```
24
+
25
+ - input: The mnemonic or URI used to set up the wallet.
26
+ - Returns a KeyringPair.
27
+
28
+ #### activateWallet
29
+
30
+ Activates a wallet for a given network or domain and returns API and accounts.
31
+
32
+ ```typescript
33
+ export const activateWallet = async (input: ActivateWalletInput): Promise<WalletActivated>
34
+ ```
35
+
36
+ - input: Network or domain input along with mnemonic or URI and app name.
37
+ - Returns an object containing api and accounts.
38
+
39
+ #### mockWallets
40
+
41
+ Creates mock wallets for a given network or domain.
42
+
43
+ ```typescript
44
+ export const mockWallets = async (network: NetworkInput | DomainInput): Promise<WalletActivated[]>
45
+ ```
46
+
47
+ - network: Network or domain input.
48
+ - Returns an array of WalletActivated objects.
49
+
50
+ #### getMockWallet
51
+
52
+ Retrieves a mock wallet by name.
53
+
54
+ ```typescript
55
+ export const getMockWallet = (name: string, wallets: WalletActivated[]): WalletActivated
56
+ ```
57
+
58
+ - name: Name of the mock wallet.
59
+ - wallets: Array of activated wallets.
60
+ - Returns a WalletActivated object.
61
+
62
+ ### Network Management
63
+
64
+ #### getNetworkDetails
65
+
66
+ Retrieves details of a specified network.
67
+
68
+ ```typescript
69
+ export const getNetworkDetails = (input?: NetworkInput) => Network
70
+ ```
71
+
72
+ - input: Network input.
73
+ - Returns network details.
74
+
75
+ #### getNetworkRpcUrls
76
+
77
+ Retrieves RPC URLs for a specified network.
78
+
79
+ ```typescript
80
+ export const getNetworkRpcUrls = (input?: NetworkInput) => string[]
81
+ ```
82
+
83
+ - input: Network input.
84
+ - Returns an array of RPC URLs.
85
+
86
+ #### getNetworkDomainDetails
87
+
88
+ Retrieves details of a specified network domain.
89
+
90
+ ```typescript
91
+ export const getNetworkDomainDetails = (input: DomainInput) => Domain
92
+ ```
93
+
94
+ - input: Domain input.
95
+ - Returns domain details.
96
+
97
+ #### getNetworkDomainRpcUrls
98
+
99
+ Retrieves RPC URLs for a specified network domain.
100
+
101
+ ```typescript
102
+ export const getNetworkDomainRpcUrls = (input: DomainInput) => string[]
103
+ ```
104
+
105
+ - input: Domain input.
106
+ - Returns an array of RPC URLs.
package/dist/api.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { ApiPromise } from '@polkadot/api';
2
+ import type { DomainInput, NetworkInput } from './types/network';
3
+ export declare const activate: (input?: NetworkInput) => Promise<ApiPromise>;
4
+ export declare const activateDomain: (input: DomainInput) => Promise<ApiPromise>;
5
+ export declare const disconnect: () => Promise<void>;
6
+ export declare const disconnectDomain: () => Promise<void>;
7
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAc,MAAM,eAAe,CAAA;AAEtD,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAQhE,eAAO,MAAM,QAAQ,WAAkB,YAAY,wBASlD,CAAA;AAED,eAAO,MAAM,cAAc,UAAiB,WAAW,wBAStD,CAAA;AAED,eAAO,MAAM,UAAU,qBAOtB,CAAA;AAED,eAAO,MAAM,gBAAgB,qBAO5B,CAAA"}
package/dist/api.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ // file: src/api.ts
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.disconnectDomain = exports.disconnect = exports.activateDomain = exports.activate = void 0;
14
+ const api_1 = require("@polkadot/api");
15
+ const network_1 = require("./network");
16
+ let provider = null;
17
+ let apiInstance = null;
18
+ let domainProvider = null;
19
+ let apiDomainInstance = null;
20
+ const activate = (input) => __awaiter(void 0, void 0, void 0, function* () {
21
+ // Get the first rpc urls for the network
22
+ const rpcUrl = (0, network_1.getNetworkRpcUrls)(input);
23
+ // Create the provider
24
+ provider = new api_1.WsProvider(rpcUrl[0]);
25
+ // Create the API instance
26
+ apiInstance = yield api_1.ApiPromise.create({ provider });
27
+ return apiInstance;
28
+ });
29
+ exports.activate = activate;
30
+ const activateDomain = (input) => __awaiter(void 0, void 0, void 0, function* () {
31
+ // Get the first rpc urls for the network
32
+ const rpcUrl = (0, network_1.getNetworkDomainRpcUrls)(input);
33
+ // Create the provider
34
+ domainProvider = new api_1.WsProvider(rpcUrl[0]);
35
+ // Create the API instance
36
+ apiDomainInstance = yield api_1.ApiPromise.create({ provider: domainProvider });
37
+ return apiDomainInstance;
38
+ });
39
+ exports.activateDomain = activateDomain;
40
+ const disconnect = () => __awaiter(void 0, void 0, void 0, function* () {
41
+ // Disconnect the API instance and the provider if they exist
42
+ if (apiInstance) {
43
+ yield apiInstance.disconnect();
44
+ apiInstance = null;
45
+ provider = null;
46
+ }
47
+ });
48
+ exports.disconnect = disconnect;
49
+ const disconnectDomain = () => __awaiter(void 0, void 0, void 0, function* () {
50
+ // Disconnect the API instance and the provider if they exist
51
+ if (apiDomainInstance) {
52
+ yield apiDomainInstance.disconnect();
53
+ apiDomainInstance = null;
54
+ domainProvider = null;
55
+ }
56
+ });
57
+ exports.disconnectDomain = disconnectDomain;
@@ -0,0 +1,3 @@
1
+ export * from './network';
2
+ export * from './wallet';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/constants/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ // file: src/constants/index.ts
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ __exportStar(require("./network"), exports);
19
+ __exportStar(require("./wallet"), exports);
@@ -0,0 +1,4 @@
1
+ import type { Network } from '../types/network';
2
+ export declare const networks: Network[];
3
+ export declare const defaultNetwork: Network;
4
+ //# sourceMappingURL=network.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/constants/network.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAE/C,eAAO,MAAM,QAAQ,EAAE,OAAO,EAyD7B,CAAA;AAED,eAAO,MAAM,cAAc,SAAc,CAAA"}
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ // file: src/constants/network.ts
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.defaultNetwork = exports.networks = void 0;
5
+ exports.networks = [
6
+ {
7
+ id: 'autonomys-gemini-3h',
8
+ name: 'Autonomys Testnet - Gemini 3H',
9
+ rpcUrls: [
10
+ 'wss://rpc-0.gemini-3h.subspace.network/ws',
11
+ 'wss://rpc-1.gemini-3h.subspace.network/ws',
12
+ ],
13
+ explorer: [
14
+ {
15
+ name: 'Astral',
16
+ url: 'https://explorer.subspace.network/gemini-3h/consensus/',
17
+ },
18
+ {
19
+ name: 'Subscan',
20
+ url: 'https://subspace.subscan.io/',
21
+ },
22
+ ],
23
+ domains: [
24
+ {
25
+ id: 'auto-id', // Placeholder
26
+ name: 'Autonomys - Auto-ID',
27
+ rpcUrls: ['wss://rpc.auto-id.subspace.network/ws'],
28
+ },
29
+ {
30
+ id: 'auto-evm', // Placeholder
31
+ name: 'Autonomys - Auto-EVM (Nova)',
32
+ rpcUrls: ['https://nova-0.gemini-3h.subspace.network/ws'],
33
+ },
34
+ ],
35
+ isTestnet: true,
36
+ },
37
+ {
38
+ id: 'autonomys-localhost',
39
+ name: 'Autonomys - Localhost',
40
+ rpcUrls: ['ws://127.0.0.1:9944/ws'],
41
+ explorer: [
42
+ {
43
+ name: 'Astral',
44
+ url: 'https://explorer.subspace.network/localhost/consensus/',
45
+ },
46
+ ],
47
+ domains: [
48
+ {
49
+ id: 'auto-id', // Placeholder
50
+ name: 'Autonomys - Auto-ID',
51
+ rpcUrls: ['ws://127.0.0.1:9945/ws'],
52
+ },
53
+ {
54
+ id: 'auto-evm', // Placeholder
55
+ name: 'Autonomys - Auto-EVM (Nova)',
56
+ rpcUrls: ['https:///127.0.0.1:9946/ws'],
57
+ },
58
+ ],
59
+ isTestnet: true,
60
+ isLocalhost: true,
61
+ },
62
+ ];
63
+ exports.defaultNetwork = exports.networks[0];
@@ -0,0 +1,2 @@
1
+ export declare const mockURIs: string[];
2
+ //# sourceMappingURL=wallet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet.d.ts","sourceRoot":"","sources":["../../src/constants/wallet.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,QAAQ,UAWpB,CAAA"}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ // file: src/constants/wallet.ts
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.mockURIs = void 0;
5
+ exports.mockURIs = [
6
+ '//Alice',
7
+ '//Bob',
8
+ '//Charlie',
9
+ '//Dave',
10
+ '//Eve',
11
+ '//Frank',
12
+ '//Grace',
13
+ '//Harry',
14
+ '//Ivy',
15
+ '//Jacob',
16
+ ];
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Hashes the given data using BLAKE2b-256.
3
+ *
4
+ * @param data Uint8Array - The data to be hashed.
5
+ * @returns string - The BLAKE2b-256 hash of the data as a hex string.
6
+ */
7
+ export declare function blake2b_256(data: Uint8Array): string;
8
+ /**
9
+ * Converts a string to a Uint8Array using UTF-8 encoding.
10
+ *
11
+ * This function uses the TextEncoder API to convert a plain string into its equivalent byte array
12
+ * representation in UTF-8 format. It is useful for scenarios where string data needs to be processed
13
+ * in a binary format, such as hashing or cryptographic operations.
14
+ *
15
+ * @param text The string to be converted into a byte array.
16
+ * @returns Uint8Array - The UTF-8 encoded byte array representation of the input string.
17
+ *
18
+ * @example
19
+ * const text = "Hello, world!";
20
+ * const byteArray = stringToUint8Array(text);
21
+ * console.log(byteArray); // Outputs the byte array of the string
22
+ */
23
+ export declare function stringToUint8Array(text: string): Uint8Array;
24
+ /**
25
+ * Concatenates two Uint8Array instances into a single Uint8Array.
26
+ */
27
+ export declare function concatenateUint8Arrays(array1: Uint8Array, array2: Uint8Array): Uint8Array;
28
+ //# sourceMappingURL=crypto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAEpD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAG3D;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,GAAG,UAAU,CAKzF"}
package/dist/crypto.js ADDED
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ // file: src/crypto.ts
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.concatenateUint8Arrays = exports.stringToUint8Array = exports.blake2b_256 = void 0;
5
+ const util_crypto_1 = require("@polkadot/util-crypto");
6
+ /**
7
+ * Hashes the given data using BLAKE2b-256.
8
+ *
9
+ * @param data Uint8Array - The data to be hashed.
10
+ * @returns string - The BLAKE2b-256 hash of the data as a hex string.
11
+ */
12
+ function blake2b_256(data) {
13
+ return (0, util_crypto_1.blake2AsHex)(data, 256);
14
+ }
15
+ exports.blake2b_256 = blake2b_256;
16
+ /**
17
+ * Converts a string to a Uint8Array using UTF-8 encoding.
18
+ *
19
+ * This function uses the TextEncoder API to convert a plain string into its equivalent byte array
20
+ * representation in UTF-8 format. It is useful for scenarios where string data needs to be processed
21
+ * in a binary format, such as hashing or cryptographic operations.
22
+ *
23
+ * @param text The string to be converted into a byte array.
24
+ * @returns Uint8Array - The UTF-8 encoded byte array representation of the input string.
25
+ *
26
+ * @example
27
+ * const text = "Hello, world!";
28
+ * const byteArray = stringToUint8Array(text);
29
+ * console.log(byteArray); // Outputs the byte array of the string
30
+ */
31
+ function stringToUint8Array(text) {
32
+ const encoder = new TextEncoder(); // Create a new TextEncoder instance
33
+ return encoder.encode(text); // Encode the string to a Uint8Array using UTF-8 encoding
34
+ }
35
+ exports.stringToUint8Array = stringToUint8Array;
36
+ /**
37
+ * Concatenates two Uint8Array instances into a single Uint8Array.
38
+ */
39
+ function concatenateUint8Arrays(array1, array2) {
40
+ const combined = new Uint8Array(array1.length + array2.length);
41
+ combined.set(array1);
42
+ combined.set(array2, array1.length);
43
+ return combined;
44
+ }
45
+ exports.concatenateUint8Arrays = concatenateUint8Arrays;
@@ -0,0 +1,9 @@
1
+ export * from './api';
2
+ export * from './crypto';
3
+ export * from './network';
4
+ export * from './read';
5
+ export * from './save';
6
+ export * from './wallet';
7
+ export * from './constants';
8
+ export * from './types';
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,OAAO,CAAA;AACrB,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,QAAQ,CAAA;AACtB,cAAc,QAAQ,CAAA;AACtB,cAAc,UAAU,CAAA;AAExB,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ // file: src/index.ts
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ __exportStar(require("./api"), exports);
19
+ __exportStar(require("./crypto"), exports);
20
+ __exportStar(require("./network"), exports);
21
+ __exportStar(require("./read"), exports);
22
+ __exportStar(require("./save"), exports);
23
+ __exportStar(require("./wallet"), exports);
24
+ __exportStar(require("./constants"), exports);
25
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,6 @@
1
+ import type { DomainInput, NetworkInput } from './types/network';
2
+ export declare const getNetworkDetails: (input?: NetworkInput) => import("./types/network").Network;
3
+ export declare const getNetworkRpcUrls: (input?: NetworkInput) => string[];
4
+ export declare const getNetworkDomainDetails: (input: DomainInput) => import("./types/network").Domain;
5
+ export declare const getNetworkDomainRpcUrls: (input: DomainInput) => string[];
6
+ //# sourceMappingURL=network.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../src/network.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAEhE,eAAO,MAAM,iBAAiB,WAAY,YAAY,sCAWrD,CAAA;AAED,eAAO,MAAM,iBAAiB,WAAY,YAAY,aAOrD,CAAA;AAED,eAAO,MAAM,uBAAuB,UAAW,WAAW,qCAYzD,CAAA;AAED,eAAO,MAAM,uBAAuB,UAAW,WAAW,aAMzD,CAAA"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ // file: src/network.ts
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.getNetworkDomainRpcUrls = exports.getNetworkDomainDetails = exports.getNetworkRpcUrls = exports.getNetworkDetails = void 0;
5
+ const network_1 = require("./constants/network");
6
+ const getNetworkDetails = (input) => {
7
+ // If no id is provided, return the default network
8
+ if (!input || !input.networkId)
9
+ return network_1.defaultNetwork;
10
+ const { networkId } = input;
11
+ // Find the network with the provided id
12
+ const network = network_1.networks.find((network) => network.id === networkId);
13
+ if (!network)
14
+ throw new Error(`Network with id ${networkId} not found`);
15
+ return network;
16
+ };
17
+ exports.getNetworkDetails = getNetworkDetails;
18
+ const getNetworkRpcUrls = (input) => {
19
+ // Get the network details
20
+ const network = (0, exports.getNetworkDetails)(input);
21
+ if (!network.rpcUrls || network.rpcUrls.length === 0)
22
+ throw new Error(`Network has no rpcUrls`);
23
+ return network.rpcUrls;
24
+ };
25
+ exports.getNetworkRpcUrls = getNetworkRpcUrls;
26
+ const getNetworkDomainDetails = (input) => {
27
+ const { networkId, domainId } = input;
28
+ // Find the network with the provided id
29
+ const network = (0, exports.getNetworkDetails)({ networkId });
30
+ if (!network)
31
+ throw new Error(`Network with id ${networkId} not found`);
32
+ // Find the domain with the provided id
33
+ const domain = network.domains.find((domain) => domain.id === domainId);
34
+ if (!domain)
35
+ throw new Error(`Domain with id ${domainId} not found`);
36
+ return domain;
37
+ };
38
+ exports.getNetworkDomainDetails = getNetworkDomainDetails;
39
+ const getNetworkDomainRpcUrls = (input) => {
40
+ // Get the network details
41
+ const domain = (0, exports.getNetworkDomainDetails)(input);
42
+ if (!domain.rpcUrls || domain.rpcUrls.length === 0)
43
+ throw new Error(`Domain has no rpcUrls`);
44
+ return domain.rpcUrls;
45
+ };
46
+ exports.getNetworkDomainRpcUrls = getNetworkDomainRpcUrls;
package/dist/read.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export declare const read: (key: string) => Promise<any>;
2
+ export declare const readFromLocalStorage: (key: string) => Promise<any>;
3
+ export declare const readFromFileSystem: (key: string) => Promise<any>;
4
+ //# sourceMappingURL=read.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read.d.ts","sourceRoot":"","sources":["../src/read.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,IAAI,QAAe,MAAM,iBAIrC,CAAA;AAED,eAAO,MAAM,oBAAoB,QAAe,MAAM,iBAUrD,CAAA;AAED,eAAO,MAAM,kBAAkB,QAAe,MAAM,iBAWnD,CAAA"}
package/dist/read.js ADDED
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ // file: src/read.ts
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
+ return new (P || (P = Promise))(function (resolve, reject) {
29
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
33
+ });
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.readFromFileSystem = exports.readFromLocalStorage = exports.read = void 0;
37
+ const read = (key) => __awaiter(void 0, void 0, void 0, function* () {
38
+ // detect if we are in the browser or in node
39
+ if (typeof window !== 'undefined')
40
+ return (0, exports.readFromLocalStorage)(key);
41
+ else
42
+ return (0, exports.readFromFileSystem)(key);
43
+ });
44
+ exports.read = read;
45
+ const readFromLocalStorage = (key) => __awaiter(void 0, void 0, void 0, function* () {
46
+ if (typeof window !== 'undefined') {
47
+ // read from local storage
48
+ const value = localStorage.getItem(key);
49
+ try {
50
+ return value ? JSON.parse(value) : null;
51
+ }
52
+ catch (error) {
53
+ throw new Error('Failed to parse data from localStorage: ' + error);
54
+ }
55
+ }
56
+ else
57
+ throw new Error('This function can only be used in the browser');
58
+ });
59
+ exports.readFromLocalStorage = readFromLocalStorage;
60
+ const readFromFileSystem = (key) => __awaiter(void 0, void 0, void 0, function* () {
61
+ if (typeof window === 'undefined') {
62
+ // read from file system
63
+ const fs = yield Promise.resolve().then(() => __importStar(require('fs/promises')));
64
+ try {
65
+ const data = yield fs.readFile(key, { encoding: 'utf-8' });
66
+ return JSON.parse(data);
67
+ }
68
+ catch (error) {
69
+ throw new Error('Failed to read or parse file: ' + error);
70
+ }
71
+ }
72
+ else
73
+ throw new Error('This function can only be used in node');
74
+ });
75
+ exports.readFromFileSystem = readFromFileSystem;
package/dist/save.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export declare const save: (key: string, value: any) => Promise<void>;
2
+ export declare const saveOnLocalStorage: (key: string, value: any) => Promise<void>;
3
+ export declare const saveOnFileSystem: (key: string, value: any) => Promise<void>;
4
+ //# sourceMappingURL=save.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"save.d.ts","sourceRoot":"","sources":["../src/save.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,IAAI,QAAe,MAAM,SAAS,GAAG,kBAIjD,CAAA;AAED,eAAO,MAAM,kBAAkB,QAAe,MAAM,SAAS,GAAG,kBAK/D,CAAA;AAED,eAAO,MAAM,gBAAgB,QAAe,MAAM,SAAS,GAAG,kBAQ7D,CAAA"}
package/dist/save.js ADDED
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ // file: src/save.ts
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
+ return new (P || (P = Promise))(function (resolve, reject) {
29
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
33
+ });
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.saveOnFileSystem = exports.saveOnLocalStorage = exports.save = void 0;
37
+ const save = (key, value) => __awaiter(void 0, void 0, void 0, function* () {
38
+ // detect if we are in the browser or in node
39
+ if (typeof window !== 'undefined')
40
+ yield (0, exports.saveOnLocalStorage)(key, value);
41
+ else
42
+ yield (0, exports.saveOnFileSystem)(key, value);
43
+ });
44
+ exports.save = save;
45
+ const saveOnLocalStorage = (key, value) => __awaiter(void 0, void 0, void 0, function* () {
46
+ if (typeof window !== 'undefined')
47
+ // save on local storage
48
+ localStorage.setItem(key, JSON.stringify(value));
49
+ else
50
+ throw new Error('This function can only be used in the browser');
51
+ });
52
+ exports.saveOnLocalStorage = saveOnLocalStorage;
53
+ const saveOnFileSystem = (key, value) => __awaiter(void 0, void 0, void 0, function* () {
54
+ if (typeof window === 'undefined') {
55
+ // save on file system
56
+ const fs = yield Promise.resolve().then(() => __importStar(require('fs/promises')));
57
+ // Check if value is already a string to avoid unnecessary JSON string conversion
58
+ const data = typeof value === 'string' ? value : JSON.stringify(value);
59
+ yield fs.writeFile(key, JSON.stringify(data));
60
+ }
61
+ else
62
+ throw new Error('This function can only be used in node');
63
+ });
64
+ exports.saveOnFileSystem = saveOnFileSystem;
@@ -0,0 +1,3 @@
1
+ export * from './network';
2
+ export * from './wallet';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ // file: src/types/index.ts
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ __exportStar(require("./network"), exports);
19
+ __exportStar(require("./wallet"), exports);
@@ -0,0 +1,26 @@
1
+ export type Explorer = {
2
+ name: string;
3
+ url: string;
4
+ };
5
+ export type Domain = {
6
+ id: string;
7
+ name: string;
8
+ rpcUrls: string[];
9
+ };
10
+ export type Network = {
11
+ id: string;
12
+ name: string;
13
+ rpcUrls: string[];
14
+ explorer: Explorer[];
15
+ domains: Domain[];
16
+ isTestnet?: boolean;
17
+ isLocalhost?: boolean;
18
+ };
19
+ export type NetworkInput = {
20
+ networkId?: string;
21
+ } | undefined;
22
+ export type DomainInput = {
23
+ networkId?: string;
24
+ domainId: string;
25
+ };
26
+ //# sourceMappingURL=network.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/types/network.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,QAAQ,GAAG;IACrB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAED,MAAM,MAAM,MAAM,GAAG;IACnB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,EAAE,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,OAAO,GAAG;IACpB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,QAAQ,EAAE,QAAQ,EAAE,CAAA;IACpB,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,WAAW,CAAC,EAAE,OAAO,CAAA;CACtB,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAAA;AAE7D,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // file: src/types/network.ts
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ export type Mnemonic = {
2
+ mnemonic: string;
3
+ };
4
+ export type URI = {
5
+ uri: string;
6
+ };
7
+ export type AppName = {
8
+ appName: string;
9
+ };
10
+ export type MnemonicOrURI = Mnemonic | URI;
11
+ //# sourceMappingURL=wallet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet.d.ts","sourceRoot":"","sources":["../../src/types/wallet.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,GAAG,GAAG;IAChB,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAED,MAAM,MAAM,OAAO,GAAG;IACpB,OAAO,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,GAAG,CAAA"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // file: src/types/wallet.ts
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,14 @@
1
+ import type { ApiPromise } from '@polkadot/api';
2
+ import type { InjectedAccountWithMeta } from '@polkadot/extension-inject/types';
3
+ import type { KeyringPair } from '@polkadot/keyring/types';
4
+ import type { AppName, DomainInput, MnemonicOrURI, NetworkInput } from './types';
5
+ export declare const setupWallet: (input: MnemonicOrURI) => Promise<KeyringPair>;
6
+ export type ActivateWalletInput = (NetworkInput | DomainInput) & MnemonicOrURI & AppName;
7
+ export type WalletActivated = {
8
+ api: ApiPromise;
9
+ accounts: InjectedAccountWithMeta[] & KeyringPair[];
10
+ };
11
+ export declare const activateWallet: (input: ActivateWalletInput) => Promise<WalletActivated>;
12
+ export declare const mockWallets: (network?: NetworkInput | DomainInput) => Promise<WalletActivated[]>;
13
+ export declare const getMockWallet: (name: string, wallets: WalletActivated[]) => WalletActivated;
14
+ //# sourceMappingURL=wallet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet.d.ts","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAE/C,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,kCAAkC,CAAA;AAC/E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAK1D,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAY,aAAa,EAAE,YAAY,EAAO,MAAM,SAAS,CAAA;AAE/F,eAAO,MAAM,WAAW,UAAiB,aAAa,KAAG,QAAQ,WAAW,CAc3E,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG,CAAC,YAAY,GAAG,WAAW,CAAC,GAAG,aAAa,GAAG,OAAO,CAAA;AACxF,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,EAAE,UAAU,CAAA;IACf,QAAQ,EAAE,uBAAuB,EAAE,GAAG,WAAW,EAAE,CAAA;CACpD,CAAA;AAED,eAAO,MAAM,cAAc,UAAiB,mBAAmB,KAAG,QAAQ,eAAe,CA0BxF,CAAA;AAED,eAAO,MAAM,WAAW,aACb,YAAY,GAAG,WAAW,KAClC,QAAQ,eAAe,EAAE,CAQzB,CAAA;AAEH,eAAO,MAAM,aAAa,SAAU,MAAM,WAAW,eAAe,EAAE,KAAG,eAClB,CAAA"}
package/dist/wallet.js ADDED
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ // file: src/wallet.ts
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
+ return new (P || (P = Promise))(function (resolve, reject) {
29
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
33
+ });
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.getMockWallet = exports.mockWallets = exports.activateWallet = exports.setupWallet = void 0;
37
+ const api_1 = require("@polkadot/api");
38
+ const util_crypto_1 = require("@polkadot/util-crypto");
39
+ const api_2 = require("./api");
40
+ const network_1 = require("./constants/network");
41
+ const wallet_1 = require("./constants/wallet");
42
+ const setupWallet = (input) => __awaiter(void 0, void 0, void 0, function* () {
43
+ yield (0, util_crypto_1.cryptoWaitReady)();
44
+ const keyring = new api_1.Keyring({ type: 'sr25519' });
45
+ let pair;
46
+ if (input.uri) {
47
+ // Treat as uri
48
+ pair = keyring.addFromUri(input.uri);
49
+ }
50
+ else if (input.mnemonic) {
51
+ // Treat as mnemonic
52
+ pair = keyring.addFromUri(input.mnemonic);
53
+ }
54
+ else
55
+ throw new Error('Invalid mnemonic or private key');
56
+ return pair;
57
+ });
58
+ exports.setupWallet = setupWallet;
59
+ const activateWallet = (input) => __awaiter(void 0, void 0, void 0, function* () {
60
+ // Create the API instance
61
+ const api = input.domainId === undefined
62
+ ? yield (0, api_2.activate)(input)
63
+ : yield (0, api_2.activateDomain)(input);
64
+ const accounts = [];
65
+ if (typeof window !== 'undefined') {
66
+ const { web3Enable, web3Accounts } = yield Promise.resolve().then(() => __importStar(require('@polkadot/extension-dapp')));
67
+ // Enable Polkadot.js extension in the browser
68
+ yield web3Enable(input.appName || 'Auto');
69
+ // Get the list of accounts from the extension
70
+ const allAccounts = yield web3Accounts();
71
+ accounts.push(...allAccounts);
72
+ if (allAccounts.length === 0)
73
+ console.warn('No accounts found in the Polkadot.js extension');
74
+ }
75
+ else if (input.mnemonic || input.uri) {
76
+ // Attach the wallet in a node environment
77
+ const account = yield (0, exports.setupWallet)(input);
78
+ accounts.push(account);
79
+ }
80
+ else
81
+ throw new Error('No wallet provided');
82
+ return { api, accounts };
83
+ });
84
+ exports.activateWallet = activateWallet;
85
+ const mockWallets = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (network = { networkId: network_1.defaultNetwork.id }) {
86
+ return yield Promise.all(wallet_1.mockURIs.map((uri) => (0, exports.activateWallet)(Object.assign(Object.assign({}, network), { uri }))));
87
+ });
88
+ exports.mockWallets = mockWallets;
89
+ const getMockWallet = (name, wallets) => wallets[Object.values(wallet_1.mockURIs).indexOf(`//${name}`)];
90
+ exports.getMockWallet = getMockWallet;
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@autonomys/auto-utils",
3
+ "version": "0.1.1",
4
+ "main": "dist/index.js",
5
+ "scripts": {
6
+ "build": "tsc",
7
+ "clean": "rm -rf dist",
8
+ "format": "prettier --write \"src/**/*.ts\"",
9
+ "test": "jest"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "README.md"
14
+ ],
15
+ "dependencies": {
16
+ "@polkadot/api": "^11.2.1",
17
+ "@polkadot/extension-dapp": "^0.47.5",
18
+ "fs": "^0.0.1-security"
19
+ },
20
+ "devDependencies": {
21
+ "@types/jest": "^29.5.12",
22
+ "eslint": "^8.57.0",
23
+ "jest": "^29.7.0",
24
+ "prettier": "^3.2.5",
25
+ "ts-jest": "^29.1.4",
26
+ "typescript": "^5.4.5"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/subspace/auto-sdk"
31
+ },
32
+ "author": {
33
+ "name": "Autonomys",
34
+ "url": "https://www.autonomys.net"
35
+ },
36
+ "browser": {
37
+ "fs": false
38
+ },
39
+ "gitHead": "6de838d2d4b144450cc27d5fc05536904735fcc1"
40
+ }