zano_web3 2.2.0 → 2.4.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 +121 -97
- package/index.ts +3 -0
- package/package.json +38 -36
- package/src/server.ts +94 -0
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -10
- package/dist/src/hooks.d.ts +0 -3
- package/dist/src/hooks.js +0 -18
- package/dist/src/zanoWallet.d.ts +0 -31
- package/dist/src/zanoWallet.js +0 -133
package/README.md
CHANGED
|
@@ -1,97 +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
|
-
##
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,36 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "zano_web3",
|
|
3
|
-
"version": "2.
|
|
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/
|
|
13
|
-
},
|
|
14
|
-
"keywords": [
|
|
15
|
-
"zano",
|
|
16
|
-
"web3",
|
|
17
|
-
"crypto",
|
|
18
|
-
"blockchain",
|
|
19
|
-
"wallet"
|
|
20
|
-
],
|
|
21
|
-
"author": "",
|
|
22
|
-
"license": "ISC",
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"@types/
|
|
25
|
-
"@types/
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "zano_web3",
|
|
3
|
+
"version": "2.4.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/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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;
|
package/dist/src/hooks.d.ts
DELETED
package/dist/src/hooks.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
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");
|
|
9
|
-
function useZanoWallet(params) {
|
|
10
|
-
const [zanoWallet, setZanoWallet] = (0, react_1.useState)(null);
|
|
11
|
-
(0, react_1.useEffect)(() => {
|
|
12
|
-
if (typeof window === 'undefined') {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
setZanoWallet(new zanoWallet_1.default(params));
|
|
16
|
-
}, []);
|
|
17
|
-
return zanoWallet;
|
|
18
|
-
}
|
package/dist/src/zanoWallet.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export interface ZanoWalletParams {
|
|
2
|
-
authPath: string;
|
|
3
|
-
useLocalStorage?: boolean;
|
|
4
|
-
aliasRequired?: boolean;
|
|
5
|
-
customLocalStorageKey?: string;
|
|
6
|
-
customNonce?: string;
|
|
7
|
-
customServerPath?: string;
|
|
8
|
-
disableServerRequest?: boolean;
|
|
9
|
-
onConnectStart?: (...params: any) => any;
|
|
10
|
-
onConnectEnd?: (...params: any) => any;
|
|
11
|
-
onConnectError?: (...params: any) => any;
|
|
12
|
-
beforeConnect?: (...params: any) => any;
|
|
13
|
-
onLocalConnectEnd?: (...params: any) => any;
|
|
14
|
-
}
|
|
15
|
-
interface WalletCredentials {
|
|
16
|
-
nonce: string;
|
|
17
|
-
signature: string;
|
|
18
|
-
publicKey: string;
|
|
19
|
-
}
|
|
20
|
-
declare class ZanoWallet {
|
|
21
|
-
private DEFAULT_LOCAL_STORAGE_KEY;
|
|
22
|
-
private localStorageKey;
|
|
23
|
-
private params;
|
|
24
|
-
private zanoWallet;
|
|
25
|
-
constructor(params: ZanoWalletParams);
|
|
26
|
-
private handleError;
|
|
27
|
-
getSavedWalletCredentials(): WalletCredentials | undefined;
|
|
28
|
-
setWalletCredentials(credentials: WalletCredentials | undefined): void;
|
|
29
|
-
connect(): Promise<void>;
|
|
30
|
-
}
|
|
31
|
-
export default ZanoWallet;
|
package/dist/src/zanoWallet.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
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");
|
|
13
|
-
class ZanoWallet {
|
|
14
|
-
constructor(params) {
|
|
15
|
-
this.DEFAULT_LOCAL_STORAGE_KEY = "wallet";
|
|
16
|
-
if (typeof window === 'undefined') {
|
|
17
|
-
throw new Error('ZanoWallet can only be used in the browser');
|
|
18
|
-
}
|
|
19
|
-
if (!window.zano) {
|
|
20
|
-
throw new Error('ZanoWallet requires the ZanoWallet extension to be installed');
|
|
21
|
-
}
|
|
22
|
-
this.params = params;
|
|
23
|
-
this.zanoWallet = window.zano;
|
|
24
|
-
this.localStorageKey = params.customLocalStorageKey || this.DEFAULT_LOCAL_STORAGE_KEY;
|
|
25
|
-
}
|
|
26
|
-
handleError({ message }) {
|
|
27
|
-
if (this.params.onConnectError) {
|
|
28
|
-
this.params.onConnectError(message);
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
console.error(message);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
getSavedWalletCredentials() {
|
|
35
|
-
const savedWallet = localStorage.getItem(this.localStorageKey);
|
|
36
|
-
if (!savedWallet)
|
|
37
|
-
return undefined;
|
|
38
|
-
try {
|
|
39
|
-
return JSON.parse(savedWallet);
|
|
40
|
-
}
|
|
41
|
-
catch (_a) {
|
|
42
|
-
return undefined;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
setWalletCredentials(credentials) {
|
|
46
|
-
if (credentials) {
|
|
47
|
-
localStorage.setItem(this.localStorageKey, JSON.stringify(credentials));
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
localStorage.removeItem(this.localStorageKey);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
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;
|
|
77
|
-
}
|
|
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;
|
|
89
|
-
}
|
|
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);
|
|
100
|
-
}
|
|
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
|
-
}
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
exports.default = ZanoWallet;
|