@metamask/delegation-core 1.0.0 → 2.0.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/CHANGELOG.md +19 -1
- package/README.md +8 -8
- package/dist/index.cjs +621 -43
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +143 -66
- package/dist/index.d.ts +143 -66
- package/dist/index.mjs +626 -48
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
// src/caveats/types.ts
|
|
2
|
+
var BalanceChangeType = /* @__PURE__ */ ((BalanceChangeType2) => {
|
|
3
|
+
BalanceChangeType2[BalanceChangeType2["Increase"] = 0] = "Increase";
|
|
4
|
+
BalanceChangeType2[BalanceChangeType2["Decrease"] = 1] = "Decrease";
|
|
5
|
+
return BalanceChangeType2;
|
|
6
|
+
})(BalanceChangeType || {});
|
|
7
|
+
|
|
1
8
|
// src/internalUtils.ts
|
|
2
9
|
import {
|
|
3
10
|
bytesToHex,
|
|
@@ -47,6 +54,52 @@ var normalizeAddressLowercase = (value, errorMessage) => {
|
|
|
47
54
|
var concatHex = (parts) => {
|
|
48
55
|
return `0x${parts.map(remove0x).join("")}`;
|
|
49
56
|
};
|
|
57
|
+
var extractBigInt = (value, offset, size) => {
|
|
58
|
+
const start = 2 + offset * 2;
|
|
59
|
+
const end = start + size * 2;
|
|
60
|
+
const slice = value.slice(start, end);
|
|
61
|
+
return BigInt(`0x${slice}`);
|
|
62
|
+
};
|
|
63
|
+
var extractNumber = (value, offset, size) => {
|
|
64
|
+
const bigIntValue = extractBigInt(value, offset, size);
|
|
65
|
+
if (bigIntValue > Number.MAX_SAFE_INTEGER) {
|
|
66
|
+
throw new Error("Number is too large");
|
|
67
|
+
}
|
|
68
|
+
return Number(bigIntValue);
|
|
69
|
+
};
|
|
70
|
+
var extractAddress = (value, offset) => {
|
|
71
|
+
const start = 2 + offset * 2;
|
|
72
|
+
const end = start + 40;
|
|
73
|
+
return `0x${value.slice(start, end)}`;
|
|
74
|
+
};
|
|
75
|
+
var extractHex = (value, offset, size) => {
|
|
76
|
+
const start = 2 + offset * 2;
|
|
77
|
+
const end = start + size * 2;
|
|
78
|
+
return `0x${value.slice(start, end)}`;
|
|
79
|
+
};
|
|
80
|
+
var extractRemainingHex = (value, offset) => {
|
|
81
|
+
const start = 2 + offset * 2;
|
|
82
|
+
return `0x${value.slice(start)}`;
|
|
83
|
+
};
|
|
84
|
+
function getByteLength(value) {
|
|
85
|
+
return (value.length - 2) / 2;
|
|
86
|
+
}
|
|
87
|
+
function assertHexByteExactLength(hexTerms, expectedBytes, errorMessage) {
|
|
88
|
+
if (getByteLength(hexTerms) !== expectedBytes) {
|
|
89
|
+
throw new Error(errorMessage);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function assertHexByteLengthAtLeastOneMultipleOf(hexTerms, unitBytes, errorMessage) {
|
|
93
|
+
const byteLength = getByteLength(hexTerms);
|
|
94
|
+
if (byteLength === 0 || byteLength % unitBytes !== 0) {
|
|
95
|
+
throw new Error(errorMessage);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function assertHexBytesMinLength(hexTerms, minBytes, errorMessage) {
|
|
99
|
+
if (getByteLength(hexTerms) < minBytes) {
|
|
100
|
+
throw new Error(errorMessage);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
50
103
|
|
|
51
104
|
// src/returns.ts
|
|
52
105
|
import { bytesToHex as bytesToHex2, hexToBytes as hexToBytes2 } from "@metamask/utils";
|
|
@@ -81,47 +134,64 @@ function createValueLteTerms(terms, options = defaultOptions) {
|
|
|
81
134
|
const hexValue = toHexString({ value: maxValue, size: 32 });
|
|
82
135
|
return prepareResult(hexValue, options);
|
|
83
136
|
}
|
|
137
|
+
function decodeValueLteTerms(terms) {
|
|
138
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
139
|
+
assertHexByteExactLength(
|
|
140
|
+
hexTerms,
|
|
141
|
+
32,
|
|
142
|
+
"Invalid ValueLte terms: must be exactly 32 bytes"
|
|
143
|
+
);
|
|
144
|
+
const maxValue = extractBigInt(hexTerms, 0, 32);
|
|
145
|
+
return { maxValue };
|
|
146
|
+
}
|
|
84
147
|
|
|
85
148
|
// src/caveats/timestamp.ts
|
|
86
149
|
var TIMESTAMP_UPPER_BOUND_SECONDS = 253402300799;
|
|
87
150
|
function createTimestampTerms(terms, encodingOptions = defaultOptions) {
|
|
88
|
-
const {
|
|
89
|
-
if (
|
|
90
|
-
throw new Error(
|
|
91
|
-
"Invalid timestampAfterThreshold: must be zero or positive"
|
|
92
|
-
);
|
|
151
|
+
const { afterThreshold, beforeThreshold } = terms;
|
|
152
|
+
if (afterThreshold < 0) {
|
|
153
|
+
throw new Error("Invalid afterThreshold: must be zero or positive");
|
|
93
154
|
}
|
|
94
|
-
if (
|
|
95
|
-
throw new Error(
|
|
96
|
-
"Invalid timestampBeforeThreshold: must be zero or positive"
|
|
97
|
-
);
|
|
155
|
+
if (beforeThreshold < 0) {
|
|
156
|
+
throw new Error("Invalid beforeThreshold: must be zero or positive");
|
|
98
157
|
}
|
|
99
|
-
if (
|
|
158
|
+
if (beforeThreshold > TIMESTAMP_UPPER_BOUND_SECONDS) {
|
|
100
159
|
throw new Error(
|
|
101
|
-
`Invalid
|
|
160
|
+
`Invalid beforeThreshold: must be less than or equal to ${TIMESTAMP_UPPER_BOUND_SECONDS}`
|
|
102
161
|
);
|
|
103
162
|
}
|
|
104
|
-
if (
|
|
163
|
+
if (afterThreshold > TIMESTAMP_UPPER_BOUND_SECONDS) {
|
|
105
164
|
throw new Error(
|
|
106
|
-
`Invalid
|
|
165
|
+
`Invalid afterThreshold: must be less than or equal to ${TIMESTAMP_UPPER_BOUND_SECONDS}`
|
|
107
166
|
);
|
|
108
167
|
}
|
|
109
|
-
if (
|
|
168
|
+
if (beforeThreshold !== 0 && afterThreshold >= beforeThreshold) {
|
|
110
169
|
throw new Error(
|
|
111
|
-
"Invalid thresholds:
|
|
170
|
+
"Invalid thresholds: beforeThreshold must be greater than afterThreshold when both are specified"
|
|
112
171
|
);
|
|
113
172
|
}
|
|
114
173
|
const afterThresholdHex = toHexString({
|
|
115
|
-
value:
|
|
174
|
+
value: afterThreshold,
|
|
116
175
|
size: 16
|
|
117
176
|
});
|
|
118
177
|
const beforeThresholdHex = toHexString({
|
|
119
|
-
value:
|
|
178
|
+
value: beforeThreshold,
|
|
120
179
|
size: 16
|
|
121
180
|
});
|
|
122
181
|
const hexValue = `0x${afterThresholdHex}${beforeThresholdHex}`;
|
|
123
182
|
return prepareResult(hexValue, encodingOptions);
|
|
124
183
|
}
|
|
184
|
+
function decodeTimestampTerms(terms) {
|
|
185
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
186
|
+
assertHexByteExactLength(
|
|
187
|
+
hexTerms,
|
|
188
|
+
32,
|
|
189
|
+
"Invalid Timestamp terms: must be exactly 32 bytes"
|
|
190
|
+
);
|
|
191
|
+
const afterThreshold = extractNumber(hexTerms, 0, 16);
|
|
192
|
+
const beforeThreshold = extractNumber(hexTerms, 16, 16);
|
|
193
|
+
return { afterThreshold, beforeThreshold };
|
|
194
|
+
}
|
|
125
195
|
|
|
126
196
|
// src/caveats/nativeTokenPeriodTransfer.ts
|
|
127
197
|
function createNativeTokenPeriodTransferTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -141,6 +211,18 @@ function createNativeTokenPeriodTransferTerms(terms, encodingOptions = defaultOp
|
|
|
141
211
|
const hexValue = `0x${periodAmountHex}${periodDurationHex}${startDateHex}`;
|
|
142
212
|
return prepareResult(hexValue, encodingOptions);
|
|
143
213
|
}
|
|
214
|
+
function decodeNativeTokenPeriodTransferTerms(terms) {
|
|
215
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
216
|
+
assertHexByteExactLength(
|
|
217
|
+
hexTerms,
|
|
218
|
+
96,
|
|
219
|
+
"Invalid NativeTokenPeriodTransfer terms: must be exactly 96 bytes"
|
|
220
|
+
);
|
|
221
|
+
const periodAmount = extractBigInt(hexTerms, 0, 32);
|
|
222
|
+
const periodDuration = extractNumber(hexTerms, 32, 32);
|
|
223
|
+
const startDate = extractNumber(hexTerms, 64, 32);
|
|
224
|
+
return { periodAmount, periodDuration, startDate };
|
|
225
|
+
}
|
|
144
226
|
|
|
145
227
|
// src/caveats/exactCalldata.ts
|
|
146
228
|
function createExactCalldataTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -153,9 +235,14 @@ function createExactCalldataTerms(terms, encodingOptions = defaultOptions) {
|
|
|
153
235
|
}
|
|
154
236
|
return prepareResult(calldata, encodingOptions);
|
|
155
237
|
}
|
|
238
|
+
function decodeExactCalldataTerms(terms, encodingOptions = defaultOptions) {
|
|
239
|
+
const calldataHex = bytesLikeToHex(terms);
|
|
240
|
+
const calldata = prepareResult(calldataHex, encodingOptions);
|
|
241
|
+
return { calldata };
|
|
242
|
+
}
|
|
156
243
|
|
|
157
244
|
// src/caveats/exactCalldataBatch.ts
|
|
158
|
-
import { encodeSingle } from "@metamask/abi-utils";
|
|
245
|
+
import { decodeSingle, encodeSingle } from "@metamask/abi-utils";
|
|
159
246
|
import { bytesToHex as bytesToHex3 } from "@metamask/utils";
|
|
160
247
|
var EXECUTION_ARRAY_ABI = "(address,uint256,bytes)[]";
|
|
161
248
|
function createExactCalldataBatchTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -187,6 +274,18 @@ function createExactCalldataBatchTerms(terms, encodingOptions = defaultOptions)
|
|
|
187
274
|
const hexValue = encodeSingle(EXECUTION_ARRAY_ABI, encodableExecutions);
|
|
188
275
|
return prepareResult(hexValue, encodingOptions);
|
|
189
276
|
}
|
|
277
|
+
function decodeExactCalldataBatchTerms(terms, encodingOptions = defaultOptions) {
|
|
278
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
279
|
+
const decoded = decodeSingle(EXECUTION_ARRAY_ABI, hexTerms);
|
|
280
|
+
const executions = decoded.map(
|
|
281
|
+
([target, value, callData]) => ({
|
|
282
|
+
target: prepareResult(target, encodingOptions),
|
|
283
|
+
value,
|
|
284
|
+
callData: prepareResult(bytesToHex3(callData), encodingOptions)
|
|
285
|
+
})
|
|
286
|
+
);
|
|
287
|
+
return { executions };
|
|
288
|
+
}
|
|
190
289
|
|
|
191
290
|
// src/caveats/exactExecution.ts
|
|
192
291
|
import { bytesToHex as bytesToHex4 } from "@metamask/utils";
|
|
@@ -214,9 +313,27 @@ function createExactExecutionTerms(terms, encodingOptions = defaultOptions) {
|
|
|
214
313
|
const hexValue = concatHex([targetHex, valueHex, callDataHex]);
|
|
215
314
|
return prepareResult(hexValue, encodingOptions);
|
|
216
315
|
}
|
|
316
|
+
function decodeExactExecutionTerms(terms, encodingOptions = defaultOptions) {
|
|
317
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
318
|
+
assertHexBytesMinLength(
|
|
319
|
+
hexTerms,
|
|
320
|
+
52,
|
|
321
|
+
"Invalid ExactExecution terms: must be at least 52 bytes"
|
|
322
|
+
);
|
|
323
|
+
const targetHex = extractAddress(hexTerms, 0);
|
|
324
|
+
const value = extractBigInt(hexTerms, 20, 32);
|
|
325
|
+
const callDataHex = extractRemainingHex(hexTerms, 52);
|
|
326
|
+
return {
|
|
327
|
+
execution: {
|
|
328
|
+
target: prepareResult(targetHex, encodingOptions),
|
|
329
|
+
value,
|
|
330
|
+
callData: prepareResult(callDataHex, encodingOptions)
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
}
|
|
217
334
|
|
|
218
335
|
// src/caveats/exactExecutionBatch.ts
|
|
219
|
-
import { encodeSingle as encodeSingle2 } from "@metamask/abi-utils";
|
|
336
|
+
import { decodeSingle as decodeSingle2, encodeSingle as encodeSingle2 } from "@metamask/abi-utils";
|
|
220
337
|
import { bytesToHex as bytesToHex5 } from "@metamask/utils";
|
|
221
338
|
var EXECUTION_ARRAY_ABI2 = "(address,uint256,bytes)[]";
|
|
222
339
|
function createExactExecutionBatchTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -248,6 +365,18 @@ function createExactExecutionBatchTerms(terms, encodingOptions = defaultOptions)
|
|
|
248
365
|
const hexValue = encodeSingle2(EXECUTION_ARRAY_ABI2, encodableExecutions);
|
|
249
366
|
return prepareResult(hexValue, encodingOptions);
|
|
250
367
|
}
|
|
368
|
+
function decodeExactExecutionBatchTerms(terms, encodingOptions = defaultOptions) {
|
|
369
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
370
|
+
const decoded = decodeSingle2(EXECUTION_ARRAY_ABI2, hexTerms);
|
|
371
|
+
const executions = decoded.map(
|
|
372
|
+
([target, value, callData]) => ({
|
|
373
|
+
target: prepareResult(target, encodingOptions),
|
|
374
|
+
value,
|
|
375
|
+
callData: prepareResult(bytesToHex5(callData), encodingOptions)
|
|
376
|
+
})
|
|
377
|
+
);
|
|
378
|
+
return { executions };
|
|
379
|
+
}
|
|
251
380
|
|
|
252
381
|
// src/caveats/nativeTokenStreaming.ts
|
|
253
382
|
var TIMESTAMP_UPPER_BOUND_SECONDS2 = 253402300799;
|
|
@@ -280,6 +409,19 @@ function createNativeTokenStreamingTerms(terms, encodingOptions = defaultOptions
|
|
|
280
409
|
const hexValue = `0x${initialAmountHex}${maxAmountHex}${amountPerSecondHex}${startTimeHex}`;
|
|
281
410
|
return prepareResult(hexValue, encodingOptions);
|
|
282
411
|
}
|
|
412
|
+
function decodeNativeTokenStreamingTerms(terms) {
|
|
413
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
414
|
+
assertHexByteExactLength(
|
|
415
|
+
hexTerms,
|
|
416
|
+
128,
|
|
417
|
+
"Invalid NativeTokenStreaming terms: must be exactly 128 bytes"
|
|
418
|
+
);
|
|
419
|
+
const initialAmount = extractBigInt(hexTerms, 0, 32);
|
|
420
|
+
const maxAmount = extractBigInt(hexTerms, 32, 32);
|
|
421
|
+
const amountPerSecond = extractBigInt(hexTerms, 64, 32);
|
|
422
|
+
const startTime = extractNumber(hexTerms, 96, 32);
|
|
423
|
+
return { initialAmount, maxAmount, amountPerSecond, startTime };
|
|
424
|
+
}
|
|
283
425
|
|
|
284
426
|
// src/caveats/nativeTokenTransferAmount.ts
|
|
285
427
|
function createNativeTokenTransferAmountTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -290,6 +432,16 @@ function createNativeTokenTransferAmountTerms(terms, encodingOptions = defaultOp
|
|
|
290
432
|
const hexValue = `0x${toHexString({ value: maxAmount, size: 32 })}`;
|
|
291
433
|
return prepareResult(hexValue, encodingOptions);
|
|
292
434
|
}
|
|
435
|
+
function decodeNativeTokenTransferAmountTerms(terms) {
|
|
436
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
437
|
+
assertHexByteExactLength(
|
|
438
|
+
hexTerms,
|
|
439
|
+
32,
|
|
440
|
+
"Invalid NativeTokenTransferAmount terms: must be exactly 32 bytes"
|
|
441
|
+
);
|
|
442
|
+
const maxAmount = extractBigInt(hexTerms, 0, 32);
|
|
443
|
+
return { maxAmount };
|
|
444
|
+
}
|
|
293
445
|
|
|
294
446
|
// src/caveats/nativeTokenPayment.ts
|
|
295
447
|
function createNativeTokenPaymentTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -305,10 +457,24 @@ function createNativeTokenPaymentTerms(terms, encodingOptions = defaultOptions)
|
|
|
305
457
|
const hexValue = concatHex([recipientHex, amountHex]);
|
|
306
458
|
return prepareResult(hexValue, encodingOptions);
|
|
307
459
|
}
|
|
460
|
+
function decodeNativeTokenPaymentTerms(terms, encodingOptions = defaultOptions) {
|
|
461
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
462
|
+
assertHexByteExactLength(
|
|
463
|
+
hexTerms,
|
|
464
|
+
52,
|
|
465
|
+
"Invalid NativeTokenPayment terms: must be exactly 52 bytes"
|
|
466
|
+
);
|
|
467
|
+
const recipientHex = extractAddress(hexTerms, 0);
|
|
468
|
+
const amount = extractBigInt(hexTerms, 20, 32);
|
|
469
|
+
return {
|
|
470
|
+
recipient: prepareResult(recipientHex, encodingOptions),
|
|
471
|
+
amount
|
|
472
|
+
};
|
|
473
|
+
}
|
|
308
474
|
|
|
309
475
|
// src/caveats/nativeBalanceChange.ts
|
|
310
476
|
function createNativeBalanceChangeTerms(terms, encodingOptions = defaultOptions) {
|
|
311
|
-
const { recipient, balance, changeType
|
|
477
|
+
const { recipient, balance, changeType } = terms;
|
|
312
478
|
const recipientHex = normalizeAddressLowercase(
|
|
313
479
|
recipient,
|
|
314
480
|
"Invalid recipient: must be a valid Address"
|
|
@@ -316,7 +482,6 @@ function createNativeBalanceChangeTerms(terms, encodingOptions = defaultOptions)
|
|
|
316
482
|
if (balance <= 0n) {
|
|
317
483
|
throw new Error("Invalid balance: must be a positive number");
|
|
318
484
|
}
|
|
319
|
-
const changeType = changeTypeNumber;
|
|
320
485
|
if (changeType !== 0 /* Increase */ && changeType !== 1 /* Decrease */) {
|
|
321
486
|
throw new Error("Invalid changeType: must be either Increase or Decrease");
|
|
322
487
|
}
|
|
@@ -325,6 +490,22 @@ function createNativeBalanceChangeTerms(terms, encodingOptions = defaultOptions)
|
|
|
325
490
|
const hexValue = concatHex([changeTypeHex, recipientHex, balanceHex]);
|
|
326
491
|
return prepareResult(hexValue, encodingOptions);
|
|
327
492
|
}
|
|
493
|
+
function decodeNativeBalanceChangeTerms(terms, encodingOptions = defaultOptions) {
|
|
494
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
495
|
+
assertHexByteExactLength(
|
|
496
|
+
hexTerms,
|
|
497
|
+
53,
|
|
498
|
+
"Invalid NativeBalanceChange terms: must be exactly 53 bytes"
|
|
499
|
+
);
|
|
500
|
+
const changeType = extractNumber(hexTerms, 0, 1);
|
|
501
|
+
const recipientHex = extractAddress(hexTerms, 1);
|
|
502
|
+
const balance = extractBigInt(hexTerms, 21, 32);
|
|
503
|
+
return {
|
|
504
|
+
changeType,
|
|
505
|
+
recipient: prepareResult(recipientHex, encodingOptions),
|
|
506
|
+
balance
|
|
507
|
+
};
|
|
508
|
+
}
|
|
328
509
|
|
|
329
510
|
// src/caveats/erc20Streaming.ts
|
|
330
511
|
import { bytesToHex as bytesToHex6, isHexString as isHexString2 } from "@metamask/utils";
|
|
@@ -373,6 +554,26 @@ function createERC20StreamingTerms(terms, encodingOptions = defaultOptions) {
|
|
|
373
554
|
const hexValue = `${prefixedTokenAddressHex}${initialAmountHex}${maxAmountHex}${amountPerSecondHex}${startTimeHex}`;
|
|
374
555
|
return prepareResult(hexValue, encodingOptions);
|
|
375
556
|
}
|
|
557
|
+
function decodeERC20StreamingTerms(terms, encodingOptions = defaultOptions) {
|
|
558
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
559
|
+
assertHexByteExactLength(
|
|
560
|
+
hexTerms,
|
|
561
|
+
148,
|
|
562
|
+
"Invalid ERC20Streaming terms: must be exactly 148 bytes"
|
|
563
|
+
);
|
|
564
|
+
const tokenAddressHex = extractAddress(hexTerms, 0);
|
|
565
|
+
const initialAmount = extractBigInt(hexTerms, 20, 32);
|
|
566
|
+
const maxAmount = extractBigInt(hexTerms, 52, 32);
|
|
567
|
+
const amountPerSecond = extractBigInt(hexTerms, 84, 32);
|
|
568
|
+
const startTime = extractNumber(hexTerms, 116, 32);
|
|
569
|
+
return {
|
|
570
|
+
tokenAddress: prepareResult(tokenAddressHex, encodingOptions),
|
|
571
|
+
initialAmount,
|
|
572
|
+
maxAmount,
|
|
573
|
+
amountPerSecond,
|
|
574
|
+
startTime
|
|
575
|
+
};
|
|
576
|
+
}
|
|
376
577
|
|
|
377
578
|
// src/caveats/erc20TokenPeriodTransfer.ts
|
|
378
579
|
import { isHexString as isHexString3, bytesToHex as bytesToHex7 } from "@metamask/utils";
|
|
@@ -408,6 +609,24 @@ function createERC20TokenPeriodTransferTerms(terms, encodingOptions = defaultOpt
|
|
|
408
609
|
const hexValue = `${prefixedTokenAddressHex}${periodAmountHex}${periodDurationHex}${startDateHex}`;
|
|
409
610
|
return prepareResult(hexValue, encodingOptions);
|
|
410
611
|
}
|
|
612
|
+
function decodeERC20TokenPeriodTransferTerms(terms, encodingOptions = defaultOptions) {
|
|
613
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
614
|
+
assertHexByteExactLength(
|
|
615
|
+
hexTerms,
|
|
616
|
+
116,
|
|
617
|
+
"Invalid ERC20TokenPeriodTransfer terms: must be exactly 116 bytes"
|
|
618
|
+
);
|
|
619
|
+
const tokenAddressHex = extractAddress(hexTerms, 0);
|
|
620
|
+
const periodAmount = extractBigInt(hexTerms, 20, 32);
|
|
621
|
+
const periodDuration = extractNumber(hexTerms, 52, 32);
|
|
622
|
+
const startDate = extractNumber(hexTerms, 84, 32);
|
|
623
|
+
return {
|
|
624
|
+
tokenAddress: prepareResult(tokenAddressHex, encodingOptions),
|
|
625
|
+
periodAmount,
|
|
626
|
+
periodDuration,
|
|
627
|
+
startDate
|
|
628
|
+
};
|
|
629
|
+
}
|
|
411
630
|
|
|
412
631
|
// src/caveats/erc20TransferAmount.ts
|
|
413
632
|
function createERC20TransferAmountTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -423,15 +642,24 @@ function createERC20TransferAmountTerms(terms, encodingOptions = defaultOptions)
|
|
|
423
642
|
const hexValue = concatHex([tokenAddressHex, maxAmountHex]);
|
|
424
643
|
return prepareResult(hexValue, encodingOptions);
|
|
425
644
|
}
|
|
645
|
+
function decodeERC20TransferAmountTerms(terms, encodingOptions = defaultOptions) {
|
|
646
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
647
|
+
assertHexByteExactLength(
|
|
648
|
+
hexTerms,
|
|
649
|
+
52,
|
|
650
|
+
"Invalid ERC20TransferAmount terms: must be exactly 52 bytes"
|
|
651
|
+
);
|
|
652
|
+
const tokenAddressHex = extractAddress(hexTerms, 0);
|
|
653
|
+
const maxAmount = extractBigInt(hexTerms, 20, 32);
|
|
654
|
+
return {
|
|
655
|
+
tokenAddress: prepareResult(tokenAddressHex, encodingOptions),
|
|
656
|
+
maxAmount
|
|
657
|
+
};
|
|
658
|
+
}
|
|
426
659
|
|
|
427
660
|
// src/caveats/erc20BalanceChange.ts
|
|
428
661
|
function createERC20BalanceChangeTerms(terms, encodingOptions = defaultOptions) {
|
|
429
|
-
const {
|
|
430
|
-
tokenAddress,
|
|
431
|
-
recipient,
|
|
432
|
-
balance,
|
|
433
|
-
changeType: changeTypeNumber
|
|
434
|
-
} = terms;
|
|
662
|
+
const { tokenAddress, recipient, balance, changeType } = terms;
|
|
435
663
|
const tokenAddressHex = normalizeAddressLowercase(
|
|
436
664
|
tokenAddress,
|
|
437
665
|
"Invalid tokenAddress: must be a valid address"
|
|
@@ -443,7 +671,6 @@ function createERC20BalanceChangeTerms(terms, encodingOptions = defaultOptions)
|
|
|
443
671
|
if (balance <= 0n) {
|
|
444
672
|
throw new Error("Invalid balance: must be a positive number");
|
|
445
673
|
}
|
|
446
|
-
const changeType = changeTypeNumber;
|
|
447
674
|
if (changeType !== 0 /* Increase */ && changeType !== 1 /* Decrease */) {
|
|
448
675
|
throw new Error("Invalid changeType: must be either Increase or Decrease");
|
|
449
676
|
}
|
|
@@ -457,15 +684,28 @@ function createERC20BalanceChangeTerms(terms, encodingOptions = defaultOptions)
|
|
|
457
684
|
]);
|
|
458
685
|
return prepareResult(hexValue, encodingOptions);
|
|
459
686
|
}
|
|
687
|
+
function decodeERC20BalanceChangeTerms(terms, encodingOptions = defaultOptions) {
|
|
688
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
689
|
+
assertHexByteExactLength(
|
|
690
|
+
hexTerms,
|
|
691
|
+
73,
|
|
692
|
+
"Invalid ERC20BalanceChange terms: must be exactly 73 bytes"
|
|
693
|
+
);
|
|
694
|
+
const changeType = extractNumber(hexTerms, 0, 1);
|
|
695
|
+
const tokenAddressHex = extractAddress(hexTerms, 1);
|
|
696
|
+
const recipientHex = extractAddress(hexTerms, 21);
|
|
697
|
+
const balance = extractBigInt(hexTerms, 41, 32);
|
|
698
|
+
return {
|
|
699
|
+
changeType,
|
|
700
|
+
tokenAddress: prepareResult(tokenAddressHex, encodingOptions),
|
|
701
|
+
recipient: prepareResult(recipientHex, encodingOptions),
|
|
702
|
+
balance
|
|
703
|
+
};
|
|
704
|
+
}
|
|
460
705
|
|
|
461
706
|
// src/caveats/erc721BalanceChange.ts
|
|
462
707
|
function createERC721BalanceChangeTerms(terms, encodingOptions = defaultOptions) {
|
|
463
|
-
const {
|
|
464
|
-
tokenAddress,
|
|
465
|
-
recipient,
|
|
466
|
-
amount,
|
|
467
|
-
changeType: changeTypeNumber
|
|
468
|
-
} = terms;
|
|
708
|
+
const { tokenAddress, recipient, amount, changeType } = terms;
|
|
469
709
|
const tokenAddressHex = normalizeAddressLowercase(
|
|
470
710
|
tokenAddress,
|
|
471
711
|
"Invalid tokenAddress: must be a valid address"
|
|
@@ -477,7 +717,6 @@ function createERC721BalanceChangeTerms(terms, encodingOptions = defaultOptions)
|
|
|
477
717
|
if (amount <= 0n) {
|
|
478
718
|
throw new Error("Invalid balance: must be a positive number");
|
|
479
719
|
}
|
|
480
|
-
const changeType = changeTypeNumber;
|
|
481
720
|
if (changeType !== 0 /* Increase */ && changeType !== 1 /* Decrease */) {
|
|
482
721
|
throw new Error("Invalid changeType: must be either Increase or Decrease");
|
|
483
722
|
}
|
|
@@ -491,6 +730,24 @@ function createERC721BalanceChangeTerms(terms, encodingOptions = defaultOptions)
|
|
|
491
730
|
]);
|
|
492
731
|
return prepareResult(hexValue, encodingOptions);
|
|
493
732
|
}
|
|
733
|
+
function decodeERC721BalanceChangeTerms(terms, encodingOptions = defaultOptions) {
|
|
734
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
735
|
+
assertHexByteExactLength(
|
|
736
|
+
hexTerms,
|
|
737
|
+
73,
|
|
738
|
+
"Invalid ERC721BalanceChange terms: must be exactly 73 bytes"
|
|
739
|
+
);
|
|
740
|
+
const changeType = extractNumber(hexTerms, 0, 1);
|
|
741
|
+
const tokenAddressHex = extractAddress(hexTerms, 1);
|
|
742
|
+
const recipientHex = extractAddress(hexTerms, 21);
|
|
743
|
+
const amount = extractBigInt(hexTerms, 41, 32);
|
|
744
|
+
return {
|
|
745
|
+
changeType,
|
|
746
|
+
tokenAddress: prepareResult(tokenAddressHex, encodingOptions),
|
|
747
|
+
recipient: prepareResult(recipientHex, encodingOptions),
|
|
748
|
+
amount
|
|
749
|
+
};
|
|
750
|
+
}
|
|
494
751
|
|
|
495
752
|
// src/caveats/erc721Transfer.ts
|
|
496
753
|
function createERC721TransferTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -506,16 +763,24 @@ function createERC721TransferTerms(terms, encodingOptions = defaultOptions) {
|
|
|
506
763
|
const hexValue = concatHex([tokenAddressHex, tokenIdHex]);
|
|
507
764
|
return prepareResult(hexValue, encodingOptions);
|
|
508
765
|
}
|
|
766
|
+
function decodeERC721TransferTerms(terms, encodingOptions = defaultOptions) {
|
|
767
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
768
|
+
assertHexByteExactLength(
|
|
769
|
+
hexTerms,
|
|
770
|
+
52,
|
|
771
|
+
"Invalid ERC721Transfer terms: must be exactly 52 bytes"
|
|
772
|
+
);
|
|
773
|
+
const tokenAddressHex = extractAddress(hexTerms, 0);
|
|
774
|
+
const tokenId = extractBigInt(hexTerms, 20, 32);
|
|
775
|
+
return {
|
|
776
|
+
tokenAddress: prepareResult(tokenAddressHex, encodingOptions),
|
|
777
|
+
tokenId
|
|
778
|
+
};
|
|
779
|
+
}
|
|
509
780
|
|
|
510
781
|
// src/caveats/erc1155BalanceChange.ts
|
|
511
782
|
function createERC1155BalanceChangeTerms(terms, encodingOptions = defaultOptions) {
|
|
512
|
-
const {
|
|
513
|
-
tokenAddress,
|
|
514
|
-
recipient,
|
|
515
|
-
tokenId,
|
|
516
|
-
balance,
|
|
517
|
-
changeType: changeTypeNumber
|
|
518
|
-
} = terms;
|
|
783
|
+
const { tokenAddress, recipient, tokenId, balance, changeType } = terms;
|
|
519
784
|
const tokenAddressHex = normalizeAddressLowercase(
|
|
520
785
|
tokenAddress,
|
|
521
786
|
"Invalid tokenAddress: must be a valid address"
|
|
@@ -530,7 +795,6 @@ function createERC1155BalanceChangeTerms(terms, encodingOptions = defaultOptions
|
|
|
530
795
|
if (tokenId < 0n) {
|
|
531
796
|
throw new Error("Invalid tokenId: must be a non-negative number");
|
|
532
797
|
}
|
|
533
|
-
const changeType = changeTypeNumber;
|
|
534
798
|
if (changeType !== 0 /* Increase */ && changeType !== 1 /* Decrease */) {
|
|
535
799
|
throw new Error("Invalid changeType: must be either Increase or Decrease");
|
|
536
800
|
}
|
|
@@ -546,6 +810,26 @@ function createERC1155BalanceChangeTerms(terms, encodingOptions = defaultOptions
|
|
|
546
810
|
]);
|
|
547
811
|
return prepareResult(hexValue, encodingOptions);
|
|
548
812
|
}
|
|
813
|
+
function decodeERC1155BalanceChangeTerms(terms, encodingOptions = defaultOptions) {
|
|
814
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
815
|
+
assertHexByteExactLength(
|
|
816
|
+
hexTerms,
|
|
817
|
+
105,
|
|
818
|
+
"Invalid ERC1155BalanceChange terms: must be exactly 105 bytes"
|
|
819
|
+
);
|
|
820
|
+
const changeType = extractNumber(hexTerms, 0, 1);
|
|
821
|
+
const tokenAddressHex = extractAddress(hexTerms, 1);
|
|
822
|
+
const recipientHex = extractAddress(hexTerms, 21);
|
|
823
|
+
const tokenId = extractBigInt(hexTerms, 41, 32);
|
|
824
|
+
const balance = extractBigInt(hexTerms, 73, 32);
|
|
825
|
+
return {
|
|
826
|
+
changeType,
|
|
827
|
+
tokenAddress: prepareResult(tokenAddressHex, encodingOptions),
|
|
828
|
+
recipient: prepareResult(recipientHex, encodingOptions),
|
|
829
|
+
tokenId,
|
|
830
|
+
balance
|
|
831
|
+
};
|
|
832
|
+
}
|
|
549
833
|
|
|
550
834
|
// src/caveats/nonce.ts
|
|
551
835
|
import { isHexString as isHexString4 } from "@metamask/utils";
|
|
@@ -573,6 +857,16 @@ function createNonceTerms(terms, encodingOptions = defaultOptions) {
|
|
|
573
857
|
const hexValue = `0x${paddedNonce}`;
|
|
574
858
|
return prepareResult(hexValue, encodingOptions);
|
|
575
859
|
}
|
|
860
|
+
function decodeNonceTerms(terms, encodingOptions = defaultOptions) {
|
|
861
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
862
|
+
assertHexByteExactLength(
|
|
863
|
+
hexTerms,
|
|
864
|
+
32,
|
|
865
|
+
"Invalid Nonce terms: must be exactly 32 bytes"
|
|
866
|
+
);
|
|
867
|
+
const nonce = prepareResult(hexTerms, encodingOptions);
|
|
868
|
+
return { nonce };
|
|
869
|
+
}
|
|
576
870
|
|
|
577
871
|
// src/caveats/allowedCalldata.ts
|
|
578
872
|
import { bytesToHex as bytesToHex8, remove0x as remove0x2 } from "@metamask/utils";
|
|
@@ -596,6 +890,18 @@ function createAllowedCalldataTerms(terms, encodingOptions = defaultOptions) {
|
|
|
596
890
|
const indexHex = toHexString({ value: startIndex, size: 32 });
|
|
597
891
|
return prepareResult(`0x${indexHex}${unprefixedValue}`, encodingOptions);
|
|
598
892
|
}
|
|
893
|
+
function decodeAllowedCalldataTerms(terms, encodingOptions = defaultOptions) {
|
|
894
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
895
|
+
assertHexBytesMinLength(
|
|
896
|
+
hexTerms,
|
|
897
|
+
32,
|
|
898
|
+
"Invalid AllowedCalldata terms: must be at least 32 bytes"
|
|
899
|
+
);
|
|
900
|
+
const startIndex = extractNumber(hexTerms, 0, 32);
|
|
901
|
+
const valueHex = extractRemainingHex(hexTerms, 32);
|
|
902
|
+
const value = prepareResult(valueHex, encodingOptions);
|
|
903
|
+
return { startIndex, value };
|
|
904
|
+
}
|
|
599
905
|
|
|
600
906
|
// src/caveats/allowedMethods.ts
|
|
601
907
|
import { bytesToHex as bytesToHex9, isHexString as isHexString5 } from "@metamask/utils";
|
|
@@ -621,6 +927,22 @@ function createAllowedMethodsTerms(terms, encodingOptions = defaultOptions) {
|
|
|
621
927
|
const hexValue = concatHex(normalizedSelectors);
|
|
622
928
|
return prepareResult(hexValue, encodingOptions);
|
|
623
929
|
}
|
|
930
|
+
function decodeAllowedMethodsTerms(terms, encodingOptions = defaultOptions) {
|
|
931
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
932
|
+
const selectorSize = 4;
|
|
933
|
+
assertHexByteLengthAtLeastOneMultipleOf(
|
|
934
|
+
hexTerms,
|
|
935
|
+
selectorSize,
|
|
936
|
+
"Invalid selectors: must be a multiple of 4"
|
|
937
|
+
);
|
|
938
|
+
const selectorCount = getByteLength(hexTerms) / selectorSize;
|
|
939
|
+
const selectors = [];
|
|
940
|
+
for (let i = 0; i < selectorCount; i++) {
|
|
941
|
+
const selector = extractHex(hexTerms, i * selectorSize, selectorSize);
|
|
942
|
+
selectors.push(prepareResult(selector, encodingOptions));
|
|
943
|
+
}
|
|
944
|
+
return { selectors };
|
|
945
|
+
}
|
|
624
946
|
|
|
625
947
|
// src/caveats/allowedTargets.ts
|
|
626
948
|
function createAllowedTargetsTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -636,6 +958,22 @@ function createAllowedTargetsTerms(terms, encodingOptions = defaultOptions) {
|
|
|
636
958
|
const hexValue = concatHex(normalizedTargets);
|
|
637
959
|
return prepareResult(hexValue, encodingOptions);
|
|
638
960
|
}
|
|
961
|
+
function decodeAllowedTargetsTerms(terms, encodingOptions = defaultOptions) {
|
|
962
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
963
|
+
const addressSize = 20;
|
|
964
|
+
assertHexByteLengthAtLeastOneMultipleOf(
|
|
965
|
+
hexTerms,
|
|
966
|
+
addressSize,
|
|
967
|
+
"Invalid targets: must be a multiple of 20"
|
|
968
|
+
);
|
|
969
|
+
const addressCount = getByteLength(hexTerms) / addressSize;
|
|
970
|
+
const targets = [];
|
|
971
|
+
for (let i = 0; i < addressCount; i++) {
|
|
972
|
+
const target = extractAddress(hexTerms, i * addressSize);
|
|
973
|
+
targets.push(prepareResult(target, encodingOptions));
|
|
974
|
+
}
|
|
975
|
+
return { targets };
|
|
976
|
+
}
|
|
639
977
|
|
|
640
978
|
// src/caveats/argsEqualityCheck.ts
|
|
641
979
|
function createArgsEqualityCheckTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -649,6 +987,11 @@ function createArgsEqualityCheckTerms(terms, encodingOptions = defaultOptions) {
|
|
|
649
987
|
);
|
|
650
988
|
return prepareResult(hexValue, encodingOptions);
|
|
651
989
|
}
|
|
990
|
+
function decodeArgsEqualityCheckTerms(terms, encodingOptions = defaultOptions) {
|
|
991
|
+
const argsHex = bytesLikeToHex(terms);
|
|
992
|
+
const args = prepareResult(argsHex, encodingOptions);
|
|
993
|
+
return { args };
|
|
994
|
+
}
|
|
652
995
|
|
|
653
996
|
// src/caveats/blockNumber.ts
|
|
654
997
|
function createBlockNumberTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -671,6 +1014,17 @@ function createBlockNumberTerms(terms, encodingOptions = defaultOptions) {
|
|
|
671
1014
|
const hexValue = `0x${afterThresholdHex}${beforeThresholdHex}`;
|
|
672
1015
|
return prepareResult(hexValue, encodingOptions);
|
|
673
1016
|
}
|
|
1017
|
+
function decodeBlockNumberTerms(terms) {
|
|
1018
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
1019
|
+
assertHexByteExactLength(
|
|
1020
|
+
hexTerms,
|
|
1021
|
+
32,
|
|
1022
|
+
"Invalid BlockNumber terms: must be exactly 32 bytes"
|
|
1023
|
+
);
|
|
1024
|
+
const afterThreshold = extractBigInt(hexTerms, 0, 16);
|
|
1025
|
+
const beforeThreshold = extractBigInt(hexTerms, 16, 16);
|
|
1026
|
+
return { afterThreshold, beforeThreshold };
|
|
1027
|
+
}
|
|
674
1028
|
|
|
675
1029
|
// src/caveats/deployed.ts
|
|
676
1030
|
import { remove0x as remove0x3 } from "@metamask/utils";
|
|
@@ -696,6 +1050,22 @@ function createDeployedTerms(terms, encodingOptions = defaultOptions) {
|
|
|
696
1050
|
const hexValue = concatHex([contractAddressHex, paddedSalt, bytecodeHex]);
|
|
697
1051
|
return prepareResult(hexValue, encodingOptions);
|
|
698
1052
|
}
|
|
1053
|
+
function decodeDeployedTerms(terms, encodingOptions = defaultOptions) {
|
|
1054
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
1055
|
+
assertHexBytesMinLength(
|
|
1056
|
+
hexTerms,
|
|
1057
|
+
52,
|
|
1058
|
+
"Invalid Deployed terms: must be at least 52 bytes"
|
|
1059
|
+
);
|
|
1060
|
+
const contractAddressHex = extractAddress(hexTerms, 0);
|
|
1061
|
+
const saltHex = extractHex(hexTerms, 20, 32);
|
|
1062
|
+
const bytecodeHex = extractRemainingHex(hexTerms, 52);
|
|
1063
|
+
return {
|
|
1064
|
+
contractAddress: prepareResult(contractAddressHex, encodingOptions),
|
|
1065
|
+
salt: prepareResult(saltHex, encodingOptions),
|
|
1066
|
+
bytecode: prepareResult(bytecodeHex, encodingOptions)
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
699
1069
|
|
|
700
1070
|
// src/caveats/id.ts
|
|
701
1071
|
var MAX_UINT256 = BigInt(`0x${"f".repeat(64)}`);
|
|
@@ -721,6 +1091,16 @@ function createIdTerms(terms, encodingOptions = defaultOptions) {
|
|
|
721
1091
|
const hexValue = `0x${toHexString({ value: idBigInt, size: 32 })}`;
|
|
722
1092
|
return prepareResult(hexValue, encodingOptions);
|
|
723
1093
|
}
|
|
1094
|
+
function decodeIdTerms(terms) {
|
|
1095
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
1096
|
+
assertHexByteExactLength(
|
|
1097
|
+
hexTerms,
|
|
1098
|
+
32,
|
|
1099
|
+
"Invalid Id terms: must be exactly 32 bytes"
|
|
1100
|
+
);
|
|
1101
|
+
const id = extractBigInt(hexTerms, 0, 32);
|
|
1102
|
+
return { id };
|
|
1103
|
+
}
|
|
724
1104
|
|
|
725
1105
|
// src/caveats/limitedCalls.ts
|
|
726
1106
|
function createLimitedCallsTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -734,6 +1114,16 @@ function createLimitedCallsTerms(terms, encodingOptions = defaultOptions) {
|
|
|
734
1114
|
const hexValue = `0x${toHexString({ value: limit, size: 32 })}`;
|
|
735
1115
|
return prepareResult(hexValue, encodingOptions);
|
|
736
1116
|
}
|
|
1117
|
+
function decodeLimitedCallsTerms(terms) {
|
|
1118
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
1119
|
+
assertHexByteExactLength(
|
|
1120
|
+
hexTerms,
|
|
1121
|
+
32,
|
|
1122
|
+
"Invalid LimitedCalls terms: must be exactly 32 bytes"
|
|
1123
|
+
);
|
|
1124
|
+
const limit = extractNumber(hexTerms, 0, 32);
|
|
1125
|
+
return { limit };
|
|
1126
|
+
}
|
|
737
1127
|
|
|
738
1128
|
// src/caveats/multiTokenPeriod.ts
|
|
739
1129
|
function createMultiTokenPeriodTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -768,6 +1158,31 @@ function createMultiTokenPeriodTerms(terms, encodingOptions = defaultOptions) {
|
|
|
768
1158
|
const hexValue = concatHex(hexParts);
|
|
769
1159
|
return prepareResult(hexValue, encodingOptions);
|
|
770
1160
|
}
|
|
1161
|
+
function decodeMultiTokenPeriodTerms(terms, encodingOptions = defaultOptions) {
|
|
1162
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
1163
|
+
const configSize = 116;
|
|
1164
|
+
assertHexByteLengthAtLeastOneMultipleOf(
|
|
1165
|
+
hexTerms,
|
|
1166
|
+
configSize,
|
|
1167
|
+
"Invalid MultiTokenPeriod terms: must be a multiple of 116 bytes"
|
|
1168
|
+
);
|
|
1169
|
+
const configCount = getByteLength(hexTerms) / configSize;
|
|
1170
|
+
const tokenConfigs = [];
|
|
1171
|
+
for (let i = 0; i < configCount; i++) {
|
|
1172
|
+
const offset = i * configSize;
|
|
1173
|
+
const tokenHex = extractAddress(hexTerms, offset);
|
|
1174
|
+
const periodAmount = extractBigInt(hexTerms, offset + 20, 32);
|
|
1175
|
+
const periodDuration = extractNumber(hexTerms, offset + 52, 32);
|
|
1176
|
+
const startDate = extractNumber(hexTerms, offset + 84, 32);
|
|
1177
|
+
tokenConfigs.push({
|
|
1178
|
+
token: prepareResult(tokenHex, encodingOptions),
|
|
1179
|
+
periodAmount,
|
|
1180
|
+
periodDuration,
|
|
1181
|
+
startDate
|
|
1182
|
+
});
|
|
1183
|
+
}
|
|
1184
|
+
return { tokenConfigs };
|
|
1185
|
+
}
|
|
771
1186
|
|
|
772
1187
|
// src/caveats/ownershipTransfer.ts
|
|
773
1188
|
function createOwnershipTransferTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -778,6 +1193,18 @@ function createOwnershipTransferTerms(terms, encodingOptions = defaultOptions) {
|
|
|
778
1193
|
);
|
|
779
1194
|
return prepareResult(contractAddressHex, encodingOptions);
|
|
780
1195
|
}
|
|
1196
|
+
function decodeOwnershipTransferTerms(terms, encodingOptions = defaultOptions) {
|
|
1197
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
1198
|
+
assertHexByteExactLength(
|
|
1199
|
+
hexTerms,
|
|
1200
|
+
20,
|
|
1201
|
+
"Invalid OwnershipTransfer terms: must be exactly 20 bytes"
|
|
1202
|
+
);
|
|
1203
|
+
const contractAddressHex = extractAddress(hexTerms, 0);
|
|
1204
|
+
return {
|
|
1205
|
+
contractAddress: prepareResult(contractAddressHex, encodingOptions)
|
|
1206
|
+
};
|
|
1207
|
+
}
|
|
781
1208
|
|
|
782
1209
|
// src/caveats/redeemer.ts
|
|
783
1210
|
function createRedeemerTerms(terms, encodingOptions = defaultOptions) {
|
|
@@ -793,6 +1220,22 @@ function createRedeemerTerms(terms, encodingOptions = defaultOptions) {
|
|
|
793
1220
|
const hexValue = concatHex(normalizedRedeemers);
|
|
794
1221
|
return prepareResult(hexValue, encodingOptions);
|
|
795
1222
|
}
|
|
1223
|
+
function decodeRedeemerTerms(terms, encodingOptions = defaultOptions) {
|
|
1224
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
1225
|
+
const addressSize = 20;
|
|
1226
|
+
assertHexByteLengthAtLeastOneMultipleOf(
|
|
1227
|
+
hexTerms,
|
|
1228
|
+
addressSize,
|
|
1229
|
+
"Invalid redeemers: must be a multiple of 20"
|
|
1230
|
+
);
|
|
1231
|
+
const addressCount = getByteLength(hexTerms) / addressSize;
|
|
1232
|
+
const redeemers = [];
|
|
1233
|
+
for (let i = 0; i < addressCount; i++) {
|
|
1234
|
+
const redeemer = extractAddress(hexTerms, i * addressSize);
|
|
1235
|
+
redeemers.push(prepareResult(redeemer, encodingOptions));
|
|
1236
|
+
}
|
|
1237
|
+
return { redeemers };
|
|
1238
|
+
}
|
|
796
1239
|
|
|
797
1240
|
// src/caveats/specificActionERC20TransferBatch.ts
|
|
798
1241
|
import { bytesToHex as bytesToHex10 } from "@metamask/utils";
|
|
@@ -834,9 +1277,108 @@ function createSpecificActionERC20TransferBatchTerms(terms, encodingOptions = de
|
|
|
834
1277
|
]);
|
|
835
1278
|
return prepareResult(hexValue, encodingOptions);
|
|
836
1279
|
}
|
|
1280
|
+
function decodeSpecificActionERC20TransferBatchTerms(terms, encodingOptions = defaultOptions) {
|
|
1281
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
1282
|
+
assertHexBytesMinLength(
|
|
1283
|
+
hexTerms,
|
|
1284
|
+
92,
|
|
1285
|
+
"Invalid SpecificActionERC20TransferBatch terms: must be at least 92 bytes"
|
|
1286
|
+
);
|
|
1287
|
+
const tokenAddressHex = extractAddress(hexTerms, 0);
|
|
1288
|
+
const recipientHex = extractAddress(hexTerms, 20);
|
|
1289
|
+
const amount = extractBigInt(hexTerms, 40, 32);
|
|
1290
|
+
const targetHex = extractAddress(hexTerms, 72);
|
|
1291
|
+
const calldataHex = extractRemainingHex(hexTerms, 92);
|
|
1292
|
+
return {
|
|
1293
|
+
tokenAddress: prepareResult(tokenAddressHex, encodingOptions),
|
|
1294
|
+
recipient: prepareResult(recipientHex, encodingOptions),
|
|
1295
|
+
amount,
|
|
1296
|
+
target: prepareResult(targetHex, encodingOptions),
|
|
1297
|
+
calldata: prepareResult(calldataHex, encodingOptions)
|
|
1298
|
+
};
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
// src/caveats/logicalOrWrapper.ts
|
|
1302
|
+
import { decodeSingle as decodeSingle3, encodeSingle as encodeSingle3 } from "@metamask/abi-utils";
|
|
1303
|
+
import { bytesToHex as bytesToHex11 } from "@metamask/utils";
|
|
1304
|
+
var CAVEAT_GROUPS_ABI = "((address,bytes,bytes)[])[]";
|
|
1305
|
+
var SELECTED_GROUP_ABI = "(uint256,bytes[])";
|
|
1306
|
+
function assertValidCaveatGroups(groups) {
|
|
1307
|
+
if (!groups?.length) {
|
|
1308
|
+
throw new Error(
|
|
1309
|
+
"Invalid caveatGroups: must provide at least one caveat group"
|
|
1310
|
+
);
|
|
1311
|
+
}
|
|
1312
|
+
for (let i = 0; i < groups.length; i++) {
|
|
1313
|
+
const group = groups[i];
|
|
1314
|
+
if (!group?.length) {
|
|
1315
|
+
throw new Error(
|
|
1316
|
+
`Invalid caveatGroups: group at index ${i} must contain at least one caveat`
|
|
1317
|
+
);
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
function normalizeCaveatTuple(caveat) {
|
|
1322
|
+
return [
|
|
1323
|
+
normalizeAddress(
|
|
1324
|
+
caveat.enforcer,
|
|
1325
|
+
"Invalid enforcer: must be a valid address"
|
|
1326
|
+
),
|
|
1327
|
+
normalizeHex(caveat.terms, "Invalid terms: must be a valid hex string"),
|
|
1328
|
+
normalizeHex(caveat.args, "Invalid args: must be a valid hex string")
|
|
1329
|
+
];
|
|
1330
|
+
}
|
|
1331
|
+
function encodeCaveatGroupTuple(group) {
|
|
1332
|
+
return [group.map(normalizeCaveatTuple)];
|
|
1333
|
+
}
|
|
1334
|
+
function createLogicalOrWrapperTerms(terms, encodingOptions = defaultOptions) {
|
|
1335
|
+
assertValidCaveatGroups(terms.caveatGroups);
|
|
1336
|
+
const encodableGroups = terms.caveatGroups.map(encodeCaveatGroupTuple);
|
|
1337
|
+
const hexValue = encodeSingle3(CAVEAT_GROUPS_ABI, encodableGroups);
|
|
1338
|
+
return prepareResult(hexValue, encodingOptions);
|
|
1339
|
+
}
|
|
1340
|
+
function decodeLogicalOrWrapperTerms(terms, encodingOptions = defaultOptions) {
|
|
1341
|
+
const hexTerms = bytesLikeToHex(terms);
|
|
1342
|
+
const decoded = decodeSingle3(
|
|
1343
|
+
CAVEAT_GROUPS_ABI,
|
|
1344
|
+
hexTerms
|
|
1345
|
+
);
|
|
1346
|
+
const caveatGroups = decoded.map(
|
|
1347
|
+
([caveats]) => caveats.map(([enforcer, caveatTerms, args]) => ({
|
|
1348
|
+
enforcer: prepareResult(enforcer, encodingOptions),
|
|
1349
|
+
terms: prepareResult(bytesToHex11(caveatTerms), encodingOptions),
|
|
1350
|
+
args: prepareResult(bytesToHex11(args), encodingOptions)
|
|
1351
|
+
}))
|
|
1352
|
+
);
|
|
1353
|
+
return { caveatGroups };
|
|
1354
|
+
}
|
|
1355
|
+
function createLogicalOrWrapperArgs(args, encodingOptions = defaultOptions) {
|
|
1356
|
+
if (args.groupIndex < 0n) {
|
|
1357
|
+
throw new Error("Invalid groupIndex: must be a non-negative number");
|
|
1358
|
+
}
|
|
1359
|
+
const caveatArgsHex = args.caveatArgs.map(
|
|
1360
|
+
(arg) => normalizeHex(arg, "Invalid caveatArgs: must be valid hex strings")
|
|
1361
|
+
);
|
|
1362
|
+
const hexValue = encodeSingle3(SELECTED_GROUP_ABI, [
|
|
1363
|
+
args.groupIndex,
|
|
1364
|
+
caveatArgsHex
|
|
1365
|
+
]);
|
|
1366
|
+
return prepareResult(hexValue, encodingOptions);
|
|
1367
|
+
}
|
|
1368
|
+
function decodeLogicalOrWrapperArgs(args, encodingOptions = defaultOptions) {
|
|
1369
|
+
const hexArgs = bytesLikeToHex(args);
|
|
1370
|
+
const [groupIndex, caveatArgsRaw] = decodeSingle3(
|
|
1371
|
+
SELECTED_GROUP_ABI,
|
|
1372
|
+
hexArgs
|
|
1373
|
+
);
|
|
1374
|
+
const caveatArgs = caveatArgsRaw.map(
|
|
1375
|
+
(arg) => prepareResult(bytesToHex11(arg), encodingOptions)
|
|
1376
|
+
);
|
|
1377
|
+
return { groupIndex, caveatArgs };
|
|
1378
|
+
}
|
|
837
1379
|
|
|
838
1380
|
// src/delegation.ts
|
|
839
|
-
import { encode, encodeSingle as
|
|
1381
|
+
import { encode, encodeSingle as encodeSingle4, decodeSingle as decodeSingle4 } from "@metamask/abi-utils";
|
|
840
1382
|
import { hexToBytes as hexToBytes3 } from "@metamask/utils";
|
|
841
1383
|
import { keccak_256 as keccak256 } from "@noble/hashes/sha3";
|
|
842
1384
|
var ANY_BENEFICIARY = "0x0000000000000000000000000000000000000a11";
|
|
@@ -863,7 +1405,7 @@ function encodeDelegations(delegations, options = defaultOptions) {
|
|
|
863
1405
|
struct.salt,
|
|
864
1406
|
struct.signature
|
|
865
1407
|
]);
|
|
866
|
-
result =
|
|
1408
|
+
result = encodeSingle4(DELEGATION_ARRAY_ABI_TYPES, encodableStructs);
|
|
867
1409
|
}
|
|
868
1410
|
return prepareResult(result, options);
|
|
869
1411
|
}
|
|
@@ -880,7 +1422,7 @@ function encodeDelegation(delegation, options = defaultOptions) {
|
|
|
880
1422
|
delegation.salt,
|
|
881
1423
|
delegation.signature
|
|
882
1424
|
];
|
|
883
|
-
const result =
|
|
1425
|
+
const result = encodeSingle4(DELEGATION_ABI_TYPE, encodableStruct);
|
|
884
1426
|
return prepareResult(result, options);
|
|
885
1427
|
}
|
|
886
1428
|
var delegationFromDecodedDelegation = (decodedDelegation, convertFn) => {
|
|
@@ -899,7 +1441,7 @@ var delegationFromDecodedDelegation = (decodedDelegation, convertFn) => {
|
|
|
899
1441
|
};
|
|
900
1442
|
};
|
|
901
1443
|
function decodeDelegations(encoded, options = defaultOptions) {
|
|
902
|
-
const decodedStructs =
|
|
1444
|
+
const decodedStructs = decodeSingle4(
|
|
903
1445
|
DELEGATION_ARRAY_ABI_TYPES,
|
|
904
1446
|
encoded
|
|
905
1447
|
// return types cannot be inferred from complex ABI types, so we must assert the type
|
|
@@ -914,7 +1456,7 @@ function decodeDelegations(encoded, options = defaultOptions) {
|
|
|
914
1456
|
);
|
|
915
1457
|
}
|
|
916
1458
|
function decodeDelegation(encoded, options = defaultOptions) {
|
|
917
|
-
const decodedStruct =
|
|
1459
|
+
const decodedStruct = decodeSingle4(
|
|
918
1460
|
DELEGATION_ABI_TYPE,
|
|
919
1461
|
encoded
|
|
920
1462
|
// return types cannot be inferred from complex ABI types, so we must assert the type
|
|
@@ -964,6 +1506,7 @@ function getCaveatHash(caveat) {
|
|
|
964
1506
|
}
|
|
965
1507
|
export {
|
|
966
1508
|
ANY_BENEFICIARY,
|
|
1509
|
+
BalanceChangeType,
|
|
967
1510
|
CAVEAT_TYPEHASH,
|
|
968
1511
|
DELEGATION_TYPEHASH,
|
|
969
1512
|
ROOT_AUTHORITY,
|
|
@@ -986,6 +1529,8 @@ export {
|
|
|
986
1529
|
createExactExecutionTerms,
|
|
987
1530
|
createIdTerms,
|
|
988
1531
|
createLimitedCallsTerms,
|
|
1532
|
+
createLogicalOrWrapperArgs,
|
|
1533
|
+
createLogicalOrWrapperTerms,
|
|
989
1534
|
createMultiTokenPeriodTerms,
|
|
990
1535
|
createNativeBalanceChangeTerms,
|
|
991
1536
|
createNativeTokenPaymentTerms,
|
|
@@ -998,8 +1543,41 @@ export {
|
|
|
998
1543
|
createSpecificActionERC20TransferBatchTerms,
|
|
999
1544
|
createTimestampTerms,
|
|
1000
1545
|
createValueLteTerms,
|
|
1546
|
+
decodeAllowedCalldataTerms,
|
|
1547
|
+
decodeAllowedMethodsTerms,
|
|
1548
|
+
decodeAllowedTargetsTerms,
|
|
1549
|
+
decodeArgsEqualityCheckTerms,
|
|
1550
|
+
decodeBlockNumberTerms,
|
|
1001
1551
|
decodeDelegation,
|
|
1002
1552
|
decodeDelegations,
|
|
1553
|
+
decodeDeployedTerms,
|
|
1554
|
+
decodeERC1155BalanceChangeTerms,
|
|
1555
|
+
decodeERC20BalanceChangeTerms,
|
|
1556
|
+
decodeERC20StreamingTerms,
|
|
1557
|
+
decodeERC20TokenPeriodTransferTerms,
|
|
1558
|
+
decodeERC20TransferAmountTerms,
|
|
1559
|
+
decodeERC721BalanceChangeTerms,
|
|
1560
|
+
decodeERC721TransferTerms,
|
|
1561
|
+
decodeExactCalldataBatchTerms,
|
|
1562
|
+
decodeExactCalldataTerms,
|
|
1563
|
+
decodeExactExecutionBatchTerms,
|
|
1564
|
+
decodeExactExecutionTerms,
|
|
1565
|
+
decodeIdTerms,
|
|
1566
|
+
decodeLimitedCallsTerms,
|
|
1567
|
+
decodeLogicalOrWrapperArgs,
|
|
1568
|
+
decodeLogicalOrWrapperTerms,
|
|
1569
|
+
decodeMultiTokenPeriodTerms,
|
|
1570
|
+
decodeNativeBalanceChangeTerms,
|
|
1571
|
+
decodeNativeTokenPaymentTerms,
|
|
1572
|
+
decodeNativeTokenPeriodTransferTerms,
|
|
1573
|
+
decodeNativeTokenStreamingTerms,
|
|
1574
|
+
decodeNativeTokenTransferAmountTerms,
|
|
1575
|
+
decodeNonceTerms,
|
|
1576
|
+
decodeOwnershipTransferTerms,
|
|
1577
|
+
decodeRedeemerTerms,
|
|
1578
|
+
decodeSpecificActionERC20TransferBatchTerms,
|
|
1579
|
+
decodeTimestampTerms,
|
|
1580
|
+
decodeValueLteTerms,
|
|
1003
1581
|
encodeDelegation,
|
|
1004
1582
|
encodeDelegations,
|
|
1005
1583
|
hashDelegation
|