Skip to content

Commit 2c4a780

Browse files
committed
Switch to use terms decoders from @metamask/delegation-core
1 parent 251a766 commit 2c4a780

14 files changed

Lines changed: 58 additions & 561 deletions

packages/7715-permission-types/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export {
5454
makeErc20TokenStreamDecoderConfig,
5555
makeErc20TokenPeriodicDecoderConfig,
5656
makeErc20TokenAllowanceDecoderConfig,
57-
makeErc20TokenRevocationDecoderConfig,
5857
makeTokenApprovalRevocationDecoderConfig,
5958
createPermissionDecodersForContracts,
6059
} from './permissions';

packages/7715-permission-types/src/permissions/caveats/erc20TokenPeriodic.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hexToBigInt, hexToNumber } from '@metamask/utils';
1+
import { decodeERC20TokenPeriodTransferTerms } from '@metamask/delegation-core';
22

33
import { expiryRule } from '../rules/expiry';
44
import { erc20PayeeRuleDecoder } from '../rules/payee';
@@ -10,10 +10,8 @@ import type {
1010
MakePermissionDecoderConfig,
1111
} from '../types';
1212
import {
13-
getByteLength,
1413
getTermsByEnforcer,
1514
MAX_PERIOD_DURATION,
16-
splitHex,
1715
ZERO_32_BYTES,
1816
} from '../utils';
1917

@@ -79,23 +77,14 @@ function validateAndDecodeData(
7977
enforcer: erc20PeriodicEnforcer,
8078
});
8179

82-
const EXPECTED_TERMS_BYTELENGTH = 116; // 20 + 32 + 32 + 32
83-
84-
if (getByteLength(terms) !== EXPECTED_TERMS_BYTELENGTH) {
85-
throw new Error('Invalid erc20-token-periodic terms: expected 116 bytes');
86-
}
87-
88-
const [tokenAddress, periodAmount, periodDurationRaw, startTimeRaw] =
89-
splitHex(terms, [20, 32, 32, 32]);
90-
if (!tokenAddress || !periodAmount || !periodDurationRaw || !startTimeRaw) {
91-
throw new Error('Invalid erc20-token-periodic terms');
92-
}
93-
94-
const periodDuration = hexToNumber(periodDurationRaw);
95-
const periodAmountBigInt = hexToBigInt(periodAmount);
96-
const startTime = hexToNumber(startTimeRaw);
80+
const {
81+
tokenAddress,
82+
periodAmount,
83+
periodDuration,
84+
startDate: startTime,
85+
} = decodeERC20TokenPeriodTransferTerms(terms);
9786

98-
if (periodAmountBigInt === 0n) {
87+
if (periodAmount === 0n) {
9988
throw new Error(
10089
'Invalid erc20-token-periodic terms: periodAmount must be a positive number',
10190
);

packages/7715-permission-types/src/permissions/caveats/erc20TokenRevocation.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.

packages/7715-permission-types/src/permissions/caveats/erc20TokenStream.ts

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hexToBigInt, hexToNumber } from '@metamask/utils';
1+
import { decodeERC20StreamingTerms } from '@metamask/delegation-core';
22

33
import { expiryRule } from '../rules/expiry';
44
import { erc20PayeeRuleDecoder } from '../rules/payee';
@@ -9,12 +9,7 @@ import type {
99
DecodedPermission,
1010
MakePermissionDecoderConfig,
1111
} from '../types';
12-
import {
13-
getByteLength,
14-
getTermsByEnforcer,
15-
splitHex,
16-
ZERO_32_BYTES,
17-
} from '../utils';
12+
import { getTermsByEnforcer, ZERO_32_BYTES } from '../utils';
1813

1914
/**
2015
* Builds the configuration for the erc20-token-stream permission decoder.
@@ -78,42 +73,16 @@ function validateAndDecodeData(
7873
caveats,
7974
enforcer: erc20StreamingEnforcer,
8075
});
76+
const { tokenAddress, initialAmount, maxAmount, amountPerSecond, startTime } =
77+
decodeERC20StreamingTerms(terms);
8178

82-
const EXPECTED_TERMS_BYTELENGTH = 148;
83-
84-
if (getByteLength(terms) !== EXPECTED_TERMS_BYTELENGTH) {
85-
throw new Error('Invalid erc20-token-stream terms: expected 148 bytes');
86-
}
87-
88-
const [
89-
tokenAddress,
90-
initialAmount,
91-
maxAmount,
92-
amountPerSecond,
93-
startTimeRaw,
94-
] = splitHex(terms, [20, 32, 32, 32, 32]);
95-
if (
96-
!tokenAddress ||
97-
!initialAmount ||
98-
!maxAmount ||
99-
!amountPerSecond ||
100-
!startTimeRaw
101-
) {
102-
throw new Error('Invalid erc20-token-stream terms');
103-
}
104-
105-
const startTime = hexToNumber(startTimeRaw);
106-
const initialAmountBigInt = hexToBigInt(initialAmount);
107-
const maxAmountBigInt = hexToBigInt(maxAmount);
108-
const amountPerSecondBigInt = hexToBigInt(amountPerSecond);
109-
110-
if (maxAmountBigInt <= initialAmountBigInt) {
79+
if (maxAmount <= initialAmount) {
11180
throw new Error(
11281
'Invalid erc20-token-stream terms: maxAmount must be greater than initialAmount',
11382
);
11483
}
11584

116-
if (amountPerSecondBigInt === 0n) {
85+
if (amountPerSecond === 0n) {
11786
throw new Error(
11887
'Invalid erc20-token-stream terms: amountPerSecond must be a positive number',
11988
);

packages/7715-permission-types/src/permissions/caveats/nativeTokenPeriodic.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hexToBigInt, hexToNumber } from '@metamask/utils';
1+
import { decodeNativeTokenPeriodTransferTerms } from '@metamask/delegation-core';
22

33
import { expiryRule } from '../rules/expiry';
44
import { nativePayeeRuleDecoder } from '../rules/payee';
@@ -9,12 +9,7 @@ import type {
99
DecodedPermission,
1010
MakePermissionDecoderConfig,
1111
} from '../types';
12-
import {
13-
getByteLength,
14-
getTermsByEnforcer,
15-
MAX_PERIOD_DURATION,
16-
splitHex,
17-
} from '../utils';
12+
import { getTermsByEnforcer, MAX_PERIOD_DURATION } from '../utils';
1813

1914
/**
2015
* Builds the configuration for the native-token-periodic permission decoder.
@@ -79,26 +74,13 @@ function validateAndDecodeData(
7974
caveats,
8075
enforcer: nativeTokenPeriodicEnforcer,
8176
});
77+
const {
78+
periodAmount,
79+
periodDuration,
80+
startDate: startTime,
81+
} = decodeNativeTokenPeriodTransferTerms(terms);
8282

83-
const EXPECTED_TERMS_BYTELENGTH = 96; // 32 + 32 + 32
84-
85-
if (getByteLength(terms) !== EXPECTED_TERMS_BYTELENGTH) {
86-
throw new Error('Invalid native-token-periodic terms: expected 96 bytes');
87-
}
88-
89-
const [periodAmount, periodDurationRaw, startTimeRaw] = splitHex(
90-
terms,
91-
[32, 32, 32],
92-
);
93-
if (!periodAmount || !periodDurationRaw || !startTimeRaw) {
94-
throw new Error('Invalid native-token-periodic terms');
95-
}
96-
97-
const periodDuration = hexToNumber(periodDurationRaw);
98-
const startTime = hexToNumber(startTimeRaw);
99-
const periodAmountBigInt = hexToBigInt(periodAmount);
100-
101-
if (periodAmountBigInt === 0n) {
83+
if (periodAmount === 0n) {
10284
throw new Error(
10385
'Invalid native-token-periodic terms: periodAmount must be a positive number',
10486
);

packages/7715-permission-types/src/permissions/caveats/nativeTokenStream.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hexToBigInt, hexToNumber } from '@metamask/utils';
1+
import { decodeNativeTokenStreamingTerms } from '@metamask/delegation-core';
22

33
import { expiryRule } from '../rules/expiry';
44
import { nativePayeeRuleDecoder } from '../rules/payee';
@@ -9,7 +9,7 @@ import type {
99
DecodedPermission,
1010
MakePermissionDecoderConfig,
1111
} from '../types';
12-
import { getByteLength, getTermsByEnforcer, splitHex } from '../utils';
12+
import { getTermsByEnforcer } from '../utils';
1313

1414
/**
1515
* Builds the configuration for the native-token-stream permission decoder.
@@ -74,33 +74,16 @@ function validateAndDecodeData(
7474
caveats,
7575
enforcer: nativeTokenStreamingEnforcer,
7676
});
77+
const { initialAmount, maxAmount, amountPerSecond, startTime } =
78+
decodeNativeTokenStreamingTerms(terms);
7779

78-
const EXPECTED_TERMS_BYTELENGTH = 128; // 32 + 32 + 32 + 32
79-
80-
if (getByteLength(terms) !== EXPECTED_TERMS_BYTELENGTH) {
81-
throw new Error('Invalid native-token-stream terms: expected 128 bytes');
82-
}
83-
84-
const [initialAmount, maxAmount, amountPerSecond, startTimeRaw] = splitHex(
85-
terms,
86-
[32, 32, 32, 32],
87-
);
88-
if (!initialAmount || !maxAmount || !amountPerSecond || !startTimeRaw) {
89-
throw new Error('Invalid native-token-stream terms');
90-
}
91-
92-
const initialAmountBigInt = hexToBigInt(initialAmount);
93-
const maxAmountBigInt = hexToBigInt(maxAmount);
94-
const amountPerSecondBigInt = hexToBigInt(amountPerSecond);
95-
const startTime = hexToNumber(startTimeRaw);
96-
97-
if (maxAmountBigInt <= initialAmountBigInt) {
80+
if (maxAmount <= initialAmount) {
9881
throw new Error(
9982
'Invalid native-token-stream terms: maxAmount must be greater than initialAmount',
10083
);
10184
}
10285

103-
if (amountPerSecondBigInt === 0n) {
86+
if (amountPerSecond === 0n) {
10487
throw new Error(
10588
'Invalid native-token-stream terms: amountPerSecond must be a positive number',
10689
);

0 commit comments

Comments
 (0)