Skip to content

Commit bed7f05

Browse files
committed
fix: add assert never pattern, refactor
1 parent 125311e commit bed7f05

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

account-kit/smart-contracts/src/ma-v2/account/nativeSMASigner.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
packUOSignature,
2020
pack1271Signature,
2121
DEFAULT_OWNER_ENTITY_ID,
22+
assertNeverSignatureRequestType,
2223
} from "../utils.js";
2324
import { SignatureType } from "../modules/utils.js";
2425

@@ -55,7 +56,30 @@ export const nativeSMASigner = (
5556
prepareSign: async (
5657
request: SignatureRequest
5758
): Promise<SignatureRequest> => {
58-
let ret: SignatureRequest = {
59+
let hash;
60+
61+
switch (request.type) {
62+
case "personal_sign":
63+
hash = hashMessage(request.data);
64+
break;
65+
66+
case "eth_signTypedData_v4":
67+
const isDeferredAction =
68+
request.data?.primaryType === "DeferredAction" &&
69+
request.data?.domain?.verifyingContract === accountAddress;
70+
71+
if (isDeferredAction) {
72+
return request;
73+
} else {
74+
hash = await hashTypedData(request.data);
75+
break;
76+
}
77+
78+
default:
79+
assertNeverSignatureRequestType();
80+
}
81+
82+
return {
5983
type: "eth_signTypedData_v4",
6084
data: {
6185
domain: {
@@ -66,29 +90,11 @@ export const nativeSMASigner = (
6690
ReplaySafeHash: [{ name: "hash", type: "bytes32" }],
6791
},
6892
message: {
69-
hash: "",
93+
hash,
7094
},
7195
primaryType: "ReplaySafeHash",
7296
},
7397
};
74-
75-
if (request.type === "personal_sign") {
76-
ret.data.message.hash = hashMessage(request.data);
77-
} else if (request.type === "eth_signTypedData_v4") {
78-
const isDeferredAction =
79-
request.data?.primaryType === "DeferredAction" &&
80-
request.data?.domain?.verifyingContract === accountAddress;
81-
82-
if (isDeferredAction) {
83-
ret.data = request.data;
84-
} else {
85-
ret.data.message.hash = hashTypedData(request.data);
86-
}
87-
} else {
88-
throw new Error(`Unsupported signature request type`);
89-
}
90-
91-
return ret;
9298
},
9399
formatSign: async (signature: Hex) => {
94100
return pack1271Signature({

account-kit/smart-contracts/src/ma-v2/modules/single-signer-validation/signer.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ import {
1919
getDefaultSingleSignerValidationModuleAddress,
2020
SignatureType,
2121
} from "../utils.js";
22+
import {
23+
packUOSignature,
24+
pack1271Signature,
25+
assertNeverSignatureRequestType,
26+
} from "../../utils.js";
2227

23-
import { packUOSignature, pack1271Signature } from "../../utils.js";
2428
/**
2529
* Creates an object with methods for generating a dummy signature, signing user operation hashes, signing messages, and signing typed data.
2630
*
@@ -57,7 +61,22 @@ export const singleSignerMessageSigner = (
5761
prepareSign: async (
5862
request: SignatureRequest
5963
): Promise<SignatureRequest> => {
60-
let ret: SignatureRequest = {
64+
let hash;
65+
66+
switch (request.type) {
67+
case "personal_sign":
68+
hash = hashMessage(request.data);
69+
break;
70+
71+
case "eth_signTypedData_v4":
72+
hash = await hashTypedData(request.data);
73+
break;
74+
75+
default:
76+
assertNeverSignatureRequestType();
77+
}
78+
79+
return {
6180
type: "eth_signTypedData_v4",
6281
data: {
6382
domain: {
@@ -70,21 +89,11 @@ export const singleSignerMessageSigner = (
7089
ReplaySafeHash: [{ name: "hash", type: "bytes32" }],
7190
},
7291
message: {
73-
hash: "",
92+
hash,
7493
},
7594
primaryType: "ReplaySafeHash",
7695
},
7796
};
78-
79-
if (request.type === "personal_sign") {
80-
ret.data.message.hash = hashMessage(request.data);
81-
} else if (request.type === "eth_signTypedData_v4") {
82-
ret.data.message.hash = hashTypedData(request.data);
83-
} else {
84-
throw new Error(`Unsupported signature request type`);
85-
}
86-
87-
return ret;
8897
},
8998
formatSign: async (signature: Hex) => {
9099
return pack1271Signature({

account-kit/smart-contracts/src/ma-v2/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,7 @@ export const buildDeferredActionDigest = ({
315315
]);
316316
return encodedData;
317317
};
318+
319+
export const assertNeverSignatureRequestType = (): never => {
320+
throw new Error("Invalid signature request type ");
321+
};

0 commit comments

Comments
 (0)