Web3-Js: ethereumjs-tx发送签名交易

  • 小明
  • 更新于 2022-12-24 19:40
  • 阅读 1757

Web3-Js: ethereumjs-tx发送签名交易

以太坊发送签名交易 ethereumjs-tx推荐版本1.3.7 直接上代码:

const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const web3 = new Web3(new Web3.providers.WebsocketProvider("ws://localhost:8546") ) ;     
var _from = "签名以太坊账户";
var privateKey1 = Buffer.from('对应私钥','hex');
web3.eth.getTransactionCount(_from,(err,txcount)=>{
     var txObject ={
        nonce: web3.utils.toHex(txcount),
        gasPrice: web3.utils.toHex(web3.utils.toWei('10','gwei')),
        gasLimit: web3.utils.toHex(21000),
        to: '对方账户',
        value:web3.utils.toHex(web3.utils.toWei('1','ether')),
}
var tx = new Tx(txObject);
tx.sign(privateKey1);
var serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
    if (!err){
        console.log(hash);
    }else{
        console.log(err);
    }
});

上面是单纯的转账交易,下面是把调用合约函数打包成交易发送出去 无论哪一个 ethereumjs-tx版本是重点!!!不然会报错

const Web3 = require('web3');
const EthereumTx = require('ethereumjs-tx');//因为ethereum-tx2.0版本实例化需要带未知参数,目前私有链好像行不通,所以npm install ethereum-tx@1.3.7
const web3 = new Web3(new Web3.providers.WebsocketProvider("ws://localhost:8546") ) ;   

//智能合约地址
const registryAddress = "0xb77065c8adf97e94ca2924c602ac7c3fd92ecdaa"
//智能合约对应Abi文件
var contractAbi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"},{"name":"tokenName","type":"string"},{"name":"tokenSymbol","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"}];

//私钥转换为Buffer
const privateKey =  Buffer.from('你的私钥',"hex")//推荐使用cmd set命令然后env.process导出来
//私钥转换为账号                
const account = web3.eth.accounts.privateKeyToAccount("0x"+"你的私钥");
//私钥对应的账号地地址
const address = account.address
console.log("address: ",address)

//获取合约实例
var myContract = new web3.eth.Contract(contractAbi,registryAddress)

//获取nonce,使用本地私钥发送交易
web3.eth.getTransactionCount(address).then(
    nonce => {
        console.log("nonce: ",nonce)
        const txParams = {
            nonce: nonce,
            gasLimit: '0x271000',
            to: registryAddress,
            data: myContract.methods.transfer('0x198b2feE780F944F4b9D80e87C59AAe5ee8460bd',1000).encodeABI(), //ERC20转账

          }
          const tx = new EthereumTx(txParams)
        tx.sign(privateKey)
        const serializedTx = tx.serialize()
        web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);

    },
    e => console.log(e)
)
点赞 1
收藏 2
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
小明
小明
江湖只有他的大名,没有他的介绍。