Skip to content

Commit

Permalink
Transaction Serializable & RlpHex Guards
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith committed Nov 12, 2024
1 parent 502a98c commit 19efa7a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/types/guards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { isAddress, TypedDataDomain } from "viem";
import {
Hex,
isAddress,
parseTransaction,
serializeTransaction,
TransactionSerializable,
TypedDataDomain,
} from "viem";
import { EIP712TypedData, SignMethod, TypedMessageTypes } from ".";

export function isSignMethod(method: unknown): method is SignMethod {
Expand Down Expand Up @@ -76,3 +83,24 @@ export const isEIP712TypedData = (obj: unknown): obj is EIP712TypedData => {
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;
}
}
70 changes: 69 additions & 1 deletion tests/unit/types.guards.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { isEIP712TypedData, isSignMethod } from "../../src/";
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,
};
describe("Type Guards", () => {
it("isSignMethod", async () => {
expect(isSignMethod("poop")).toBe(false);
Expand Down Expand Up @@ -52,3 +65,58 @@ describe("Type Guards", () => {
expect(isEIP712TypedData(typedData)).toBe(true);
});
});

describe("isTransactionSerializable", () => {
it("should return true for valid transaction data", () => {
expect(isTransactionSerializable(validEIP1559Transaction)).toBe(true);
});

it("should return false for invalid transaction data", () => {
const invalidCases = [
null,
undefined,
{},
{ to: "invalid-address" },
{ value: "not-a-bigint" },
{ chainId: "not-a-number" },
"random string",
123,
[],
];

invalidCases.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);
});
});
});

0 comments on commit 19efa7a

Please sign in to comment.