Skip to content

Instantly share code, notes, and snippets.

@ZhenQian-keystone
Last active June 12, 2024 04:03
Show Gist options
  • Save ZhenQian-keystone/c8de717bd04132bf2d6afd0517c78326 to your computer and use it in GitHub Desktop.
Save ZhenQian-keystone/c8de717bd04132bf2d6afd0517c78326 to your computer and use it in GitHub Desktop.
Implementing EIP-155 using Ethers.js v5.7
/// eip155 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
/// use ethers v5.7
const ethers = require('ethers')
const CHAIN_ID = 1
async function main() {
// private key
let private_key =
'0x4646464646464646464646464646464646464646464646464646464646464646'
let wallet = new ethers.Wallet(private_key)
console.log('wallet address : ', wallet.address)
// nonce = 9, gasprice = 20 * 10**9, startgas = 21000, to = 0x3535353535353535353535353535353535353535, value = 10**18, data=''
let sign_data = {
nonce: 9,
gasPrice: 20 * 10 ** 9,
gasLimit: 21000,
to: '0x3535353535353535353535353535353535353535',
value: ethers.utils.parseEther('1.0'),
data: '',
chainId: CHAIN_ID,
}
// sign the transaction
// 0xec098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a764000080018080
let unsigned_tx = ethers.utils.serializeTransaction(sign_data)
console.log('unsigned_tx : ', unsigned_tx)
// NOTICE: this unsigned_tx_hash will send to keystone for signing
// 0xdaf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53
let unsigned_tx_hash = ethers.utils.keccak256(unsigned_tx)
console.log('unsigned_tx_hash : ', unsigned_tx_hash)
// simulate keystone sign
const signingKey = new ethers.utils.SigningKey(private_key)
const keystone_sigature = signingKey.signDigest(unsigned_tx_hash)
const keystone_signature_string =
ethers.utils.joinSignature(keystone_sigature)
console.log('signature : ', keystone_signature_string)
// split signature to r, s, v
const signature = ethers.utils.splitSignature(keystone_signature_string)
let r = signature.r
let s = signature.s
let v = signature.v
console.log('r : ', r)
console.log('s : ', s)
console.log('v : ', v)
const r_number = ethers.BigNumber.from(r)
const r_string = r_number.toString()
// 18515461264373351373200002665853028612451056578545711640558177340181847433846
console.log('r_string : ', r_string)
const s_number = ethers.BigNumber.from(s)
const s_string = s_number.toString()
// 46948507304638947509940763649030358759909902576025900602547168820602576006531
console.log('s_string : ', s_string)
const v_number = ethers.BigNumber.from(v.toString())
const v_string = v_number.toString()
console.log('v_string : ', v_string)
// serialize the signed transaction
// 0xf86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83
const signed_tx = ethers.utils.serializeTransaction(sign_data, {
r: r,
s: s,
v: v,
})
console.log('signed_tx : ', signed_tx)
// send the transaction
try {
const public_eth_rpc = 'https://rpc.ankr.com/eth'
const provider = new ethers.providers.JsonRpcProvider(public_eth_rpc)
const txResult = await provider.sendTransaction(signed_tx)
return txResult.hash
} catch (e) {
console.log(e)
}
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment