zano_web3 2.3.0 → 2.5.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/README.md CHANGED
@@ -1,121 +1,121 @@
1
-
2
- # ZanoWallet
3
-
4
- `zano_web3` is a TypeScript library for interacting with the ZanoWallet extension in the browser. It allows you to connect to a user's ZanoWallet, handle authentication, and manage wallet credentials.
5
-
6
- ## Features
7
-
8
- - **Easy Integration**: Simplifies the process of connecting to the ZanoWallet extension.
9
- - **Local Storage Support**: Optionally store wallet credentials in local storage.
10
- - **Customizable**: Offers hooks for various connection lifecycle events.
11
- - **Error Handling**: Provides a structured way to handle errors during the connection process.
12
-
13
- ## Installation
14
-
15
- To install `zano_web3`, use npm or yarn:
16
-
17
- ```bash
18
- npm install zano_web3
19
- ```
20
-
21
- or
22
-
23
- ```bash
24
- yarn add zano_web3
25
- ```
26
-
27
- ## Usage
28
-
29
- ### Importing the Library
30
-
31
- ```typescript
32
- import ZanoWallet from 'zano_web3';
33
- ```
34
-
35
- ### Creating a ZanoWallet Instance
36
-
37
- To create a `ZanoWallet` instance, you need to provide configuration options via the `ZanoWalletParams` interface.
38
-
39
- ```typescript
40
- const zanoWallet = new ZanoWallet({
41
- authPath: '/api/auth', // Custom server path for authentication
42
- useLocalStorage: true, // Store wallet credentials in local storage (default: true)
43
- aliasRequired: false, // Whether an alias is required (optional)
44
- customLocalStorageKey: 'myWalletKey', // Custom key for local storage (optional)
45
- customNonce: 'customNonceValue', // Custom nonce for signing (optional)
46
- disableServerRequest: false, // Disable server request after signing (optional)
47
-
48
- onConnectStart: () => {
49
- console.log('Connecting to ZanoWallet...');
50
- },
51
- onConnectEnd: (data) => {
52
- console.log('Connected:', data);
53
- },
54
- onConnectError: (error) => {
55
- console.error('Connection error:', error);
56
- },
57
- beforeConnect: async () => {
58
- console.log('Preparing to connect...');
59
- },
60
- onLocalConnectEnd: (data) => {
61
- console.log('Local connection established:', data);
62
- }
63
- });
64
- ```
65
-
66
- ### Connecting to ZanoWallet
67
-
68
- To initiate the connection process, call the `connect` method:
69
-
70
- ```typescript
71
- await zanoWallet.connect();
72
- ```
73
-
74
- ### Handling Wallet Credentials
75
-
76
- You can manually manage wallet credentials using `getSavedWalletCredentials` and `setWalletCredentials` methods:
77
-
78
- ```typescript
79
- const credentials = zanoWallet.getSavedWalletCredentials();
80
- if (credentials) {
81
- console.log('Stored credentials:', credentials);
82
- }
83
-
84
- zanoWallet.setWalletCredentials({
85
- nonce: 'newNonce',
86
- signature: 'newSignature',
87
- publicKey: 'newPublicKey'
88
- });
89
- ```
90
-
91
- ## Using the `useZanoWallet` Hook
92
-
93
- The `useZanoWallet` hook is a custom React hook provided by the `zano_web3` library. It simplifies the process of interacting with the ZanoWallet extension in a React application.
94
-
95
- This hook is designed to handle server-side rendering (SSR) limitations by ensuring that it only runs on the client-side. This means that any code using the `useZanoWallet` hook will not be executed during server-side rendering, but will work as expected once the application is running in the browser.
96
-
97
- To use the `useZanoWallet` hook, you can import it from the `zano_web3` library and call it within a functional component:
98
-
99
- ```typescript
100
- import { useZanoWallet } from 'zano_web3';
101
-
102
- function MyComponent() {
103
-
104
- const wallet = useZanoWallet({
105
- // same params as for new ZanoWallet
106
- });
107
-
108
- return (
109
- <div>Your component...</div>
110
- );
111
- }
112
- ```
113
-
114
-
115
- ## Requirements
116
-
117
- - ZanoWallet browser extension must be installed.
118
-
119
- ## Contributing
120
-
121
- If you find any issues or want to contribute, please create a pull request or submit an issue.
1
+
2
+ # ZanoWallet
3
+
4
+ `zano_web3` is a TypeScript library for interacting with the ZanoWallet extension in the browser. It allows you to connect to a user's ZanoWallet, handle authentication, and manage wallet credentials.
5
+
6
+ ## Features
7
+
8
+ - **Easy Integration**: Simplifies the process of connecting to the ZanoWallet extension.
9
+ - **Local Storage Support**: Optionally store wallet credentials in local storage.
10
+ - **Customizable**: Offers hooks for various connection lifecycle events.
11
+ - **Error Handling**: Provides a structured way to handle errors during the connection process.
12
+
13
+ ## Installation
14
+
15
+ To install `zano_web3`, use npm or yarn:
16
+
17
+ ```bash
18
+ npm install zano_web3
19
+ ```
20
+
21
+ or
22
+
23
+ ```bash
24
+ yarn add zano_web3
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ### Importing the Library
30
+
31
+ ```typescript
32
+ import ZanoWallet from 'zano_web3';
33
+ ```
34
+
35
+ ### Creating a ZanoWallet Instance
36
+
37
+ To create a `ZanoWallet` instance, you need to provide configuration options via the `ZanoWalletParams` interface.
38
+
39
+ ```typescript
40
+ const zanoWallet = new ZanoWallet({
41
+ authPath: '/api/auth', // Custom server path for authentication
42
+ useLocalStorage: true, // Store wallet credentials in local storage (default: true)
43
+ aliasRequired: false, // Whether an alias is required (optional)
44
+ customLocalStorageKey: 'myWalletKey', // Custom key for local storage (optional)
45
+ customNonce: 'customNonceValue', // Custom nonce for signing (optional)
46
+ disableServerRequest: false, // Disable server request after signing (optional)
47
+
48
+ onConnectStart: () => {
49
+ console.log('Connecting to ZanoWallet...');
50
+ },
51
+ onConnectEnd: (data) => {
52
+ console.log('Connected:', data);
53
+ },
54
+ onConnectError: (error) => {
55
+ console.error('Connection error:', error);
56
+ },
57
+ beforeConnect: async () => {
58
+ console.log('Preparing to connect...');
59
+ },
60
+ onLocalConnectEnd: (data) => {
61
+ console.log('Local connection established:', data);
62
+ }
63
+ });
64
+ ```
65
+
66
+ ### Connecting to ZanoWallet
67
+
68
+ To initiate the connection process, call the `connect` method:
69
+
70
+ ```typescript
71
+ await zanoWallet.connect();
72
+ ```
73
+
74
+ ### Handling Wallet Credentials
75
+
76
+ You can manually manage wallet credentials using `getSavedWalletCredentials` and `setWalletCredentials` methods:
77
+
78
+ ```typescript
79
+ const credentials = zanoWallet.getSavedWalletCredentials();
80
+ if (credentials) {
81
+ console.log('Stored credentials:', credentials);
82
+ }
83
+
84
+ zanoWallet.setWalletCredentials({
85
+ nonce: 'newNonce',
86
+ signature: 'newSignature',
87
+ publicKey: 'newPublicKey'
88
+ });
89
+ ```
90
+
91
+ ## Using the `useZanoWallet` Hook
92
+
93
+ The `useZanoWallet` hook is a custom React hook provided by the `zano_web3` library. It simplifies the process of interacting with the ZanoWallet extension in a React application.
94
+
95
+ This hook is designed to handle server-side rendering (SSR) limitations by ensuring that it only runs on the client-side. This means that any code using the `useZanoWallet` hook will not be executed during server-side rendering, but will work as expected once the application is running in the browser.
96
+
97
+ To use the `useZanoWallet` hook, you can import it from the `zano_web3` library and call it within a functional component:
98
+
99
+ ```typescript
100
+ import { useZanoWallet } from 'zano_web3';
101
+
102
+ function MyComponent() {
103
+
104
+ const wallet = useZanoWallet({
105
+ // same params as for new ZanoWallet
106
+ });
107
+
108
+ return (
109
+ <div>Your component...</div>
110
+ );
111
+ }
112
+ ```
113
+
114
+
115
+ ## Requirements
116
+
117
+ - ZanoWallet browser extension must be installed.
118
+
119
+ ## Contributing
120
+
121
+ If you find any issues or want to contribute, please create a pull request or submit an issue.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import zanoWallet from "./src/zanoWallet";
2
2
  import { useZanoWallet } from "./src/hooks";
3
3
  export { useZanoWallet };
4
+ import validateWallet from "./src/server";
5
+ export { validateWallet };
4
6
  export default zanoWallet;
package/dist/index.js CHANGED
@@ -1,10 +1,7 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.useZanoWallet = void 0;
7
- const zanoWallet_1 = __importDefault(require("./src/zanoWallet"));
8
- const hooks_1 = require("./src/hooks");
9
- Object.defineProperty(exports, "useZanoWallet", { enumerable: true, get: function () { return hooks_1.useZanoWallet; } });
10
- exports.default = zanoWallet_1.default;
1
+ import zanoWallet from "./src/zanoWallet";
2
+ import { useZanoWallet } from "./src/hooks";
3
+ export { useZanoWallet };
4
+ import validateWallet from "./src/server";
5
+ export { validateWallet };
6
+ export default zanoWallet;
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,kBAAkB,CAAC;AAE1C,OAAO,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAC,aAAa,EAAC,CAAC;AAEvB,OAAO,cAAc,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAC,cAAc,EAAC,CAAC;AAExB,eAAe,UAAU,CAAC"}
package/dist/src/hooks.js CHANGED
@@ -1,18 +1,14 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.useZanoWallet = useZanoWallet;
7
- const zanoWallet_1 = __importDefault(require("./zanoWallet"));
8
- const react_1 = require("react");
1
+ import ZanoWallet from './zanoWallet';
2
+ import { useEffect, useState } from 'react';
9
3
  function useZanoWallet(params) {
10
- const [zanoWallet, setZanoWallet] = (0, react_1.useState)(null);
11
- (0, react_1.useEffect)(() => {
4
+ const [zanoWallet, setZanoWallet] = useState(null);
5
+ useEffect(() => {
12
6
  if (typeof window === 'undefined') {
13
7
  return;
14
8
  }
15
- setZanoWallet(new zanoWallet_1.default(params));
9
+ setZanoWallet(new ZanoWallet(params));
16
10
  }, []);
17
11
  return zanoWallet;
18
12
  }
13
+ export { useZanoWallet };
14
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,UAAgC,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAE5C,SAAS,aAAa,CAAC,MAAwB;IAC3C,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAoB,IAAI,CAAC,CAAC;IAEtE,SAAS,CAAC,GAAG,EAAE;QACX,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YAChC,OAAO;QACX,CAAC;QAED,aAAa,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1C,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,UAAU,CAAC;AACtB,CAAC;AAED,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -0,0 +1,14 @@
1
+ interface BaseAuthData {
2
+ address: string;
3
+ signature: string;
4
+ message: string;
5
+ }
6
+ interface AliasAuth extends BaseAuthData {
7
+ alias: string;
8
+ }
9
+ interface PkeyAuth extends BaseAuthData {
10
+ pkey: string;
11
+ }
12
+ type AuthData = AliasAuth | PkeyAuth;
13
+ declare function validateWallet(rpcUrl: string, authData: AuthData): Promise<boolean>;
14
+ export default validateWallet;
@@ -0,0 +1,46 @@
1
+ import axios from 'axios';
2
+ async function validateWallet(rpcUrl, authData) {
3
+ async function fetchZanoApi(method, params) {
4
+ return await axios.post('http://195.201.107.230:33340/json_rpc', {
5
+ "id": 0,
6
+ "jsonrpc": "2.0",
7
+ "method": method,
8
+ "params": params,
9
+ }).then(res => res.data);
10
+ }
11
+ const { message, address, signature } = authData;
12
+ const alias = authData.alias || undefined;
13
+ const pkey = authData.pkey || undefined;
14
+ if (!message || (!alias && !pkey) || !signature) {
15
+ return false;
16
+ }
17
+ const validationParams = {
18
+ "buff": Buffer.from(message).toString("base64"),
19
+ "sig": signature
20
+ };
21
+ if (alias) {
22
+ validationParams['alias'] = alias;
23
+ }
24
+ else {
25
+ validationParams['pkey'] = pkey;
26
+ }
27
+ const response = await fetchZanoApi('validate_signature', validationParams);
28
+ const valid = response?.result?.status === 'OK';
29
+ if (!valid) {
30
+ return false;
31
+ }
32
+ if (alias) {
33
+ const aliasDetailsResponse = await fetchZanoApi('get_alias_details', {
34
+ "alias": alias,
35
+ });
36
+ const aliasDetails = aliasDetailsResponse?.result?.alias_details;
37
+ const aliasAddress = aliasDetails?.address;
38
+ const addressValid = !!aliasAddress && aliasAddress === address;
39
+ if (!addressValid) {
40
+ return false;
41
+ }
42
+ }
43
+ return valid;
44
+ }
45
+ export default validateWallet;
46
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AA2B1B,KAAK,UAAU,cAAc,CAAC,MAAc,EAAE,QAAkB;IAE5D,KAAK,UAAU,YAAY,CAAC,MAAc,EAAE,MAAW;QACnD,OAAO,MAAM,KAAK,CAAC,IAAI,CACnB,uCAAuC,EACvC;YACI,IAAI,EAAE,CAAC;YACP,SAAS,EAAE,KAAK;YAChB,QAAQ,EAAE,MAAM;YAChB,QAAQ,EAAE,MAAM;SACnB,CACJ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAED,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,QAAQ,CAAC;IAEjD,MAAM,KAAK,GAAI,QAAsB,CAAC,KAAK,IAAI,SAAS,CAAC;IACzD,MAAM,IAAI,GAAI,QAAqB,CAAC,IAAI,IAAI,SAAS,CAAC;IAEtD,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;QAC9C,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,MAAM,gBAAgB,GAAqB;QACvC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC;QAC/C,KAAK,EAAE,SAAS;KACnB,CAAC;IAEF,IAAI,KAAK,EAAE,CAAC;QACR,gBAAgB,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC;IACtC,CAAC;SAAM,CAAC;QACJ,gBAAgB,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC;IACpC,CAAC;IAED,MAAM,QAAQ,GAAG,MAAM,YAAY,CAC/B,oBAAoB,EACpB,gBAAgB,CACnB,CAAC;IAEF,MAAM,KAAK,GAAG,QAAQ,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAEhD,IAAI,CAAC,KAAK,EAAE,CAAC;QACT,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,IAAI,KAAK,EAAE,CAAC;QACR,MAAM,oBAAoB,GAAG,MAAM,YAAY,CAC3C,mBAAmB,EACnB;YACI,OAAO,EAAE,KAAK;SACjB,CACJ,CAAC;QAEF,MAAM,YAAY,GAAG,oBAAoB,EAAE,MAAM,EAAE,aAAa,CAAC;QACjE,MAAM,YAAY,GAAG,YAAY,EAAE,OAAO,CAAC;QAE3C,MAAM,YAAY,GAAG,CAAC,CAAC,YAAY,IAAI,YAAY,KAAK,OAAO,CAAC;QAEhE,IAAI,CAAC,YAAY,EAAE,CAAC;YAChB,OAAO,KAAK,CAAC;QACjB,CAAC;IACL,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC;AAED,eAAe,cAAc,CAAC"}
@@ -1,18 +1,10 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const uuid_1 = require("uuid");
1
+ import { v4 as uuidv4 } from 'uuid';
13
2
  class ZanoWallet {
3
+ DEFAULT_LOCAL_STORAGE_KEY = "wallet";
4
+ localStorageKey;
5
+ params;
6
+ zanoWallet;
14
7
  constructor(params) {
15
- this.DEFAULT_LOCAL_STORAGE_KEY = "wallet";
16
8
  if (typeof window === 'undefined') {
17
9
  throw new Error('ZanoWallet can only be used in the browser');
18
10
  }
@@ -38,7 +30,7 @@ class ZanoWallet {
38
30
  try {
39
31
  return JSON.parse(savedWallet);
40
32
  }
41
- catch (_a) {
33
+ catch {
42
34
  return undefined;
43
35
  }
44
36
  }
@@ -50,84 +42,85 @@ class ZanoWallet {
50
42
  localStorage.removeItem(this.localStorageKey);
51
43
  }
52
44
  }
53
- connect() {
54
- return __awaiter(this, void 0, void 0, function* () {
55
- var _a;
56
- if (this.params.beforeConnect) {
57
- yield this.params.beforeConnect();
58
- }
59
- if (this.params.onConnectStart) {
60
- this.params.onConnectStart();
61
- }
62
- const walletData = (yield window.zano.request('GET_WALLET_DATA')).data;
63
- if (!(walletData === null || walletData === void 0 ? void 0 : walletData.address)) {
64
- return this.handleError({ message: 'Companion is offline' });
65
- }
66
- if (!(walletData === null || walletData === void 0 ? void 0 : walletData.alias) && this.params.aliasRequired) {
67
- return this.handleError({ message: 'Alias not found' });
68
- }
69
- let nonce = "";
70
- let signature = "";
71
- let publicKey = "";
72
- const existingWallet = this.params.useLocalStorage ? this.getSavedWalletCredentials() : undefined;
73
- if (existingWallet) {
74
- nonce = existingWallet.nonce;
75
- signature = existingWallet.signature;
76
- publicKey = existingWallet.publicKey;
45
+ async connect() {
46
+ if (this.params.beforeConnect) {
47
+ await this.params.beforeConnect();
48
+ }
49
+ if (this.params.onConnectStart) {
50
+ this.params.onConnectStart();
51
+ }
52
+ const walletData = (await window.zano.request('GET_WALLET_DATA')).data;
53
+ if (!walletData?.address) {
54
+ return this.handleError({ message: 'Companion is offline' });
55
+ }
56
+ if (!walletData?.alias && this.params.aliasRequired) {
57
+ return this.handleError({ message: 'Alias not found' });
58
+ }
59
+ let nonce = "";
60
+ let signature = "";
61
+ let publicKey = "";
62
+ const existingWallet = this.params.useLocalStorage ? this.getSavedWalletCredentials() : undefined;
63
+ if (existingWallet) {
64
+ nonce = existingWallet.nonce;
65
+ signature = existingWallet.signature;
66
+ publicKey = existingWallet.publicKey;
67
+ }
68
+ else {
69
+ const generatedNonce = this.params.customNonce || uuidv4();
70
+ const signResult = await this.zanoWallet.request('REQUEST_MESSAGE_SIGN', {
71
+ message: generatedNonce
72
+ }, null);
73
+ if (!signResult?.data?.result) {
74
+ return this.handleError({ message: 'Failed to sign message' });
77
75
  }
78
- else {
79
- const generatedNonce = this.params.customNonce || (0, uuid_1.v4)();
80
- const signResult = yield this.zanoWallet.request('REQUEST_MESSAGE_SIGN', {
81
- message: generatedNonce
82
- }, null);
83
- if (!((_a = signResult === null || signResult === void 0 ? void 0 : signResult.data) === null || _a === void 0 ? void 0 : _a.result)) {
84
- return this.handleError({ message: 'Failed to sign message' });
85
- }
86
- nonce = generatedNonce;
87
- signature = signResult.data.result.sig;
88
- publicKey = signResult.data.result.pkey;
76
+ nonce = generatedNonce;
77
+ signature = signResult.data.result.sig;
78
+ publicKey = signResult.data.result.pkey;
79
+ }
80
+ const serverData = {
81
+ alias: walletData.alias,
82
+ address: walletData.address,
83
+ signature,
84
+ publicKey,
85
+ message: nonce,
86
+ isSavedData: !!existingWallet
87
+ };
88
+ if (this.params.onLocalConnectEnd) {
89
+ this.params.onLocalConnectEnd(serverData);
90
+ }
91
+ if (!this.params.disableServerRequest) {
92
+ const result = await fetch(this.params.customServerPath || "/api/auth", {
93
+ method: "POST",
94
+ headers: {
95
+ "Content-Type": "application/json",
96
+ },
97
+ body: JSON.stringify({
98
+ data: serverData
99
+ })
100
+ })
101
+ .then(res => res.json())
102
+ .catch((e) => ({
103
+ success: false,
104
+ error: e.message
105
+ }));
106
+ if (!result?.success || !result?.data) {
107
+ return this.handleError({ message: result.error });
89
108
  }
90
- const serverData = {
91
- alias: walletData.alias,
92
- address: walletData.address,
93
- signature,
94
- publicKey,
95
- message: nonce,
96
- isSavedData: !!existingWallet
97
- };
98
- if (this.params.onLocalConnectEnd) {
99
- this.params.onLocalConnectEnd(serverData);
109
+ if (!existingWallet && this.params.useLocalStorage) {
110
+ this.setWalletCredentials({
111
+ publicKey,
112
+ signature,
113
+ nonce
114
+ });
100
115
  }
101
- if (!this.params.disableServerRequest) {
102
- const result = yield fetch(this.params.customServerPath || "/api/auth", {
103
- method: "POST",
104
- headers: {
105
- "Content-Type": "application/json",
106
- },
107
- body: JSON.stringify({
108
- data: serverData
109
- })
110
- })
111
- .then(res => res.json())
112
- .catch((e) => ({
113
- success: false,
114
- error: e.message
115
- }));
116
- if (!(result === null || result === void 0 ? void 0 : result.success) || !(result === null || result === void 0 ? void 0 : result.data)) {
117
- return this.handleError({ message: result.error });
118
- }
119
- if (!existingWallet && this.params.useLocalStorage) {
120
- this.setWalletCredentials({
121
- publicKey,
122
- signature,
123
- nonce
124
- });
125
- }
126
- if (this.params.onConnectEnd) {
127
- this.params.onConnectEnd(Object.assign(Object.assign({}, serverData), { token: result.data.token }));
128
- }
116
+ if (this.params.onConnectEnd) {
117
+ this.params.onConnectEnd({
118
+ ...serverData,
119
+ token: result.data.token
120
+ });
129
121
  }
130
- });
122
+ }
131
123
  }
132
124
  }
133
- exports.default = ZanoWallet;
125
+ export default ZanoWallet;
126
+ //# sourceMappingURL=zanoWallet.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zanoWallet.js","sourceRoot":"","sources":["../../src/zanoWallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAmCpC,MAAM,UAAU;IAEJ,yBAAyB,GAAG,QAAQ,CAAC;IACrC,eAAe,CAAS;IAExB,MAAM,CAAmB;IACzB,UAAU,CAAmB;IAErC,YAAY,MAAwB;QAEhC,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YAChC,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;QAClE,CAAC;QAED,IAAI,CAAG,MAAiC,CAAC,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AAC,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;YACvD,CAAC;YAED,IAAI,CAAC,cAAc,IAAI,IAAI,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;gBACjD,IAAI,CAAC,oBAAoB,CAAC;oBACtB,SAAS;oBACT,SAAS;oBACT,KAAK;iBACR,CAAC,CAAC;YACP,CAAC;YAED,IAAI,IAAI,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC;gBAC3B,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC;oBACrB,GAAG,UAAU;oBACb,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK;iBAC3B,CAAC,CAAC;YACP,CAAC;QACL,CAAC;IACL,CAAC;CACJ;AAED,eAAe,UAAU,CAAC"}
package/index.ts CHANGED
@@ -3,4 +3,7 @@ import zanoWallet from "./src/zanoWallet";
3
3
  import {useZanoWallet} from "./src/hooks";
4
4
  export {useZanoWallet};
5
5
 
6
+ import validateWallet from "./src/server";
7
+ export {validateWallet};
8
+
6
9
  export default zanoWallet;
package/package.json CHANGED
@@ -1,36 +1,38 @@
1
- {
2
- "name": "zano_web3",
3
- "version": "2.3.0",
4
- "description": "",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "scripts": {
8
- "build": "tsc"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/jejolare/zano_web3.git"
13
- },
14
- "keywords": [
15
- "zano",
16
- "web3",
17
- "crypto",
18
- "blockchain",
19
- "wallet"
20
- ],
21
- "author": "",
22
- "license": "ISC",
23
- "devDependencies": {
24
- "@types/react": "^18.3.3",
25
- "@types/uuid": "^10.0.0",
26
- "typescript": "^5.5.4"
27
- },
28
- "dependencies": {
29
- "react": "^18.3.1",
30
- "uuid": "^10.0.0"
31
- },
32
- "bugs": {
33
- "url": "https://github.com/jejolare/zano_web3/issues"
34
- },
35
- "homepage": "https://github.com/jejolare/zano_web3#readme"
36
- }
1
+ {
2
+ "name": "zano_web3",
3
+ "version": "2.5.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/hyle-team/zano_web3.git"
13
+ },
14
+ "keywords": [
15
+ "zano",
16
+ "web3",
17
+ "crypto",
18
+ "blockchain",
19
+ "wallet"
20
+ ],
21
+ "author": "",
22
+ "license": "ISC",
23
+ "devDependencies": {
24
+ "@types/node": "^20.14.12",
25
+ "@types/react": "^18.3.3",
26
+ "@types/uuid": "^10.0.0",
27
+ "typescript": "^5.5.4"
28
+ },
29
+ "dependencies": {
30
+ "axios": "^1.7.2",
31
+ "react": "^18.3.1",
32
+ "uuid": "^10.0.0"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/hyle-team/zano_web3/issues"
36
+ },
37
+ "homepage": "https://github.com/hyle-team/zano_web3#readme"
38
+ }
package/src/server.ts ADDED
@@ -0,0 +1,94 @@
1
+ import axios from 'axios';
2
+
3
+ interface BaseAuthData {
4
+ address: string;
5
+ signature: string;
6
+ message: string;
7
+ }
8
+
9
+ interface AliasAuth extends BaseAuthData {
10
+ alias: string;
11
+ }
12
+
13
+ interface PkeyAuth extends BaseAuthData {
14
+ pkey: string;
15
+ }
16
+
17
+ type AuthData = AliasAuth | PkeyAuth;
18
+
19
+
20
+ interface ValidationParams {
21
+ buff: string;
22
+ sig: string;
23
+ alias?: string;
24
+ pkey?: string;
25
+ }
26
+
27
+
28
+ async function validateWallet(rpcUrl: string, authData: AuthData) {
29
+
30
+ async function fetchZanoApi(method: string, params: any) {
31
+ return await axios.post(
32
+ 'http://195.201.107.230:33340/json_rpc',
33
+ {
34
+ "id": 0,
35
+ "jsonrpc": "2.0",
36
+ "method": method,
37
+ "params": params,
38
+ }
39
+ ).then(res => res.data);
40
+ }
41
+
42
+ const { message, address, signature } = authData;
43
+
44
+ const alias = (authData as AliasAuth).alias || undefined;
45
+ const pkey = (authData as PkeyAuth).pkey || undefined;
46
+
47
+ if (!message || (!alias && !pkey) || !signature) {
48
+ return false;
49
+ }
50
+
51
+ const validationParams: ValidationParams = {
52
+ "buff": Buffer.from(message).toString("base64"),
53
+ "sig": signature
54
+ };
55
+
56
+ if (alias) {
57
+ validationParams['alias'] = alias;
58
+ } else {
59
+ validationParams['pkey'] = pkey;
60
+ }
61
+
62
+ const response = await fetchZanoApi(
63
+ 'validate_signature',
64
+ validationParams
65
+ );
66
+
67
+ const valid = response?.result?.status === 'OK';
68
+
69
+ if (!valid) {
70
+ return false;
71
+ }
72
+
73
+ if (alias) {
74
+ const aliasDetailsResponse = await fetchZanoApi(
75
+ 'get_alias_details',
76
+ {
77
+ "alias": alias,
78
+ }
79
+ );
80
+
81
+ const aliasDetails = aliasDetailsResponse?.result?.alias_details;
82
+ const aliasAddress = aliasDetails?.address;
83
+
84
+ const addressValid = !!aliasAddress && aliasAddress === address;
85
+
86
+ if (!addressValid) {
87
+ return false;
88
+ }
89
+ }
90
+
91
+ return valid;
92
+ }
93
+
94
+ export default validateWallet;
package/tsconfig.json CHANGED
@@ -1,12 +1,17 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "ES6",
4
- "module": "CommonJS",
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
5
  "declaration": true,
6
6
  "outDir": "./dist",
7
7
  "strict": true,
8
8
  "esModuleInterop": true,
9
- "lib": ["ES6", "DOM"]
9
+ "moduleResolution": "node",
10
+ "resolveJsonModule": true,
11
+ "lib": ["ESNext", "DOM"],
12
+ "sourceMap": true,
13
+ "allowSyntheticDefaultImports": true
10
14
  },
11
- "include": ["src/**/*", "index.ts"]
12
- }
15
+ "include": ["src/**/*", "index.ts"],
16
+ "exclude": ["node_modules"]
17
+ }