Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschwarz committed Feb 11, 2025
1 parent 0a6d3d9 commit fbd5022
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
9 changes: 7 additions & 2 deletions src/allowEip712Signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { ParamType } from "ethers";
import { Prettify, TypedData, TypedDataDomain } from "viem";
import { FunctionPermissionCoerced, Scoping, c } from "zodiac-roles-sdk";

import { EIP_712_SIGNER_ADDRESS, SIGN_FUNCTION_SELECTOR, TYPED_VALUE_TUPLE } from "./const";
import { EIP_712_SIGNER_ADDRESS } from "./const";
import { scopeTypedData } from "./scopeTypedData";
import { asAbiType } from "./utils";
import { encodeTypes } from "./encodeTypedData";

type AllowEip712SignatureParameters<
typedData extends TypedData | Record<string, unknown> = TypedData,
Expand Down Expand Up @@ -68,10 +69,14 @@ export const allowEip712Signature = <const typedData extends TypedData, primaryT
type: primaryType,
});



encodeTypes({ types, primaryType }),

return {
targetAddress: EIP_712_SIGNER_ADDRESS.toLowerCase() as `0x${string}`,
selector: SIGN_FUNCTION_SELECTOR,
delegatecall: true,
condition: c.calldataMatches([domainCondition, messageCondition], [TYPED_VALUE_TUPLE, TYPED_VALUE_TUPLE])(),
condition: c.calldataMatches([domainCondition, messageCondition, 1n], [TYPED_VALUE_TUPLE, TYPED_VALUE_TUPLE])(),
};
};
4 changes: 0 additions & 4 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export const EIP_712_SIGNER_ADDRESS = "0x" as const;
export const SIGN_FUNCTION_SELECTOR = "0xa500d5ba" as const;

export const TYPED_VALUE_TUPLE = "(uint8 dataType, bytes value, string structSignature)" as const;
export const TYPED_VALUE_TUPLE_ARRAY = "(uint8 dataType, bytes value, string structSignature)[]" as const;
2 changes: 1 addition & 1 deletion src/encodeTypedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const encodeTypedValue = <T extends TypedData>({
return encodeAbiParameters([abiParam], [value]);
};

const encodeTypes = ({ types, primaryType }: { types: TypedData; primaryType: string }): Type[] => {
export const encodeTypes = ({ types, primaryType }: { types: TypedData; primaryType: string }): Type[] => {
const { EIP712Domain: _0, [primaryType]: _1, ...rest } = types;

const orderedTypeKeys = ["EIP712Domain", primaryType, ...Object.keys(rest)];
Expand Down
54 changes: 25 additions & 29 deletions src/scopeTypedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { ParamType } from "ethers";
import { TypedData } from "viem";
import { Condition, Operator, ParameterType, c } from "zodiac-roles-sdk";

import { TYPED_VALUE_TUPLE, TYPED_VALUE_TUPLE_ARRAY } from "./const";
import { DataType } from "./types";
import { encodeStructType, isAtomic } from "./utils";

/** Maps over a condition structure formulated to scope the typed data object, turning it into a condition structure scoping the corresponding TypedValue encoding */
/**
* Maps over a condition structure formulated to scope the typed data object,
* turning it into a condition structure scoping the recursively ABI encoded value as sent to the contract.
* Once the contract can decode directly from calldata, we should be able to send the original conditions structure so this function will be obsolete.
**/
export const scopeTypedData = ({
condition,
types,
Expand Down Expand Up @@ -42,12 +42,15 @@ export const scopeTypedData = ({
}

const elementType = type.split("[")[0];
return {
...condition,
children: condition.children?.map((child) =>
scopeTypedData({ condition: child, types, type: elementType, scopeSignature }),
),
};
return c.abiEncodedMatches(
[
{
...condition,
children: condition.children?.map((child) => scopeTypedData({ condition: child, types, type: elementType })),
},
],
["bytes[]"],
)(ParamType.from("bytes"));
}

// struct
Expand All @@ -61,25 +64,18 @@ export const scopeTypedData = ({
}

const structFields = types[type];
return c.matches({
dataType: DataType.Struct,
value: c.abiEncodedMatches(
[
structFields.map(({ type }, index) =>
condition.children && !!condition.children[index]
? scopeTypedData({ condition: condition.children[index], types, type })
: undefined,
),
],
[TYPED_VALUE_TUPLE_ARRAY],
),
structSignature: scopeSignature ? encodeStructType({ types, primaryType: type }) : undefined,
})(ParamType.from(TYPED_VALUE_TUPLE));
return c.abiEncodedMatches(
[
structFields.map(({ type }, index) =>
condition.children && !!condition.children[index]
? scopeTypedData({ condition: condition.children[index], types, type })
: undefined,
),
],
["bytes[]"],
)(ParamType.from("bytes"));
}

// basic types
return c.matches({
dataType: isAtomic(type) ? DataType.Atomic : DataType.Dynamic,
value: c.abiEncodedMatches([condition], [type]),
})(ParamType.from(TYPED_VALUE_TUPLE));
return c.abiEncodedMatches([condition], [type])(ParamType.from("bytes"));
};

0 comments on commit fbd5022

Please sign in to comment.