@manifest-network/manifest-mcp-browser 0.1.1 → 0.1.6

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 (131) hide show
  1. package/.github/workflows/publish.yml +4 -2
  2. package/CLAUDE.md +11 -6
  3. package/README.md +1 -1
  4. package/dist/client.d.ts +6 -1
  5. package/dist/client.d.ts.map +1 -1
  6. package/dist/client.js +77 -21
  7. package/dist/client.js.map +1 -1
  8. package/dist/cosmos.d.ts.map +1 -1
  9. package/dist/cosmos.js +7 -57
  10. package/dist/cosmos.js.map +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +16 -25
  13. package/dist/index.js.map +1 -1
  14. package/dist/modules.d.ts +30 -1
  15. package/dist/modules.d.ts.map +1 -1
  16. package/dist/modules.js +98 -1
  17. package/dist/modules.js.map +1 -1
  18. package/dist/modules.test.js +60 -1
  19. package/dist/modules.test.js.map +1 -1
  20. package/dist/queries/auth.d.ts +7 -1
  21. package/dist/queries/auth.d.ts.map +1 -1
  22. package/dist/queries/auth.js +18 -45
  23. package/dist/queries/auth.js.map +1 -1
  24. package/dist/queries/bank.d.ts +7 -1
  25. package/dist/queries/bank.d.ts.map +1 -1
  26. package/dist/queries/bank.js +24 -38
  27. package/dist/queries/bank.js.map +1 -1
  28. package/dist/queries/billing.d.ts +7 -1
  29. package/dist/queries/billing.d.ts.map +1 -1
  30. package/dist/queries/billing.js +28 -54
  31. package/dist/queries/billing.js.map +1 -1
  32. package/dist/queries/distribution.d.ts +7 -1
  33. package/dist/queries/distribution.d.ts.map +1 -1
  34. package/dist/queries/distribution.js +18 -36
  35. package/dist/queries/distribution.js.map +1 -1
  36. package/dist/queries/gov.d.ts +7 -1
  37. package/dist/queries/gov.d.ts.map +1 -1
  38. package/dist/queries/gov.js +24 -41
  39. package/dist/queries/gov.js.map +1 -1
  40. package/dist/queries/index.d.ts +2 -1
  41. package/dist/queries/index.d.ts.map +1 -1
  42. package/dist/queries/index.js +1 -1
  43. package/dist/queries/index.js.map +1 -1
  44. package/dist/queries/sku.d.ts +13 -0
  45. package/dist/queries/sku.d.ts.map +1 -0
  46. package/dist/queries/sku.js +60 -0
  47. package/dist/queries/sku.js.map +1 -0
  48. package/dist/queries/staking.d.ts +7 -1
  49. package/dist/queries/staking.d.ts.map +1 -1
  50. package/dist/queries/staking.js +36 -59
  51. package/dist/queries/staking.js.map +1 -1
  52. package/dist/queries/utils.d.ts +60 -10
  53. package/dist/queries/utils.d.ts.map +1 -1
  54. package/dist/queries/utils.js +80 -12
  55. package/dist/queries/utils.js.map +1 -1
  56. package/dist/queries/utils.test.js +68 -8
  57. package/dist/queries/utils.test.js.map +1 -1
  58. package/dist/transactions/bank.d.ts +2 -2
  59. package/dist/transactions/bank.d.ts.map +1 -1
  60. package/dist/transactions/bank.js +9 -18
  61. package/dist/transactions/bank.js.map +1 -1
  62. package/dist/transactions/billing.d.ts +2 -2
  63. package/dist/transactions/billing.d.ts.map +1 -1
  64. package/dist/transactions/billing.js +125 -89
  65. package/dist/transactions/billing.js.map +1 -1
  66. package/dist/transactions/distribution.d.ts +2 -2
  67. package/dist/transactions/distribution.d.ts.map +1 -1
  68. package/dist/transactions/distribution.js +7 -13
  69. package/dist/transactions/distribution.js.map +1 -1
  70. package/dist/transactions/gov.d.ts +2 -2
  71. package/dist/transactions/gov.d.ts.map +1 -1
  72. package/dist/transactions/gov.js +29 -20
  73. package/dist/transactions/gov.js.map +1 -1
  74. package/dist/transactions/index.d.ts +1 -1
  75. package/dist/transactions/index.d.ts.map +1 -1
  76. package/dist/transactions/index.js +1 -1
  77. package/dist/transactions/index.js.map +1 -1
  78. package/dist/transactions/manifest.d.ts +2 -2
  79. package/dist/transactions/manifest.d.ts.map +1 -1
  80. package/dist/transactions/manifest.js +7 -14
  81. package/dist/transactions/manifest.js.map +1 -1
  82. package/dist/transactions/sku.d.ts +7 -0
  83. package/dist/transactions/sku.d.ts.map +1 -0
  84. package/dist/transactions/sku.js +184 -0
  85. package/dist/transactions/sku.js.map +1 -0
  86. package/dist/transactions/staking.d.ts +2 -2
  87. package/dist/transactions/staking.d.ts.map +1 -1
  88. package/dist/transactions/staking.js +7 -13
  89. package/dist/transactions/staking.js.map +1 -1
  90. package/dist/transactions/utils.d.ts +65 -1
  91. package/dist/transactions/utils.d.ts.map +1 -1
  92. package/dist/transactions/utils.js +123 -2
  93. package/dist/transactions/utils.js.map +1 -1
  94. package/dist/transactions/utils.test.js +351 -1
  95. package/dist/transactions/utils.test.js.map +1 -1
  96. package/dist/types.d.ts +218 -8
  97. package/dist/types.d.ts.map +1 -1
  98. package/dist/types.js.map +1 -1
  99. package/dist/wallet/mnemonic.d.ts +1 -0
  100. package/dist/wallet/mnemonic.d.ts.map +1 -1
  101. package/dist/wallet/mnemonic.js +34 -13
  102. package/dist/wallet/mnemonic.js.map +1 -1
  103. package/package.json +5 -1
  104. package/src/client.ts +84 -21
  105. package/src/cosmos.ts +13 -109
  106. package/src/index.ts +17 -23
  107. package/src/modules.test.ts +62 -0
  108. package/src/modules.ts +155 -5
  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/sku.ts +85 -0
  116. package/src/queries/staking.ts +55 -91
  117. package/src/queries/utils.test.ts +103 -8
  118. package/src/queries/utils.ts +119 -12
  119. package/src/transactions/bank.ts +9 -33
  120. package/src/transactions/billing.ts +155 -141
  121. package/src/transactions/distribution.ts +7 -29
  122. package/src/transactions/gov.ts +33 -37
  123. package/src/transactions/index.ts +1 -1
  124. package/src/transactions/manifest.ts +7 -29
  125. package/src/transactions/sku.ts +232 -0
  126. package/src/transactions/staking.ts +7 -29
  127. package/src/transactions/utils.test.ts +390 -1
  128. package/src/transactions/utils.ts +194 -2
  129. package/src/types.ts +344 -9
  130. package/src/wallet/mnemonic.ts +41 -17
  131. package/.claude/settings.local.json +0 -20
@@ -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,107 @@
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
+ /** Maximum meta hash length in bytes (64 bytes for SHA-512) */
9
+ export const MAX_META_HASH_BYTES = 64;
10
+
11
+ /**
12
+ * Result from extracting a flag from args
13
+ */
14
+ export interface ExtractedFlag {
15
+ /** The flag value, or undefined if flag not present */
16
+ value: string | undefined;
17
+ /** Indices consumed by the flag and its value (for filtering) */
18
+ consumedIndices: number[];
19
+ }
20
+
21
+ /**
22
+ * Extract a flag value from args array.
23
+ * Returns { value, consumedIndices } or { value: undefined, consumedIndices: [] } if flag not present.
24
+ * Throws if flag is present but value is missing or looks like another flag.
25
+ *
26
+ * @param args - The arguments array to search
27
+ * @param flagName - The flag to look for (e.g., '--memo')
28
+ * @param context - Description for error messages (e.g., 'bank send')
29
+ */
30
+ export function extractFlag(
31
+ args: string[],
32
+ flagName: string,
33
+ context: string
34
+ ): ExtractedFlag {
35
+ const flagIndex = args.indexOf(flagName);
36
+ if (flagIndex === -1) {
37
+ return { value: undefined, consumedIndices: [] };
38
+ }
39
+
40
+ const value = args[flagIndex + 1];
41
+ if (!value || value.startsWith('--')) {
42
+ throw new ManifestMCPError(
43
+ ManifestMCPErrorCode.TX_FAILED,
44
+ `${flagName} flag requires a value in ${context}`
45
+ );
46
+ }
47
+
48
+ return { value, consumedIndices: [flagIndex, flagIndex + 1] };
49
+ }
50
+
51
+ /**
52
+ * Filter args to remove consumed flag indices
53
+ */
54
+ export function filterConsumedArgs(args: string[], consumedIndices: number[]): string[] {
55
+ if (consumedIndices.length === 0) {
56
+ return args;
57
+ }
58
+ const consumedSet = new Set(consumedIndices);
59
+ return args.filter((_, index) => !consumedSet.has(index));
60
+ }
61
+
8
62
  /** Maximum memo length (Cosmos SDK default) */
9
63
  export const MAX_MEMO_LENGTH = 256;
10
64
 
11
65
  /**
12
- * Validate args array length
66
+ * Parse a colon-separated pair (e.g., "address:amount", "sku:quantity").
67
+ * Throws with helpful error if format is invalid.
68
+ *
69
+ * @param input - The string to parse (e.g., "manifest1abc:1000umfx")
70
+ * @param leftName - Name of the left value for error messages (e.g., "address")
71
+ * @param rightName - Name of the right value for error messages (e.g., "amount")
72
+ * @param context - Context for error messages (e.g., "multi-send pair")
73
+ * @returns Tuple of [left, right] values
74
+ */
75
+ export function parseColonPair(
76
+ input: string,
77
+ leftName: string,
78
+ rightName: string,
79
+ context: string
80
+ ): [string, string] {
81
+ const colonIndex = input.indexOf(':');
82
+ if (colonIndex === -1) {
83
+ throw new ManifestMCPError(
84
+ ManifestMCPErrorCode.TX_FAILED,
85
+ `Invalid ${context} format: "${input}". Missing colon separator. Expected format: ${leftName}:${rightName}`
86
+ );
87
+ }
88
+ if (colonIndex === 0) {
89
+ throw new ManifestMCPError(
90
+ ManifestMCPErrorCode.TX_FAILED,
91
+ `Invalid ${context} format: "${input}". Missing ${leftName}. Expected format: ${leftName}:${rightName}`
92
+ );
93
+ }
94
+ if (colonIndex === input.length - 1) {
95
+ throw new ManifestMCPError(
96
+ ManifestMCPErrorCode.TX_FAILED,
97
+ `Invalid ${context} format: "${input}". Missing ${rightName}. Expected format: ${leftName}:${rightName}`
98
+ );
99
+ }
100
+ return [input.slice(0, colonIndex), input.slice(colonIndex + 1)];
101
+ }
102
+
103
+ /**
104
+ * Validate args array length (max limit)
13
105
  */
14
106
  export function validateArgsLength(args: string[], context: string): void {
15
107
  if (args.length > MAX_ARGS) {
@@ -20,6 +112,45 @@ export function validateArgsLength(args: string[], context: string): void {
20
112
  }
21
113
  }
22
114
 
115
+ /**
116
+ * Validate that required arguments are present.
117
+ * Provides helpful error messages with received vs expected args.
118
+ *
119
+ * @param args - The arguments array to validate
120
+ * @param minCount - Minimum number of required arguments
121
+ * @param expectedNames - Names of expected arguments for error messages
122
+ * @param context - Context for error messages (e.g., 'bank send', 'staking delegate')
123
+ * @param errorCode - Error code to use (defaults to TX_FAILED)
124
+ * @throws ManifestMCPError if args.length < minCount
125
+ */
126
+ export function requireArgs(
127
+ args: string[],
128
+ minCount: number,
129
+ expectedNames: string[],
130
+ context: string,
131
+ errorCode: ManifestMCPErrorCode = ManifestMCPErrorCode.TX_FAILED
132
+ ): void {
133
+ if (args.length >= minCount) {
134
+ return;
135
+ }
136
+
137
+ const expectedList = expectedNames.slice(0, minCount).join(', ');
138
+ const receivedList = args.length === 0
139
+ ? 'none'
140
+ : args.map(a => `"${a}"`).join(', ');
141
+
142
+ throw new ManifestMCPError(
143
+ errorCode,
144
+ `${context} requires ${minCount} argument(s): ${expectedList}. Received ${args.length}: ${receivedList}`,
145
+ {
146
+ expectedArgs: expectedNames.slice(0, minCount),
147
+ receivedArgs: args,
148
+ receivedCount: args.length,
149
+ requiredCount: minCount,
150
+ }
151
+ );
152
+ }
153
+
23
154
  /**
24
155
  * Validate a bech32 address using @cosmjs/encoding
25
156
  */
@@ -62,6 +193,66 @@ export function validateMemo(memo: string): void {
62
193
  }
63
194
  }
64
195
 
196
+ /**
197
+ * Parse and validate a hex string into Uint8Array.
198
+ * Uses @cosmjs/encoding for browser compatibility.
199
+ *
200
+ * @param hexString - The hex string to parse
201
+ * @param fieldName - Name of the field for error messages
202
+ * @param maxBytes - Maximum allowed byte length
203
+ * @param errorCode - Error code to use (defaults to TX_FAILED)
204
+ * @returns Uint8Array of the parsed bytes
205
+ */
206
+ export function parseHexBytes(
207
+ hexString: string,
208
+ fieldName: string,
209
+ maxBytes: number,
210
+ errorCode: ManifestMCPErrorCode = ManifestMCPErrorCode.TX_FAILED
211
+ ): Uint8Array {
212
+ // Check for empty string
213
+ if (!hexString || hexString.trim() === '') {
214
+ throw new ManifestMCPError(
215
+ errorCode,
216
+ `Invalid ${fieldName}: empty value. Expected a hex string.`
217
+ );
218
+ }
219
+
220
+ // Check even length (each byte is 2 hex chars)
221
+ if (hexString.length % 2 !== 0) {
222
+ throw new ManifestMCPError(
223
+ errorCode,
224
+ `Invalid ${fieldName}: hex string must have even length. Got ${hexString.length} characters.`
225
+ );
226
+ }
227
+
228
+ // Check max length
229
+ const byteLength = hexString.length / 2;
230
+ if (byteLength > maxBytes) {
231
+ throw new ManifestMCPError(
232
+ errorCode,
233
+ `Invalid ${fieldName}: exceeds maximum ${maxBytes} bytes. Got ${byteLength} bytes (${hexString.length} hex chars).`
234
+ );
235
+ }
236
+
237
+ // Use @cosmjs/encoding for browser-compatible hex parsing
238
+ try {
239
+ return fromHex(hexString);
240
+ } catch (error) {
241
+ throw new ManifestMCPError(
242
+ errorCode,
243
+ `Invalid ${fieldName}: "${hexString}". Must contain only hexadecimal characters (0-9, a-f, A-F).`
244
+ );
245
+ }
246
+ }
247
+
248
+ /**
249
+ * Convert Uint8Array to hex string.
250
+ * Uses @cosmjs/encoding for browser compatibility.
251
+ */
252
+ export function bytesToHex(bytes: Uint8Array): string {
253
+ return toHex(bytes);
254
+ }
255
+
65
256
  /**
66
257
  * Safely parse a string to BigInt with proper error handling and configurable error code.
67
258
  * This is the base implementation used by both transaction and query utilities.
@@ -145,6 +336,7 @@ export function buildTxResult(
145
336
  rawLog: result.rawLog || undefined,
146
337
  gasUsed: String(result.gasUsed),
147
338
  gasWanted: String(result.gasWanted),
339
+ events: result.events,
148
340
  };
149
341
 
150
342
  if (waitForConfirmation) {