web3.js增加eth.getRawTransactionByHash(txhash)方法步骤

eth_getRawTransactionByHash

https://ethereum.stackexchange.com/questions/7473/get-raw-transaction-from-hash

 

There is an "undocumented" method eth_getRawTransactionByHash from JSON-RPC

curl -H "Content-Type: application/json" -X POST --data \
'{"jsonrpc":"2.0","method":"eth_getRawTransactionByHash","params":[""],"id":1}' http://localhost:8545

- transaction id

1.项目node_modules 下找到web3  下types.d.ts文件

 

Eth

   类中增加方法

getRawTransaction(hash: string, cb?: Callback<TransactionRaw>): Promise<TransactionRaw>

 

增加TransactionRaw定义

 

export declare interface TransactionRaw {

raw: string

}

 

2.项目node_modules 下找到 web3-eth中   index.js

 

methods={}

增加方法

 

new Method({

name: 'getRawTransaction',

call: 'eth_getRawTransactionByHash',

params: 1,

inputFormatter: [null],

outputFormatter: formatter.outputTransactionRawFormatter

}),

 

3.项目node_modules 下找到 web3-core-helpers中  formatters.js

增加 outputTransactionRawFormatter并module.exports中也增加对应

 

/**

* Formats the output of a transaction raw value

*

* @method outputTransactionRawFormatter

* @param {Object} tx

* @returns {Object}

*/

var outputTransactionRawFormatter = function (tx){

return tx;

};

module.exports = {

inputDefaultBlockNumberFormatter: inputDefaultBlockNumberFormatter,

inputBlockNumberFormatter: inputBlockNumberFormatter,

inputCallFormatter: inputCallFormatter,

inputTransactionFormatter: inputTransactionFormatter,

inputAddressFormatter: inputAddressFormatter,

inputPostFormatter: inputPostFormatter,

inputLogFormatter: inputLogFormatter,

inputSignFormatter: inputSignFormatter,

outputBigNumberFormatter: outputBigNumberFormatter,

outputTransactionFormatter: outputTransactionFormatter,

outputTransactionRawFormatter: outputTransactionRawFormatter,

outputTransactionReceiptFormatter: outputTransactionReceiptFormatter,

outputBlockFormatter: outputBlockFormatter,

outputLogFormatter: outputLogFormatter,

outputPostFormatter: outputPostFormatter,

outputSyncingFormatter: outputSyncingFormatter

};

 

 

 

备注:以上代码是1.0.版本的

一下是0.2

 

/node_modules/web3/lib/web3/methods/eth.js

 

var getRawTransaction = new Method({

name: 'getRawTransaction',

call: 'eth_getRawTransactionByHash',

params: 1,

outputFormatter: formatters.outputTransactionRawFormatter

});

return [

getBalance,

……………………………………………………………………………………………………

getWork,

getRawTransaction

];

 

 

 

node_modules/web3/lib/web3/formatters.js

 

/**

* Formats the output of a transaction raw value

*

* @method outputTransactionRawFormatter

* @param {Object} tx

* @returns {Object}

*/

var outputTransactionRawFormatter = function (tx){

return tx;

};

module.exports = {

inputDefaultBlockNumberFormatter: inputDefaultBlockNumberFormatter,

…………………………………………………………………………………………………………………………………………

outputSyncingFormatter: outputSyncingFormatter,

outputTransactionRawFormatter:outputTransactionRawFormatter

};

 

备注:可以直接用partiy搭建节点,内部web3直接返回raw

web3.eth.getTransaction('0xc0c1984a44bbc8f1933d732920b8b549636147d4e45c42787ca53994e5986d63');
{
     blockHash: "0x1e633c17d7e51c2e5f42c5087646d1e9c3635b48d127c9a0f7bcbd177704ba82",
     blockNumber: 6025314,
     chainId: "0x1",
     condition: null,
     creates: null,
     from: "0x0d4ac6b66d6d9c204069c304da3ebbaf83eaea80",
     gas: 38001,
     gasPrice: 1100000000,
     hash: "0xc0c1984a44bbc8f1933d732920b8b549636147d4e45c42787ca53994e5986d63",
     input: "0xa9059cbb000000000000000000000000347072fcd71a9e251d18a292d9bbc8bc3149cd8a0000000000000000000000000000000000000000000000008ac7230489e80000",
     nonce: 7,
    publicKey: "0x481e82fd238cfbe00c15e4b87cd05c0d2b41a322a149a0a8ca128a8563b44c356ece44cfa987a75daf46ca7263623e22dcf0689c6ebc0aca7d460e493d0e1f3c",
     r: "0x6be79f8bf82c9197bacc0c06c095a8bd0d97d0d9e69d55c10e8a01bf8475c8e5",
raw: "0xf8a807844190ab0082947194aa0bb10cec1fa372eb3abc17c933fc6ba863dd9e80b844a9059cbb000000000000000000000000347072fcd71a9e251d18a292d9bbc8bc3149cd8a0000000000000000000000000000000000000000000000008ac7230489e8000026a06be79f8bf82c9197bacc0c06c095a8bd0d97d0d9e69d55c10e8a01bf8475c8e5a008276ff7a917842c632bcdb59c8a301e3ee3b6bce0aee1a7562a5c90ab9e9dfe",
     s: "0x8276ff7a917842c632bcdb59c8a301e3ee3b6bce0aee1a7562a5c90ab9e9dfe",
     standardV: "0x1",
     to: "0xaa0bb10cec1fa372eb3abc17c933fc6ba863dd9e",
     transactionIndex: 200,
     v: "0x26",
     value: 0
}

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(区块链)