@beclab/olaresid 0.1.1 → 0.1.2

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 (87) hide show
  1. package/CLI.md +1300 -0
  2. package/README.md +37 -31
  3. package/TAG.md +589 -0
  4. package/dist/abi/RootResolver2ABI.d.ts +54 -0
  5. package/dist/abi/RootResolver2ABI.d.ts.map +1 -0
  6. package/dist/abi/RootResolver2ABI.js +240 -0
  7. package/dist/abi/RootResolver2ABI.js.map +1 -0
  8. package/dist/business/index.d.ts +302 -0
  9. package/dist/business/index.d.ts.map +1 -0
  10. package/dist/business/index.js +1209 -0
  11. package/dist/business/index.js.map +1 -0
  12. package/dist/business/tag-context.d.ts +219 -0
  13. package/dist/business/tag-context.d.ts.map +1 -0
  14. package/dist/business/tag-context.js +537 -0
  15. package/dist/business/tag-context.js.map +1 -0
  16. package/dist/cli.js +2085 -39
  17. package/dist/cli.js.map +1 -1
  18. package/dist/debug.d.ts.map +1 -1
  19. package/dist/debug.js +14 -2
  20. package/dist/debug.js.map +1 -1
  21. package/dist/index.d.ts +50 -2
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +229 -9
  24. package/dist/index.js.map +1 -1
  25. package/dist/utils/crypto-utils.d.ts +130 -0
  26. package/dist/utils/crypto-utils.d.ts.map +1 -0
  27. package/dist/utils/crypto-utils.js +402 -0
  28. package/dist/utils/crypto-utils.js.map +1 -0
  29. package/dist/utils/error-parser.d.ts +35 -0
  30. package/dist/utils/error-parser.d.ts.map +1 -0
  31. package/dist/utils/error-parser.js +202 -0
  32. package/dist/utils/error-parser.js.map +1 -0
  33. package/dist/utils/tag-abi-codec.d.ts +69 -0
  34. package/dist/utils/tag-abi-codec.d.ts.map +1 -0
  35. package/dist/utils/tag-abi-codec.js +144 -0
  36. package/dist/utils/tag-abi-codec.js.map +1 -0
  37. package/dist/utils/tag-type-builder.d.ts +158 -0
  38. package/dist/utils/tag-type-builder.d.ts.map +1 -0
  39. package/dist/utils/tag-type-builder.js +410 -0
  40. package/dist/utils/tag-type-builder.js.map +1 -0
  41. package/examples/crypto-utilities.ts +140 -0
  42. package/examples/domain-context.ts +80 -0
  43. package/examples/generate-mnemonic.ts +149 -0
  44. package/examples/index.ts +1 -1
  45. package/examples/ip.ts +171 -0
  46. package/examples/legacy.ts +10 -10
  47. package/examples/list-wallets.ts +81 -0
  48. package/examples/quasar-demo/.eslintrc.js +23 -0
  49. package/examples/quasar-demo/.quasar/app.js +43 -0
  50. package/examples/quasar-demo/.quasar/client-entry.js +38 -0
  51. package/examples/quasar-demo/.quasar/client-prefetch.js +130 -0
  52. package/examples/quasar-demo/.quasar/quasar-user-options.js +16 -0
  53. package/examples/quasar-demo/README.md +49 -0
  54. package/examples/quasar-demo/index.html +11 -0
  55. package/examples/quasar-demo/package-lock.json +6407 -0
  56. package/examples/quasar-demo/package.json +36 -0
  57. package/examples/quasar-demo/quasar.config.js +73 -0
  58. package/examples/quasar-demo/src/App.vue +13 -0
  59. package/examples/quasar-demo/src/css/app.scss +1 -0
  60. package/examples/quasar-demo/src/layouts/MainLayout.vue +21 -0
  61. package/examples/quasar-demo/src/pages/IndexPage.vue +905 -0
  62. package/examples/quasar-demo/src/router/index.ts +25 -0
  63. package/examples/quasar-demo/src/router/routes.ts +11 -0
  64. package/examples/quasar-demo/tsconfig.json +28 -0
  65. package/examples/register-subdomain.ts +152 -0
  66. package/examples/rsa-keypair.ts +148 -0
  67. package/examples/tag-builder.ts +235 -0
  68. package/examples/tag-management.ts +534 -0
  69. package/examples/tag-nested-tuple.ts +190 -0
  70. package/examples/tag-simple.ts +149 -0
  71. package/examples/tag-tagger.ts +217 -0
  72. package/examples/test-nested-tuple-conversion.ts +143 -0
  73. package/examples/test-type-bytes-parser.ts +70 -0
  74. package/examples/transfer-domain.ts +197 -0
  75. package/examples/wallet-management.ts +196 -0
  76. package/package.json +24 -15
  77. package/src/abi/RootResolver2ABI.ts +237 -0
  78. package/src/business/index.ts +1490 -0
  79. package/src/business/tag-context.ts +713 -0
  80. package/src/cli.ts +2755 -39
  81. package/src/debug.ts +17 -2
  82. package/src/index.ts +300 -14
  83. package/src/utils/crypto-utils.ts +459 -0
  84. package/src/utils/error-parser.ts +225 -0
  85. package/src/utils/tag-abi-codec.ts +158 -0
  86. package/src/utils/tag-type-builder.ts +469 -0
  87. package/tsconfig.json +1 -1
@@ -0,0 +1,459 @@
1
+ /**
2
+ * Cross-platform crypto utilities using Trust Wallet Core
3
+ * Works in both Node.js and Browser environments
4
+ *
5
+ * This implementation uses the same approach as TermiPass to ensure
6
+ * compatibility across the entire Olares ecosystem.
7
+ */
8
+
9
+ import * as bip39 from 'bip39';
10
+ import * as varint from 'varint';
11
+
12
+ // Browser globals type declaration
13
+ declare const window: any;
14
+ declare const btoa: (input: string) => string;
15
+
16
+ // Base58 Bitcoin alphabet
17
+ const BASE58_ALPHABET =
18
+ '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
19
+
20
+ /**
21
+ * Encode bytes to base58btc format
22
+ * This is a pure implementation compatible with multiformats/bases/base58
23
+ */
24
+ function base58Encode(bytes: Uint8Array): string {
25
+ // Convert bytes to bigint
26
+ let num = 0n;
27
+ for (let i = 0; i < bytes.length; i++) {
28
+ num = num * 256n + BigInt(bytes[i]);
29
+ }
30
+
31
+ // Convert to base58
32
+ let encoded = '';
33
+ while (num > 0n) {
34
+ const remainder = Number(num % 58n);
35
+ encoded = BASE58_ALPHABET[remainder] + encoded;
36
+ num = num / 58n;
37
+ }
38
+
39
+ // Add leading '1's for leading zero bytes
40
+ for (let i = 0; i < bytes.length && bytes[i] === 0; i++) {
41
+ encoded = '1' + encoded;
42
+ }
43
+
44
+ // Add 'z' prefix for base58btc multibase
45
+ return 'z' + encoded;
46
+ }
47
+
48
+ export interface RSAPublicKeyData {
49
+ rsaPublicKey: string;
50
+ rsaPrivateKey: string;
51
+ }
52
+
53
+ export interface DIDKeyData {
54
+ owner: string; // Ethereum address
55
+ did: string; // DID string in format: did:key:z...
56
+ mnemonic: string;
57
+ }
58
+
59
+ // ============================================================================
60
+ // Trust Wallet Core Management
61
+ // ============================================================================
62
+
63
+ let walletCore: any = null;
64
+ let walletCoreLoaded = false;
65
+ let loadingPromise: Promise<any> | null = null;
66
+
67
+ /**
68
+ * Load Trust Wallet Core (lazy loading)
69
+ * Works in both Node.js and browser environments
70
+ */
71
+ async function loadWalletCore(): Promise<any> {
72
+ // Return cached instance if already loaded
73
+ if (walletCoreLoaded && walletCore) {
74
+ return walletCore;
75
+ }
76
+
77
+ // If already loading, wait for that promise
78
+ if (loadingPromise) {
79
+ return loadingPromise;
80
+ }
81
+
82
+ // Start loading
83
+ loadingPromise = (async () => {
84
+ try {
85
+ // Check if running in browser or Node.js
86
+ if (
87
+ typeof window !== 'undefined' &&
88
+ typeof require === 'undefined'
89
+ ) {
90
+ // Browser environment with ES modules
91
+ const { initWasm } = await import('@trustwallet/wallet-core');
92
+ walletCore = await initWasm();
93
+ } else {
94
+ // Node.js environment
95
+ const { initWasm } = require('@trustwallet/wallet-core');
96
+ walletCore = await initWasm();
97
+ }
98
+
99
+ walletCoreLoaded = true;
100
+ return walletCore;
101
+ } catch (error) {
102
+ loadingPromise = null; // Reset on error to allow retry
103
+ throw new Error(
104
+ `Failed to load Trust Wallet Core: ${
105
+ error instanceof Error ? error.message : String(error)
106
+ }`
107
+ );
108
+ }
109
+ })();
110
+
111
+ return loadingPromise;
112
+ }
113
+
114
+ // multicodec code for Ed25519 keys (0xed)
115
+ const ED25519_CODEC_ID = varint.encode(parseInt('0xed', 16));
116
+
117
+ // ============================================================================
118
+ // Mnemonic and Key Derivation Functions
119
+ // ============================================================================
120
+
121
+ /**
122
+ * Generate a random BIP39 mnemonic phrase
123
+ * @param wordCount Number of words (12, 15, 18, 21, or 24), default is 12
124
+ * @returns A mnemonic phrase string
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * const mnemonic = generateMnemonic(12);
129
+ * console.log(mnemonic);
130
+ * // Output: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
131
+ * ```
132
+ */
133
+ export function generateMnemonic(wordCount: number = 12): string {
134
+ // Convert word count to entropy bits
135
+ // 12 words = 128 bits, 15 words = 160 bits, etc.
136
+ const strengthMap: Record<number, number> = {
137
+ 12: 128,
138
+ 15: 160,
139
+ 18: 192,
140
+ 21: 224,
141
+ 24: 256
142
+ };
143
+
144
+ const strength = strengthMap[wordCount];
145
+ if (!strength) {
146
+ throw new Error(
147
+ 'Invalid word count. Must be one of: 12, 15, 18, 21, 24'
148
+ );
149
+ }
150
+
151
+ return bip39.generateMnemonic(strength);
152
+ }
153
+
154
+ /**
155
+ * Derive Ethereum address from mnemonic using Trust Wallet Core
156
+ * Uses standard BIP44 derivation path for Ethereum
157
+ *
158
+ * @param mnemonic BIP39 mnemonic phrase
159
+ * @returns Ethereum address (0x...)
160
+ *
161
+ * @example
162
+ * ```typescript
163
+ * const address = await getEthereumAddressFromMnemonic(mnemonic);
164
+ * console.log(address); // "0x9858EfFD232B4033E47d90003D41EC34EcaEda94"
165
+ * ```
166
+ */
167
+ export async function getEthereumAddressFromMnemonic(
168
+ mnemonic: string
169
+ ): Promise<string> {
170
+ // Validate mnemonic
171
+ if (!bip39.validateMnemonic(mnemonic)) {
172
+ throw new Error('Invalid mnemonic phrase');
173
+ }
174
+
175
+ const core = await loadWalletCore();
176
+ const { HDWallet, CoinType } = core;
177
+
178
+ const wallet = HDWallet.createWithMnemonic(mnemonic, '');
179
+ return wallet.getAddressForCoin(CoinType.ethereum);
180
+ }
181
+
182
+ /**
183
+ * Derive EVM-compatible private key from mnemonic using Trust Wallet Core
184
+ * Uses standard BIP44 derivation path for Ethereum (m/44'/60'/0'/0/0)
185
+ *
186
+ * @param mnemonic BIP39 mnemonic phrase
187
+ * @returns Private key in hex format with 0x prefix (0x...)
188
+ *
189
+ * @example
190
+ * ```typescript
191
+ * const privateKey = await getEVMPrivateKeyFromMnemonic(mnemonic);
192
+ * console.log(privateKey); // "0x1234567890abcdef..."
193
+ * // Can be used with ethers.Wallet or setSigner
194
+ * ```
195
+ */
196
+ export async function getEVMPrivateKeyFromMnemonic(
197
+ mnemonic: string
198
+ ): Promise<string> {
199
+ // Validate mnemonic
200
+ if (!bip39.validateMnemonic(mnemonic)) {
201
+ throw new Error('Invalid mnemonic phrase');
202
+ }
203
+
204
+ const core = await loadWalletCore();
205
+ const { HDWallet, CoinType } = core;
206
+
207
+ const wallet = HDWallet.createWithMnemonic(mnemonic, '');
208
+
209
+ // Get private key for Ethereum
210
+ const privateKeyData = wallet.getKeyForCoin(CoinType.ethereum);
211
+
212
+ // Convert to hex string with 0x prefix
213
+ const privateKeyHex =
214
+ '0x' + Buffer.from(privateKeyData.data()).toString('hex');
215
+
216
+ return privateKeyHex;
217
+ }
218
+
219
+ /**
220
+ * Get Ed25519 public key ID from mnemonic (internal helper)
221
+ * This is the same implementation as TermiPass
222
+ *
223
+ * @param mnemonic BIP39 mnemonic phrase
224
+ * @returns Base58btc encoded ID with multicodec prefix
225
+ */
226
+ async function getID(mnemonic: string): Promise<string> {
227
+ const core = await loadWalletCore();
228
+ const { HDWallet, Curve } = core;
229
+
230
+ const wallet = HDWallet.createWithMnemonic(mnemonic, '');
231
+ const privateKey = wallet.getMasterKey(Curve.ed25519);
232
+ const publicKey = privateKey.getPublicKeyEd25519();
233
+
234
+ // Combine multicodec prefix with public key
235
+ const idBytes = new Uint8Array(
236
+ publicKey.data().length + ED25519_CODEC_ID.length
237
+ );
238
+ idBytes.set(ED25519_CODEC_ID, 0);
239
+ idBytes.set(publicKey.data(), ED25519_CODEC_ID.length);
240
+
241
+ // Encode to base58btc
242
+ const id = base58Encode(idBytes);
243
+ return id;
244
+ }
245
+
246
+ /**
247
+ * Generate DID from mnemonic using Trust Wallet Core
248
+ * Uses Ed25519 key and follows did:key specification
249
+ * This implementation is identical to TermiPass
250
+ *
251
+ * @param mnemonic BIP39 mnemonic phrase
252
+ * @returns DID string in format: did:key:z...
253
+ *
254
+ * @example
255
+ * ```typescript
256
+ * const did = await getDIDFromMnemonic(mnemonic);
257
+ * console.log(did);
258
+ * // Output: "did:key:z6MkhtRJqzrZNaeEvGvgJVJv6dyFWDRYpZuHW61e8hjF6Fow"
259
+ * ```
260
+ */
261
+ export async function getDIDFromMnemonic(mnemonic: string): Promise<string> {
262
+ // Validate mnemonic
263
+ if (!bip39.validateMnemonic(mnemonic)) {
264
+ throw new Error('Invalid mnemonic phrase');
265
+ }
266
+
267
+ const id = await getID(mnemonic);
268
+ return `did:key:${id}`;
269
+ }
270
+
271
+ /**
272
+ * Derive both owner (Ethereum address) and DID from existing mnemonic
273
+ *
274
+ * @param mnemonic BIP39 mnemonic phrase
275
+ * @returns Object containing owner (Ethereum address) and DID
276
+ *
277
+ * @example
278
+ * ```typescript
279
+ * const { owner, did } = await deriveDIDFromMnemonic(mnemonic);
280
+ * console.log('Owner:', owner);
281
+ * console.log('DID:', did);
282
+ * ```
283
+ */
284
+ export async function deriveDIDFromMnemonic(mnemonic: string): Promise<{
285
+ owner: string;
286
+ did: string;
287
+ }> {
288
+ // Validate mnemonic once upfront
289
+ if (!bip39.validateMnemonic(mnemonic)) {
290
+ throw new Error('Invalid mnemonic phrase');
291
+ }
292
+
293
+ // Derive both in parallel for better performance
294
+ const [owner, did] = await Promise.all([
295
+ getEthereumAddressFromMnemonic(mnemonic),
296
+ getDIDFromMnemonic(mnemonic)
297
+ ]);
298
+
299
+ return { owner, did };
300
+ }
301
+
302
+ /**
303
+ * Generate mnemonic, Ethereum address, and DID all at once
304
+ *
305
+ * @param wordCount Number of words in mnemonic (default 12)
306
+ * @returns Object containing mnemonic, owner (Ethereum address), and DID
307
+ *
308
+ * @example
309
+ * ```typescript
310
+ * const keyData = await generateDIDKeyData(12);
311
+ * console.log('Mnemonic:', keyData.mnemonic);
312
+ * console.log('Owner:', keyData.owner);
313
+ * console.log('DID:', keyData.did);
314
+ * ```
315
+ */
316
+ export async function generateDIDKeyData(
317
+ wordCount: number = 12
318
+ ): Promise<DIDKeyData> {
319
+ const mnemonic = generateMnemonic(wordCount);
320
+ const { owner, did } = await deriveDIDFromMnemonic(mnemonic);
321
+
322
+ return {
323
+ mnemonic,
324
+ owner,
325
+ did
326
+ };
327
+ }
328
+
329
+ // ============================================================================
330
+ // RSA Key Pair Generation (Browser and Node.js compatible)
331
+ // ============================================================================
332
+
333
+ /**
334
+ * Check if running in Node.js environment
335
+ */
336
+ function isNode(): boolean {
337
+ return (
338
+ typeof process !== 'undefined' &&
339
+ process.versions != null &&
340
+ process.versions.node != null
341
+ );
342
+ }
343
+
344
+ /**
345
+ * Convert ArrayBuffer to PEM format
346
+ */
347
+ function arrayBufferToPem(buffer: ArrayBuffer, label: string): string {
348
+ const binary = String.fromCharCode(...new Uint8Array(buffer));
349
+ const base64 = btoa(binary);
350
+ const formatted = base64.match(/.{1,64}/g)?.join('\n') || base64;
351
+ return `-----BEGIN ${label}-----\n${formatted}\n-----END ${label}-----`;
352
+ }
353
+
354
+ /**
355
+ * Generate RSA key pair in browser using Web Crypto API
356
+ */
357
+ async function generateKeyPairBrowser(
358
+ length: number
359
+ ): Promise<RSAPublicKeyData> {
360
+ if (
361
+ typeof window === 'undefined' ||
362
+ !window.crypto ||
363
+ !window.crypto.subtle
364
+ ) {
365
+ throw new Error('Web Crypto API not available in this browser');
366
+ }
367
+
368
+ const keyPair = await window.crypto.subtle.generateKey(
369
+ {
370
+ name: 'RSA-OAEP',
371
+ modulusLength: length,
372
+ publicExponent: new Uint8Array([1, 0, 1]), // 65537
373
+ hash: 'SHA-256'
374
+ },
375
+ true, // extractable
376
+ ['encrypt', 'decrypt']
377
+ );
378
+
379
+ const publicKeySpki = await window.crypto.subtle.exportKey(
380
+ 'spki',
381
+ keyPair.publicKey
382
+ );
383
+ const privateKeyPkcs8 = await window.crypto.subtle.exportKey(
384
+ 'pkcs8',
385
+ keyPair.privateKey
386
+ );
387
+
388
+ return {
389
+ rsaPublicKey: arrayBufferToPem(publicKeySpki, 'PUBLIC KEY'),
390
+ rsaPrivateKey: arrayBufferToPem(privateKeyPkcs8, 'PRIVATE KEY')
391
+ };
392
+ }
393
+
394
+ /**
395
+ * Generate RSA key pair in Node.js using crypto module
396
+ */
397
+ function generateKeyPairNode(length: number): RSAPublicKeyData {
398
+ // Dynamic require to avoid bundler issues
399
+ const crypto = require('crypto');
400
+
401
+ const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
402
+ modulusLength: length,
403
+ publicKeyEncoding: {
404
+ type: 'spki',
405
+ format: 'pem'
406
+ },
407
+ privateKeyEncoding: {
408
+ type: 'pkcs8',
409
+ format: 'pem'
410
+ }
411
+ });
412
+
413
+ return {
414
+ rsaPublicKey: publicKey,
415
+ rsaPrivateKey: privateKey
416
+ };
417
+ }
418
+
419
+ /**
420
+ * Generate RSA key pair (cross-platform)
421
+ *
422
+ * - In Node.js: Returns immediately (synchronous)
423
+ * - In Browser: Returns a Promise (asynchronous, uses Web Crypto API)
424
+ *
425
+ * @param length Key length in bits (minimum 512, must be multiple of 8, default 2048)
426
+ * @returns Public and private keys in PEM PKCS#8 format
427
+ * @throws Error if key length is invalid or crypto API is unavailable
428
+ *
429
+ * @example
430
+ * ```typescript
431
+ * // Node.js (synchronous, but wrapped in Promise for compatibility)
432
+ * const keyPair = await createRsaKeyPair(2048);
433
+ *
434
+ * // Browser (asynchronous)
435
+ * const keyPair = await createRsaKeyPair(2048);
436
+ *
437
+ * console.log(keyPair.rsaPublicKey); // PEM format
438
+ * console.log(keyPair.rsaPrivateKey); // PEM format
439
+ * ```
440
+ */
441
+ export function createRsaKeyPair(
442
+ length: number = 2048
443
+ ): Promise<RSAPublicKeyData> {
444
+ // Validate key length
445
+ if (length < 512) {
446
+ throw new Error('RSA key length must be at least 512 bits');
447
+ }
448
+ if (length % 8 !== 0) {
449
+ throw new Error('RSA key length must be a multiple of 8');
450
+ }
451
+
452
+ if (isNode()) {
453
+ // Node.js environment: use crypto module (synchronous, but wrap in Promise)
454
+ return Promise.resolve(generateKeyPairNode(length));
455
+ } else {
456
+ // Browser environment: use Web Crypto API (asynchronous)
457
+ return generateKeyPairBrowser(length);
458
+ }
459
+ }
@@ -0,0 +1,225 @@
1
+ /**
2
+ * Contract Error Parser Utility
3
+ *
4
+ * This module provides utilities for parsing contract errors from ethers.js
5
+ * and converting them into user-friendly error messages.
6
+ */
7
+
8
+ /**
9
+ * Parsed error information
10
+ */
11
+ export interface ParsedContractError {
12
+ message: string;
13
+ isNetworkError: boolean;
14
+ errorName?: string;
15
+ errorArgs?: any[];
16
+ }
17
+
18
+ /**
19
+ * Complete error selector mapping for all contracts (TerminusDID, RootResolver, RootTagger2, TagRegistry)
20
+ */
21
+ const ERROR_SELECTORS: { [key: string]: string } = {
22
+ // TerminusDID contract errors
23
+ '0x89c62b64': 'ERC721InvalidOwner',
24
+ '0x7e273289': 'ERC721NonexistentToken',
25
+ '0x64283d7b': 'ERC721IncorrectOwner',
26
+ '0x73c6ac6e': 'ERC721InvalidSender',
27
+ '0x64a0ae92': 'ERC721InvalidReceiver',
28
+ '0x177e802f': 'ERC721InsufficientApproval',
29
+ '0xa9fbf51f': 'ERC721InvalidApprover',
30
+ '0x5b08ba18': 'ERC721InvalidOperator',
31
+ '0x6db122af': 'DomainNotExist',
32
+ '0x27e1f1e5': 'OnlyOperator',
33
+ '0xfb5c3755': 'InvalidDomain',
34
+ '0xc668cf69': 'SubdomainNotAllowed',
35
+ // RootTagger2 contract errors
36
+ '0xa3114200': 'RootTagNoExists',
37
+ '0xe7c4886e': 'AddressNoExists',
38
+ '0xca7dc5c3': 'AddressHasExisted',
39
+ '0x390642d4': 'SignatureIsValidOnlyInOneHour',
40
+ '0x9cc5a685': 'InvalidAddressSignature',
41
+ '0x871a1000': 'InvalidSolanaAddressSignature',
42
+ // RootResolver contract errors
43
+ '0x4a7f394f': 'InvalidAction',
44
+ '0xfde625f6': 'TagNotExist',
45
+ '0x4bdbac1b': 'InvalidBytes',
46
+ // TagRegistry contract errors
47
+ '0x651e7ee4': 'UndefinedTag',
48
+ '0x7417940b': 'RedefinedTag',
49
+ '0x1f7fe073': 'InvalidTagDefinition',
50
+ '0xc2d32a00': 'TagInvalidOp',
51
+ '0xe3f3bb98': 'TagTypeIncompatible',
52
+ '0xa6df187b': 'IncompatibleArrayLengthsForEncode',
53
+ '0xb62b06eb': 'TagTooManyEntries',
54
+ // ABI utility errors
55
+ '0x63df8171': 'InvalidIndex',
56
+ '0x93c19f61': 'InvalidOp',
57
+ '0xb9688461': 'InvalidType',
58
+ '0xaa7feadc': 'InvalidValue',
59
+ // Common errors
60
+ '0x82b42900': 'Unauthorized'
61
+ };
62
+
63
+ /**
64
+ * Network error codes that indicate connectivity issues
65
+ */
66
+ const NETWORK_ERROR_CODES = [
67
+ 'NETWORK_ERROR',
68
+ 'TIMEOUT',
69
+ 'SERVER_ERROR',
70
+ 'ETIMEDOUT',
71
+ 'ECONNREFUSED',
72
+ 'ENOTFOUND'
73
+ ];
74
+
75
+ /**
76
+ * Parse contract error and determine if it's a network error or contract error
77
+ * @param error - The error object from ethers.js
78
+ * @returns Parsed error information with friendly message
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * try {
83
+ * await contract.someFunction();
84
+ * } catch (error) {
85
+ * const parsed = parseContractError(error);
86
+ * if (parsed.isNetworkError) {
87
+ * throw new Error(`Network error: ${parsed.message}`);
88
+ * }
89
+ * console.log(parsed.message); // User-friendly message
90
+ * }
91
+ * ```
92
+ */
93
+ export function parseContractError(error: any): ParsedContractError {
94
+ const errorCode = error.code || '';
95
+ const errorMessage = error.message || String(error);
96
+
97
+ // Check if it's a network error
98
+ const isNetworkError = NETWORK_ERROR_CODES.some(
99
+ (code) => errorCode.includes(code) || errorMessage.includes(code)
100
+ );
101
+
102
+ let errorName: string | undefined;
103
+ let errorArgs: any[] | undefined;
104
+
105
+ // Check for revert with custom error
106
+ if (error.data && typeof error.data === 'string') {
107
+ try {
108
+ const errorSelector = error.data.slice(0, 10);
109
+ errorName = ERROR_SELECTORS[errorSelector];
110
+ } catch (e) {
111
+ // Ignore parsing errors
112
+ }
113
+ }
114
+
115
+ // Check error message for known patterns (fallback)
116
+ if (!errorName && errorMessage) {
117
+ Object.values(ERROR_SELECTORS).forEach((name) => {
118
+ if (errorMessage.includes(name)) {
119
+ errorName = name;
120
+ }
121
+ });
122
+ }
123
+
124
+ // Build friendly error message
125
+ const friendlyMessage = errorName
126
+ ? getFriendlyErrorMessage(errorName)
127
+ : errorMessage;
128
+
129
+ return {
130
+ message: friendlyMessage,
131
+ isNetworkError,
132
+ errorName,
133
+ errorArgs
134
+ };
135
+ }
136
+
137
+ /**
138
+ * Get user-friendly error message for a given error name
139
+ * @param errorName - The contract error name
140
+ * @returns User-friendly error message
141
+ */
142
+ function getFriendlyErrorMessage(errorName: string): string {
143
+ switch (errorName) {
144
+ // RootTagger2 errors
145
+ case 'RootTagNoExists':
146
+ return `Tag does not exist for this domain`;
147
+ case 'AddressNoExists':
148
+ return `Wallet address does not exist`;
149
+ case 'AddressHasExisted':
150
+ return `This wallet address has already been added to the domain`;
151
+ case 'SignatureIsValidOnlyInOneHour':
152
+ return `Signature expired: Signatures are only valid for 1 hour`;
153
+ case 'InvalidAddressSignature':
154
+ return `Invalid signature: The wallet signature verification failed`;
155
+ case 'InvalidSolanaAddressSignature':
156
+ return `Invalid Solana signature: The wallet signature verification failed`;
157
+
158
+ // TerminusDID errors
159
+ case 'ERC721InvalidOwner':
160
+ return `Invalid owner address`;
161
+ case 'ERC721NonexistentToken':
162
+ return `Domain does not exist`;
163
+ case 'ERC721IncorrectOwner':
164
+ return `Incorrect owner: You are not the owner of this domain`;
165
+ case 'ERC721InvalidSender':
166
+ return `Invalid sender address`;
167
+ case 'ERC721InvalidReceiver':
168
+ return `Invalid receiver address`;
169
+ case 'ERC721InsufficientApproval':
170
+ return `Insufficient approval: Caller is not approved to operate this domain`;
171
+ case 'ERC721InvalidApprover':
172
+ return `Invalid approver address`;
173
+ case 'ERC721InvalidOperator':
174
+ return `Invalid operator address`;
175
+ case 'DomainNotExist':
176
+ return `Domain does not exist`;
177
+ case 'OnlyOperator':
178
+ return `Only operator can perform this action`;
179
+ case 'InvalidDomain':
180
+ return `Invalid domain name format`;
181
+ case 'SubdomainNotAllowed':
182
+ return `Subdomain creation is not allowed for this domain`;
183
+
184
+ // RootResolver errors
185
+ case 'InvalidAction':
186
+ return `Invalid action`;
187
+ case 'TagNotExist':
188
+ return `Tag does not exist`;
189
+ case 'InvalidBytes':
190
+ return `Invalid bytes data format`;
191
+
192
+ // TagRegistry errors
193
+ case 'UndefinedTag':
194
+ return `Tag is not defined: You must define the tag type before using it`;
195
+ case 'RedefinedTag':
196
+ return `Tag already exists: This tag has already been defined for this domain`;
197
+ case 'InvalidTagDefinition':
198
+ return `Invalid tag definition: The tag type definition is malformed`;
199
+ case 'TagInvalidOp':
200
+ return `Invalid tag operation: The operation is not supported for this tag type or state`;
201
+ case 'TagTypeIncompatible':
202
+ return `Tag type mismatch: The value type does not match the defined tag type`;
203
+ case 'IncompatibleArrayLengthsForEncode':
204
+ return `Array encoding error: Mismatched array lengths during encoding`;
205
+ case 'TagTooManyEntries':
206
+ return `Too many tag entries: The maximum number of tags has been reached`;
207
+
208
+ // ABI utility errors
209
+ case 'InvalidIndex':
210
+ return `Invalid array index: The index is out of bounds`;
211
+ case 'InvalidOp':
212
+ return `Invalid ABI operation: The operation is not supported for this type`;
213
+ case 'InvalidType':
214
+ return `Invalid ABI type: The type definition is malformed`;
215
+ case 'InvalidValue':
216
+ return `Invalid value: The value cannot be encoded with the specified type`;
217
+
218
+ // Common errors
219
+ case 'Unauthorized':
220
+ return `Unauthorized: You do not have permission to perform this action`;
221
+
222
+ default:
223
+ return `Contract error: ${errorName}`;
224
+ }
225
+ }