zano_web3 10.1.0 → 10.2.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.
Files changed (108) hide show
  1. package/README.md +17 -142
  2. package/package.json +2 -5
  3. package/web/dist/constants/common/index.d.ts +4 -0
  4. package/web/dist/constants/common/index.js +12 -0
  5. package/web/dist/constants/common/index.js.map +1 -0
  6. package/web/dist/constants/index.d.ts +1 -0
  7. package/web/dist/constants/index.js +2 -0
  8. package/web/dist/constants/index.js.map +1 -0
  9. package/web/dist/index.d.ts +1 -0
  10. package/web/dist/index.js +1 -0
  11. package/web/dist/index.js.map +1 -1
  12. package/web/dist/types/params/companion-requests/accept-ionic-swap/index.d.ts +3 -0
  13. package/web/dist/types/params/companion-requests/accept-ionic-swap/index.js +2 -0
  14. package/web/dist/types/params/companion-requests/accept-ionic-swap/index.js.map +1 -0
  15. package/web/dist/types/params/companion-requests/add-whitelist-asset/index.d.ts +3 -0
  16. package/web/dist/types/params/companion-requests/add-whitelist-asset/index.js +2 -0
  17. package/web/dist/types/params/companion-requests/add-whitelist-asset/index.js.map +1 -0
  18. package/web/dist/types/params/companion-requests/burn-asset/index.d.ts +28 -0
  19. package/web/dist/types/params/companion-requests/burn-asset/index.js +2 -0
  20. package/web/dist/types/params/companion-requests/burn-asset/index.js.map +1 -0
  21. package/web/dist/types/params/companion-requests/create-alias/index.d.ts +3 -0
  22. package/web/dist/types/params/companion-requests/create-alias/index.js +2 -0
  23. package/web/dist/types/params/companion-requests/create-alias/index.js.map +1 -0
  24. package/web/dist/types/params/companion-requests/get-address-by-alias/index.d.ts +3 -0
  25. package/web/dist/types/params/companion-requests/get-address-by-alias/index.js +2 -0
  26. package/web/dist/types/params/companion-requests/get-address-by-alias/index.js.map +1 -0
  27. package/web/dist/types/params/companion-requests/get-ionic-swap-info/index.d.ts +3 -0
  28. package/web/dist/types/params/companion-requests/get-ionic-swap-info/index.js +2 -0
  29. package/web/dist/types/params/companion-requests/get-ionic-swap-info/index.js.map +1 -0
  30. package/web/dist/types/params/companion-requests/index.d.ts +4 -1
  31. package/web/dist/types/params/companion-requests/initialize-ionic-swap/index.d.ts +14 -0
  32. package/web/dist/types/params/companion-requests/initialize-ionic-swap/index.js +2 -0
  33. package/web/dist/types/params/companion-requests/initialize-ionic-swap/index.js.map +1 -0
  34. package/web/dist/types/params/companion-requests/request-message-sign/index.d.ts +3 -0
  35. package/web/dist/types/params/companion-requests/request-message-sign/index.js +2 -0
  36. package/web/dist/types/params/companion-requests/request-message-sign/index.js.map +1 -0
  37. package/web/dist/types/params/companion-requests/request-permissions/index.d.ts +7 -2
  38. package/web/dist/types/params/companion-requests/transfer/index.d.ts +22 -0
  39. package/web/dist/types/params/companion-requests/transfer/index.js +2 -0
  40. package/web/dist/types/params/companion-requests/transfer/index.js.map +1 -0
  41. package/web/dist/types/responses/companion-methods/accept-ionic-swap/index.d.ts +24 -0
  42. package/web/dist/types/responses/companion-methods/accept-ionic-swap/index.js +21 -0
  43. package/web/dist/types/responses/companion-methods/accept-ionic-swap/index.js.map +1 -0
  44. package/web/dist/types/responses/companion-methods/add-whitelist-asset/index.d.ts +48 -0
  45. package/web/dist/types/responses/companion-methods/add-whitelist-asset/index.js +40 -0
  46. package/web/dist/types/responses/companion-methods/add-whitelist-asset/index.js.map +1 -0
  47. package/web/dist/types/responses/companion-methods/burn-asset/index.d.ts +24 -0
  48. package/web/dist/types/responses/companion-methods/burn-asset/index.js +21 -0
  49. package/web/dist/types/responses/companion-methods/burn-asset/index.js.map +1 -0
  50. package/web/dist/types/responses/companion-methods/create-alias/index.d.ts +14 -1
  51. package/web/dist/types/responses/companion-methods/create-alias/index.js +12 -1
  52. package/web/dist/types/responses/companion-methods/create-alias/index.js.map +1 -1
  53. package/web/dist/types/responses/companion-methods/get-address-by-alias/index.d.ts +10 -2
  54. package/web/dist/types/responses/companion-methods/get-address-by-alias/index.js +7 -1
  55. package/web/dist/types/responses/companion-methods/get-address-by-alias/index.js.map +1 -1
  56. package/web/dist/types/responses/companion-methods/get-ionic-swap-info/index.d.ts +49 -0
  57. package/web/dist/types/responses/companion-methods/get-ionic-swap-info/index.js +32 -0
  58. package/web/dist/types/responses/companion-methods/get-ionic-swap-info/index.js.map +1 -0
  59. package/web/dist/types/responses/companion-methods/get-permissions/index.d.ts +21 -0
  60. package/web/dist/types/responses/companion-methods/get-permissions/index.js +10 -0
  61. package/web/dist/types/responses/companion-methods/get-permissions/index.js.map +1 -0
  62. package/web/dist/types/responses/companion-methods/get-wallet-balance/index.d.ts +73 -0
  63. package/web/dist/types/responses/companion-methods/get-wallet-balance/index.js +44 -0
  64. package/web/dist/types/responses/companion-methods/get-wallet-balance/index.js.map +1 -0
  65. package/web/dist/types/responses/companion-methods/get-whitelist/index.d.ts +29 -0
  66. package/web/dist/types/responses/companion-methods/get-whitelist/index.js +17 -0
  67. package/web/dist/types/responses/companion-methods/get-whitelist/index.js.map +1 -0
  68. package/web/dist/types/responses/companion-methods/index.d.ts +9 -0
  69. package/web/dist/types/responses/companion-methods/initialize-ionic-swap/index.d.ts +22 -0
  70. package/web/dist/types/responses/companion-methods/initialize-ionic-swap/index.js +21 -0
  71. package/web/dist/types/responses/companion-methods/initialize-ionic-swap/index.js.map +1 -0
  72. package/web/dist/types/responses/companion-methods/request-message-sign/index.d.ts +6 -2
  73. package/web/dist/types/responses/companion-methods/request-message-sign/index.js +13 -6
  74. package/web/dist/types/responses/companion-methods/request-message-sign/index.js.map +1 -1
  75. package/web/dist/types/responses/companion-methods/transfer/index.d.ts +35 -0
  76. package/web/dist/types/responses/companion-methods/transfer/index.js +25 -0
  77. package/web/dist/types/responses/companion-methods/transfer/index.js.map +1 -0
  78. package/web/dist/zano-wallet.d.ts +43 -6
  79. package/web/dist/zano-wallet.js +359 -14
  80. package/web/dist/zano-wallet.js.map +1 -1
  81. package/web/src/constants/common/index.ts +15 -0
  82. package/web/src/constants/index.ts +5 -0
  83. package/web/src/index.ts +1 -0
  84. package/web/src/types/params/companion-requests/accept-ionic-swap/index.ts +3 -0
  85. package/web/src/types/params/companion-requests/add-whitelist-asset/index.ts +3 -0
  86. package/web/src/types/params/companion-requests/burn-asset/index.ts +31 -0
  87. package/web/src/types/params/companion-requests/create-alias/index.ts +3 -0
  88. package/web/src/types/params/companion-requests/get-address-by-alias/index.ts +3 -0
  89. package/web/src/types/params/companion-requests/get-ionic-swap-info/index.ts +3 -0
  90. package/web/src/types/params/companion-requests/index.ts +4 -1
  91. package/web/src/types/params/companion-requests/initialize-ionic-swap/index.ts +15 -0
  92. package/web/src/types/params/companion-requests/request-message-sign/index.ts +3 -0
  93. package/web/src/types/params/companion-requests/request-permissions/index.ts +9 -2
  94. package/web/src/types/params/companion-requests/transfer/index.ts +17 -0
  95. package/web/src/types/responses/companion-methods/accept-ionic-swap/index.ts +33 -0
  96. package/web/src/types/responses/companion-methods/add-whitelist-asset/index.ts +53 -0
  97. package/web/src/types/responses/companion-methods/burn-asset/index.ts +33 -0
  98. package/web/src/types/responses/companion-methods/create-alias/index.ts +15 -1
  99. package/web/src/types/responses/companion-methods/get-address-by-alias/index.ts +13 -2
  100. package/web/src/types/responses/companion-methods/get-ionic-swap-info/index.ts +45 -0
  101. package/web/src/types/responses/companion-methods/get-permissions/index.ts +24 -0
  102. package/web/src/types/responses/companion-methods/get-wallet-balance/index.ts +57 -0
  103. package/web/src/types/responses/companion-methods/get-whitelist/index.ts +30 -0
  104. package/web/src/types/responses/companion-methods/index.ts +10 -1
  105. package/web/src/types/responses/companion-methods/initialize-ionic-swap/index.ts +31 -0
  106. package/web/src/types/responses/companion-methods/request-message-sign/index.ts +13 -6
  107. package/web/src/types/responses/companion-methods/transfer/index.ts +38 -0
  108. package/web/src/zano-wallet.ts +470 -15
@@ -1,17 +1,53 @@
1
+ import z from 'zod';
2
+
1
3
  import {
2
4
  ZanoWebError,
3
5
  RequestPermissionsResponse,
4
6
  GetAddressByAliasResponse,
5
7
  CreateAliasResponse,
6
8
  RequestMessageSignResponse,
7
- CompanionPermissionsParam,
9
+ PermissionsParam,
10
+ InitializeIonicSwapParams,
11
+ InitializeIonicSwapResponse,
12
+ AcceptIonicSwapResponse,
13
+ GetPermissionsResponse,
14
+ GetWalletBalanceResponse,
15
+ TransferParams,
16
+ TransferResponse,
17
+ GetIonicSwapProposalInfoResponse,
18
+ GetWhitelistResponse,
19
+ AddWhitelistAssetResponse,
20
+ BurnAssetParams,
21
+ BurnAssetResponse
8
22
  } from './types';
23
+ import { ZanoWindowObject, ZanoWindow } from './types/special/zano-window';
24
+ import { WALLET_RPC_GENERIC_ERROR_CODE, getWalletRPCErrorCode, getWalletRPCErrorCodeByStatus } from './constants';
25
+
9
26
  import { createAliasCompanionResponseSchema } from './types/responses/companion-methods/create-alias';
10
27
  import { getAddressByAliasCompanionResponseSchema } from './types/responses/companion-methods/get-address-by-alias';
11
28
  import { getWalletDataCompanionResponseSchema, GetWalletDataResponse } from './types/responses/companion-methods/get-wallet-data';
12
29
  import { requestMessageSignCompanionResponseSchema } from './types/responses/companion-methods/request-message-sign';
13
30
  import { requestPermissionsCompanionResponseSchema } from './types/responses/companion-methods/request-permissions';
14
- import { ZanoWindowObject, ZanoWindow } from './types/special/zano-window';
31
+ import { CompanionIonicSwapParams } from './types/params/companion-requests/initialize-ionic-swap';
32
+ import { CompanionRequestAccessParams } from './types/params/companion-requests/request-permissions';
33
+ import { CompanionGetAddressByAliasParams } from './types/params/companion-requests/get-address-by-alias';
34
+ import { CompanionCreateAliasParams } from './types/params/companion-requests/create-alias';
35
+ import { CompanionRequestMessageSignParams } from './types/params/companion-requests/request-message-sign';
36
+ import { initializeIonicSwapCompanionResponseSchema } from './types/responses/companion-methods/initialize-ionic-swap';
37
+ import { CompanionAcceptIonicSwapParams } from './types/params/companion-requests/accept-ionic-swap';
38
+ import { acceptIonicSwapCompanionResponseSchema } from './types/responses/companion-methods/accept-ionic-swap';
39
+ import { getPermissionsCompanionResponseSchema } from './types/responses/companion-methods/get-permissions';
40
+ import { getWalletBalanceCompanionResponseSchema } from './types/responses/companion-methods/get-wallet-balance';
41
+ import { CompanionTransferParams } from './types/params/companion-requests/transfer';
42
+ import { transferCompanionResponseSchema } from './types/responses/companion-methods/transfer';
43
+ import { CompanionGetIonicSwapInfoParams } from './types/params/companion-requests/get-ionic-swap-info';
44
+ import { getIonicSwapProposalInfoCompanionResponseSchema } from './types/responses/companion-methods/get-ionic-swap-info';
45
+ import { getWhitelistCompanionResponseSchema } from './types/responses/companion-methods/get-whitelist';
46
+ import { CompanionAddWhitelistAssetParams } from './types/params/companion-requests/add-whitelist-asset';
47
+ import { addWhitelistAssetCompanionResponseSchema } from './types/responses/companion-methods/add-whitelist-asset';
48
+ import { CompanionBurnAssetParams } from './types/params/companion-requests/burn-asset';
49
+ import { NON_NEGATIVE_REAL_NUMBER_REGEX } from './constants/common';
50
+ import { burnAssetCompanionResponseScheme } from './types/responses/companion-methods/burn-asset';
15
51
 
16
52
  class ZanoWallet {
17
53
  private getZanoWallet = (): ZanoWindowObject => {
@@ -27,10 +63,15 @@ class ZanoWallet {
27
63
  return zanoWindow.zano;
28
64
  }
29
65
 
30
- requestPermissions = async (permissions: CompanionPermissionsParam): Promise<RequestPermissionsResponse> => {
31
- const companionResponseRaw = await this.getZanoWallet().request('REQUEST_ACCESS', {
32
- permissions,
33
- });
66
+ requestPermissions = async (permissions: PermissionsParam, { timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<RequestPermissionsResponse> => {
67
+
68
+ const companionRequestParams: CompanionRequestAccessParams = {
69
+ permissions: permissions.map(permission => ({
70
+ type: permission.type,
71
+ })),
72
+ }
73
+
74
+ const companionResponseRaw = await this.getZanoWallet().request('REQUEST_ACCESS', companionRequestParams, timeoutMs);
34
75
 
35
76
  const companionResponseParsingResult = requestPermissionsCompanionResponseSchema.safeParse(companionResponseRaw);
36
77
  if (!companionResponseParsingResult.success) {
@@ -54,8 +95,8 @@ class ZanoWallet {
54
95
  };
55
96
  }
56
97
 
57
- getWallet = async (): Promise<GetWalletDataResponse> => {
58
- const companionResponseRaw = await this.getZanoWallet().request('GET_WALLET_DATA');
98
+ getWallet = async ({ timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<GetWalletDataResponse> => {
99
+ const companionResponseRaw = await this.getZanoWallet().request('GET_WALLET_DATA', {}, timeoutMs);
59
100
 
60
101
  const companionResponseParsingResult = getWalletDataCompanionResponseSchema.safeParse(companionResponseRaw);
61
102
  if (!companionResponseParsingResult.success) {
@@ -80,8 +121,10 @@ class ZanoWallet {
80
121
  };
81
122
  }
82
123
 
83
- getAddressByAlias = async (alias: string): Promise<GetAddressByAliasResponse> => {
84
- const companionResponseRaw = await this.getZanoWallet().request('GET_ALIAS_DETAILS', { alias });
124
+ getAddressByAlias = async (alias: string, { timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<GetAddressByAliasResponse> => {
125
+ const companionRequestParams: CompanionGetAddressByAliasParams = { alias };
126
+
127
+ const companionResponseRaw = await this.getZanoWallet().request('GET_ALIAS_DETAILS', companionRequestParams, timeoutMs);
85
128
 
86
129
  const companionResponseParsingResult = getAddressByAliasCompanionResponseSchema.safeParse(companionResponseRaw);
87
130
  if (!companionResponseParsingResult.success) {
@@ -93,7 +136,14 @@ class ZanoWallet {
93
136
 
94
137
  const companionResponse = companionResponseParsingResult.data;
95
138
 
96
- if (typeof companionResponse !== 'string') {
139
+ if (companionResponse === '') {
140
+ return {
141
+ success: false,
142
+ error: WALLET_RPC_GENERIC_ERROR_CODE,
143
+ };
144
+ }
145
+
146
+ if ('error' in companionResponse) {
97
147
  return {
98
148
  success: false,
99
149
  error: companionResponse.error,
@@ -106,8 +156,10 @@ class ZanoWallet {
106
156
  };
107
157
  }
108
158
 
109
- createAlias = async (alias: string): Promise<CreateAliasResponse> => {
110
- const companionResponseRaw = await this.getZanoWallet().request('CREATE_ALIAS', { alias });
159
+ createAlias = async (alias: string, { timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<CreateAliasResponse> => {
160
+ const companionRequestParams: CompanionCreateAliasParams = { alias };
161
+
162
+ const companionResponseRaw = await this.getZanoWallet().request('CREATE_ALIAS', companionRequestParams, timeoutMs);
111
163
 
112
164
  const companionResponseParsingResult = createAliasCompanionResponseSchema.safeParse(companionResponseRaw);
113
165
  if (!companionResponseParsingResult.success) {
@@ -126,13 +178,25 @@ class ZanoWallet {
126
178
  };
127
179
  }
128
180
 
181
+ if ('error' in companionResponse.data) {
182
+ return {
183
+ success: false,
184
+ error: getWalletRPCErrorCode(companionResponse.data.error.code),
185
+ };
186
+ }
187
+
129
188
  return {
130
189
  success: true,
190
+ data: {
191
+ tx_id: companionResponse.data.result.tx_id,
192
+ },
131
193
  };
132
194
  }
133
195
 
134
- requestMessageSign = async (message: string): Promise<RequestMessageSignResponse> => {
135
- const companionResponseRaw = await this.getZanoWallet().request('REQUEST_MESSAGE_SIGN', { message });
196
+ requestMessageSign = async (message: string, { timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<RequestMessageSignResponse> => {
197
+ const companionRequestParams: CompanionRequestMessageSignParams = { message };
198
+
199
+ const companionResponseRaw = await this.getZanoWallet().request('REQUEST_MESSAGE_SIGN', companionRequestParams, timeoutMs);
136
200
 
137
201
  const companionResponseParsingResult = requestMessageSignCompanionResponseSchema.safeParse(companionResponseRaw);
138
202
  if (!companionResponseParsingResult.success) {
@@ -151,11 +215,402 @@ class ZanoWallet {
151
215
  };
152
216
  }
153
217
 
218
+ if ('error' in companionResponse.data) {
219
+ return {
220
+ success: false,
221
+ error: getWalletRPCErrorCode(companionResponse.data.error.code),
222
+ };
223
+ }
224
+
225
+ return {
226
+ success: true,
227
+ data: companionResponse.data.result,
228
+ };
229
+ }
230
+
231
+ initializeIonicSwap = async ({
232
+ destinationAssetID,
233
+ destinationAssetAmount,
234
+ currentAssetID,
235
+ currentAssetAmount,
236
+ destinationAddress
237
+ }: InitializeIonicSwapParams, { timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<InitializeIonicSwapResponse> => {
238
+
239
+ const destinationAmountValidationResult = z.string().regex(NON_NEGATIVE_REAL_NUMBER_REGEX).safeParse(destinationAssetAmount);
240
+ const currentAmountValidationResult = z.string().regex(NON_NEGATIVE_REAL_NUMBER_REGEX).safeParse(currentAssetAmount);
241
+
242
+ const areNumbersValid =
243
+ destinationAmountValidationResult.success &&
244
+ currentAmountValidationResult.success;
245
+
246
+ if (!areNumbersValid) {
247
+ throw new ZanoWebError({
248
+ message: 'Invalid asset amounts provided. Both destinationAssetAmount and currentAssetAmount must be valid, finite, and greater than zero.',
249
+ code: 'INVALID_PARAMS',
250
+ });
251
+ }
252
+
253
+ const companionRequestParams: CompanionIonicSwapParams = {
254
+ destinationAssetID,
255
+ destinationAssetAmount,
256
+ currentAssetID,
257
+ currentAssetAmount,
258
+ destinationAddress
259
+ }
260
+
261
+ const companionResponseRaw = await this.getZanoWallet().request('IONIC_SWAP', companionRequestParams, timeoutMs);
262
+
263
+ const companionResponseParsingResult = initializeIonicSwapCompanionResponseSchema.safeParse(companionResponseRaw);
264
+ if (!companionResponseParsingResult.success) {
265
+ throw new ZanoWebError({
266
+ message: 'Failed to parse companion response.',
267
+ code: 'INTERNAL_ERROR',
268
+ });
269
+ }
270
+
271
+ const companionResponse = companionResponseParsingResult.data;
272
+
273
+ if (!('data' in companionResponse)) {
274
+ return {
275
+ success: false,
276
+ error: companionResponse.error,
277
+ };
278
+ }
279
+
280
+ if ('error' in companionResponse.data) {
281
+ return {
282
+ success: false,
283
+ error: getWalletRPCErrorCode(companionResponse.data.error.code),
284
+ };
285
+ }
286
+
287
+ return {
288
+ success: true,
289
+ data: companionResponse.data.result.hex_raw_proposal,
290
+ };
291
+ }
292
+
293
+ acceptIonicSwap = async (hexRawProposal: string, { timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<AcceptIonicSwapResponse> => {
294
+ const companionRequestParams: CompanionAcceptIonicSwapParams = { hex_raw_proposal: hexRawProposal };
295
+
296
+ const companionResponseRaw = await this.getZanoWallet().request('IONIC_SWAP_ACCEPT', companionRequestParams, timeoutMs);
297
+
298
+ const companionResponseParsingResult = acceptIonicSwapCompanionResponseSchema.safeParse(companionResponseRaw);
299
+ if (!companionResponseParsingResult.success) {
300
+ throw new ZanoWebError({
301
+ message: 'Failed to parse companion response.',
302
+ code: 'INTERNAL_ERROR',
303
+ });
304
+ }
305
+
306
+ const companionResponse = companionResponseParsingResult.data;
307
+
308
+ if (!('data' in companionResponse)) {
309
+ return {
310
+ success: false,
311
+ error: companionResponse.error,
312
+ };
313
+ }
314
+
315
+ if ('error' in companionResponse.data) {
316
+ return {
317
+ success: false,
318
+ error: getWalletRPCErrorCode(companionResponse.data.error.code),
319
+ };
320
+ }
321
+
322
+ return {
323
+ success: true,
324
+ data: {
325
+ result_tx_id: companionResponse.data.result.result_tx_id,
326
+ },
327
+ };
328
+ }
329
+
330
+ getPermissions = async ({ timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<GetPermissionsResponse> => {
331
+ const companionResponseRaw = await this.getZanoWallet().request('GET_PERMISSIONS', {}, timeoutMs);
332
+
333
+ const companionResponseParsingResult = getPermissionsCompanionResponseSchema.safeParse(companionResponseRaw);
334
+ if (!companionResponseParsingResult.success) {
335
+ throw new ZanoWebError({
336
+ message: 'Failed to parse companion response.',
337
+ code: 'INTERNAL_ERROR',
338
+ });
339
+ }
340
+
341
+ const companionResponse = companionResponseParsingResult.data;
342
+
343
+ if (!('data' in companionResponse)) {
344
+ return {
345
+ success: false,
346
+ error: companionResponse.error,
347
+ };
348
+ }
349
+
350
+ return {
351
+ success: true,
352
+ data: {
353
+ permissions: companionResponse.data,
354
+ },
355
+ };
356
+ }
357
+
358
+ getWalletBalance = async ({ timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<GetWalletBalanceResponse> => {
359
+ const companionResponseRaw = await this.getZanoWallet().request('GET_WALLET_BALANCE', {}, timeoutMs);
360
+
361
+ const companionResponseParsingResult = getWalletBalanceCompanionResponseSchema.safeParse(companionResponseRaw);
362
+ if (!companionResponseParsingResult.success) {
363
+ throw new ZanoWebError({
364
+ message: 'Failed to parse companion response.',
365
+ code: 'INTERNAL_ERROR',
366
+ });
367
+ }
368
+
369
+ const companionResponse = companionResponseParsingResult.data;
370
+
371
+ if (!('data' in companionResponse)) {
372
+ return {
373
+ success: false,
374
+ error: companionResponse.error,
375
+ };
376
+ }
377
+
378
+ if ('error' in companionResponse.data) {
379
+ return {
380
+ success: false,
381
+ error: getWalletRPCErrorCode(companionResponse.data.error.code),
382
+ };
383
+ }
384
+
154
385
  return {
155
386
  success: true,
156
387
  data: companionResponse.data.result,
157
388
  };
158
389
  }
390
+
391
+ transfer = async (params: TransferParams, { timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<TransferResponse> => {
392
+
393
+ // Validation of params
394
+ if ('amount' in params) {
395
+ const amountValidationResult = z.string().regex(NON_NEGATIVE_REAL_NUMBER_REGEX).safeParse(params.amount);
396
+
397
+ if (!amountValidationResult.success) {
398
+ throw new ZanoWebError({
399
+ message: 'Invalid transfer amount provided. Amount must be a valid, finite number greater than zero.',
400
+ code: 'INVALID_PARAMS',
401
+ });
402
+ }
403
+ } else {
404
+ for (const destination of params.destinations) {
405
+ const amountValidationResult = z.string().regex(NON_NEGATIVE_REAL_NUMBER_REGEX).safeParse(destination.amount);
406
+
407
+ if (!amountValidationResult.success) {
408
+ throw new ZanoWebError({
409
+ message: `Invalid transfer amount provided. Amount must be a valid, finite number greater than zero.`,
410
+ code: 'INVALID_PARAMS',
411
+ });
412
+ }
413
+ }
414
+ }
415
+
416
+ const companionRequestParams: CompanionTransferParams = params;
417
+
418
+ const companionResponseRaw = await this.getZanoWallet().request('TRANSFER', companionRequestParams, timeoutMs);
419
+ const companionResponseParsingResult = transferCompanionResponseSchema.safeParse(companionResponseRaw);
420
+ if (!companionResponseParsingResult.success) {
421
+ throw new ZanoWebError({
422
+ message: 'Failed to parse companion response.',
423
+ code: 'INTERNAL_ERROR',
424
+ });
425
+ }
426
+
427
+ const companionResponse = companionResponseParsingResult.data;
428
+
429
+ if (!('data' in companionResponse)) {
430
+ return {
431
+ success: false,
432
+ error: companionResponse.error,
433
+ };
434
+ }
435
+
436
+ if ('error' in companionResponse.data) {
437
+ return {
438
+ success: false,
439
+ error: getWalletRPCErrorCode(companionResponse.data.error.code),
440
+ };
441
+ }
442
+
443
+ return {
444
+ success: true,
445
+ data: companionResponse.data.result,
446
+ };
447
+ }
448
+
449
+ getIonicSwapProposalInfo = async (hexRawProposal: string, { timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<GetIonicSwapProposalInfoResponse> => {
450
+ const companionRequestParams: CompanionGetIonicSwapInfoParams = { hex_raw_proposal: hexRawProposal };
451
+
452
+ const companionResponseRaw = await this.getZanoWallet().request('GET_IONIC_SWAP_PROPOSAL_INFO', companionRequestParams, timeoutMs);
453
+ const companionResponseParsingResult = getIonicSwapProposalInfoCompanionResponseSchema.safeParse(companionResponseRaw);
454
+
455
+ if (!companionResponseParsingResult.success) {
456
+ throw new ZanoWebError({
457
+ message: 'Failed to parse companion response.',
458
+ code: 'INTERNAL_ERROR',
459
+ });
460
+ }
461
+
462
+ const companionResponse = companionResponseParsingResult.data;
463
+
464
+ if (!('data' in companionResponse)) {
465
+ return {
466
+ success: false,
467
+ error: companionResponse.error,
468
+ };
469
+ }
470
+
471
+ if ('error' in companionResponse.data) {
472
+ return {
473
+ success: false,
474
+ error: getWalletRPCErrorCode(companionResponse.data.error.code),
475
+ };
476
+ }
477
+
478
+ return {
479
+ success: true,
480
+ data: companionResponse.data.result,
481
+ };
482
+ }
483
+
484
+ getWhitelist = async ({ timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<GetWhitelistResponse> => {
485
+ const companionResponseRaw = await this.getZanoWallet().request('GET_WHITELIST', {}, timeoutMs);
486
+
487
+ const companionResponseParsingResult = getWhitelistCompanionResponseSchema.safeParse(companionResponseRaw);
488
+ if (!companionResponseParsingResult.success) {
489
+ throw new ZanoWebError({
490
+ message: 'Failed to parse companion response.',
491
+ code: 'INTERNAL_ERROR',
492
+ });
493
+ }
494
+
495
+ const companionResponse = companionResponseParsingResult.data;
496
+
497
+ if (!('data' in companionResponse)) {
498
+ return {
499
+ success: false,
500
+ error: companionResponse.error,
501
+ };
502
+ }
503
+
504
+ return {
505
+ success: true,
506
+ data: companionResponse.data,
507
+ };
508
+ }
509
+
510
+ addWhitelistAsset = async (assetId: string, { timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<AddWhitelistAssetResponse> => {
511
+ const companionRequestParams: CompanionAddWhitelistAssetParams = { asset_id: assetId };
512
+
513
+ const companionResponseRaw = await this.getZanoWallet().request('ADD_WHITELIST_ASSET', companionRequestParams, timeoutMs);
514
+
515
+ const companionResponseParsingResult = addWhitelistAssetCompanionResponseSchema.safeParse(companionResponseRaw);
516
+ if (!companionResponseParsingResult.success) {
517
+ throw new ZanoWebError({
518
+ message: 'Failed to parse companion response.',
519
+ code: 'INTERNAL_ERROR',
520
+ });
521
+ }
522
+
523
+ const companionResponse = companionResponseParsingResult.data;
524
+
525
+ if (!('data' in companionResponse)) {
526
+ return {
527
+ success: false,
528
+ error: companionResponse.error,
529
+ };
530
+ }
531
+
532
+ if ('error' in companionResponse.data) {
533
+ return {
534
+ success: false,
535
+ error: getWalletRPCErrorCode(companionResponse.data.error.code),
536
+ };
537
+ }
538
+
539
+ if (companionResponse.data.result.status !== 'OK') {
540
+ return {
541
+ success: false,
542
+ error: getWalletRPCErrorCodeByStatus(companionResponse.data.result.status),
543
+ };
544
+ }
545
+
546
+ if (!('asset_descriptor' in companionResponse.data.result)) {
547
+ throw new ZanoWebError({
548
+ message: 'Failed to parse companion response.',
549
+ code: 'INTERNAL_ERROR',
550
+ });
551
+ }
552
+
553
+ return {
554
+ success: true,
555
+ data: companionResponse.data.result.asset_descriptor,
556
+ };
557
+ }
558
+
559
+ burnAsset = async (params: BurnAssetParams, { timeoutMs = null }: { timeoutMs?: number | null } = {}): Promise<BurnAssetResponse> => {
560
+ const burnAmountValidationResult = z.string().regex(NON_NEGATIVE_REAL_NUMBER_REGEX).safeParse(params.burnAmount);
561
+
562
+ if (!burnAmountValidationResult.success) {
563
+ throw new ZanoWebError({
564
+ message: 'Invalid "burnAmount" provided. "burnAmount" must be a valid, finite number greater than zero.',
565
+ code: 'INVALID_PARAMS',
566
+ });
567
+ }
568
+
569
+ const nativeAmountValidationResult = z.string().regex(NON_NEGATIVE_REAL_NUMBER_REGEX).optional().safeParse(params.nativeAmount);
570
+
571
+ if (!nativeAmountValidationResult.success) {
572
+ throw new ZanoWebError({
573
+ message: 'Invalid "nativeAmount" provided. "nativeAmount" must be a valid, finite number greater than zero, or undefined.',
574
+ code: 'INVALID_PARAMS',
575
+ });
576
+ }
577
+
578
+ const companionRequestParams: CompanionBurnAssetParams = params;
579
+
580
+ const companionResponseRaw = await this.getZanoWallet().request('BURN_ASSET', companionRequestParams, timeoutMs);
581
+ const companionResponseParsingResult = burnAssetCompanionResponseScheme.safeParse(companionResponseRaw);
582
+
583
+ if (!companionResponseParsingResult.success) {
584
+ throw new ZanoWebError({
585
+ message: 'Failed to parse companion response.',
586
+ code: 'INTERNAL_ERROR',
587
+ });
588
+ }
589
+
590
+ const companionResponse = companionResponseParsingResult.data;
591
+
592
+
593
+ if (!('data' in companionResponse)) {
594
+ return {
595
+ success: false,
596
+ error: companionResponse.error,
597
+ };
598
+ }
599
+
600
+ if ('error' in companionResponse.data) {
601
+ return {
602
+ success: false,
603
+ error: getWalletRPCErrorCode(companionResponse.data.error.code),
604
+ };
605
+ }
606
+
607
+ return {
608
+ success: true,
609
+ data: {
610
+ tx_id: companionResponse.data.result.tx_id,
611
+ }
612
+ }
613
+ }
159
614
  }
160
615
 
161
616
  export default ZanoWallet;