Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

feat: Support non eip155 tx #490

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions crates/indexer/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub struct Web3Indexer {
rollup_type_hash: H256,
allowed_eoa_hashes: HashSet<H256>,
godwoken_rpc_client: GodwokenRpcClient,
chain_id: u64,
godwoken_async_client: GodwokenAsyncClient,
}

Expand All @@ -51,7 +50,6 @@ impl Web3Indexer {
rollup_type_hash: H256,
eth_account_lock_hash: H256,
gw_rpc_url: &str,
chain_id: u64,
) -> Self {
let mut allowed_eoa_hashes = HashSet::default();
allowed_eoa_hashes.insert(eth_account_lock_hash);
Expand All @@ -64,7 +62,6 @@ impl Web3Indexer {
rollup_type_hash,
allowed_eoa_hashes,
godwoken_rpc_client,
chain_id,
godwoken_async_client,
}
}
Expand Down Expand Up @@ -194,7 +191,7 @@ impl Web3Indexer {
};
(Some(address), polyjuice_chain_id)
};
let chain_id: u64 = self.chain_id;
let chain_id: u64 = l2_transaction.raw().chain_id().unpack();
let nonce: u32 = l2_transaction.raw().nonce().unpack();
let input = polyjuice_args.input.clone().unwrap_or_default();

Expand Down
1 change: 0 additions & 1 deletion crates/indexer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ impl Runner {
config.rollup_type_hash,
config.eth_account_lock_hash,
config.godwoken_rpc_url.as_str(),
config.chain_id,
);
let godwoken_rpc_client = GodwokenRpcClient::new(config.godwoken_rpc_url.as_str());
let runner = Runner {
Expand Down
9 changes: 7 additions & 2 deletions crates/indexer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ impl Transaction {

fn add_chain_replay_protection(&self) -> u64 {
self.v as u64
+ if let Some(n) = self.chain_id {
35 + n * 2
+ if let Some(id) = self.chain_id {
// For non eip-155 txs
if id == 0 {
27
} else {
35 + id * 2
}
} else {
27
}
Expand Down
4 changes: 3 additions & 1 deletion packages/api-server/cli/fix-eth-tx-hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ function updateEthTxHash(tx: DBTransaction, chainId: bigint): Hash {
const r = BigInt("0x" + tx.r.toString("hex"));
const s = BigInt("0x" + tx.s.toString("hex"));

// for non eip-155 txs(chain id = 0), v = 27 + last byte of signature(0 / 1)
// v = chain_id * 2 + 35 + last byte of signature(0 / 1)
keroro520 marked this conversation as resolved.
Show resolved Hide resolved
const v: bigint = chainId * 2n + 35n + BigInt(tx.v);
const v: bigint =
chainId === 0n ? 27n + BigInt(tx.v) : chainId * 2n + 35n + BigInt(tx.v);

const data = "0x" + tx.input?.toString("hex");
const to =
Expand Down
71 changes: 36 additions & 35 deletions packages/api-server/src/convert-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,45 +132,41 @@ export function getSignature(tx: PolyjuiceTransaction): HexString {
return "0x" + tx.r.slice(2) + tx.s.slice(2) + realVWithoutPrefix;
}

function numberToRlpEncode(num: HexString): HexString {
if (num === "0x0" || num === "0x") {
return "0x";
}

return "0x" + BigInt(num).toString(16);
}

// https://eips.ethereum.org/EIPS/eip-155
// For non eip-155 txs, (nonce, gasprice, startgas, to, value, data)
// For eip155 txs, (nonce, gasprice, startgas, to, value, data, chainid, 0, 0)
function calcMessage(tx: PolyjuiceTransaction): HexString {
let vInt = +tx.v;
let finalVInt = undefined;
const v: bigint = BigInt(tx.v);

const beforeEncode: any[] = [
toRlpNumber(tx.nonce),
toRlpNumber(tx.gasPrice),
toRlpNumber(tx.gasLimit),
tx.to,
toRlpNumber(tx.value),
tx.data,
];

if (vInt === 27 || vInt === 28) {
throw new Error(
`only EIP155 transaction is allowed, illegal transaction recId ${tx.v}. more info: ${COMPATIBLE_DOCS_URL}`
);
}
// if v = 27 / 28, it's non eip-155 txs
if (v !== 27n && v !== 28n) {
let chainId: bigint;
if (v % 2n === 0n) {
chainId = (v - 36n) / 2n;
} else {
chainId = (v - 35n) / 2n;
}

if (vInt % 2 === 0) {
finalVInt = "0x" + BigInt((vInt - 36) / 2).toString(16);
} else {
finalVInt = "0x" + BigInt((vInt - 35) / 2).toString(16);
// chain id
beforeEncode.push(chainId);
// r
beforeEncode.push(0);
// s
beforeEncode.push(0);
}

const rawTx: PolyjuiceTransaction = {
...tx,
nonce: numberToRlpEncode(tx.nonce),
gasPrice: numberToRlpEncode(tx.gasPrice),
gasLimit: numberToRlpEncode(tx.gasLimit),
value: numberToRlpEncode(tx.value),
r: "0x",
s: "0x",
v: numberToRlpEncode(finalVInt),
};

const encoded = encodePolyjuiceTransaction(rawTx);
const encoded: Buffer = rlp.encode(beforeEncode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating standalone util functions to make it more readable would be helpful, such as bool isEip155Transaction, bytes rlpEncodeEip155Transaction, and bytes rlpEncodeNonEip155Transaction.


const message =
"0x" + keccak256(Buffer.from(encoded.slice(2), "hex")).toString("hex");
const message = "0x" + keccak256(encoded).toString("hex");

return message;
}
Expand Down Expand Up @@ -203,7 +199,7 @@ export async function parseRawTransactionData(
rpc: GodwokenClient,
polyjuiceRawTx: HexString
): Promise<[L2Transaction, [string, string] | undefined]> {
const { nonce, gasPrice, gasLimit, to, value, data } = rawTx;
const { nonce, gasPrice, gasLimit, to, value, data, v } = rawTx;

// Reject transactions with too large size
const rlpEncoded = encodePolyjuiceTransaction(rawTx);
Expand Down Expand Up @@ -350,8 +346,13 @@ export async function parseRawTransactionData(
args_48_52.slice(2) +
args_data.slice(2);

let chainId = gwConfig.web3ChainId;
// if v = 27 || 28, set chain_id = 0
keroro520 marked this conversation as resolved.
Show resolved Hide resolved
if (v === "0x1b" || v === "0x1c") {
chainId = "0x0";
}
const godwokenRawL2Tx: RawL2Transaction = {
chain_id: gwConfig.web3ChainId,
chain_id: chainId,
from_id: fromId,
to_id: toId,
nonce: nonce === "0x" ? "0x0" : nonce,
Expand Down