Skip to content

Commit 905f873

Browse files
committed
Consume implementation from delegation-core for hashing caveats, and related constants.
1 parent f70909b commit 905f873

9 files changed

Lines changed: 30 additions & 65 deletions

File tree

packages/delegation-core/src/delegation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { encode, encodeSingle, decodeSingle } from '@metamask/abi-utils';
22
import { type BytesLike } from '@metamask/utils';
3-
import { keccak_256 } from '@noble/hashes/sha3';
3+
import { keccak_256 as keccak256 } from '@noble/hashes/sha3';
44

55
import {
66
bytesLikeToBytes,
@@ -222,7 +222,7 @@ export function getDelegationHash(
222222
delegation.salt,
223223
],
224224
);
225-
const hash = keccak_256(encoded);
225+
const hash = keccak256(encoded);
226226
return prepareResult(hash, options);
227227
}
228228

@@ -247,7 +247,7 @@ function getCaveatsArrayHash(caveats: CaveatStruct[]): Uint8Array {
247247
encoded.set(caveatHash, i * 32);
248248
}
249249

250-
return keccak_256(encoded);
250+
return keccak256(encoded);
251251
}
252252

253253
/**
@@ -260,6 +260,6 @@ function getCaveatHash(caveat: CaveatStruct): Uint8Array {
260260
['bytes32', 'address', 'bytes32'],
261261
[CAVEAT_TYPEHASH, caveat.enforcer, caveat.terms],
262262
);
263-
const hash = keccak_256(encoded);
263+
const hash = keccak256(encoded);
264264
return hash;
265265
}

packages/delegation-core/test/delegation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { bytesToHex } from '@metamask/utils';
12
import { describe, it, expect } from 'vitest';
23

34
import {
@@ -7,7 +8,6 @@ import {
78
getDelegationHash,
89
} from '../src/delegation';
910
import type { DelegationStruct, CaveatStruct } from '../src/types';
10-
import { bytesToHex } from '@metamask/utils';
1111

1212
describe('delegation', () => {
1313
describe('ROOT_AUTHORITY', () => {

packages/delegation-toolkit/src/DelegationFramework/DelegationManager/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { ROOT_AUTHORITY, ANY_DELEGATE } from '../../constants';
1+
export { ROOT_AUTHORITY, ANY_BENEFICIARY } from '../../delegation';
22

33
export const NAME = 'DelegationManager';
44
export const VERSION = '1.3.0';

packages/delegation-toolkit/src/DelegationFramework/HybridDeleGator/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { ROOT_AUTHORITY, ANY_DELEGATE } from '../../constants';
1+
export { ROOT_AUTHORITY, ANY_BENEFICIARY } from '../../delegation';
22

33
export const NAME = 'HybridDeleGator';
44
export const VERSION = '1.3.0';

packages/delegation-toolkit/src/constants.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,3 @@ export enum Implementation {
77
Hybrid = 'Hybrid',
88
Stateless7702 = 'Stateless7702',
99
}
10-
11-
/**
12-
* To be used on a delegation as the root authority.
13-
*/
14-
export const ROOT_AUTHORITY =
15-
'0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
16-
17-
/**
18-
* To be used on a delegation as the any delegate.
19-
*/
20-
export const ANY_DELEGATE = '0x0000000000000000000000000000000000000a11';

packages/delegation-toolkit/src/delegation.ts

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import {
22
encodeDelegations as encodeDelegationsCore,
33
decodeDelegations as decodeDelegationsCore,
4+
getDelegationHash as getDelegationHashCore,
5+
ANY_BENEFICIARY,
6+
DELEGATION_TYPEHASH,
7+
CAVEAT_TYPEHASH,
8+
ROOT_AUTHORITY,
49
} from '@metamask/delegation-core';
5-
import {
6-
encodeAbiParameters,
7-
parseAbiParameters,
8-
keccak256,
9-
hashMessage,
10-
toBytes,
11-
toHex,
12-
getAddress,
13-
} from 'viem';
10+
import { hashMessage, toBytes, toHex, getAddress } from 'viem';
1411
import type {
1512
TypedData,
1613
AbiParameter,
@@ -23,13 +20,16 @@ import type {
2320
} from 'viem';
2421

2522
import { type Caveats, resolveCaveats } from './caveatBuilder';
26-
import {
27-
CAVEAT_ABI_TYPE_COMPONENTS,
28-
getCaveatArrayPacketHash,
29-
} from './caveats';
30-
import { ROOT_AUTHORITY } from './constants';
23+
import { CAVEAT_ABI_TYPE_COMPONENTS } from './caveats';
3124
import type { Delegation } from './types';
3225

26+
export {
27+
ANY_BENEFICIARY,
28+
DELEGATION_TYPEHASH,
29+
CAVEAT_TYPEHASH,
30+
ROOT_AUTHORITY,
31+
};
32+
3333
/**
3434
* The ABI type components of a Delegation.
3535
*/
@@ -141,21 +141,6 @@ export const decodePermissionContexts = (encoded: Hex[]): Delegation[][] => {
141141
return delegationChains;
142142
};
143143

144-
/**
145-
* To be used in the allowList field of a gas delegation so as not to restrict who can redeem the gas delegation.
146-
*/
147-
export const ANY_BENEFICIARY = '0x0000000000000000000000000000000000000a11';
148-
149-
/**
150-
* To be used when generating a delegation hash to be signed
151-
* NOTE: signature is omitted from the Delegation typehash
152-
*/
153-
export const DELEGATION_TYPEHASH = keccak256(
154-
toHex(
155-
'Delegation(address delegate,address delegator,bytes32 authority,Caveat[] caveats,uint256 salt)Caveat(address enforcer,bytes terms)',
156-
),
157-
);
158-
159144
/**
160145
* TypedData to be used when signing a Delegation. Delegation value for `signature` and Caveat values for `args` are omitted as they cannot be known at signing time.
161146
*/
@@ -200,19 +185,7 @@ export const prepDelegationHashForPasskeySign = (delegationHash: Hex) => {
200185
export const getDelegationHashOffchain = (input: Delegation): Hex => {
201186
const delegationStruct = toDelegationStruct(input);
202187

203-
const encoded = encodeAbiParameters(
204-
parseAbiParameters('bytes32, address, address, bytes32, bytes32, uint'),
205-
[
206-
DELEGATION_TYPEHASH,
207-
delegationStruct.delegate,
208-
delegationStruct.delegator,
209-
delegationStruct.authority,
210-
getCaveatArrayPacketHash(delegationStruct.caveats),
211-
delegationStruct.salt,
212-
],
213-
);
214-
215-
return keccak256(encoded);
188+
return getDelegationHashCore(delegationStruct);
216189
};
217190

218191
type BaseCreateDelegationOptions = {

packages/delegation-toolkit/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
export { toMetaMaskSmartAccount } from './toMetaMaskSmartAccount';
22

33
export {
4-
ANY_BENEFICIARY,
54
createDelegation,
65
createOpenDelegation,
76
signDelegation,
7+
ROOT_AUTHORITY,
8+
ANY_BENEFICIARY,
89
} from './delegation';
910

1011
export type {
@@ -35,7 +36,7 @@ export {
3536
getDeleGatorEnvironment,
3637
} from './delegatorEnvironment';
3738

38-
export { Implementation, ROOT_AUTHORITY, ANY_DELEGATE } from './constants';
39+
export { Implementation } from './constants';
3940

4041
export { createExecution } from './executions';
4142

packages/delegation-toolkit/test/delegation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { expect } from 'chai';
22
import { stub } from 'sinon';
33

44
import { resolveCaveats } from '../src/caveatBuilder';
5-
import { ROOT_AUTHORITY } from '../src/constants';
65
import {
76
type DelegationStruct,
7+
ROOT_AUTHORITY,
88
toDelegationStruct,
99
createDelegation,
1010
createOpenDelegation,

packages/delegation-toolkit/test/experimental/delegationStorageTestData.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { ROOT_AUTHORITY } from '../../src/constants';
2-
import { getDelegationHashOffchain } from '../../src/delegation';
1+
import {
2+
ROOT_AUTHORITY,
3+
getDelegationHashOffchain,
4+
} from '../../src/delegation';
35
import type { Delegation } from '../../src/types';
46

57
export const TEST_ACCOUNT = '0x0E9bBA6e2D962645c5FB1064d86cc6dE6050739C';

0 commit comments

Comments
 (0)