@manifest-network/manifest-mcp-browser 0.1.0 → 0.1.5

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 (137) hide show
  1. package/.github/workflows/publish.yml +4 -2
  2. package/CLAUDE.md +9 -3
  3. package/README.md +6 -1
  4. package/dist/client.d.ts +13 -1
  5. package/dist/client.d.ts.map +1 -1
  6. package/dist/client.js +105 -9
  7. package/dist/client.js.map +1 -1
  8. package/dist/config.d.ts +4 -0
  9. package/dist/config.d.ts.map +1 -1
  10. package/dist/config.js +52 -7
  11. package/dist/config.js.map +1 -1
  12. package/dist/config.test.js +128 -0
  13. package/dist/config.test.js.map +1 -1
  14. package/dist/cosmos.d.ts.map +1 -1
  15. package/dist/cosmos.js +11 -57
  16. package/dist/cosmos.js.map +1 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +16 -25
  19. package/dist/index.js.map +1 -1
  20. package/dist/modules.d.ts +30 -1
  21. package/dist/modules.d.ts.map +1 -1
  22. package/dist/modules.js +67 -3
  23. package/dist/modules.js.map +1 -1
  24. package/dist/modules.test.js +58 -1
  25. package/dist/modules.test.js.map +1 -1
  26. package/dist/queries/auth.d.ts +7 -1
  27. package/dist/queries/auth.d.ts.map +1 -1
  28. package/dist/queries/auth.js +18 -45
  29. package/dist/queries/auth.js.map +1 -1
  30. package/dist/queries/bank.d.ts +7 -1
  31. package/dist/queries/bank.d.ts.map +1 -1
  32. package/dist/queries/bank.js +24 -38
  33. package/dist/queries/bank.js.map +1 -1
  34. package/dist/queries/billing.d.ts +7 -1
  35. package/dist/queries/billing.d.ts.map +1 -1
  36. package/dist/queries/billing.js +28 -54
  37. package/dist/queries/billing.js.map +1 -1
  38. package/dist/queries/distribution.d.ts +7 -1
  39. package/dist/queries/distribution.d.ts.map +1 -1
  40. package/dist/queries/distribution.js +18 -36
  41. package/dist/queries/distribution.js.map +1 -1
  42. package/dist/queries/gov.d.ts +7 -1
  43. package/dist/queries/gov.d.ts.map +1 -1
  44. package/dist/queries/gov.js +24 -41
  45. package/dist/queries/gov.js.map +1 -1
  46. package/dist/queries/index.d.ts +2 -1
  47. package/dist/queries/index.d.ts.map +1 -1
  48. package/dist/queries/index.js +1 -1
  49. package/dist/queries/index.js.map +1 -1
  50. package/dist/queries/staking.d.ts +7 -1
  51. package/dist/queries/staking.d.ts.map +1 -1
  52. package/dist/queries/staking.js +36 -59
  53. package/dist/queries/staking.js.map +1 -1
  54. package/dist/queries/utils.d.ts +42 -10
  55. package/dist/queries/utils.d.ts.map +1 -1
  56. package/dist/queries/utils.js +64 -12
  57. package/dist/queries/utils.js.map +1 -1
  58. package/dist/queries/utils.test.js +68 -8
  59. package/dist/queries/utils.test.js.map +1 -1
  60. package/dist/transactions/bank.d.ts +2 -2
  61. package/dist/transactions/bank.d.ts.map +1 -1
  62. package/dist/transactions/bank.js +9 -18
  63. package/dist/transactions/bank.js.map +1 -1
  64. package/dist/transactions/billing.d.ts +2 -2
  65. package/dist/transactions/billing.d.ts.map +1 -1
  66. package/dist/transactions/billing.js +52 -34
  67. package/dist/transactions/billing.js.map +1 -1
  68. package/dist/transactions/distribution.d.ts +2 -2
  69. package/dist/transactions/distribution.d.ts.map +1 -1
  70. package/dist/transactions/distribution.js +7 -13
  71. package/dist/transactions/distribution.js.map +1 -1
  72. package/dist/transactions/gov.d.ts +2 -2
  73. package/dist/transactions/gov.d.ts.map +1 -1
  74. package/dist/transactions/gov.js +32 -17
  75. package/dist/transactions/gov.js.map +1 -1
  76. package/dist/transactions/index.d.ts +1 -1
  77. package/dist/transactions/index.d.ts.map +1 -1
  78. package/dist/transactions/index.js +1 -1
  79. package/dist/transactions/index.js.map +1 -1
  80. package/dist/transactions/manifest.d.ts +2 -2
  81. package/dist/transactions/manifest.d.ts.map +1 -1
  82. package/dist/transactions/manifest.js +7 -14
  83. package/dist/transactions/manifest.js.map +1 -1
  84. package/dist/transactions/staking.d.ts +2 -2
  85. package/dist/transactions/staking.d.ts.map +1 -1
  86. package/dist/transactions/staking.js +7 -13
  87. package/dist/transactions/staking.js.map +1 -1
  88. package/dist/transactions/utils.d.ts +63 -1
  89. package/dist/transactions/utils.d.ts.map +1 -1
  90. package/dist/transactions/utils.js +121 -2
  91. package/dist/transactions/utils.js.map +1 -1
  92. package/dist/transactions/utils.test.js +351 -1
  93. package/dist/transactions/utils.test.js.map +1 -1
  94. package/dist/types.d.ts +212 -8
  95. package/dist/types.d.ts.map +1 -1
  96. package/dist/types.js.map +1 -1
  97. package/dist/wallet/mnemonic.d.ts +1 -0
  98. package/dist/wallet/mnemonic.d.ts.map +1 -1
  99. package/dist/wallet/mnemonic.js +34 -13
  100. package/dist/wallet/mnemonic.js.map +1 -1
  101. package/package.json +11 -6
  102. package/src/client.ts +119 -9
  103. package/src/config.test.ts +156 -0
  104. package/src/config.ts +59 -7
  105. package/src/cosmos.ts +19 -109
  106. package/src/index.ts +17 -23
  107. package/src/modules.test.ts +60 -0
  108. package/src/modules.ts +124 -7
  109. package/src/queries/auth.ts +35 -74
  110. package/src/queries/bank.ts +40 -58
  111. package/src/queries/billing.ts +46 -86
  112. package/src/queries/distribution.ts +35 -59
  113. package/src/queries/gov.ts +40 -64
  114. package/src/queries/index.ts +10 -1
  115. package/src/queries/staking.ts +55 -91
  116. package/src/queries/utils.test.ts +103 -8
  117. package/src/queries/utils.ts +92 -12
  118. package/src/transactions/bank.ts +9 -33
  119. package/src/transactions/billing.ts +64 -59
  120. package/src/transactions/distribution.ts +7 -29
  121. package/src/transactions/gov.ts +40 -34
  122. package/src/transactions/index.ts +1 -1
  123. package/src/transactions/manifest.ts +7 -29
  124. package/src/transactions/staking.ts +7 -29
  125. package/src/transactions/utils.test.ts +390 -1
  126. package/src/transactions/utils.ts +191 -2
  127. package/src/types.ts +328 -9
  128. package/src/wallet/mnemonic.ts +41 -17
  129. package/.claude/settings.local.json +0 -17
  130. package/dist/browser.d.ts.map +0 -1
  131. package/dist/browser.js.map +0 -1
  132. package/dist/queries/manifest.d.ts +0 -10
  133. package/dist/queries/manifest.d.ts.map +0 -1
  134. package/dist/queries/manifest.js +0 -14
  135. package/dist/queries/manifest.js.map +0 -1
  136. package/dist/wallet/keplr.d.ts.map +0 -1
  137. package/dist/wallet/keplr.js.map +0 -1
@@ -1,5 +1,6 @@
1
1
  import { describe, it, expect } from 'vitest';
2
- import { parseAmount, parseBigInt } from './utils.js';
2
+ import { toBech32 } from '@cosmjs/encoding';
3
+ import { parseAmount, parseBigInt, extractFlag, filterConsumedArgs, parseColonPair, validateAddress, validateMemo, validateArgsLength, requireArgs, parseHexBytes, bytesToHex } from './utils.js';
3
4
  import { ManifestMCPError, ManifestMCPErrorCode } from '../types.js';
4
5
 
5
6
  describe('parseAmount', () => {
@@ -127,3 +128,391 @@ describe('parseBigInt', () => {
127
128
  }
128
129
  });
129
130
  });
131
+
132
+ describe('extractFlag', () => {
133
+ it('should extract flag value when present', () => {
134
+ const result = extractFlag(['--memo', 'hello'], '--memo', 'test');
135
+ expect(result.value).toBe('hello');
136
+ expect(result.consumedIndices).toEqual([0, 1]);
137
+ });
138
+
139
+ it('should return undefined when flag not present', () => {
140
+ const result = extractFlag(['arg1', 'arg2'], '--memo', 'test');
141
+ expect(result.value).toBeUndefined();
142
+ expect(result.consumedIndices).toEqual([]);
143
+ });
144
+
145
+ it('should handle flag in middle of args', () => {
146
+ const result = extractFlag(['arg1', '--memo', 'hello', 'arg2'], '--memo', 'test');
147
+ expect(result.value).toBe('hello');
148
+ expect(result.consumedIndices).toEqual([1, 2]);
149
+ });
150
+
151
+ it('should throw when flag has no value', () => {
152
+ expect(() => extractFlag(['--memo'], '--memo', 'test')).toThrow(ManifestMCPError);
153
+ });
154
+
155
+ it('should throw when flag value looks like another flag', () => {
156
+ expect(() => extractFlag(['--memo', '--other'], '--memo', 'test')).toThrow(ManifestMCPError);
157
+ });
158
+
159
+ it('should include context in error message', () => {
160
+ try {
161
+ extractFlag(['--memo'], '--memo', 'bank send');
162
+ } catch (error) {
163
+ expect((error as ManifestMCPError).message).toContain('bank send');
164
+ expect((error as ManifestMCPError).message).toContain('--memo');
165
+ }
166
+ });
167
+ });
168
+
169
+ describe('filterConsumedArgs', () => {
170
+ it('should filter out consumed indices', () => {
171
+ const result = filterConsumedArgs(['a', 'b', 'c', 'd'], [1, 2]);
172
+ expect(result).toEqual(['a', 'd']);
173
+ });
174
+
175
+ it('should return original array when no indices consumed', () => {
176
+ const result = filterConsumedArgs(['a', 'b', 'c'], []);
177
+ expect(result).toEqual(['a', 'b', 'c']);
178
+ });
179
+
180
+ it('should handle all indices consumed', () => {
181
+ const result = filterConsumedArgs(['a', 'b'], [0, 1]);
182
+ expect(result).toEqual([]);
183
+ });
184
+
185
+ it('should handle non-contiguous indices', () => {
186
+ const result = filterConsumedArgs(['a', 'b', 'c', 'd', 'e'], [0, 2, 4]);
187
+ expect(result).toEqual(['b', 'd']);
188
+ });
189
+ });
190
+
191
+ describe('parseColonPair', () => {
192
+ it('should parse valid colon-separated pairs', () => {
193
+ expect(parseColonPair('address:amount', 'address', 'amount', 'test')).toEqual(['address', 'amount']);
194
+ expect(parseColonPair('key:value', 'key', 'value', 'test')).toEqual(['key', 'value']);
195
+ });
196
+
197
+ it('should handle values with colons (takes first colon as separator)', () => {
198
+ const result = parseColonPair('address:100:extra', 'address', 'amount', 'test');
199
+ expect(result).toEqual(['address', '100:extra']);
200
+ });
201
+
202
+ it('should throw for missing colon', () => {
203
+ expect(() => parseColonPair('nodelimiter', 'left', 'right', 'test')).toThrow(ManifestMCPError);
204
+ });
205
+
206
+ it('should throw for empty left side', () => {
207
+ expect(() => parseColonPair(':value', 'left', 'right', 'test')).toThrow(ManifestMCPError);
208
+ });
209
+
210
+ it('should throw for empty right side', () => {
211
+ expect(() => parseColonPair('key:', 'left', 'right', 'test')).toThrow(ManifestMCPError);
212
+ });
213
+
214
+ it('should include context and field names in error messages', () => {
215
+ try {
216
+ parseColonPair('invalid', 'address', 'amount', 'multi-send');
217
+ } catch (error) {
218
+ const message = (error as ManifestMCPError).message;
219
+ expect(message).toContain('multi-send');
220
+ expect(message).toContain('address');
221
+ expect(message).toContain('amount');
222
+ }
223
+ });
224
+
225
+ it('should provide specific error for missing left value', () => {
226
+ try {
227
+ parseColonPair(':value', 'address', 'amount', 'test');
228
+ } catch (error) {
229
+ expect((error as ManifestMCPError).message).toContain('Missing address');
230
+ }
231
+ });
232
+
233
+ it('should provide specific error for missing right value', () => {
234
+ try {
235
+ parseColonPair('key:', 'address', 'amount', 'test');
236
+ } catch (error) {
237
+ expect((error as ManifestMCPError).message).toContain('Missing amount');
238
+ }
239
+ });
240
+ });
241
+
242
+ describe('validateAddress', () => {
243
+ // Generate valid bech32 addresses programmatically using @cosmjs/encoding
244
+ // 20 bytes is the standard Cosmos address length
245
+ const validManifestAddress = toBech32('manifest', new Uint8Array(20));
246
+ const validCosmosAddress = toBech32('cosmos', new Uint8Array(20));
247
+
248
+ it('should accept valid bech32 addresses', () => {
249
+ expect(() => validateAddress(validManifestAddress, 'test')).not.toThrow();
250
+ expect(() => validateAddress(validCosmosAddress, 'test')).not.toThrow();
251
+ });
252
+
253
+ it('should throw for empty address', () => {
254
+ expect(() => validateAddress('', 'recipient')).toThrow(ManifestMCPError);
255
+ expect(() => validateAddress(' ', 'recipient')).toThrow(ManifestMCPError);
256
+ });
257
+
258
+ it('should throw for invalid bech32 address', () => {
259
+ expect(() => validateAddress('notanaddress', 'recipient')).toThrow(ManifestMCPError);
260
+ expect(() => validateAddress('manifest1invalid', 'recipient')).toThrow(ManifestMCPError);
261
+ });
262
+
263
+ it('should use INVALID_ADDRESS error code', () => {
264
+ try {
265
+ validateAddress('invalid', 'recipient');
266
+ } catch (error) {
267
+ expect((error as ManifestMCPError).code).toBe(ManifestMCPErrorCode.INVALID_ADDRESS);
268
+ }
269
+ });
270
+
271
+ it('should include field name in error message', () => {
272
+ try {
273
+ validateAddress('invalid', 'validator address');
274
+ } catch (error) {
275
+ expect((error as ManifestMCPError).message).toContain('validator address');
276
+ }
277
+ });
278
+
279
+ it('should validate expected prefix when provided', () => {
280
+ // Should pass when prefix matches
281
+ expect(() => validateAddress(validManifestAddress, 'test', 'manifest')).not.toThrow();
282
+
283
+ // Should fail when prefix doesn't match
284
+ expect(() => validateAddress(validManifestAddress, 'test', 'cosmos')).toThrow(ManifestMCPError);
285
+ });
286
+
287
+ it('should include expected prefix in error message', () => {
288
+ try {
289
+ validateAddress(validManifestAddress, 'recipient', 'cosmos');
290
+ } catch (error) {
291
+ const message = (error as ManifestMCPError).message;
292
+ expect(message).toContain('cosmos');
293
+ expect(message).toContain('manifest');
294
+ }
295
+ });
296
+ });
297
+
298
+ describe('validateMemo', () => {
299
+ it('should accept memo within limit', () => {
300
+ expect(() => validateMemo('')).not.toThrow();
301
+ expect(() => validateMemo('short memo')).not.toThrow();
302
+ expect(() => validateMemo('a'.repeat(256))).not.toThrow();
303
+ });
304
+
305
+ it('should throw for memo exceeding 256 characters', () => {
306
+ expect(() => validateMemo('a'.repeat(257))).toThrow(ManifestMCPError);
307
+ expect(() => validateMemo('a'.repeat(500))).toThrow(ManifestMCPError);
308
+ });
309
+
310
+ it('should use TX_FAILED error code', () => {
311
+ try {
312
+ validateMemo('a'.repeat(300));
313
+ } catch (error) {
314
+ expect((error as ManifestMCPError).code).toBe(ManifestMCPErrorCode.TX_FAILED);
315
+ }
316
+ });
317
+
318
+ it('should include length info in error message', () => {
319
+ try {
320
+ validateMemo('a'.repeat(300));
321
+ } catch (error) {
322
+ const message = (error as ManifestMCPError).message;
323
+ expect(message).toContain('300');
324
+ expect(message).toContain('256');
325
+ }
326
+ });
327
+ });
328
+
329
+ describe('validateArgsLength', () => {
330
+ it('should accept args within limit', () => {
331
+ expect(() => validateArgsLength([], 'test')).not.toThrow();
332
+ expect(() => validateArgsLength(['a', 'b', 'c'], 'test')).not.toThrow();
333
+ expect(() => validateArgsLength(new Array(100).fill('arg'), 'test')).not.toThrow();
334
+ });
335
+
336
+ it('should throw for args exceeding 100 items', () => {
337
+ expect(() => validateArgsLength(new Array(101).fill('arg'), 'test')).toThrow(ManifestMCPError);
338
+ expect(() => validateArgsLength(new Array(200).fill('arg'), 'test')).toThrow(ManifestMCPError);
339
+ });
340
+
341
+ it('should use TX_FAILED error code', () => {
342
+ try {
343
+ validateArgsLength(new Array(150).fill('arg'), 'test');
344
+ } catch (error) {
345
+ expect((error as ManifestMCPError).code).toBe(ManifestMCPErrorCode.TX_FAILED);
346
+ }
347
+ });
348
+
349
+ it('should include context and count in error message', () => {
350
+ try {
351
+ validateArgsLength(new Array(150).fill('arg'), 'bank send');
352
+ } catch (error) {
353
+ const message = (error as ManifestMCPError).message;
354
+ expect(message).toContain('bank send');
355
+ expect(message).toContain('150');
356
+ expect(message).toContain('100');
357
+ }
358
+ });
359
+ });
360
+
361
+ describe('requireArgs', () => {
362
+ it('should pass when args meet minimum count', () => {
363
+ expect(() => requireArgs(['a', 'b'], 2, ['arg1', 'arg2'], 'test')).not.toThrow();
364
+ expect(() => requireArgs(['a', 'b', 'c'], 2, ['arg1', 'arg2'], 'test')).not.toThrow();
365
+ });
366
+
367
+ it('should pass when zero args required', () => {
368
+ expect(() => requireArgs([], 0, [], 'test')).not.toThrow();
369
+ });
370
+
371
+ it('should throw when args below minimum count', () => {
372
+ expect(() => requireArgs(['a'], 2, ['arg1', 'arg2'], 'test')).toThrow(ManifestMCPError);
373
+ expect(() => requireArgs([], 1, ['arg1'], 'test')).toThrow(ManifestMCPError);
374
+ });
375
+
376
+ it('should use TX_FAILED error code by default', () => {
377
+ try {
378
+ requireArgs([], 1, ['arg1'], 'test');
379
+ } catch (error) {
380
+ expect((error as ManifestMCPError).code).toBe(ManifestMCPErrorCode.TX_FAILED);
381
+ }
382
+ });
383
+
384
+ it('should include context in error message', () => {
385
+ try {
386
+ requireArgs(['a'], 2, ['recipient', 'amount'], 'bank send');
387
+ } catch (error) {
388
+ const message = (error as ManifestMCPError).message;
389
+ expect(message).toContain('bank send');
390
+ expect(message).toContain('recipient');
391
+ expect(message).toContain('amount');
392
+ }
393
+ });
394
+
395
+ it('should include received args in error message', () => {
396
+ try {
397
+ requireArgs(['value1'], 2, ['arg1', 'arg2'], 'test');
398
+ } catch (error) {
399
+ const message = (error as ManifestMCPError).message;
400
+ expect(message).toContain('"value1"');
401
+ expect(message).toContain('Received 1');
402
+ }
403
+ });
404
+
405
+ it('should show "none" when no args received', () => {
406
+ try {
407
+ requireArgs([], 1, ['arg1'], 'test');
408
+ } catch (error) {
409
+ const message = (error as ManifestMCPError).message;
410
+ expect(message).toContain('none');
411
+ }
412
+ });
413
+
414
+ it('should include details with expected and received args', () => {
415
+ try {
416
+ requireArgs(['a'], 2, ['arg1', 'arg2'], 'test');
417
+ } catch (error) {
418
+ const details = (error as ManifestMCPError).details;
419
+ expect(details?.expectedArgs).toEqual(['arg1', 'arg2']);
420
+ expect(details?.receivedArgs).toEqual(['a']);
421
+ expect(details?.receivedCount).toBe(1);
422
+ expect(details?.requiredCount).toBe(2);
423
+ }
424
+ });
425
+
426
+ it('should accept custom error code', () => {
427
+ try {
428
+ requireArgs([], 1, ['arg1'], 'test', ManifestMCPErrorCode.QUERY_FAILED);
429
+ } catch (error) {
430
+ expect((error as ManifestMCPError).code).toBe(ManifestMCPErrorCode.QUERY_FAILED);
431
+ }
432
+ });
433
+ });
434
+
435
+ describe('parseHexBytes', () => {
436
+ it('should parse valid hex strings', () => {
437
+ expect(parseHexBytes('deadbeef', 'test', 100)).toEqual(new Uint8Array([0xde, 0xad, 0xbe, 0xef]));
438
+ expect(parseHexBytes('00', 'test', 100)).toEqual(new Uint8Array([0x00]));
439
+ expect(parseHexBytes('ff', 'test', 100)).toEqual(new Uint8Array([0xff]));
440
+ expect(parseHexBytes('0123456789abcdef', 'test', 100)).toEqual(
441
+ new Uint8Array([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef])
442
+ );
443
+ });
444
+
445
+ it('should be case-insensitive', () => {
446
+ expect(parseHexBytes('DEADBEEF', 'test', 100)).toEqual(new Uint8Array([0xde, 0xad, 0xbe, 0xef]));
447
+ expect(parseHexBytes('DeAdBeEf', 'test', 100)).toEqual(new Uint8Array([0xde, 0xad, 0xbe, 0xef]));
448
+ });
449
+
450
+ it('should throw for empty string', () => {
451
+ expect(() => parseHexBytes('', 'field', 100)).toThrow(ManifestMCPError);
452
+ expect(() => parseHexBytes(' ', 'field', 100)).toThrow(ManifestMCPError);
453
+ });
454
+
455
+ it('should throw for odd-length hex strings', () => {
456
+ expect(() => parseHexBytes('abc', 'field', 100)).toThrow(ManifestMCPError);
457
+ expect(() => parseHexBytes('1', 'field', 100)).toThrow(ManifestMCPError);
458
+ expect(() => parseHexBytes('12345', 'field', 100)).toThrow(ManifestMCPError);
459
+ });
460
+
461
+ it('should throw for exceeding max bytes', () => {
462
+ expect(() => parseHexBytes('deadbeef', 'field', 2)).toThrow(ManifestMCPError);
463
+ expect(() => parseHexBytes('0102030405', 'field', 2)).toThrow(ManifestMCPError);
464
+ });
465
+
466
+ it('should accept exactly max bytes', () => {
467
+ expect(parseHexBytes('deadbeef', 'test', 4)).toEqual(new Uint8Array([0xde, 0xad, 0xbe, 0xef]));
468
+ });
469
+
470
+ it('should throw for invalid hex characters', () => {
471
+ expect(() => parseHexBytes('ghij', 'field', 100)).toThrow(ManifestMCPError);
472
+ expect(() => parseHexBytes('12gg', 'field', 100)).toThrow(ManifestMCPError);
473
+ expect(() => parseHexBytes('xy00', 'field', 100)).toThrow(ManifestMCPError);
474
+ });
475
+
476
+ it('should use TX_FAILED error code by default', () => {
477
+ try {
478
+ parseHexBytes('', 'field', 100);
479
+ } catch (error) {
480
+ expect((error as ManifestMCPError).code).toBe(ManifestMCPErrorCode.TX_FAILED);
481
+ }
482
+ });
483
+
484
+ it('should accept custom error code', () => {
485
+ try {
486
+ parseHexBytes('', 'field', 100, ManifestMCPErrorCode.QUERY_FAILED);
487
+ } catch (error) {
488
+ expect((error as ManifestMCPError).code).toBe(ManifestMCPErrorCode.QUERY_FAILED);
489
+ }
490
+ });
491
+
492
+ it('should include field name in error message', () => {
493
+ try {
494
+ parseHexBytes('', 'my-custom-field', 100);
495
+ } catch (error) {
496
+ expect((error as ManifestMCPError).message).toContain('my-custom-field');
497
+ }
498
+ });
499
+ });
500
+
501
+ describe('bytesToHex', () => {
502
+ it('should convert bytes to hex string', () => {
503
+ expect(bytesToHex(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))).toBe('deadbeef');
504
+ expect(bytesToHex(new Uint8Array([0x00]))).toBe('00');
505
+ expect(bytesToHex(new Uint8Array([0xff]))).toBe('ff');
506
+ expect(bytesToHex(new Uint8Array([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]))).toBe('0123456789abcdef');
507
+ });
508
+
509
+ it('should handle empty array', () => {
510
+ expect(bytesToHex(new Uint8Array([]))).toBe('');
511
+ });
512
+
513
+ it('should round-trip with parseHexBytes', () => {
514
+ const original = 'deadbeefcafe1234';
515
+ const bytes = parseHexBytes(original, 'test', 100);
516
+ expect(bytesToHex(bytes)).toBe(original);
517
+ });
518
+ });
@@ -1,15 +1,104 @@
1
1
  import { SigningStargateClient } from '@cosmjs/stargate';
2
- import { fromBech32 } from '@cosmjs/encoding';
2
+ import { fromBech32, fromHex, toHex } from '@cosmjs/encoding';
3
3
  import { ManifestMCPError, ManifestMCPErrorCode, CosmosTxResult } from '../types.js';
4
4
 
5
5
  /** Maximum number of arguments allowed */
6
6
  export const MAX_ARGS = 100;
7
7
 
8
+ /**
9
+ * Result from extracting a flag from args
10
+ */
11
+ export interface ExtractedFlag {
12
+ /** The flag value, or undefined if flag not present */
13
+ value: string | undefined;
14
+ /** Indices consumed by the flag and its value (for filtering) */
15
+ consumedIndices: number[];
16
+ }
17
+
18
+ /**
19
+ * Extract a flag value from args array.
20
+ * Returns { value, consumedIndices } or { value: undefined, consumedIndices: [] } if flag not present.
21
+ * Throws if flag is present but value is missing or looks like another flag.
22
+ *
23
+ * @param args - The arguments array to search
24
+ * @param flagName - The flag to look for (e.g., '--memo')
25
+ * @param context - Description for error messages (e.g., 'bank send')
26
+ */
27
+ export function extractFlag(
28
+ args: string[],
29
+ flagName: string,
30
+ context: string
31
+ ): ExtractedFlag {
32
+ const flagIndex = args.indexOf(flagName);
33
+ if (flagIndex === -1) {
34
+ return { value: undefined, consumedIndices: [] };
35
+ }
36
+
37
+ const value = args[flagIndex + 1];
38
+ if (!value || value.startsWith('--')) {
39
+ throw new ManifestMCPError(
40
+ ManifestMCPErrorCode.TX_FAILED,
41
+ `${flagName} flag requires a value in ${context}`
42
+ );
43
+ }
44
+
45
+ return { value, consumedIndices: [flagIndex, flagIndex + 1] };
46
+ }
47
+
48
+ /**
49
+ * Filter args to remove consumed flag indices
50
+ */
51
+ export function filterConsumedArgs(args: string[], consumedIndices: number[]): string[] {
52
+ if (consumedIndices.length === 0) {
53
+ return args;
54
+ }
55
+ const consumedSet = new Set(consumedIndices);
56
+ return args.filter((_, index) => !consumedSet.has(index));
57
+ }
58
+
8
59
  /** Maximum memo length (Cosmos SDK default) */
9
60
  export const MAX_MEMO_LENGTH = 256;
10
61
 
11
62
  /**
12
- * Validate args array length
63
+ * Parse a colon-separated pair (e.g., "address:amount", "sku:quantity").
64
+ * Throws with helpful error if format is invalid.
65
+ *
66
+ * @param input - The string to parse (e.g., "manifest1abc:1000umfx")
67
+ * @param leftName - Name of the left value for error messages (e.g., "address")
68
+ * @param rightName - Name of the right value for error messages (e.g., "amount")
69
+ * @param context - Context for error messages (e.g., "multi-send pair")
70
+ * @returns Tuple of [left, right] values
71
+ */
72
+ export function parseColonPair(
73
+ input: string,
74
+ leftName: string,
75
+ rightName: string,
76
+ context: string
77
+ ): [string, string] {
78
+ const colonIndex = input.indexOf(':');
79
+ if (colonIndex === -1) {
80
+ throw new ManifestMCPError(
81
+ ManifestMCPErrorCode.TX_FAILED,
82
+ `Invalid ${context} format: "${input}". Missing colon separator. Expected format: ${leftName}:${rightName}`
83
+ );
84
+ }
85
+ if (colonIndex === 0) {
86
+ throw new ManifestMCPError(
87
+ ManifestMCPErrorCode.TX_FAILED,
88
+ `Invalid ${context} format: "${input}". Missing ${leftName}. Expected format: ${leftName}:${rightName}`
89
+ );
90
+ }
91
+ if (colonIndex === input.length - 1) {
92
+ throw new ManifestMCPError(
93
+ ManifestMCPErrorCode.TX_FAILED,
94
+ `Invalid ${context} format: "${input}". Missing ${rightName}. Expected format: ${leftName}:${rightName}`
95
+ );
96
+ }
97
+ return [input.slice(0, colonIndex), input.slice(colonIndex + 1)];
98
+ }
99
+
100
+ /**
101
+ * Validate args array length (max limit)
13
102
  */
14
103
  export function validateArgsLength(args: string[], context: string): void {
15
104
  if (args.length > MAX_ARGS) {
@@ -20,6 +109,45 @@ export function validateArgsLength(args: string[], context: string): void {
20
109
  }
21
110
  }
22
111
 
112
+ /**
113
+ * Validate that required arguments are present.
114
+ * Provides helpful error messages with received vs expected args.
115
+ *
116
+ * @param args - The arguments array to validate
117
+ * @param minCount - Minimum number of required arguments
118
+ * @param expectedNames - Names of expected arguments for error messages
119
+ * @param context - Context for error messages (e.g., 'bank send', 'staking delegate')
120
+ * @param errorCode - Error code to use (defaults to TX_FAILED)
121
+ * @throws ManifestMCPError if args.length < minCount
122
+ */
123
+ export function requireArgs(
124
+ args: string[],
125
+ minCount: number,
126
+ expectedNames: string[],
127
+ context: string,
128
+ errorCode: ManifestMCPErrorCode = ManifestMCPErrorCode.TX_FAILED
129
+ ): void {
130
+ if (args.length >= minCount) {
131
+ return;
132
+ }
133
+
134
+ const expectedList = expectedNames.slice(0, minCount).join(', ');
135
+ const receivedList = args.length === 0
136
+ ? 'none'
137
+ : args.map(a => `"${a}"`).join(', ');
138
+
139
+ throw new ManifestMCPError(
140
+ errorCode,
141
+ `${context} requires ${minCount} argument(s): ${expectedList}. Received ${args.length}: ${receivedList}`,
142
+ {
143
+ expectedArgs: expectedNames.slice(0, minCount),
144
+ receivedArgs: args,
145
+ receivedCount: args.length,
146
+ requiredCount: minCount,
147
+ }
148
+ );
149
+ }
150
+
23
151
  /**
24
152
  * Validate a bech32 address using @cosmjs/encoding
25
153
  */
@@ -62,6 +190,66 @@ export function validateMemo(memo: string): void {
62
190
  }
63
191
  }
64
192
 
193
+ /**
194
+ * Parse and validate a hex string into Uint8Array.
195
+ * Uses @cosmjs/encoding for browser compatibility.
196
+ *
197
+ * @param hexString - The hex string to parse
198
+ * @param fieldName - Name of the field for error messages
199
+ * @param maxBytes - Maximum allowed byte length
200
+ * @param errorCode - Error code to use (defaults to TX_FAILED)
201
+ * @returns Uint8Array of the parsed bytes
202
+ */
203
+ export function parseHexBytes(
204
+ hexString: string,
205
+ fieldName: string,
206
+ maxBytes: number,
207
+ errorCode: ManifestMCPErrorCode = ManifestMCPErrorCode.TX_FAILED
208
+ ): Uint8Array {
209
+ // Check for empty string
210
+ if (!hexString || hexString.trim() === '') {
211
+ throw new ManifestMCPError(
212
+ errorCode,
213
+ `Invalid ${fieldName}: empty value. Expected a hex string.`
214
+ );
215
+ }
216
+
217
+ // Check even length (each byte is 2 hex chars)
218
+ if (hexString.length % 2 !== 0) {
219
+ throw new ManifestMCPError(
220
+ errorCode,
221
+ `Invalid ${fieldName}: hex string must have even length. Got ${hexString.length} characters.`
222
+ );
223
+ }
224
+
225
+ // Check max length
226
+ const byteLength = hexString.length / 2;
227
+ if (byteLength > maxBytes) {
228
+ throw new ManifestMCPError(
229
+ errorCode,
230
+ `Invalid ${fieldName}: exceeds maximum ${maxBytes} bytes. Got ${byteLength} bytes (${hexString.length} hex chars).`
231
+ );
232
+ }
233
+
234
+ // Use @cosmjs/encoding for browser-compatible hex parsing
235
+ try {
236
+ return fromHex(hexString);
237
+ } catch (error) {
238
+ throw new ManifestMCPError(
239
+ errorCode,
240
+ `Invalid ${fieldName}: "${hexString}". Must contain only hexadecimal characters (0-9, a-f, A-F).`
241
+ );
242
+ }
243
+ }
244
+
245
+ /**
246
+ * Convert Uint8Array to hex string.
247
+ * Uses @cosmjs/encoding for browser compatibility.
248
+ */
249
+ export function bytesToHex(bytes: Uint8Array): string {
250
+ return toHex(bytes);
251
+ }
252
+
65
253
  /**
66
254
  * Safely parse a string to BigInt with proper error handling and configurable error code.
67
255
  * This is the base implementation used by both transaction and query utilities.
@@ -145,6 +333,7 @@ export function buildTxResult(
145
333
  rawLog: result.rawLog || undefined,
146
334
  gasUsed: String(result.gasUsed),
147
335
  gasWanted: String(result.gasWanted),
336
+ events: result.events,
148
337
  };
149
338
 
150
339
  if (waitForConfirmation) {