zano_web3 2.1.0 → 2.3.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 +24 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +4 -1
- package/dist/src/hooks.d.ts +3 -0
- package/dist/src/hooks.js +18 -0
- package/dist/src/zanoWallet.d.ts +1 -1
- package/dist/src/zanoWallet.js +87 -144
- package/package.json +1 -1
- package/tsconfig.json +8 -7
package/README.md
CHANGED
|
@@ -88,6 +88,30 @@ zanoWallet.setWalletCredentials({
|
|
|
88
88
|
});
|
|
89
89
|
```
|
|
90
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
|
+
|
|
91
115
|
## Requirements
|
|
92
116
|
|
|
93
117
|
- ZanoWallet browser extension must be installed.
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -3,5 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
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; } });
|
|
7
10
|
exports.default = zanoWallet_1.default;
|
|
@@ -0,0 +1,18 @@
|
|
|
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
CHANGED
package/dist/src/zanoWallet.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -19,37 +8,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
19
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
9
|
});
|
|
21
10
|
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
12
|
+
const uuid_1 = require("uuid");
|
|
13
|
+
class ZanoWallet {
|
|
14
|
+
constructor(params) {
|
|
53
15
|
this.DEFAULT_LOCAL_STORAGE_KEY = "wallet";
|
|
54
16
|
if (typeof window === 'undefined') {
|
|
55
17
|
throw new Error('ZanoWallet can only be used in the browser');
|
|
@@ -61,17 +23,16 @@ var ZanoWallet = /** @class */ (function () {
|
|
|
61
23
|
this.zanoWallet = window.zano;
|
|
62
24
|
this.localStorageKey = params.customLocalStorageKey || this.DEFAULT_LOCAL_STORAGE_KEY;
|
|
63
25
|
}
|
|
64
|
-
|
|
65
|
-
var message = _a.message;
|
|
26
|
+
handleError({ message }) {
|
|
66
27
|
if (this.params.onConnectError) {
|
|
67
28
|
this.params.onConnectError(message);
|
|
68
29
|
}
|
|
69
30
|
else {
|
|
70
31
|
console.error(message);
|
|
71
32
|
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
33
|
+
}
|
|
34
|
+
getSavedWalletCredentials() {
|
|
35
|
+
const savedWallet = localStorage.getItem(this.localStorageKey);
|
|
75
36
|
if (!savedWallet)
|
|
76
37
|
return undefined;
|
|
77
38
|
try {
|
|
@@ -80,111 +41,93 @@ var ZanoWallet = /** @class */ (function () {
|
|
|
80
41
|
catch (_a) {
|
|
81
42
|
return undefined;
|
|
82
43
|
}
|
|
83
|
-
}
|
|
84
|
-
|
|
44
|
+
}
|
|
45
|
+
setWalletCredentials(credentials) {
|
|
85
46
|
if (credentials) {
|
|
86
47
|
localStorage.setItem(this.localStorageKey, JSON.stringify(credentials));
|
|
87
48
|
}
|
|
88
49
|
else {
|
|
89
50
|
localStorage.removeItem(this.localStorageKey);
|
|
90
51
|
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
94
|
-
var walletData, nonce, signature, publicKey, existingWallet, generatedNonce, signResult, serverData, result;
|
|
52
|
+
}
|
|
53
|
+
connect() {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
55
|
var _a;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
result = _b.sent();
|
|
169
|
-
if (!(result === null || result === void 0 ? void 0 : result.success) || !(result === null || result === void 0 ? void 0 : result.data)) {
|
|
170
|
-
return [2 /*return*/, this.handleError({ message: result.error })];
|
|
171
|
-
}
|
|
172
|
-
if (!existingWallet && this.params.useLocalStorage) {
|
|
173
|
-
this.setWalletCredentials({
|
|
174
|
-
publicKey: publicKey,
|
|
175
|
-
signature: signature,
|
|
176
|
-
nonce: nonce
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
if (this.params.onConnectEnd) {
|
|
180
|
-
this.params.onConnectEnd(__assign(__assign({}, serverData), { token: result.data.token }));
|
|
181
|
-
}
|
|
182
|
-
_b.label = 8;
|
|
183
|
-
case 8: return [2 /*return*/];
|
|
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 }));
|
|
184
128
|
}
|
|
185
|
-
}
|
|
129
|
+
}
|
|
186
130
|
});
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
}());
|
|
131
|
+
}
|
|
132
|
+
}
|
|
190
133
|
exports.default = ZanoWallet;
|
package/package.json
CHANGED
package/tsconfig.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"target": "
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"outDir": "./dist",
|
|
7
|
-
"strict": true,
|
|
8
|
-
"esModuleInterop": true
|
|
3
|
+
"target": "ES6",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"lib": ["ES6", "DOM"]
|
|
9
10
|
},
|
|
10
11
|
"include": ["src/**/*", "index.ts"]
|
|
11
|
-
}
|
|
12
|
+
}
|