zano_web3 1.0.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 ADDED
@@ -0,0 +1,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
+ ## Requirements
92
+
93
+ - ZanoWallet browser extension must be installed.
94
+
95
+ ## Contributing
96
+
97
+ If you find any issues or want to contribute, please create a pull request or submit an issue.
@@ -0,0 +1,31 @@
1
+ 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;
@@ -0,0 +1,190 @@
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
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
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ var uuid_1 = require("uuid");
51
+ var ZanoWallet = /** @class */ (function () {
52
+ function ZanoWallet(params) {
53
+ this.DEFAULT_LOCAL_STORAGE_KEY = "wallet";
54
+ if (typeof window === 'undefined') {
55
+ throw new Error('ZanoWallet can only be used in the browser');
56
+ }
57
+ if (!window.zano) {
58
+ throw new Error('ZanoWallet requires the ZanoWallet extension to be installed');
59
+ }
60
+ this.params = params;
61
+ this.zanoWallet = window.zano;
62
+ this.localStorageKey = params.customLocalStorageKey || this.DEFAULT_LOCAL_STORAGE_KEY;
63
+ }
64
+ ZanoWallet.prototype.handleError = function (_a) {
65
+ var message = _a.message;
66
+ if (this.params.onConnectError) {
67
+ this.params.onConnectError(message);
68
+ }
69
+ else {
70
+ console.error(message);
71
+ }
72
+ };
73
+ ZanoWallet.prototype.getSavedWalletCredentials = function () {
74
+ var savedWallet = localStorage.getItem(this.localStorageKey);
75
+ if (!savedWallet)
76
+ return undefined;
77
+ try {
78
+ return JSON.parse(savedWallet);
79
+ }
80
+ catch (_a) {
81
+ return undefined;
82
+ }
83
+ };
84
+ ZanoWallet.prototype.setWalletCredentials = function (credentials) {
85
+ if (credentials) {
86
+ localStorage.setItem(this.localStorageKey, JSON.stringify(credentials));
87
+ }
88
+ else {
89
+ localStorage.removeItem(this.localStorageKey);
90
+ }
91
+ };
92
+ ZanoWallet.prototype.connect = function () {
93
+ return __awaiter(this, void 0, void 0, function () {
94
+ var walletData, nonce, signature, publicKey, existingWallet, generatedNonce, signResult, serverData, result;
95
+ var _a;
96
+ return __generator(this, function (_b) {
97
+ switch (_b.label) {
98
+ case 0:
99
+ if (!this.params.beforeConnect) return [3 /*break*/, 2];
100
+ return [4 /*yield*/, this.params.beforeConnect()];
101
+ case 1:
102
+ _b.sent();
103
+ _b.label = 2;
104
+ case 2:
105
+ if (this.params.onConnectStart) {
106
+ this.params.onConnectStart();
107
+ }
108
+ return [4 /*yield*/, window.zano.request('GET_WALLET_DATA')];
109
+ case 3:
110
+ walletData = (_b.sent()).data;
111
+ if (!(walletData === null || walletData === void 0 ? void 0 : walletData.address)) {
112
+ return [2 /*return*/, this.handleError({ message: 'Companion is offline' })];
113
+ }
114
+ if (!(walletData === null || walletData === void 0 ? void 0 : walletData.alias) && this.params.aliasRequired) {
115
+ return [2 /*return*/, this.handleError({ message: 'Alias not found' })];
116
+ }
117
+ nonce = "";
118
+ signature = "";
119
+ publicKey = "";
120
+ existingWallet = this.params.useLocalStorage ? this.getSavedWalletCredentials() : undefined;
121
+ if (!existingWallet) return [3 /*break*/, 4];
122
+ nonce = existingWallet.nonce;
123
+ signature = existingWallet.signature;
124
+ publicKey = existingWallet.publicKey;
125
+ return [3 /*break*/, 6];
126
+ case 4:
127
+ generatedNonce = this.params.customNonce || (0, uuid_1.v4)();
128
+ return [4 /*yield*/, this.zanoWallet.request('REQUEST_MESSAGE_SIGN', {
129
+ message: generatedNonce
130
+ }, null)];
131
+ case 5:
132
+ signResult = _b.sent();
133
+ if (!((_a = signResult === null || signResult === void 0 ? void 0 : signResult.data) === null || _a === void 0 ? void 0 : _a.result)) {
134
+ return [2 /*return*/, this.handleError({ message: 'Failed to sign message' })];
135
+ }
136
+ nonce = generatedNonce;
137
+ signature = signResult.data.result.sig;
138
+ publicKey = signResult.data.result.pkey;
139
+ _b.label = 6;
140
+ case 6:
141
+ serverData = {
142
+ alias: walletData.alias,
143
+ address: walletData.address,
144
+ signature: signature,
145
+ publicKey: publicKey,
146
+ message: nonce,
147
+ isSavedData: !!existingWallet
148
+ };
149
+ if (this.params.onLocalConnectEnd) {
150
+ this.params.onLocalConnectEnd(serverData);
151
+ }
152
+ if (!!this.params.disableServerRequest) return [3 /*break*/, 8];
153
+ return [4 /*yield*/, fetch(this.params.customServerPath || "/api/auth", {
154
+ method: "POST",
155
+ headers: {
156
+ "Content-Type": "application/json",
157
+ },
158
+ body: JSON.stringify({
159
+ data: serverData
160
+ })
161
+ })
162
+ .then(function (res) { return res.json(); })
163
+ .catch(function (e) { return ({
164
+ success: false,
165
+ error: e.message
166
+ }); })];
167
+ case 7:
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*/];
184
+ }
185
+ });
186
+ });
187
+ };
188
+ return ZanoWallet;
189
+ }());
190
+ exports.default = ZanoWallet;
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "zano_web3",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc"
9
+ },
10
+ "keywords": [],
11
+ "author": "",
12
+ "license": "ISC",
13
+ "devDependencies": {
14
+ "@types/uuid": "^10.0.0",
15
+ "typescript": "^5.5.4"
16
+ },
17
+ "dependencies": {
18
+ "uuid": "^10.0.0"
19
+ }
20
+ }
@@ -0,0 +1,192 @@
1
+ import { v4 as uuidv4 } from 'uuid';
2
+
3
+ interface ZanoWalletParams {
4
+ authPath: string;
5
+ useLocalStorage?: boolean; // default: true
6
+ aliasRequired?: boolean;
7
+ customLocalStorageKey?: string;
8
+ customNonce?: string;
9
+ customServerPath?: string;
10
+ disableServerRequest?: boolean;
11
+
12
+ onConnectStart?: (...params: any) => any;
13
+ onConnectEnd?: (...params: any) => any;
14
+ onConnectError?: (...params: any) => any;
15
+
16
+ beforeConnect?: (...params: any) => any;
17
+ onLocalConnectEnd?: (...params: any) => any;
18
+ }
19
+
20
+ type GlobalWindow = Window & typeof globalThis;
21
+
22
+ interface ZanoWindowParams {
23
+ request: (str: string, params?: any, timeoutMs?: number | null) => Promise<any>;
24
+ }
25
+
26
+ type ZanoWindow = Omit<GlobalWindow, 'Infinity'> & {
27
+ zano: ZanoWindowParams
28
+ }
29
+
30
+ interface WalletCredentials {
31
+ nonce: string;
32
+ signature: string;
33
+ publicKey: string;
34
+ }
35
+
36
+ class ZanoWallet {
37
+
38
+ private DEFAULT_LOCAL_STORAGE_KEY = "wallet";
39
+ private localStorageKey: string;
40
+
41
+ private params: ZanoWalletParams;
42
+ private zanoWallet: ZanoWindowParams;
43
+
44
+ constructor(params: ZanoWalletParams) {
45
+
46
+ if (typeof window === 'undefined') {
47
+ throw new Error('ZanoWallet can only be used in the browser');
48
+ }
49
+
50
+ if (!((window as unknown) as ZanoWindow).zano) {
51
+ throw new Error('ZanoWallet requires the ZanoWallet extension to be installed');
52
+ }
53
+
54
+ this.params = params;
55
+ this.zanoWallet = ((window as unknown) as ZanoWindow).zano;
56
+ this.localStorageKey = params.customLocalStorageKey || this.DEFAULT_LOCAL_STORAGE_KEY;
57
+ }
58
+
59
+
60
+ private handleError({ message } : { message: string }) {
61
+ if (this.params.onConnectError) {
62
+ this.params.onConnectError(message);
63
+ } else {
64
+ console.error(message);
65
+ }
66
+ }
67
+
68
+ getSavedWalletCredentials() {
69
+ const savedWallet = localStorage.getItem(this.localStorageKey);
70
+ if (!savedWallet) return undefined;
71
+ try {
72
+ return JSON.parse(savedWallet) as WalletCredentials;
73
+ } catch {
74
+ return undefined;
75
+ }
76
+ }
77
+
78
+ setWalletCredentials(credentials: WalletCredentials | undefined) {
79
+ if (credentials) {
80
+ localStorage.setItem(this.localStorageKey, JSON.stringify(credentials));
81
+ } else {
82
+ localStorage.removeItem(this.localStorageKey);
83
+ }
84
+ }
85
+
86
+ async connect() {
87
+
88
+ if (this.params.beforeConnect) {
89
+ await this.params.beforeConnect();
90
+ }
91
+
92
+ if (this.params.onConnectStart) {
93
+ this.params.onConnectStart();
94
+ }
95
+
96
+ const walletData = (await ((window as unknown) as ZanoWindow).zano.request('GET_WALLET_DATA')).data;
97
+
98
+
99
+ if (!walletData?.address) {
100
+ return this.handleError({ message: 'Companion is offline' });
101
+ }
102
+
103
+ if (!walletData?.alias && this.params.aliasRequired) {
104
+ return this.handleError({ message: 'Alias not found' });
105
+ }
106
+
107
+ let nonce = "";
108
+ let signature = "";
109
+ let publicKey = "";
110
+
111
+
112
+ const existingWallet = this.params.useLocalStorage ? this.getSavedWalletCredentials() : undefined;
113
+
114
+ if (existingWallet) {
115
+ nonce = existingWallet.nonce;
116
+ signature = existingWallet.signature;
117
+ publicKey = existingWallet.publicKey;
118
+ } else {
119
+ const generatedNonce = this.params.customNonce || uuidv4();
120
+
121
+ const signResult = await this.zanoWallet.request(
122
+ 'REQUEST_MESSAGE_SIGN',
123
+ {
124
+ message: generatedNonce
125
+ },
126
+ null
127
+ );
128
+
129
+ if (!signResult?.data?.result) {
130
+ return this.handleError({ message: 'Failed to sign message' });
131
+ }
132
+
133
+ nonce = generatedNonce;
134
+ signature = signResult.data.result.sig;
135
+ publicKey = signResult.data.result.pkey;
136
+ }
137
+
138
+
139
+ const serverData = {
140
+ alias: walletData.alias,
141
+ address: walletData.address,
142
+ signature,
143
+ publicKey,
144
+ message: nonce,
145
+ isSavedData: !!existingWallet
146
+ }
147
+
148
+ if (this.params.onLocalConnectEnd) {
149
+ this.params.onLocalConnectEnd(serverData);
150
+ }
151
+
152
+ if (!this.params.disableServerRequest) {
153
+ const result = await fetch( this.params.customServerPath || "/api/auth", {
154
+ method: "POST",
155
+ headers: {
156
+ "Content-Type": "application/json",
157
+ },
158
+ body: JSON.stringify(
159
+ {
160
+ data: serverData
161
+ }
162
+ )
163
+ })
164
+ .then(res => res.json())
165
+ .catch((e) => ({
166
+ success: false,
167
+ error: e.message
168
+ }));
169
+
170
+ if (!result?.success || !result?.data) {
171
+ return this.handleError({ message: result.error });
172
+ }
173
+
174
+ if (!existingWallet && this.params.useLocalStorage) {
175
+ this.setWalletCredentials({
176
+ publicKey,
177
+ signature,
178
+ nonce
179
+ });
180
+ }
181
+
182
+ if (this.params.onConnectEnd) {
183
+ this.params.onConnectEnd({
184
+ ...serverData,
185
+ token: result.data.token
186
+ });
187
+ }
188
+ }
189
+ }
190
+ }
191
+
192
+ export default ZanoWallet;
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES5",
4
+ "module": "CommonJS",
5
+ "declaration": true,
6
+ "outDir": "./dist",
7
+ "strict": true,
8
+ "esModuleInterop": true
9
+ },
10
+ "include": ["src/**/*"]
11
+ }