Skip to content
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

signing transaction with ethereumjs/tx #344

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"fs-extra": "^9.0.0",
"husky": "^3.1.0",
"jest": "^24.7.1",
"jest-environment-node": "26.6.2",
chancehudson marked this conversation as resolved.
Show resolved Hide resolved
"jest-junit": "^10.0.0",
"lerna": "^3.13.2",
"link-module-alias": "^1.2.0",
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"~coordinator": "../coordinator/dist"
},
"dependencies": {
"@ethereumjs/common": "2.4.0",
"@ethereumjs/tx": "3.2.0",
"@openzeppelin/contracts": "3.4.1",
"@zkopru/utils": "file:../utils",
Expand Down
59 changes: 50 additions & 9 deletions packages/contracts/src/tx-util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import BN from 'bn.js'
import Web3 from 'web3'
import Common from '@ethereumjs/common'
import { TransactionFactory } from '@ethereumjs/tx'
import { Account, TransactionReceipt } from 'web3-core'
import { logger } from '@zkopru/utils'
import {
Expand Down Expand Up @@ -43,20 +45,59 @@ export class TxUtil {
}

const value = option ? (option as PayableTx).value : undefined
const { rawTransaction } = await web3.eth.accounts.signTransaction(
{
gasPrice,
gas,

// this can be replace with `web3.eth.account.signTransaction`
// but, there are some benefits using @ethereumjs/tx for signing tx
// less dependency on web3 version and more accurate gas can be use it
let rawTransaction
const nonce = option?.nonce
? +option.nonce.toString()
: await web3.eth.getTransactionCount(account.address)
try {
const txParams = {
from: account.address,
to: address,
value,
gasPrice: `0x${new BN(gasPrice).toString('hex')}`,
gasLimit: `0x${new BN(gas).toString('hex')}`,
value: `0x${new BN(value ?? 0).toString('hex')}`,
data: tx.encodeABI(),
nonce: option?.nonce ? +option.nonce.toString() : undefined,
},
account.privateKey,
)
nonce,
}

const common = await this.getCommon(web3)
const unSignedTx = TransactionFactory.fromTxData(txParams, { common })
const signedTx = unSignedTx.sign(
Buffer.from(account.privateKey.substring(2), 'hex'),
)

if (signedTx.isSigned() && !signedTx.verifySignature()) {
throw new Error(`Invalid Signature`)
}

rawTransaction = `0x${signedTx.serialize().toString('hex')}`
} catch (error) {
logger.error(
`contracts/tx-utils - getting signed transaction failed : ${error}`,
)
}

return rawTransaction as string
chancehudson marked this conversation as resolved.
Show resolved Hide resolved
}

// depends on chainId, @ethereumjs supports mainnet and other testchain
static async getCommon(web3: Web3): Promise<Common> {
const chainId = await web3.eth.getChainId()
const knownChainID = [1, 3, 4, 42, 5] // base on enum Chain on @ethereumjs/common

let common: Common
if (knownChainID.includes(chainId)) {
common = new Common({ chain: chainId })
} else {
common = Common.custom({ chainId })
}
return common
}

static async sendTx<T>(
tx: TransactionObject<T>,
address: string,
Expand Down
Loading