Skip to content

Commit

Permalink
Dont expose ethers-multisend (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Oct 28, 2024
1 parent 2aa2143 commit 4783ff8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 56 deletions.
58 changes: 58 additions & 0 deletions src/decode/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { EIP712TypedData } from "near-ca";

import { isRlpHex, isTransactionSerializable } from "../lib/safe-message";
import { DecodedTxData, SafeEncodedSignRequest, UserOperation } from "../types";
import {
decodeRlpHex,
decodeTransactionSerializable,
decodeTypedData,
decodeUserOperation,
} from "./util";

/**
* Decodes transaction data for a given EVM transaction and extracts relevant details.
*
* @param {EvmTransactionData} data - The raw transaction data to be decoded.
* @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
*/
export function decodeTxData({
evmMessage,
chainId,
}: Omit<SafeEncodedSignRequest, "hashToSign">): DecodedTxData {
const data = evmMessage;
if (isRlpHex(evmMessage)) {
return decodeRlpHex(chainId, evmMessage);
}
if (isTransactionSerializable(data)) {
return decodeTransactionSerializable(chainId, data);
}
if (typeof data !== "string") {
return decodeTypedData(chainId, data);
}
try {
// Stringified UserOperation.
const userOp: UserOperation = JSON.parse(data);
return decodeUserOperation(chainId, userOp);
} catch (error: unknown) {
if (error instanceof SyntaxError) {
// Raw message string.
return {
chainId,
costEstimate: "0",
transactions: [],
message: data,
};
} else {
// TODO: This shouldn't happen anymore and can probably be reverted.
// We keep it here now, because near-ca might not have adapted its router.
console.warn("Failed UserOp Parsing, try TypedData Parsing", error);
try {
const typedData: EIP712TypedData = JSON.parse(data);
return decodeTypedData(chainId, typedData);
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(`decodeTxData: Unexpected error - ${message}`);
}
}
}
}
55 changes: 3 additions & 52 deletions src/decode.ts → src/decode/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,9 @@ import {
TransactionSerializable,
} from "viem";

import { SAFE_DEPLOYMENTS } from "./_gen/deployments";
import { isMultisendTx } from "./lib/multisend";
import { isRlpHex, isTransactionSerializable } from "./lib/safe-message";
import { DecodedTxData, SafeEncodedSignRequest, UserOperation } from "./types";

/**
* Decodes transaction data for a given EVM transaction and extracts relevant details.
*
* @param {EvmTransactionData} data - The raw transaction data to be decoded.
* @returns {DecodedTxData} - An object containing the chain ID, estimated cost, and a list of decoded meta-transactions.
*/
export function decodeTxData({
evmMessage,
chainId,
}: Omit<SafeEncodedSignRequest, "hashToSign">): DecodedTxData {
const data = evmMessage;
if (isRlpHex(evmMessage)) {
return decodeRlpHex(chainId, evmMessage);
}
if (isTransactionSerializable(data)) {
return decodeTransactionSerializable(chainId, data);
}
if (typeof data !== "string") {
return decodeTypedData(chainId, data);
}
try {
// Stringified UserOperation.
const userOp: UserOperation = JSON.parse(data);
return decodeUserOperation(chainId, userOp);
} catch (error: unknown) {
if (error instanceof SyntaxError) {
// Raw message string.
return {
chainId,
costEstimate: "0",
transactions: [],
message: data,
};
} else {
// TODO: This shouldn't happen anymore and can probably be reverted.
// We keep it here now, because near-ca might not have adapted its router.
console.warn("Failed UserOp Parsing, try TypedData Parsing", error);
try {
const typedData: EIP712TypedData = JSON.parse(data);
return decodeTypedData(chainId, typedData);
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(`decodeTxData: Unexpected error - ${message}`);
}
}
}
}
import { SAFE_DEPLOYMENTS } from "../_gen/deployments";
import { isMultisendTx } from "../lib/multisend";
import { DecodedTxData, UserOperation } from "../types";

export function decodeTransactionSerializable(
chainId: number,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export * from "./near-safe";
export * from "./types";
export * from "./util";
export * from "./constants";
export * from "./decode";
export { decodeTxData } from "./decode";
export * from "./lib/safe-message";

export {
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/decode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { EIP712TypedData } from "near-ca";
import { TransactionSerializableEIP1559 } from "viem";

import { SafeEncodedSignRequest, UserOperation } from "../../src";
import { SafeEncodedSignRequest, UserOperation, decodeTxData } from "../../src";
import {
decodeRlpHex,
decodeTransactionSerializable,
decodeTxData,
decodeTypedData,
decodeUserOperation,
} from "../../src/decode";
} from "../../src/decode/util";

const chainId = 11155111;
describe("decoding functions", () => {
Expand Down

0 comments on commit 4783ff8

Please sign in to comment.