Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to write Custom data to Block chain in Ethernet Square

Shulou Source: shulou.com Published: 2022-06-01 07:57:51 09月26日 Update

Editor to share with you how to write custom data into the block chain, I believe that most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it!

Practical example 5.5.1. Data is written to the block chain

Make a transaction and write data to the block chain

Let Web3 = require ("web3"); let fs = require ("fs"); let web3 = new Web3 ("http://localhost:8545");let log = {time: (new Date). GetTime (), type:" info ", msg:" Web3 testworthy activities! "}; let str = JSON.stringify (log); console.log (str); let data = Buffer.from (str) .toString ('hex'); data =' 0x examples / / write data to the transaction let coinbase = "0x5c18a33df2cc41a1beddc91133b8422e89f041b7" console.log (coinbase) let user1 = "0xc2b9e316f246d35052118e51b55c75bfe99d247e"; web3.eth.personal.unlockAccount (coinbase, "chen1980"); let address = web3.eth.sendTransaction ({from:coinbase, to:user1, value:'0x00', data:data}, function (error, hash) {console.log (hash);})

Running the above program will generate a transaction, write down the hash value, and then start mining.

> miner.start (); null# stops after a period of time > miner.stop (); true

And then check that this is better than the deal.

Let Web3 = require ("web3"); let fs = require ("fs"); let web3 = new Web3 ("http://localhost:8545");let address =" 0xb15681eb4bdb6b9670d305fb341ebbc95d45c2ede0ea5034ef432b74f30b1b4f "; / / get data web3.eth.getTransaction (address) .then (console.log) from the transaction address; web3.eth.getTransaction (address,function (error, result) {/ / console.log (result); inputData = result.input) Res_str = Buffer.from (inputData.replace ('0x records'), 'hex') .toString (); res_json = JSON.parse (res_str); console.log (res_json);})

Result output

{blockHash: '0x78dacc2af60900d2e4cae90b71e27446e6e883df36c53f21cbc9e071f7a586f4', blockNumber: 1258, from: '0x5c18a33DF2cc41a1bedDC91133b8422e89f041B7', gas: 90000, gasPrice: '18000000000', hash: '0xb15681eb4bdb6b9670d305fb341ebbc95d45c2ede0ea5034ef432b74f30b1b4f', input: '0x7b2274696d65223a313531383933313435323537372c2274797065223a22696e666f222c226d7367223a22576562332054657374212121227d', nonce: 4, to: '0xc2b9e316F246d35052118E51B55C75BfE99d247e', transactionIndex: 0, value: '0', v: '0x41', r: '0x7fcd86c7fd975a0e98bd0e61a99da950b0155cd6c4581fefa4defbdcd404a930', s: '0x16f14ce1fbfadb9d59f343f8ac235cdd73dcedec5db1025ef91206b8bb17a827' }{ time: 1518931452577, type: 'info', msg: 'Web3 Test!!!'}

{time: 1518931452577, type: 'info', msg:' Web3 testworthy entries'} is the data stored in the chunk chain.

These are all the contents of the article "how to write custom data to the block chain". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Data blocks transactions articles ethernet content practical not so much examples addresses most that is time more knowledge procedures results industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei Redmi Xiaomi Shulou Technology