zano_web3 9.2.0 → 9.2.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.
@@ -1,220 +1,220 @@
1
- import { v4 as uuidv4 } from 'uuid';
2
- import { Wallet } from './types';
3
-
4
- export interface ZanoWalletParams {
5
- authPath: string;
6
- useLocalStorage?: boolean; // default: true
7
- aliasRequired?: boolean;
8
- customLocalStorageKey?: string;
9
- customNonce?: string;
10
- customServerPath?: string;
11
- disableServerRequest?: boolean;
12
-
13
- onConnectStart?: (...params: any) => any;
14
- onConnectEnd?: (...params: any) => any;
15
- onConnectError?: (...params: any) => any;
16
-
17
- beforeConnect?: (...params: any) => any;
18
- onLocalConnectEnd?: (...params: any) => any;
19
- }
20
-
21
- type GlobalWindow = Window & typeof globalThis;
22
-
23
- interface ZanoWindowParams {
24
- request: (str: string, params?: any, timeoutMs?: number | null) => Promise<any>;
25
- }
26
-
27
- type ZanoWindow = Omit<GlobalWindow, 'Infinity'> & {
28
- zano: ZanoWindowParams
29
- }
30
-
31
- interface WalletCredentials {
32
- nonce: string;
33
- signature: string;
34
- publicKey: string;
35
- address: string;
36
- }
37
-
38
- class ZanoWallet {
39
-
40
- private DEFAULT_LOCAL_STORAGE_KEY = "wallet";
41
- private localStorageKey: string;
42
-
43
- private params: ZanoWalletParams;
44
- private zanoWallet: ZanoWindowParams;
45
-
46
- constructor(params: ZanoWalletParams) {
47
-
48
- if (typeof window === 'undefined') {
49
- throw new Error('ZanoWallet can only be used in the browser');
50
- }
51
-
52
- if (!((window as unknown) as ZanoWindow).zano) {
53
- console.error('ZanoWallet requires the ZanoWallet extension to be installed');
54
- }
55
-
56
- this.params = params;
57
- this.zanoWallet = ((window as unknown) as ZanoWindow).zano;
58
- this.localStorageKey = params.customLocalStorageKey || this.DEFAULT_LOCAL_STORAGE_KEY;
59
- }
60
-
61
-
62
- private handleError({ message } : { message: string }) {
63
- if (this.params.onConnectError) {
64
- this.params.onConnectError(message);
65
- } else {
66
- console.error(message);
67
- }
68
- }
69
-
70
- getSavedWalletCredentials() {
71
- const savedWallet = localStorage.getItem(this.localStorageKey);
72
- if (!savedWallet) return undefined;
73
- try {
74
- return JSON.parse(savedWallet) as WalletCredentials;
75
- } catch {
76
- return undefined;
77
- }
78
- }
79
-
80
- setWalletCredentials(credentials: WalletCredentials | undefined) {
81
- if (credentials) {
82
- localStorage.setItem(this.localStorageKey, JSON.stringify(credentials));
83
- } else {
84
- localStorage.removeItem(this.localStorageKey);
85
- }
86
- }
87
-
88
- cleanWalletCredentials() {
89
- this.setWalletCredentials(undefined);
90
- }
91
-
92
- async connect() {
93
-
94
- if (this.params.beforeConnect) {
95
- await this.params.beforeConnect();
96
- }
97
-
98
- if (this.params.onConnectStart) {
99
- this.params.onConnectStart();
100
- }
101
-
102
- const walletData = (await ((window as unknown) as ZanoWindow).zano.request('GET_WALLET_DATA')).data;
103
-
104
-
105
- if (!walletData?.address) {
106
- return this.handleError({ message: 'Companion is offline' });
107
- }
108
-
109
- if (!walletData?.alias && this.params.aliasRequired) {
110
- return this.handleError({ message: 'Alias not found' });
111
- }
112
-
113
- let nonce = "";
114
- let signature = "";
115
- let publicKey = "";
116
-
117
-
118
- const existingWallet = this.params.useLocalStorage ? this.getSavedWalletCredentials() : undefined;
119
-
120
- const existingWalletValid = existingWallet && existingWallet.address === walletData.address;
121
-
122
- console.log('existingWalletValid', existingWalletValid);
123
- console.log('existingWallet', existingWallet);
124
- console.log('walletData', walletData);
125
-
126
- if (existingWalletValid) {
127
- nonce = existingWallet.nonce;
128
- signature = existingWallet.signature;
129
- publicKey = existingWallet.publicKey;
130
- } else {
131
- const generatedNonce = this.params.customNonce || uuidv4();
132
-
133
- const signResult = await this.zanoWallet.request(
134
- 'REQUEST_MESSAGE_SIGN',
135
- {
136
- message: generatedNonce
137
- },
138
- null
139
- );
140
-
141
- if (!signResult?.data?.result) {
142
- return this.handleError({ message: 'Failed to sign message' });
143
- }
144
-
145
- nonce = generatedNonce;
146
- signature = signResult.data.result.sig;
147
- publicKey = signResult.data.result.pkey;
148
- }
149
-
150
-
151
- const serverData = {
152
- alias: walletData.alias,
153
- address: walletData.address,
154
- signature,
155
- pkey: publicKey,
156
- message: nonce,
157
- isSavedData: existingWalletValid
158
- }
159
-
160
- if (this.params.onLocalConnectEnd) {
161
- this.params.onLocalConnectEnd(serverData);
162
- }
163
-
164
-
165
- if (!this.params.disableServerRequest) {
166
- const result = await fetch( this.params.customServerPath || "/api/auth", {
167
- method: "POST",
168
- headers: {
169
- "Content-Type": "application/json",
170
- },
171
- body: JSON.stringify(
172
- {
173
- data: serverData
174
- }
175
- )
176
- })
177
- .then(res => res.json())
178
- .catch((e) => ({
179
- success: false,
180
- error: e.message
181
- }));
182
-
183
- if (!result?.success || !result?.data) {
184
- return this.handleError({ message: result.error });
185
- }
186
-
187
- if (!existingWalletValid && this.params.useLocalStorage) {
188
- this.setWalletCredentials({
189
- publicKey,
190
- signature,
191
- nonce,
192
- address: walletData.address
193
- });
194
- }
195
-
196
- if (this.params.onConnectEnd) {
197
- this.params.onConnectEnd({
198
- ...serverData,
199
- token: result.data.token
200
- });
201
- }
202
- }
203
-
204
- return true;
205
- }
206
-
207
- async getWallet() {
208
- return (await this.zanoWallet.request('GET_WALLET_DATA'))?.data as Wallet;
209
- }
210
-
211
- async getAddressByAlias(alias: string) {
212
- return ((await this.zanoWallet.request('GET_ALIAS_DETAILS', { alias })) || undefined) as string | undefined;
213
- }
214
-
215
- async createAlias(alias: string) {
216
- return ((await this.zanoWallet.request('CREATE_ALIAS', { alias })) || undefined).data;
217
- }
218
- }
219
-
1
+ import { v4 as uuidv4 } from 'uuid';
2
+ import { Wallet } from './types';
3
+
4
+ export interface ZanoWalletParams {
5
+ authPath: string;
6
+ useLocalStorage?: boolean; // default: true
7
+ aliasRequired?: boolean;
8
+ customLocalStorageKey?: string;
9
+ customNonce?: string;
10
+ customServerPath?: string;
11
+ disableServerRequest?: boolean;
12
+
13
+ onConnectStart?: (...params: any) => any;
14
+ onConnectEnd?: (...params: any) => any;
15
+ onConnectError?: (...params: any) => any;
16
+
17
+ beforeConnect?: (...params: any) => any;
18
+ onLocalConnectEnd?: (...params: any) => any;
19
+ }
20
+
21
+ type GlobalWindow = Window & typeof globalThis;
22
+
23
+ interface ZanoWindowParams {
24
+ request: (str: string, params?: any, timeoutMs?: number | null) => Promise<any>;
25
+ }
26
+
27
+ type ZanoWindow = Omit<GlobalWindow, 'Infinity'> & {
28
+ zano: ZanoWindowParams
29
+ }
30
+
31
+ interface WalletCredentials {
32
+ nonce: string;
33
+ signature: string;
34
+ publicKey: string;
35
+ address: string;
36
+ }
37
+
38
+ class ZanoWallet {
39
+
40
+ private DEFAULT_LOCAL_STORAGE_KEY = "wallet";
41
+ private localStorageKey: string;
42
+
43
+ private params: ZanoWalletParams;
44
+ private zanoWallet: ZanoWindowParams;
45
+
46
+ constructor(params: ZanoWalletParams) {
47
+
48
+ if (typeof window === 'undefined') {
49
+ throw new Error('ZanoWallet can only be used in the browser');
50
+ }
51
+
52
+ if (!((window as unknown) as ZanoWindow).zano) {
53
+ console.error('ZanoWallet requires the ZanoWallet extension to be installed');
54
+ }
55
+
56
+ this.params = params;
57
+ this.zanoWallet = ((window as unknown) as ZanoWindow).zano;
58
+ this.localStorageKey = params.customLocalStorageKey || this.DEFAULT_LOCAL_STORAGE_KEY;
59
+ }
60
+
61
+
62
+ private handleError({ message } : { message: string }) {
63
+ if (this.params.onConnectError) {
64
+ this.params.onConnectError(message);
65
+ } else {
66
+ console.error(message);
67
+ }
68
+ }
69
+
70
+ getSavedWalletCredentials() {
71
+ const savedWallet = localStorage.getItem(this.localStorageKey);
72
+ if (!savedWallet) return undefined;
73
+ try {
74
+ return JSON.parse(savedWallet) as WalletCredentials;
75
+ } catch {
76
+ return undefined;
77
+ }
78
+ }
79
+
80
+ setWalletCredentials(credentials: WalletCredentials | undefined) {
81
+ if (credentials) {
82
+ localStorage.setItem(this.localStorageKey, JSON.stringify(credentials));
83
+ } else {
84
+ localStorage.removeItem(this.localStorageKey);
85
+ }
86
+ }
87
+
88
+ cleanWalletCredentials() {
89
+ this.setWalletCredentials(undefined);
90
+ }
91
+
92
+ async connect() {
93
+
94
+ if (this.params.beforeConnect) {
95
+ await this.params.beforeConnect();
96
+ }
97
+
98
+ if (this.params.onConnectStart) {
99
+ this.params.onConnectStart();
100
+ }
101
+
102
+ const walletData = (await ((window as unknown) as ZanoWindow).zano.request('GET_WALLET_DATA')).data;
103
+
104
+
105
+ if (!walletData?.address) {
106
+ return this.handleError({ message: 'Companion is offline' });
107
+ }
108
+
109
+ if (!walletData?.alias && this.params.aliasRequired) {
110
+ return this.handleError({ message: 'Alias not found' });
111
+ }
112
+
113
+ let nonce = "";
114
+ let signature = "";
115
+ let publicKey = "";
116
+
117
+
118
+ const existingWallet = this.params.useLocalStorage ? this.getSavedWalletCredentials() : undefined;
119
+
120
+ const existingWalletValid = existingWallet && existingWallet.address === walletData.address;
121
+
122
+ console.log('existingWalletValid', existingWalletValid);
123
+ console.log('existingWallet', existingWallet);
124
+ console.log('walletData', walletData);
125
+
126
+ if (existingWalletValid) {
127
+ nonce = existingWallet.nonce;
128
+ signature = existingWallet.signature;
129
+ publicKey = existingWallet.publicKey;
130
+ } else {
131
+ const generatedNonce = this.params.customNonce || uuidv4();
132
+
133
+ const signResult = await this.zanoWallet.request(
134
+ 'REQUEST_MESSAGE_SIGN',
135
+ {
136
+ message: generatedNonce
137
+ },
138
+ null
139
+ );
140
+
141
+ if (!signResult?.data?.result) {
142
+ return this.handleError({ message: 'Failed to sign message' });
143
+ }
144
+
145
+ nonce = generatedNonce;
146
+ signature = signResult.data.result.sig;
147
+ publicKey = signResult.data.result.pkey;
148
+ }
149
+
150
+
151
+ const serverData = {
152
+ alias: walletData.alias,
153
+ address: walletData.address,
154
+ signature,
155
+ pkey: publicKey,
156
+ message: nonce,
157
+ isSavedData: existingWalletValid
158
+ }
159
+
160
+ if (this.params.onLocalConnectEnd) {
161
+ this.params.onLocalConnectEnd(serverData);
162
+ }
163
+
164
+
165
+ if (!this.params.disableServerRequest) {
166
+ const result = await fetch( this.params.customServerPath || "/api/auth", {
167
+ method: "POST",
168
+ headers: {
169
+ "Content-Type": "application/json",
170
+ },
171
+ body: JSON.stringify(
172
+ {
173
+ data: serverData
174
+ }
175
+ )
176
+ })
177
+ .then(res => res.json())
178
+ .catch((e) => ({
179
+ success: false,
180
+ error: e.message
181
+ }));
182
+
183
+ if (!result?.success || !result?.data) {
184
+ return this.handleError({ message: result.error });
185
+ }
186
+
187
+ if (!existingWalletValid && this.params.useLocalStorage) {
188
+ this.setWalletCredentials({
189
+ publicKey,
190
+ signature,
191
+ nonce,
192
+ address: walletData.address
193
+ });
194
+ }
195
+
196
+ if (this.params.onConnectEnd) {
197
+ this.params.onConnectEnd({
198
+ ...serverData,
199
+ token: result.data.token
200
+ });
201
+ }
202
+ }
203
+
204
+ return true;
205
+ }
206
+
207
+ async getWallet() {
208
+ return (await this.zanoWallet.request('GET_WALLET_DATA'))?.data as Wallet;
209
+ }
210
+
211
+ async getAddressByAlias(alias: string) {
212
+ return ((await this.zanoWallet.request('GET_ALIAS_DETAILS', { alias })) || undefined) as string | undefined;
213
+ }
214
+
215
+ async createAlias(alias: string) {
216
+ return ((await this.zanoWallet.request('CREATE_ALIAS', { alias })) || undefined).data;
217
+ }
218
+ }
219
+
220
220
  export default ZanoWallet;