Created
April 15, 2021 09:15
-
-
Save siandreev/7e28f0bf05ff9fc8a243aa82871e74cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import util from 'ethereumjs-util'; | |
import Tx from 'ethereumjs-tx'; | |
const toHex = number => '0x' + number.toString(16); | |
const privateKey = '0x4e568ee4ade234cec266d158c3f5ab23985058a4a9157d84f802ee23c3af0c89'; | |
console.log('Private key ', privateKey); | |
const publicKeyBuffer = util.privateToPublic(util.toBuffer(privateKey)); | |
const publicKey = util.bufferToHex(publicKeyBuffer); | |
console.log('Public key ', publicKey); | |
const address = '0x' + util.bufferToHex(util.keccak256(publicKeyBuffer)).slice(26); | |
console.log('Address ', address); | |
const rawTx = { | |
nonce: toHex(0), | |
gasPrice: toHex(20000000000), | |
gasLimit: toHex(100000), | |
to: '0xecA0A3eFCf009519052Dc92306fE821b9c7A32A2', | |
value: toHex(1000), | |
data: util.bufferToHex(Buffer.from('Hello world!', 'utf8')) | |
}; | |
const p = Buffer.from(privateKey.slice(2), 'hex'); | |
const transaction = new Tx.Transaction(rawTx, { chain: 'kovan', hardfork: 'petersburg' }); | |
console.log('Raw transaction ', util.bufferToHex(transaction.serialize())); | |
transaction.sign(p); | |
const serializedTx = transaction.serialize() | |
console.log('Signed transaction ', util.bufferToHex(serializedTx)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment