@metamask/delegation-core 0.2.0 → 0.4.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.
package/dist/index.cjs CHANGED
@@ -1,5 +1,55 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/returns.ts
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/internalUtils.ts
2
+
3
+
4
+
5
+
6
+
2
7
  var _utils = require('@metamask/utils');
8
+ var toHexString = ({
9
+ value,
10
+ size
11
+ }) => {
12
+ return value.toString(16).padStart(size * 2, "0");
13
+ };
14
+ var normalizeHex = (value, errorMessage) => {
15
+ if (typeof value === "string") {
16
+ if (!_utils.isHexString.call(void 0, value)) {
17
+ throw new Error(errorMessage);
18
+ }
19
+ return value;
20
+ }
21
+ return _utils.bytesToHex.call(void 0, value);
22
+ };
23
+ var normalizeAddress = (value, errorMessage) => {
24
+ if (typeof value === "string") {
25
+ if (!_utils.isHexString.call(void 0, value) || value.length !== 42) {
26
+ throw new Error(errorMessage);
27
+ }
28
+ return value;
29
+ }
30
+ if (value.length !== 20) {
31
+ throw new Error(errorMessage);
32
+ }
33
+ return _utils.bytesToHex.call(void 0, value);
34
+ };
35
+ var normalizeAddressLowercase = (value, errorMessage) => {
36
+ if (typeof value === "string") {
37
+ if (!_utils.isHexString.call(void 0, value) || value.length !== 42) {
38
+ throw new Error(errorMessage);
39
+ }
40
+ return _utils.bytesToHex.call(void 0, _utils.hexToBytes.call(void 0, value));
41
+ }
42
+ if (value.length !== 20) {
43
+ throw new Error(errorMessage);
44
+ }
45
+ return _utils.bytesToHex.call(void 0, value);
46
+ };
47
+ var concatHex = (parts) => {
48
+ return `0x${parts.map(_utils.remove0x).join("")}`;
49
+ };
50
+
51
+ // src/returns.ts
52
+
3
53
  var defaultOptions = { out: "hex" };
4
54
  function prepareResult(result, options) {
5
55
  if (options.out === "hex") {
@@ -22,14 +72,6 @@ var bytesLikeToBytes = (bytesLike) => {
22
72
  return bytesLike;
23
73
  };
24
74
 
25
- // src/utils.ts
26
- var toHexString = ({
27
- value,
28
- size
29
- }) => {
30
- return value.toString(16).padStart(size * 2, "0");
31
- };
32
-
33
75
  // src/caveats/valueLte.ts
34
76
  function createValueLteTerms(terms, options = defaultOptions) {
35
77
  const { maxValue } = terms;
@@ -103,12 +145,110 @@ function createNativeTokenPeriodTransferTerms(terms, encodingOptions = defaultOp
103
145
  // src/caveats/exactCalldata.ts
104
146
  function createExactCalldataTerms(terms, encodingOptions = defaultOptions) {
105
147
  const { calldata } = terms;
148
+ if (calldata === void 0 || calldata === null) {
149
+ throw new Error("Invalid calldata: calldata is required");
150
+ }
106
151
  if (typeof calldata === "string" && !calldata.startsWith("0x")) {
107
152
  throw new Error("Invalid calldata: must be a hex string starting with 0x");
108
153
  }
109
154
  return prepareResult(calldata, encodingOptions);
110
155
  }
111
156
 
157
+ // src/caveats/exactCalldataBatch.ts
158
+ var _abiutils = require('@metamask/abi-utils');
159
+
160
+ var EXECUTION_ARRAY_ABI = "(address,uint256,bytes)[]";
161
+ function createExactCalldataBatchTerms(terms, encodingOptions = defaultOptions) {
162
+ const { executions } = terms;
163
+ if (executions.length === 0) {
164
+ throw new Error("Invalid executions: array cannot be empty");
165
+ }
166
+ const encodableExecutions = executions.map((execution) => {
167
+ const targetHex = normalizeAddress(
168
+ execution.target,
169
+ "Invalid target: must be a valid address"
170
+ );
171
+ if (execution.value < 0n) {
172
+ throw new Error("Invalid value: must be a non-negative number");
173
+ }
174
+ let callDataHex;
175
+ if (typeof execution.callData === "string") {
176
+ if (!execution.callData.startsWith("0x")) {
177
+ throw new Error(
178
+ "Invalid calldata: must be a hex string starting with 0x"
179
+ );
180
+ }
181
+ callDataHex = execution.callData;
182
+ } else {
183
+ callDataHex = _utils.bytesToHex.call(void 0, execution.callData);
184
+ }
185
+ return [targetHex, execution.value, callDataHex];
186
+ });
187
+ const hexValue = _abiutils.encodeSingle.call(void 0, EXECUTION_ARRAY_ABI, encodableExecutions);
188
+ return prepareResult(hexValue, encodingOptions);
189
+ }
190
+
191
+ // src/caveats/exactExecution.ts
192
+
193
+ function createExactExecutionTerms(terms, encodingOptions = defaultOptions) {
194
+ const { execution } = terms;
195
+ const targetHex = normalizeAddress(
196
+ execution.target,
197
+ "Invalid target: must be a valid address"
198
+ );
199
+ if (execution.value < 0n) {
200
+ throw new Error("Invalid value: must be a non-negative number");
201
+ }
202
+ let callDataHex;
203
+ if (typeof execution.callData === "string") {
204
+ if (!execution.callData.startsWith("0x")) {
205
+ throw new Error(
206
+ "Invalid calldata: must be a hex string starting with 0x"
207
+ );
208
+ }
209
+ callDataHex = execution.callData;
210
+ } else {
211
+ callDataHex = _utils.bytesToHex.call(void 0, execution.callData);
212
+ }
213
+ const valueHex = `0x${toHexString({ value: execution.value, size: 32 })}`;
214
+ const hexValue = concatHex([targetHex, valueHex, callDataHex]);
215
+ return prepareResult(hexValue, encodingOptions);
216
+ }
217
+
218
+ // src/caveats/exactExecutionBatch.ts
219
+
220
+
221
+ var EXECUTION_ARRAY_ABI2 = "(address,uint256,bytes)[]";
222
+ function createExactExecutionBatchTerms(terms, encodingOptions = defaultOptions) {
223
+ const { executions } = terms;
224
+ if (executions.length === 0) {
225
+ throw new Error("Invalid executions: array cannot be empty");
226
+ }
227
+ const encodableExecutions = executions.map((execution) => {
228
+ const targetHex = normalizeAddress(
229
+ execution.target,
230
+ "Invalid target: must be a valid address"
231
+ );
232
+ if (execution.value < 0n) {
233
+ throw new Error("Invalid value: must be a non-negative number");
234
+ }
235
+ let callDataHex;
236
+ if (typeof execution.callData === "string") {
237
+ if (!execution.callData.startsWith("0x")) {
238
+ throw new Error(
239
+ "Invalid calldata: must be a hex string starting with 0x"
240
+ );
241
+ }
242
+ callDataHex = execution.callData;
243
+ } else {
244
+ callDataHex = _utils.bytesToHex.call(void 0, execution.callData);
245
+ }
246
+ return [targetHex, execution.value, callDataHex];
247
+ });
248
+ const hexValue = _abiutils.encodeSingle.call(void 0, EXECUTION_ARRAY_ABI2, encodableExecutions);
249
+ return prepareResult(hexValue, encodingOptions);
250
+ }
251
+
112
252
  // src/caveats/nativeTokenStreaming.ts
113
253
  var TIMESTAMP_UPPER_BOUND_SECONDS2 = 253402300799;
114
254
  function createNativeTokenStreamingTerms(terms, encodingOptions = defaultOptions) {
@@ -141,6 +281,51 @@ function createNativeTokenStreamingTerms(terms, encodingOptions = defaultOptions
141
281
  return prepareResult(hexValue, encodingOptions);
142
282
  }
143
283
 
284
+ // src/caveats/nativeTokenTransferAmount.ts
285
+ function createNativeTokenTransferAmountTerms(terms, encodingOptions = defaultOptions) {
286
+ const { maxAmount } = terms;
287
+ if (maxAmount < 0n) {
288
+ throw new Error("Invalid maxAmount: must be zero or positive");
289
+ }
290
+ const hexValue = `0x${toHexString({ value: maxAmount, size: 32 })}`;
291
+ return prepareResult(hexValue, encodingOptions);
292
+ }
293
+
294
+ // src/caveats/nativeTokenPayment.ts
295
+ function createNativeTokenPaymentTerms(terms, encodingOptions = defaultOptions) {
296
+ const { recipient, amount } = terms;
297
+ const recipientHex = normalizeAddressLowercase(
298
+ recipient,
299
+ "Invalid recipient: must be a valid address"
300
+ );
301
+ if (amount <= 0n) {
302
+ throw new Error("Invalid amount: must be positive");
303
+ }
304
+ const amountHex = `0x${toHexString({ value: amount, size: 32 })}`;
305
+ const hexValue = concatHex([recipientHex, amountHex]);
306
+ return prepareResult(hexValue, encodingOptions);
307
+ }
308
+
309
+ // src/caveats/nativeBalanceChange.ts
310
+ function createNativeBalanceChangeTerms(terms, encodingOptions = defaultOptions) {
311
+ const { recipient, balance, changeType: changeTypeNumber } = terms;
312
+ const recipientHex = normalizeAddressLowercase(
313
+ recipient,
314
+ "Invalid recipient: must be a valid Address"
315
+ );
316
+ if (balance <= 0n) {
317
+ throw new Error("Invalid balance: must be a positive number");
318
+ }
319
+ const changeType = changeTypeNumber;
320
+ if (changeType !== 0 /* Increase */ && changeType !== 1 /* Decrease */) {
321
+ throw new Error("Invalid changeType: must be either Increase or Decrease");
322
+ }
323
+ const changeTypeHex = `0x${toHexString({ value: changeType, size: 1 })}`;
324
+ const balanceHex = `0x${toHexString({ value: balance, size: 32 })}`;
325
+ const hexValue = concatHex([changeTypeHex, recipientHex, balanceHex]);
326
+ return prepareResult(hexValue, encodingOptions);
327
+ }
328
+
144
329
  // src/caveats/erc20Streaming.ts
145
330
 
146
331
  var TIMESTAMP_UPPER_BOUND_SECONDS3 = 253402300799;
@@ -224,6 +409,144 @@ function createERC20TokenPeriodTransferTerms(terms, encodingOptions = defaultOpt
224
409
  return prepareResult(hexValue, encodingOptions);
225
410
  }
226
411
 
412
+ // src/caveats/erc20TransferAmount.ts
413
+ function createERC20TransferAmountTerms(terms, encodingOptions = defaultOptions) {
414
+ const { tokenAddress, maxAmount } = terms;
415
+ const tokenAddressHex = normalizeAddress(
416
+ tokenAddress,
417
+ "Invalid tokenAddress: must be a valid address"
418
+ );
419
+ if (maxAmount <= 0n) {
420
+ throw new Error("Invalid maxAmount: must be a positive number");
421
+ }
422
+ const maxAmountHex = `0x${toHexString({ value: maxAmount, size: 32 })}`;
423
+ const hexValue = concatHex([tokenAddressHex, maxAmountHex]);
424
+ return prepareResult(hexValue, encodingOptions);
425
+ }
426
+
427
+ // src/caveats/erc20BalanceChange.ts
428
+ function createERC20BalanceChangeTerms(terms, encodingOptions = defaultOptions) {
429
+ const {
430
+ tokenAddress,
431
+ recipient,
432
+ balance,
433
+ changeType: changeTypeNumber
434
+ } = terms;
435
+ const tokenAddressHex = normalizeAddressLowercase(
436
+ tokenAddress,
437
+ "Invalid tokenAddress: must be a valid address"
438
+ );
439
+ const recipientHex = normalizeAddressLowercase(
440
+ recipient,
441
+ "Invalid recipient: must be a valid address"
442
+ );
443
+ if (balance <= 0n) {
444
+ throw new Error("Invalid balance: must be a positive number");
445
+ }
446
+ const changeType = changeTypeNumber;
447
+ if (changeType !== 0 /* Increase */ && changeType !== 1 /* Decrease */) {
448
+ throw new Error("Invalid changeType: must be either Increase or Decrease");
449
+ }
450
+ const changeTypeHex = `0x${toHexString({ value: changeType, size: 1 })}`;
451
+ const balanceHex = `0x${toHexString({ value: balance, size: 32 })}`;
452
+ const hexValue = concatHex([
453
+ changeTypeHex,
454
+ tokenAddressHex,
455
+ recipientHex,
456
+ balanceHex
457
+ ]);
458
+ return prepareResult(hexValue, encodingOptions);
459
+ }
460
+
461
+ // src/caveats/erc721BalanceChange.ts
462
+ function createERC721BalanceChangeTerms(terms, encodingOptions = defaultOptions) {
463
+ const {
464
+ tokenAddress,
465
+ recipient,
466
+ amount,
467
+ changeType: changeTypeNumber
468
+ } = terms;
469
+ const tokenAddressHex = normalizeAddressLowercase(
470
+ tokenAddress,
471
+ "Invalid tokenAddress: must be a valid address"
472
+ );
473
+ const recipientHex = normalizeAddressLowercase(
474
+ recipient,
475
+ "Invalid recipient: must be a valid address"
476
+ );
477
+ if (amount <= 0n) {
478
+ throw new Error("Invalid balance: must be a positive number");
479
+ }
480
+ const changeType = changeTypeNumber;
481
+ if (changeType !== 0 /* Increase */ && changeType !== 1 /* Decrease */) {
482
+ throw new Error("Invalid changeType: must be either Increase or Decrease");
483
+ }
484
+ const changeTypeHex = `0x${toHexString({ value: changeType, size: 1 })}`;
485
+ const amountHex = `0x${toHexString({ value: amount, size: 32 })}`;
486
+ const hexValue = concatHex([
487
+ changeTypeHex,
488
+ tokenAddressHex,
489
+ recipientHex,
490
+ amountHex
491
+ ]);
492
+ return prepareResult(hexValue, encodingOptions);
493
+ }
494
+
495
+ // src/caveats/erc721Transfer.ts
496
+ function createERC721TransferTerms(terms, encodingOptions = defaultOptions) {
497
+ const { tokenAddress, tokenId } = terms;
498
+ const tokenAddressHex = normalizeAddress(
499
+ tokenAddress,
500
+ "Invalid tokenAddress: must be a valid address"
501
+ );
502
+ if (tokenId < 0n) {
503
+ throw new Error("Invalid tokenId: must be a non-negative number");
504
+ }
505
+ const tokenIdHex = `0x${toHexString({ value: tokenId, size: 32 })}`;
506
+ const hexValue = concatHex([tokenAddressHex, tokenIdHex]);
507
+ return prepareResult(hexValue, encodingOptions);
508
+ }
509
+
510
+ // src/caveats/erc1155BalanceChange.ts
511
+ function createERC1155BalanceChangeTerms(terms, encodingOptions = defaultOptions) {
512
+ const {
513
+ tokenAddress,
514
+ recipient,
515
+ tokenId,
516
+ balance,
517
+ changeType: changeTypeNumber
518
+ } = terms;
519
+ const tokenAddressHex = normalizeAddressLowercase(
520
+ tokenAddress,
521
+ "Invalid tokenAddress: must be a valid address"
522
+ );
523
+ const recipientHex = normalizeAddressLowercase(
524
+ recipient,
525
+ "Invalid recipient: must be a valid address"
526
+ );
527
+ if (balance <= 0n) {
528
+ throw new Error("Invalid balance: must be a positive number");
529
+ }
530
+ if (tokenId < 0n) {
531
+ throw new Error("Invalid tokenId: must be a non-negative number");
532
+ }
533
+ const changeType = changeTypeNumber;
534
+ if (changeType !== 0 /* Increase */ && changeType !== 1 /* Decrease */) {
535
+ throw new Error("Invalid changeType: must be either Increase or Decrease");
536
+ }
537
+ const changeTypeHex = `0x${toHexString({ value: changeType, size: 1 })}`;
538
+ const tokenIdHex = `0x${toHexString({ value: tokenId, size: 32 })}`;
539
+ const balanceHex = `0x${toHexString({ value: balance, size: 32 })}`;
540
+ const hexValue = concatHex([
541
+ changeTypeHex,
542
+ tokenAddressHex,
543
+ recipientHex,
544
+ tokenIdHex,
545
+ balanceHex
546
+ ]);
547
+ return prepareResult(hexValue, encodingOptions);
548
+ }
549
+
227
550
  // src/caveats/nonce.ts
228
551
 
229
552
  var MAX_NONCE_STRING_LENGTH = 66;
@@ -251,8 +574,269 @@ function createNonceTerms(terms, encodingOptions = defaultOptions) {
251
574
  return prepareResult(hexValue, encodingOptions);
252
575
  }
253
576
 
577
+ // src/caveats/allowedCalldata.ts
578
+
579
+ function createAllowedCalldataTerms(terms, encodingOptions = defaultOptions) {
580
+ const { startIndex, value } = terms;
581
+ if (startIndex < 0) {
582
+ throw new Error("Invalid startIndex: must be zero or positive");
583
+ }
584
+ if (!Number.isInteger(startIndex)) {
585
+ throw new Error("Invalid startIndex: must be a whole number");
586
+ }
587
+ let unprefixedValue;
588
+ if (typeof value === "string") {
589
+ if (!value.startsWith("0x")) {
590
+ throw new Error("Invalid value: must be a hex string starting with 0x");
591
+ }
592
+ unprefixedValue = _utils.remove0x.call(void 0, value);
593
+ } else {
594
+ unprefixedValue = _utils.remove0x.call(void 0, _utils.bytesToHex.call(void 0, value));
595
+ }
596
+ const indexHex = toHexString({ value: startIndex, size: 32 });
597
+ return prepareResult(`0x${indexHex}${unprefixedValue}`, encodingOptions);
598
+ }
599
+
600
+ // src/caveats/allowedMethods.ts
601
+
602
+ var FUNCTION_SELECTOR_STRING_LENGTH = 10;
603
+ var INVALID_SELECTOR_ERROR = "Invalid selector: must be a 4 byte hex string, abi function signature, or AbiFunction";
604
+ function createAllowedMethodsTerms(terms, encodingOptions = defaultOptions) {
605
+ const { selectors } = terms;
606
+ if (!selectors || selectors.length === 0) {
607
+ throw new Error("Invalid selectors: must provide at least one selector");
608
+ }
609
+ const normalizedSelectors = selectors.map((selector) => {
610
+ if (typeof selector === "string") {
611
+ if (_utils.isHexString.call(void 0, selector) && selector.length === FUNCTION_SELECTOR_STRING_LENGTH) {
612
+ return selector;
613
+ }
614
+ throw new Error(INVALID_SELECTOR_ERROR);
615
+ }
616
+ if (selector.length !== 4) {
617
+ throw new Error(INVALID_SELECTOR_ERROR);
618
+ }
619
+ return _utils.bytesToHex.call(void 0, selector);
620
+ });
621
+ const hexValue = concatHex(normalizedSelectors);
622
+ return prepareResult(hexValue, encodingOptions);
623
+ }
624
+
625
+ // src/caveats/allowedTargets.ts
626
+ function createAllowedTargetsTerms(terms, encodingOptions = defaultOptions) {
627
+ const { targets } = terms;
628
+ if (!targets || targets.length === 0) {
629
+ throw new Error(
630
+ "Invalid targets: must provide at least one target address"
631
+ );
632
+ }
633
+ const normalizedTargets = targets.map(
634
+ (target) => normalizeAddress(target, "Invalid targets: must be valid addresses")
635
+ );
636
+ const hexValue = concatHex(normalizedTargets);
637
+ return prepareResult(hexValue, encodingOptions);
638
+ }
639
+
640
+ // src/caveats/argsEqualityCheck.ts
641
+ function createArgsEqualityCheckTerms(terms, encodingOptions = defaultOptions) {
642
+ const { args } = terms;
643
+ if (typeof args === "string" && args === "0x") {
644
+ return prepareResult(args, encodingOptions);
645
+ }
646
+ const hexValue = normalizeHex(
647
+ args,
648
+ "Invalid config: args must be a valid hex string"
649
+ );
650
+ return prepareResult(hexValue, encodingOptions);
651
+ }
652
+
653
+ // src/caveats/blockNumber.ts
654
+ function createBlockNumberTerms(terms, encodingOptions = defaultOptions) {
655
+ const { afterThreshold, beforeThreshold } = terms;
656
+ if (afterThreshold < 0n || beforeThreshold < 0n) {
657
+ throw new Error("Invalid thresholds: block numbers must be non-negative");
658
+ }
659
+ if (afterThreshold === 0n && beforeThreshold === 0n) {
660
+ throw new Error(
661
+ "Invalid thresholds: At least one of afterThreshold or beforeThreshold must be specified"
662
+ );
663
+ }
664
+ if (beforeThreshold !== 0n && afterThreshold >= beforeThreshold) {
665
+ throw new Error(
666
+ "Invalid thresholds: afterThreshold must be less than beforeThreshold if both are specified"
667
+ );
668
+ }
669
+ const afterThresholdHex = toHexString({ value: afterThreshold, size: 16 });
670
+ const beforeThresholdHex = toHexString({ value: beforeThreshold, size: 16 });
671
+ const hexValue = `0x${afterThresholdHex}${beforeThresholdHex}`;
672
+ return prepareResult(hexValue, encodingOptions);
673
+ }
674
+
675
+ // src/caveats/deployed.ts
676
+
677
+ function createDeployedTerms(terms, encodingOptions = defaultOptions) {
678
+ const { contractAddress, salt, bytecode } = terms;
679
+ const contractAddressHex = normalizeAddress(
680
+ contractAddress,
681
+ "Invalid contractAddress: must be a valid Ethereum address"
682
+ );
683
+ const saltHex = normalizeHex(
684
+ salt,
685
+ "Invalid salt: must be a valid hexadecimal string"
686
+ );
687
+ const bytecodeHex = normalizeHex(
688
+ bytecode,
689
+ "Invalid bytecode: must be a valid hexadecimal string"
690
+ );
691
+ const unprefixedSalt = _utils.remove0x.call(void 0, saltHex);
692
+ if (unprefixedSalt.length > 64) {
693
+ throw new Error("Invalid salt: must be a valid hexadecimal string");
694
+ }
695
+ const paddedSalt = `0x${unprefixedSalt.padStart(64, "0")}`;
696
+ const hexValue = concatHex([contractAddressHex, paddedSalt, bytecodeHex]);
697
+ return prepareResult(hexValue, encodingOptions);
698
+ }
699
+
700
+ // src/caveats/id.ts
701
+ var MAX_UINT256 = BigInt(`0x${"f".repeat(64)}`);
702
+ function createIdTerms(terms, encodingOptions = defaultOptions) {
703
+ const { id } = terms;
704
+ let idBigInt;
705
+ if (typeof id === "number") {
706
+ if (!Number.isInteger(id)) {
707
+ throw new Error("Invalid id: must be an integer");
708
+ }
709
+ idBigInt = BigInt(id);
710
+ } else if (typeof id === "bigint") {
711
+ idBigInt = id;
712
+ } else {
713
+ throw new Error("Invalid id: must be a bigint or number");
714
+ }
715
+ if (idBigInt < 0n) {
716
+ throw new Error("Invalid id: must be a non-negative number");
717
+ }
718
+ if (idBigInt > MAX_UINT256) {
719
+ throw new Error("Invalid id: must be less than 2^256");
720
+ }
721
+ const hexValue = `0x${toHexString({ value: idBigInt, size: 32 })}`;
722
+ return prepareResult(hexValue, encodingOptions);
723
+ }
724
+
725
+ // src/caveats/limitedCalls.ts
726
+ function createLimitedCallsTerms(terms, encodingOptions = defaultOptions) {
727
+ const { limit } = terms;
728
+ if (!Number.isInteger(limit)) {
729
+ throw new Error("Invalid limit: must be an integer");
730
+ }
731
+ if (limit <= 0) {
732
+ throw new Error("Invalid limit: must be a positive integer");
733
+ }
734
+ const hexValue = `0x${toHexString({ value: limit, size: 32 })}`;
735
+ return prepareResult(hexValue, encodingOptions);
736
+ }
737
+
738
+ // src/caveats/multiTokenPeriod.ts
739
+ function createMultiTokenPeriodTerms(terms, encodingOptions = defaultOptions) {
740
+ const { tokenConfigs } = terms;
741
+ if (!tokenConfigs || tokenConfigs.length === 0) {
742
+ throw new Error(
743
+ "MultiTokenPeriodBuilder: tokenConfigs array cannot be empty"
744
+ );
745
+ }
746
+ const hexParts = [];
747
+ for (const tokenConfig of tokenConfigs) {
748
+ const tokenHex = normalizeAddress(
749
+ tokenConfig.token,
750
+ `Invalid token address: ${String(tokenConfig.token)}`
751
+ );
752
+ if (tokenConfig.periodAmount <= 0n) {
753
+ throw new Error("Invalid period amount: must be greater than 0");
754
+ }
755
+ if (tokenConfig.periodDuration <= 0) {
756
+ throw new Error("Invalid period duration: must be greater than 0");
757
+ }
758
+ if (tokenConfig.startDate <= 0) {
759
+ throw new Error("Invalid start date: must be greater than 0");
760
+ }
761
+ hexParts.push(
762
+ tokenHex,
763
+ `0x${toHexString({ value: tokenConfig.periodAmount, size: 32 })}`,
764
+ `0x${toHexString({ value: tokenConfig.periodDuration, size: 32 })}`,
765
+ `0x${toHexString({ value: tokenConfig.startDate, size: 32 })}`
766
+ );
767
+ }
768
+ const hexValue = concatHex(hexParts);
769
+ return prepareResult(hexValue, encodingOptions);
770
+ }
771
+
772
+ // src/caveats/ownershipTransfer.ts
773
+ function createOwnershipTransferTerms(terms, encodingOptions = defaultOptions) {
774
+ const { contractAddress } = terms;
775
+ const contractAddressHex = normalizeAddress(
776
+ contractAddress,
777
+ "Invalid contractAddress: must be a valid address"
778
+ );
779
+ return prepareResult(contractAddressHex, encodingOptions);
780
+ }
781
+
782
+ // src/caveats/redeemer.ts
783
+ function createRedeemerTerms(terms, encodingOptions = defaultOptions) {
784
+ const { redeemers } = terms;
785
+ if (!redeemers || redeemers.length === 0) {
786
+ throw new Error(
787
+ "Invalid redeemers: must specify at least one redeemer address"
788
+ );
789
+ }
790
+ const normalizedRedeemers = redeemers.map(
791
+ (redeemer) => normalizeAddress(redeemer, "Invalid redeemers: must be a valid address")
792
+ );
793
+ const hexValue = concatHex(normalizedRedeemers);
794
+ return prepareResult(hexValue, encodingOptions);
795
+ }
796
+
797
+ // src/caveats/specificActionERC20TransferBatch.ts
798
+
799
+ function createSpecificActionERC20TransferBatchTerms(terms, encodingOptions = defaultOptions) {
800
+ const { tokenAddress, recipient, amount, target, calldata } = terms;
801
+ const tokenAddressHex = normalizeAddress(
802
+ tokenAddress,
803
+ "Invalid tokenAddress: must be a valid address"
804
+ );
805
+ const recipientHex = normalizeAddress(
806
+ recipient,
807
+ "Invalid recipient: must be a valid address"
808
+ );
809
+ const targetHex = normalizeAddress(
810
+ target,
811
+ "Invalid target: must be a valid address"
812
+ );
813
+ let calldataHex;
814
+ if (typeof calldata === "string") {
815
+ if (!calldata.startsWith("0x")) {
816
+ throw new Error(
817
+ "Invalid calldata: must be a hex string starting with 0x"
818
+ );
819
+ }
820
+ calldataHex = calldata;
821
+ } else {
822
+ calldataHex = _utils.bytesToHex.call(void 0, calldata);
823
+ }
824
+ if (amount <= 0n) {
825
+ throw new Error("Invalid amount: must be a positive number");
826
+ }
827
+ const amountHex = `0x${toHexString({ value: amount, size: 32 })}`;
828
+ const hexValue = concatHex([
829
+ tokenAddressHex,
830
+ recipientHex,
831
+ amountHex,
832
+ targetHex,
833
+ calldataHex
834
+ ]);
835
+ return prepareResult(hexValue, encodingOptions);
836
+ }
837
+
254
838
  // src/delegation.ts
255
- var _abiutils = require('@metamask/abi-utils');
839
+
256
840
 
257
841
  var _sha3 = require('@noble/hashes/sha3');
258
842
  var ANY_BENEFICIARY = "0x0000000000000000000000000000000000000a11";
@@ -260,6 +844,7 @@ var ROOT_AUTHORITY = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffff
260
844
  var DELEGATION_TYPEHASH = "0x88c1d2ecf185adf710588203a5f263f0ff61be0d33da39792cde19ba9aa4331e";
261
845
  var CAVEAT_TYPEHASH = "0x80ad7e1b04ee6d994a125f4714ca0720908bd80ed16063ec8aee4b88e9253e2d";
262
846
  var DELEGATION_ARRAY_ABI_TYPES = "(address,address,bytes32,(address,bytes,bytes)[],uint256,bytes)[]";
847
+ var DELEGATION_ABI_TYPE = "(address,address,bytes32,(address,bytes,bytes)[],uint256,bytes)";
263
848
  function encodeDelegations(delegations, options = defaultOptions) {
264
849
  let result;
265
850
  if (delegations.length === 0) {
@@ -282,6 +867,22 @@ function encodeDelegations(delegations, options = defaultOptions) {
282
867
  }
283
868
  return prepareResult(result, options);
284
869
  }
870
+ function encodeDelegation(delegation, options = defaultOptions) {
871
+ const encodableStruct = [
872
+ delegation.delegate,
873
+ delegation.delegator,
874
+ delegation.authority,
875
+ delegation.caveats.map((caveat) => [
876
+ caveat.enforcer,
877
+ caveat.terms,
878
+ caveat.args
879
+ ]),
880
+ delegation.salt,
881
+ delegation.signature
882
+ ];
883
+ const result = _abiutils.encodeSingle.call(void 0, DELEGATION_ABI_TYPE, encodableStruct);
884
+ return prepareResult(result, options);
885
+ }
285
886
  var delegationFromDecodedDelegation = (decodedDelegation, convertFn) => {
286
887
  const [delegate, delegator, authority, caveats, salt, signature] = decodedDelegation;
287
888
  return {
@@ -312,6 +913,17 @@ function decodeDelegations(encoded, options = defaultOptions) {
312
913
  (struct) => delegationFromDecodedDelegation(struct, bytesLikeToHex)
313
914
  );
314
915
  }
916
+ function decodeDelegation(encoded, options = defaultOptions) {
917
+ const decodedStruct = _abiutils.decodeSingle.call(void 0,
918
+ DELEGATION_ABI_TYPE,
919
+ encoded
920
+ // return types cannot be inferred from complex ABI types, so we must assert the type
921
+ );
922
+ if (options.out === "bytes") {
923
+ return delegationFromDecodedDelegation(decodedStruct, bytesLikeToBytes);
924
+ }
925
+ return delegationFromDecodedDelegation(decodedStruct, bytesLikeToHex);
926
+ }
315
927
  function hashDelegation(delegation, options = defaultOptions) {
316
928
  const encoded = _abiutils.encode.call(void 0,
317
929
  ["bytes32", "address", "address", "bytes32", "bytes32", "uint256"],
@@ -366,5 +978,30 @@ function getCaveatHash(caveat) {
366
978
 
367
979
 
368
980
 
369
- exports.ANY_BENEFICIARY = ANY_BENEFICIARY; exports.CAVEAT_TYPEHASH = CAVEAT_TYPEHASH; exports.DELEGATION_TYPEHASH = DELEGATION_TYPEHASH; exports.ROOT_AUTHORITY = ROOT_AUTHORITY; exports.createERC20StreamingTerms = createERC20StreamingTerms; exports.createERC20TokenPeriodTransferTerms = createERC20TokenPeriodTransferTerms; exports.createExactCalldataTerms = createExactCalldataTerms; exports.createNativeTokenPeriodTransferTerms = createNativeTokenPeriodTransferTerms; exports.createNativeTokenStreamingTerms = createNativeTokenStreamingTerms; exports.createNonceTerms = createNonceTerms; exports.createTimestampTerms = createTimestampTerms; exports.createValueLteTerms = createValueLteTerms; exports.decodeDelegations = decodeDelegations; exports.encodeDelegations = encodeDelegations; exports.hashDelegation = hashDelegation;
981
+
982
+
983
+
984
+
985
+
986
+
987
+
988
+
989
+
990
+
991
+
992
+
993
+
994
+
995
+
996
+
997
+
998
+
999
+
1000
+
1001
+
1002
+
1003
+
1004
+
1005
+
1006
+ exports.ANY_BENEFICIARY = ANY_BENEFICIARY; exports.CAVEAT_TYPEHASH = CAVEAT_TYPEHASH; exports.DELEGATION_TYPEHASH = DELEGATION_TYPEHASH; exports.ROOT_AUTHORITY = ROOT_AUTHORITY; exports.createAllowedCalldataTerms = createAllowedCalldataTerms; exports.createAllowedMethodsTerms = createAllowedMethodsTerms; exports.createAllowedTargetsTerms = createAllowedTargetsTerms; exports.createArgsEqualityCheckTerms = createArgsEqualityCheckTerms; exports.createBlockNumberTerms = createBlockNumberTerms; exports.createDeployedTerms = createDeployedTerms; exports.createERC1155BalanceChangeTerms = createERC1155BalanceChangeTerms; exports.createERC20BalanceChangeTerms = createERC20BalanceChangeTerms; exports.createERC20StreamingTerms = createERC20StreamingTerms; exports.createERC20TokenPeriodTransferTerms = createERC20TokenPeriodTransferTerms; exports.createERC20TransferAmountTerms = createERC20TransferAmountTerms; exports.createERC721BalanceChangeTerms = createERC721BalanceChangeTerms; exports.createERC721TransferTerms = createERC721TransferTerms; exports.createExactCalldataBatchTerms = createExactCalldataBatchTerms; exports.createExactCalldataTerms = createExactCalldataTerms; exports.createExactExecutionBatchTerms = createExactExecutionBatchTerms; exports.createExactExecutionTerms = createExactExecutionTerms; exports.createIdTerms = createIdTerms; exports.createLimitedCallsTerms = createLimitedCallsTerms; exports.createMultiTokenPeriodTerms = createMultiTokenPeriodTerms; exports.createNativeBalanceChangeTerms = createNativeBalanceChangeTerms; exports.createNativeTokenPaymentTerms = createNativeTokenPaymentTerms; exports.createNativeTokenPeriodTransferTerms = createNativeTokenPeriodTransferTerms; exports.createNativeTokenStreamingTerms = createNativeTokenStreamingTerms; exports.createNativeTokenTransferAmountTerms = createNativeTokenTransferAmountTerms; exports.createNonceTerms = createNonceTerms; exports.createOwnershipTransferTerms = createOwnershipTransferTerms; exports.createRedeemerTerms = createRedeemerTerms; exports.createSpecificActionERC20TransferBatchTerms = createSpecificActionERC20TransferBatchTerms; exports.createTimestampTerms = createTimestampTerms; exports.createValueLteTerms = createValueLteTerms; exports.decodeDelegation = decodeDelegation; exports.decodeDelegations = decodeDelegations; exports.encodeDelegation = encodeDelegation; exports.encodeDelegations = encodeDelegations; exports.hashDelegation = hashDelegation;
370
1007
  //# sourceMappingURL=index.cjs.map