-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Various Type Guards w. Tests #146
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { | ||
Hex, | ||
isAddress, | ||
parseTransaction, | ||
serializeTransaction, | ||
TransactionSerializable, | ||
TypedDataDomain, | ||
} from "viem"; | ||
import { EIP712TypedData, SignMethod, TypedMessageTypes } from "."; | ||
|
||
export function isSignMethod(method: unknown): method is SignMethod { | ||
return ( | ||
typeof method === "string" && | ||
[ | ||
"eth_sign", | ||
"personal_sign", | ||
"eth_sendTransaction", | ||
"eth_signTypedData", | ||
"eth_signTypedData_v4", | ||
].includes(method) | ||
); | ||
} | ||
|
||
const isTypedDataDomain = (domain: unknown): domain is TypedDataDomain => { | ||
if (typeof domain !== "object" || domain === null) return false; | ||
|
||
const candidate = domain as Record<string, unknown>; | ||
|
||
// Check that all properties, if present, are of the correct type | ||
return Object.entries(candidate).every(([key, value]) => { | ||
switch (key) { | ||
case "chainId": | ||
return typeof value === "undefined" || typeof value === "number"; | ||
case "name": | ||
case "version": | ||
return typeof value === "undefined" || typeof value === "string"; | ||
case "verifyingContract": | ||
return ( | ||
typeof value === "undefined" || | ||
(typeof value === "string" && isAddress(value)) | ||
); | ||
case "salt": | ||
return typeof value === "undefined" || typeof value === "string"; | ||
default: | ||
return false; // Reject unknown properties | ||
} | ||
}); | ||
}; | ||
|
||
const isTypedMessageTypes = (types: unknown): types is TypedMessageTypes => { | ||
if (typeof types !== "object" || types === null) return false; | ||
|
||
return Object.entries(types).every(([_, value]) => { | ||
return ( | ||
Array.isArray(value) && | ||
value.every( | ||
(item) => | ||
typeof item === "object" && | ||
item !== null && | ||
"name" in item && | ||
"type" in item && | ||
typeof item.name === "string" && | ||
typeof item.type === "string" | ||
) | ||
); | ||
}); | ||
}; | ||
|
||
export const isEIP712TypedData = (obj: unknown): obj is EIP712TypedData => { | ||
if (typeof obj !== "object" || obj === null) return false; | ||
|
||
const candidate = obj as Record<string, unknown>; | ||
|
||
return ( | ||
"domain" in candidate && | ||
"types" in candidate && | ||
"message" in candidate && | ||
"primaryType" in candidate && | ||
isTypedDataDomain(candidate.domain) && | ||
isTypedMessageTypes(candidate.types) && | ||
typeof candidate.message === "object" && | ||
candidate.message !== null && | ||
typeof candidate.primaryType === "string" | ||
); | ||
}; | ||
|
||
// Cheeky attempt to serialize. return true if successful! | ||
export function isTransactionSerializable( | ||
data: unknown | ||
): data is TransactionSerializable { | ||
try { | ||
serializeTransaction(data as TransactionSerializable); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
export function isRlpHex(data: unknown): data is Hex { | ||
try { | ||
parseTransaction(data as Hex); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { TransactionSerializable } from "viem"; | ||
import { | ||
isEIP712TypedData, | ||
isRlpHex, | ||
isSignMethod, | ||
isTransactionSerializable, | ||
} from "../../src/"; | ||
|
||
const validEIP1559Transaction: TransactionSerializable = { | ||
to: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", | ||
value: BigInt(1000000000000000000), // 1 ETH | ||
chainId: 1, | ||
maxFeePerGas: 1n, | ||
}; | ||
|
||
const commonInvalidCases = [ | ||
null, | ||
undefined, | ||
{}, | ||
{ to: "invalid-address" }, | ||
{ value: "not-a-bigint" }, | ||
{ chainId: "not-a-number" }, | ||
"random string", | ||
123, | ||
[], | ||
]; | ||
|
||
describe("SignMethod", () => { | ||
it("returns true for all valid SignMethods", async () => { | ||
[ | ||
"eth_sign", | ||
"personal_sign", | ||
"eth_sendTransaction", | ||
"eth_signTypedData", | ||
"eth_signTypedData_v4", | ||
].map((item) => expect(isSignMethod(item)).toBe(true)); | ||
}); | ||
|
||
it("returns false for invalid data inputs", async () => { | ||
["poop", undefined, false, 1, {}].map((item) => | ||
expect(isSignMethod(item)).toBe(false) | ||
); | ||
}); | ||
}); | ||
describe("isEIP712TypedData", () => { | ||
it("returns true for valid EIP712TypedData", async () => { | ||
const message = { | ||
from: { | ||
name: "Cow", | ||
wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826", | ||
}, | ||
to: { | ||
name: "Bob", | ||
wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB", | ||
}, | ||
contents: "Hello, Bob!", | ||
} as const; | ||
|
||
const domain = { | ||
name: "Ether Mail", | ||
version: "1", | ||
chainId: 1, | ||
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", | ||
} as const; | ||
|
||
const types = { | ||
Person: [ | ||
{ name: "name", type: "string" }, | ||
{ name: "wallet", type: "address" }, | ||
], | ||
Mail: [ | ||
{ name: "from", type: "Person" }, | ||
{ name: "to", type: "Person" }, | ||
{ name: "contents", type: "string" }, | ||
], | ||
} as const; | ||
|
||
const typedData = { | ||
types, | ||
primaryType: "Mail", | ||
message, | ||
domain, | ||
} as const; | ||
expect(isEIP712TypedData(typedData)).toBe(true); | ||
}); | ||
|
||
it("returns false for invalid data inputs", async () => { | ||
commonInvalidCases.map((item) => | ||
expect(isEIP712TypedData(item)).toBe(false) | ||
); | ||
}); | ||
}); | ||
|
||
describe("isTransactionSerializable", () => { | ||
it("should return true for valid transaction data", () => { | ||
expect(isTransactionSerializable(validEIP1559Transaction)).toBe(true); | ||
}); | ||
|
||
it("should return false for invalid transaction data", () => { | ||
commonInvalidCases.forEach((testCase) => { | ||
expect(isTransactionSerializable(testCase)).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("isRlpHex", () => { | ||
it("should return true for valid RLP-encoded transaction hex", () => { | ||
// This is an example of a valid RLP-encoded transaction hex: | ||
|
||
// serializeTransaction(validEIP1559Transaction) | ||
const validRlpHex = | ||
"0x02e501808001809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c0"; | ||
expect(isRlpHex(validRlpHex)).toBe(true); | ||
}); | ||
|
||
it("should return false for invalid RLP hex data", () => { | ||
const invalidCases = [ | ||
null, | ||
undefined, | ||
{}, | ||
"not-a-hex", | ||
"0x", // empty hex | ||
"0x1234", // too short | ||
"0xinvalid", | ||
123, | ||
[], | ||
// Invalid RLP structure but valid hex | ||
"0x1234567890abcdef", | ||
]; | ||
|
||
invalidCases.forEach((testCase) => { | ||
expect(isRlpHex(testCase)).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/unit/utils.mock-sign.test.ts → tests/unit/utils/mock-sign.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
tests/unit/utils.transaction.test.ts → tests/unit/utils/transaction.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now exported via types/index.ts