[besu]链下签名合约交易
2023-03-22 本文已影响0人
张亚伦
目的:演示一下如何在链下完成交易报文的签名,包含合约部署以及方法调用。
流程:
- encodeABI
- 组装交易报文
- 签名交易
合约示例
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Storage
* @dev Store & retrieve value in a variable
* @custom:dev-run-script ./scripts/deploy_with_ethers.ts
*/
contract Storage {
string name;
uint256 number;
mapping(address=>mapping(uint8=>uint256)) balances;
event Store(address indexed sender,uint256 num);
event ChangeName(address indexed sender,string name_);
constructor (uint256 num,string memory name_){
number = num;
name = name_;
}
/**
* @dev Store value in variable
* @param num value to store
*/
function store(uint256 num) public {
number = num;
emit Store(msg.sender,num);
}
function changeName(string calldata name_) public {
name = name;
emit ChangeName(msg.sender,name_);
}
/**
* @dev Return value
* @return value of 'number'
*/
function retrieve() public view returns (uint256){
return number;
}
}
前端示例代码
安装类库:
npm install @ethereumjs/common
npm install @ethereumjs/tx
npm install web3
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://192.168.1.178:21001'))
const abi = [{ "inputs": [{ "internalType": "uint256", "name": "num", "type": "uint256" }, { "internalType": "string", "name": "name_", "type": "string" }], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "address", "name": "sender", "type": "address" }, { "indexed": false, "internalType": "string", "name": "name_", "type": "string" }], "name": "ChangeName", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "address", "name": "sender", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "num", "type": "uint256" }], "name": "Store", "type": "event" }, { "inputs": [{ "internalType": "string", "name": "name_", "type": "string" }], "name": "changeName", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "retrieve", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint256", "name": "num", "type": "uint256" }], "name": "store", "outputs": [], "stateMutability": "nonpayable", "type": "function" }];
const bin = "0x60806040523480156200001157600080fd5b5060405162000c7438038062000c74833981810160405281019062000037919062000226565b8160018190555080600090816200004f9190620004cd565b505050620005b4565b6000604051905090565b600080fd5b600080fd5b6000819050919050565b62000081816200006c565b81146200008d57600080fd5b50565b600081519050620000a18162000076565b92915050565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b620000fc82620000b1565b810181811067ffffffffffffffff821117156200011e576200011d620000c2565b5b80604052505050565b60006200013362000058565b9050620001418282620000f1565b919050565b600067ffffffffffffffff821115620001645762000163620000c2565b5b6200016f82620000b1565b9050602081019050919050565b60005b838110156200019c5780820151818401526020810190506200017f565b60008484015250505050565b6000620001bf620001b98462000146565b62000127565b905082815260208101848484011115620001de57620001dd620000ac565b5b620001eb8482856200017c565b509392505050565b600082601f8301126200020b576200020a620000a7565b5b81516200021d848260208601620001a8565b91505092915050565b6000806040838503121562000240576200023f62000062565b5b6000620002508582860162000090565b925050602083015167ffffffffffffffff81111562000274576200027362000067565b5b6200028285828601620001f3565b9150509250929050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680620002df57607f821691505b602082108103620002f557620002f462000297565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026200035f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000320565b6200036b868362000320565b95508019841693508086168417925050509392505050565b6000819050919050565b6000620003ae620003a8620003a2846200006c565b62000383565b6200006c565b9050919050565b6000819050919050565b620003ca836200038d565b620003e2620003d982620003b5565b8484546200032d565b825550505050565b600090565b620003f9620003ea565b62000406818484620003bf565b505050565b5b818110156200042e5762000422600082620003ef565b6001810190506200040c565b5050565b601f8211156200047d576200044781620002fb565b620004528462000310565b8101602085101562000462578190505b6200047a620004718562000310565b8301826200040b565b50505b505050565b600082821c905092915050565b6000620004a26000198460080262000482565b1980831691505092915050565b6000620004bd83836200048f565b9150826002028217905092915050565b620004d8826200028c565b67ffffffffffffffff811115620004f457620004f3620000c2565b5b620005008254620002c6565b6200050d82828562000432565b600060209050601f83116001811462000545576000841562000530578287015190505b6200053c8582620004af565b865550620005ac565b601f1984166200055586620002fb565b60005b828110156200057f5784890151825560018201915060208501945060208101905062000558565b868310156200059f57848901516200059b601f8916826200048f565b8355505b6001600288020188555050505b505050505050565b6106b080620005c46000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632e64cec1146100465780635353a2d8146100645780636057361d14610080575b600080fd5b61004e61009c565b60405161005b919061017a565b60405180910390f35b61007e60048036038101906100799190610204565b6100a6565b005b61009a6004803603810190610095919061027d565b610109565b005b6000600154905090565b600080816100b49190610510565b503373ffffffffffffffffffffffffffffffffffffffff167fbb65f02a08ca2759ab26ef9929bf648f890a58e115823859923e3a6d0c6dfca883836040516100fd929190610656565b60405180910390a25050565b806001819055503373ffffffffffffffffffffffffffffffffffffffff167fcc9d847ba10dcd14a97c82e34eddd0f93ea7a927fb7d6e00da2ca1b26c5823ae82604051610156919061017a565b60405180910390a250565b6000819050919050565b61017481610161565b82525050565b600060208201905061018f600083018461016b565b92915050565b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b60008083601f8401126101c4576101c361019f565b5b8235905067ffffffffffffffff8111156101e1576101e06101a4565b5b6020830191508360018202830111156101fd576101fc6101a9565b5b9250929050565b6000806020838503121561021b5761021a610195565b5b600083013567ffffffffffffffff8111156102395761023861019a565b5b610245858286016101ae565b92509250509250929050565b61025a81610161565b811461026557600080fd5b50565b60008135905061027781610251565b92915050565b60006020828403121561029357610292610195565b5b60006102a184828501610268565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806102f157607f821691505b602082108103610304576103036102aa565b5b50919050565b600081549050610319816102d9565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b60008190508160005260206000209050919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026103c67fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82610389565b6103d08683610389565b95508019841693508086168417925050509392505050565b6000819050919050565b600061040d61040861040384610161565b6103e8565b610161565b9050919050565b6000819050919050565b610427836103f2565b61043b61043382610414565b848454610396565b825550505050565b600090565b610450610443565b61045b81848461041e565b505050565b5b8181101561047f57610474600082610448565b600181019050610461565b5050565b601f8211156104c4576104958161034f565b61049e84610379565b810160208510156104ad578190505b6104c16104b985610379565b830182610460565b50505b505050565b600082821c905092915050565b60006104e7600019846008026104c9565b1980831691505092915050565b600061050083836104d6565b9150826002028217905092915050565b81810361051e5750506105f6565b6105278261030a565b67ffffffffffffffff8111156105405761053f610320565b5b61054a82546102d9565b610555828285610483565b6000601f8311600181146105845760008415610572578287015490505b61057c85826104f4565b8655506105ef565b601f19841661059287610364565b965061059d8661034f565b60005b828110156105c5578489015482556001820191506001850194506020810190506105a0565b868310156105e257848901546105de601f8916826104d6565b8355505b6001600288020188555050505b5050505050505b565b600082825260208201905092915050565b82818337600083830152505050565b6000601f19601f8301169050919050565b600061063583856105f8565b9350610642838584610609565b61064b83610618565b840190509392505050565b60006020820190508181036000830152610671818486610629565b9050939250505056fea2646970667358221220caa95a47217136435cb20cffd6623635245695e9931e07be3f6516877b7554c764736f6c63430008120033";
var privateKey = Buffer.from('94baea294eb3ca492b5372ef0fbe67b8bb16969356553678e122711abe113a47', 'hex');
var from = "0x0570d518FAFeb809F229C2A0417437a5E64B6E4a";
// 合约部署编码
async function deployEncodedCode() {
var myContract = new web3.eth.Contract(abi);
console.log(myContract.deploy({
data: bin,
arguments: [123, 'My String']
}).encodeABI());
//0x60806040523480156200001157600080fd5b5060405162000c7438038062000c74833981810160405281019062000037919062000226565b8160018190555080600090816200004f9190620004cd565b505050620005b4565b6000604051905090565b600080fd5b600080fd5b6000819050919050565b62000081816200006c565b81146200008d57600080fd5b50565b600081519050620000a18162000076565b92915050565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b620000fc82620000b1565b810181811067ffffffffffffffff821117156200011e576200011d620000c2565b5b80604052505050565b60006200013362000058565b9050620001418282620000f1565b919050565b600067ffffffffffffffff821115620001645762000163620000c2565b5b6200016f82620000b1565b9050602081019050919050565b60005b838110156200019c5780820151818401526020810190506200017f565b60008484015250505050565b6000620001bf620001b98462000146565b62000127565b905082815260208101848484011115620001de57620001dd620000ac565b5b620001eb8482856200017c565b509392505050565b600082601f8301126200020b576200020a620000a7565b5b81516200021d848260208601620001a8565b91505092915050565b6000806040838503121562000240576200023f62000062565b5b6000620002508582860162000090565b925050602083015167ffffffffffffffff81111562000274576200027362000067565b5b6200028285828601620001f3565b9150509250929050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680620002df57607f821691505b602082108103620002f557620002f462000297565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026200035f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000320565b6200036b868362000320565b95508019841693508086168417925050509392505050565b6000819050919050565b6000620003ae620003a8620003a2846200006c565b62000383565b6200006c565b9050919050565b6000819050919050565b620003ca836200038d565b620003e2620003d982620003b5565b8484546200032d565b825550505050565b600090565b620003f9620003ea565b62000406818484620003bf565b505050565b5b818110156200042e5762000422600082620003ef565b6001810190506200040c565b5050565b601f8211156200047d576200044781620002fb565b620004528462000310565b8101602085101562000462578190505b6200047a620004718562000310565b8301826200040b565b50505b505050565b600082821c905092915050565b6000620004a26000198460080262000482565b1980831691505092915050565b6000620004bd83836200048f565b9150826002028217905092915050565b620004d8826200028c565b67ffffffffffffffff811115620004f457620004f3620000c2565b5b620005008254620002c6565b6200050d82828562000432565b600060209050601f83116001811462000545576000841562000530578287015190505b6200053c8582620004af565b865550620005ac565b601f1984166200055586620002fb565b60005b828110156200057f5784890151825560018201915060208501945060208101905062000558565b868310156200059f57848901516200059b601f8916826200048f565b8355505b6001600288020188555050505b505050505050565b6106b080620005c46000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632e64cec1146100465780635353a2d8146100645780636057361d14610080575b600080fd5b61004e61009c565b60405161005b919061017a565b60405180910390f35b61007e60048036038101906100799190610204565b6100a6565b005b61009a6004803603810190610095919061027d565b610109565b005b6000600154905090565b600080816100b49190610510565b503373ffffffffffffffffffffffffffffffffffffffff167fbb65f02a08ca2759ab26ef9929bf648f890a58e115823859923e3a6d0c6dfca883836040516100fd929190610656565b60405180910390a25050565b806001819055503373ffffffffffffffffffffffffffffffffffffffff167fcc9d847ba10dcd14a97c82e34eddd0f93ea7a927fb7d6e00da2ca1b26c5823ae82604051610156919061017a565b60405180910390a250565b6000819050919050565b61017481610161565b82525050565b600060208201905061018f600083018461016b565b92915050565b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b60008083601f8401126101c4576101c361019f565b5b8235905067ffffffffffffffff8111156101e1576101e06101a4565b5b6020830191508360018202830111156101fd576101fc6101a9565b5b9250929050565b6000806020838503121561021b5761021a610195565b5b600083013567ffffffffffffffff8111156102395761023861019a565b5b610245858286016101ae565b92509250509250929050565b61025a81610161565b811461026557600080fd5b50565b60008135905061027781610251565b92915050565b60006020828403121561029357610292610195565b5b60006102a184828501610268565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806102f157607f821691505b602082108103610304576103036102aa565b5b50919050565b600081549050610319816102d9565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b60008190508160005260206000209050919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026103c67fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82610389565b6103d08683610389565b95508019841693508086168417925050509392505050565b6000819050919050565b600061040d61040861040384610161565b6103e8565b610161565b9050919050565b6000819050919050565b610427836103f2565b61043b61043382610414565b848454610396565b825550505050565b600090565b610450610443565b61045b81848461041e565b505050565b5b8181101561047f57610474600082610448565b600181019050610461565b5050565b601f8211156104c4576104958161034f565b61049e84610379565b810160208510156104ad578190505b6104c16104b985610379565b830182610460565b50505b505050565b600082821c905092915050565b60006104e7600019846008026104c9565b1980831691505092915050565b600061050083836104d6565b9150826002028217905092915050565b81810361051e5750506105f6565b6105278261030a565b67ffffffffffffffff8111156105405761053f610320565b5b61054a82546102d9565b610555828285610483565b6000601f8311600181146105845760008415610572578287015490505b61057c85826104f4565b8655506105ef565b601f19841661059287610364565b965061059d8661034f565b60005b828110156105c5578489015482556001820191506001850194506020810190506105a0565b868310156105e257848901546105de601f8916826104d6565b8355505b6001600288020188555050505b5050505050505b565b600082825260208201905092915050565b82818337600083830152505050565b6000601f19601f8301169050919050565b600061063583856105f8565b9350610642838584610609565b61064b83610618565b840190509392505050565b60006020820190508181036000830152610671818486610629565b9050939250505056fea2646970667358221220caa95a47217136435cb20cffd6623635245695e9931e07be3f6516877b7554c764736f6c63430008120033000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000094d7920537472696e670000000000000000000000000000000000000000000000
}
// 方法调用编码
async function methodEncodedCode() {
var myContract = new web3.eth.Contract(abi);
console.log("store(123): ", myContract.methods.store(123).encodeABI());
console.log("changeName('zxl'): ", myContract.methods.changeName('zxl').encodeABI());
console.log("retrieve(): ", myContract.methods.retrieve().encodeABI());
// store(123): 0x6057361d000000000000000000000000000000000000000000000000000000000000007b
// changeName('zxl'): 0x5353a2d8000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000037a786c0000000000000000000000000000000000000000000000000000000000
// retrieve(): 0x2e64cec1
}
// 组装交易并签名
async function signTx() {
var Transaction = require('@ethereumjs/tx').Transaction;
const { Common, Chain, Hardfork } = require('@ethereumjs/common');
const common = Common.custom({ chainId: 1337, networkId: 1337, hardfork: Hardfork.London });
var rawTx = {
nonce: web3.utils.toHex(1),
gasPrice: '0x0',
gasLimit: '0x394611',
// to: '0x0',
value: '0x0',
data: '0x60806040523480156200001157600080fd5b5060405162000c7438038062000c74833981810160405281019062000037919062000226565b8160018190555080600090816200004f9190620004cd565b505050620005b4565b6000604051905090565b600080fd5b600080fd5b6000819050919050565b62000081816200006c565b81146200008d57600080fd5b50565b600081519050620000a18162000076565b92915050565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b620000fc82620000b1565b810181811067ffffffffffffffff821117156200011e576200011d620000c2565b5b80604052505050565b60006200013362000058565b9050620001418282620000f1565b919050565b600067ffffffffffffffff821115620001645762000163620000c2565b5b6200016f82620000b1565b9050602081019050919050565b60005b838110156200019c5780820151818401526020810190506200017f565b60008484015250505050565b6000620001bf620001b98462000146565b62000127565b905082815260208101848484011115620001de57620001dd620000ac565b5b620001eb8482856200017c565b509392505050565b600082601f8301126200020b576200020a620000a7565b5b81516200021d848260208601620001a8565b91505092915050565b6000806040838503121562000240576200023f62000062565b5b6000620002508582860162000090565b925050602083015167ffffffffffffffff81111562000274576200027362000067565b5b6200028285828601620001f3565b9150509250929050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680620002df57607f821691505b602082108103620002f557620002f462000297565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026200035f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000320565b6200036b868362000320565b95508019841693508086168417925050509392505050565b6000819050919050565b6000620003ae620003a8620003a2846200006c565b62000383565b6200006c565b9050919050565b6000819050919050565b620003ca836200038d565b620003e2620003d982620003b5565b8484546200032d565b825550505050565b600090565b620003f9620003ea565b62000406818484620003bf565b505050565b5b818110156200042e5762000422600082620003ef565b6001810190506200040c565b5050565b601f8211156200047d576200044781620002fb565b620004528462000310565b8101602085101562000462578190505b6200047a620004718562000310565b8301826200040b565b50505b505050565b600082821c905092915050565b6000620004a26000198460080262000482565b1980831691505092915050565b6000620004bd83836200048f565b9150826002028217905092915050565b620004d8826200028c565b67ffffffffffffffff811115620004f457620004f3620000c2565b5b620005008254620002c6565b6200050d82828562000432565b600060209050601f83116001811462000545576000841562000530578287015190505b6200053c8582620004af565b865550620005ac565b601f1984166200055586620002fb565b60005b828110156200057f5784890151825560018201915060208501945060208101905062000558565b868310156200059f57848901516200059b601f8916826200048f565b8355505b6001600288020188555050505b505050505050565b6106b080620005c46000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632e64cec1146100465780635353a2d8146100645780636057361d14610080575b600080fd5b61004e61009c565b60405161005b919061017a565b60405180910390f35b61007e60048036038101906100799190610204565b6100a6565b005b61009a6004803603810190610095919061027d565b610109565b005b6000600154905090565b600080816100b49190610510565b503373ffffffffffffffffffffffffffffffffffffffff167fbb65f02a08ca2759ab26ef9929bf648f890a58e115823859923e3a6d0c6dfca883836040516100fd929190610656565b60405180910390a25050565b806001819055503373ffffffffffffffffffffffffffffffffffffffff167fcc9d847ba10dcd14a97c82e34eddd0f93ea7a927fb7d6e00da2ca1b26c5823ae82604051610156919061017a565b60405180910390a250565b6000819050919050565b61017481610161565b82525050565b600060208201905061018f600083018461016b565b92915050565b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b60008083601f8401126101c4576101c361019f565b5b8235905067ffffffffffffffff8111156101e1576101e06101a4565b5b6020830191508360018202830111156101fd576101fc6101a9565b5b9250929050565b6000806020838503121561021b5761021a610195565b5b600083013567ffffffffffffffff8111156102395761023861019a565b5b610245858286016101ae565b92509250509250929050565b61025a81610161565b811461026557600080fd5b50565b60008135905061027781610251565b92915050565b60006020828403121561029357610292610195565b5b60006102a184828501610268565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806102f157607f821691505b602082108103610304576103036102aa565b5b50919050565b600081549050610319816102d9565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b60008190508160005260206000209050919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026103c67fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82610389565b6103d08683610389565b95508019841693508086168417925050509392505050565b6000819050919050565b600061040d61040861040384610161565b6103e8565b610161565b9050919050565b6000819050919050565b610427836103f2565b61043b61043382610414565b848454610396565b825550505050565b600090565b610450610443565b61045b81848461041e565b505050565b5b8181101561047f57610474600082610448565b600181019050610461565b5050565b601f8211156104c4576104958161034f565b61049e84610379565b810160208510156104ad578190505b6104c16104b985610379565b830182610460565b50505b505050565b600082821c905092915050565b60006104e7600019846008026104c9565b1980831691505092915050565b600061050083836104d6565b9150826002028217905092915050565b81810361051e5750506105f6565b6105278261030a565b67ffffffffffffffff8111156105405761053f610320565b5b61054a82546102d9565b610555828285610483565b6000601f8311600181146105845760008415610572578287015490505b61057c85826104f4565b8655506105ef565b601f19841661059287610364565b965061059d8661034f565b60005b828110156105c5578489015482556001820191506001850194506020810190506105a0565b868310156105e257848901546105de601f8916826104d6565b8355505b6001600288020188555050505b5050505050505b565b600082825260208201905092915050565b82818337600083830152505050565b6000601f19601f8301169050919050565b600061063583856105f8565b9350610642838584610609565b61064b83610618565b840190509392505050565b60006020820190508181036000830152610671818486610629565b9050939250505056fea2646970667358221220caa95a47217136435cb20cffd6623635245695e9931e07be3f6516877b7554c764736f6c63430008120033000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000094d7920537472696e670000000000000000000000000000000000000000000000'
}
const tx = Transaction.fromTxData(rawTx, { common })
const signedTx = tx.sign(privateKey)
const serializedTx = '0x' + signedTx.serialize().toString('hex');
console.log(serializedTx);
}
// 签名交易并发送
async function _sendTx(rawTx) {
var Transaction = require('@ethereumjs/tx').Transaction;
const { Common, Chain, Hardfork } = require('@ethereumjs/common');
const common = Common.custom({ chainId: 1337, networkId: 1337, hardfork: Hardfork.London });
const tx = Transaction.fromTxData(rawTx, { common })
const signedTx = tx.sign(privateKey)
const serializedTx = '0x' + signedTx.serialize().toString('hex');
console.log(serializedTx);
web3.eth.sendSignedTransaction(serializedTx).on('receipt', console.log);
}
// 合约部署
async function deploy() {
web3.eth.getTransactionCount(from).then(function (_nonce) {
console.log(`number: ${_nonce}`);
var rawTx = {
nonce: web3.utils.toHex(_nonce),
gasPrice: '0x0',
gasLimit: '0x394611',
// to: '0x0',
value: '0x0',
data: '0x60806040523480156200001157600080fd5b5060405162000c7438038062000c74833981810160405281019062000037919062000226565b8160018190555080600090816200004f9190620004cd565b505050620005b4565b6000604051905090565b600080fd5b600080fd5b6000819050919050565b62000081816200006c565b81146200008d57600080fd5b50565b600081519050620000a18162000076565b92915050565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b620000fc82620000b1565b810181811067ffffffffffffffff821117156200011e576200011d620000c2565b5b80604052505050565b60006200013362000058565b9050620001418282620000f1565b919050565b600067ffffffffffffffff821115620001645762000163620000c2565b5b6200016f82620000b1565b9050602081019050919050565b60005b838110156200019c5780820151818401526020810190506200017f565b60008484015250505050565b6000620001bf620001b98462000146565b62000127565b905082815260208101848484011115620001de57620001dd620000ac565b5b620001eb8482856200017c565b509392505050565b600082601f8301126200020b576200020a620000a7565b5b81516200021d848260208601620001a8565b91505092915050565b6000806040838503121562000240576200023f62000062565b5b6000620002508582860162000090565b925050602083015167ffffffffffffffff81111562000274576200027362000067565b5b6200028285828601620001f3565b9150509250929050565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680620002df57607f821691505b602082108103620002f557620002f462000297565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026200035f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000320565b6200036b868362000320565b95508019841693508086168417925050509392505050565b6000819050919050565b6000620003ae620003a8620003a2846200006c565b62000383565b6200006c565b9050919050565b6000819050919050565b620003ca836200038d565b620003e2620003d982620003b5565b8484546200032d565b825550505050565b600090565b620003f9620003ea565b62000406818484620003bf565b505050565b5b818110156200042e5762000422600082620003ef565b6001810190506200040c565b5050565b601f8211156200047d576200044781620002fb565b620004528462000310565b8101602085101562000462578190505b6200047a620004718562000310565b8301826200040b565b50505b505050565b600082821c905092915050565b6000620004a26000198460080262000482565b1980831691505092915050565b6000620004bd83836200048f565b9150826002028217905092915050565b620004d8826200028c565b67ffffffffffffffff811115620004f457620004f3620000c2565b5b620005008254620002c6565b6200050d82828562000432565b600060209050601f83116001811462000545576000841562000530578287015190505b6200053c8582620004af565b865550620005ac565b601f1984166200055586620002fb565b60005b828110156200057f5784890151825560018201915060208501945060208101905062000558565b868310156200059f57848901516200059b601f8916826200048f565b8355505b6001600288020188555050505b505050505050565b6106b080620005c46000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80632e64cec1146100465780635353a2d8146100645780636057361d14610080575b600080fd5b61004e61009c565b60405161005b919061017a565b60405180910390f35b61007e60048036038101906100799190610204565b6100a6565b005b61009a6004803603810190610095919061027d565b610109565b005b6000600154905090565b600080816100b49190610510565b503373ffffffffffffffffffffffffffffffffffffffff167fbb65f02a08ca2759ab26ef9929bf648f890a58e115823859923e3a6d0c6dfca883836040516100fd929190610656565b60405180910390a25050565b806001819055503373ffffffffffffffffffffffffffffffffffffffff167fcc9d847ba10dcd14a97c82e34eddd0f93ea7a927fb7d6e00da2ca1b26c5823ae82604051610156919061017a565b60405180910390a250565b6000819050919050565b61017481610161565b82525050565b600060208201905061018f600083018461016b565b92915050565b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b60008083601f8401126101c4576101c361019f565b5b8235905067ffffffffffffffff8111156101e1576101e06101a4565b5b6020830191508360018202830111156101fd576101fc6101a9565b5b9250929050565b6000806020838503121561021b5761021a610195565b5b600083013567ffffffffffffffff8111156102395761023861019a565b5b610245858286016101ae565b92509250509250929050565b61025a81610161565b811461026557600080fd5b50565b60008135905061027781610251565b92915050565b60006020828403121561029357610292610195565b5b60006102a184828501610268565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806102f157607f821691505b602082108103610304576103036102aa565b5b50919050565b600081549050610319816102d9565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b60008190508160005260206000209050919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026103c67fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82610389565b6103d08683610389565b95508019841693508086168417925050509392505050565b6000819050919050565b600061040d61040861040384610161565b6103e8565b610161565b9050919050565b6000819050919050565b610427836103f2565b61043b61043382610414565b848454610396565b825550505050565b600090565b610450610443565b61045b81848461041e565b505050565b5b8181101561047f57610474600082610448565b600181019050610461565b5050565b601f8211156104c4576104958161034f565b61049e84610379565b810160208510156104ad578190505b6104c16104b985610379565b830182610460565b50505b505050565b600082821c905092915050565b60006104e7600019846008026104c9565b1980831691505092915050565b600061050083836104d6565b9150826002028217905092915050565b81810361051e5750506105f6565b6105278261030a565b67ffffffffffffffff8111156105405761053f610320565b5b61054a82546102d9565b610555828285610483565b6000601f8311600181146105845760008415610572578287015490505b61057c85826104f4565b8655506105ef565b601f19841661059287610364565b965061059d8661034f565b60005b828110156105c5578489015482556001820191506001850194506020810190506105a0565b868310156105e257848901546105de601f8916826104d6565b8355505b6001600288020188555050505b5050505050505b565b600082825260208201905092915050565b82818337600083830152505050565b6000601f19601f8301169050919050565b600061063583856105f8565b9350610642838584610609565b61064b83610618565b840190509392505050565b60006020820190508181036000830152610671818486610629565b9050939250505056fea2646970667358221220caa95a47217136435cb20cffd6623635245695e9931e07be3f6516877b7554c764736f6c63430008120033000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000094d7920537472696e670000000000000000000000000000000000000000000000'
}
_sendTx(rawTx);
});
}
// 合约调用方法:store(123)
async function store() {
web3.eth.getTransactionCount(from).then(function (_nonce) {
console.log(`number: ${_nonce}`);
var rawTx = {
nonce: web3.utils.toHex(_nonce),
gasPrice: '0x0',
gasLimit: '0x60573',
to: '0xD83344B87e6C77F1Ac70caeA8Dea837c4C94469d',
value: '0x0',
data: '0x6057361d000000000000000000000000000000000000000000000000000000000000007b'
}
_sendTx(rawTx);
});
}
// 合约调用方法:retrieve()
async function retrieve() {
web3.eth.call({
to: "0xD83344B87e6C77F1Ac70caeA8Dea837c4C94469d", // contract address
data: "0x2e64cec1"
})
.then(console.log);
}
async function getChainId() {
web3.eth.getChainId().then(console.log);
}
async function main() {
// 成功
// deployEncodedCode();
// 成功
// methodEncodedCode();
// 成功
// signTx();
// 成功
// getChainId();
// 成功
// deploy();
// 成功
// store();
//成功
// retrieve();
}
if (require.main === module) {
main();
}
服务端解析
image.png验证交易的有效性