首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >以太坊·将自定义数据写入到区块链中

以太坊·将自定义数据写入到区块链中

原创
作者头像
netkiller old
发布2018-02-18 14:11:24
2.2K0
发布2018-02-18 14:11:24
举报
文章被收录于专栏:NetkillerNetkiller

本文节选自电子书《Netkiller Blockchain 手札》

Netkiller Blockchain 手札

本文作者最近在找工作,有意向致电 13113668890

Mr. Neo Chan, 陈景峯(BG7NYT)

中国广东省深圳市龙华新区民治街道溪山美地 518131 +86 13113668890 <netkiller@msn.com>

文档始创于2018-02-10

版权 © 2018 Netkiller(Neo Chan). All rights reserved.

版权声明

转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。

http://www.netkiller.cnhttp://netkiller.github.iohttp://netkiller.sourceforge.net

http://www.netkiller.cn

http://netkiller.github.io

http://netkiller.sourceforge.net

微信订阅号 netkiller-ebook (微信扫描二维码)QQ:13721218 请注明“读者”QQ群:128659835 请注明“读者”

微信订阅号 netkiller-ebook (微信扫描二维码)

QQ:13721218 请注明“读者”

QQ群:128659835 请注明“读者”

http://www.netkiller.cn

http://netkiller.github.io

http://netkiller.sourceforge.net

微信订阅号 netkiller-ebook (微信扫描二维码)

QQ:13721218 请注明“读者”

QQ群:128659835 请注明“读者”

5.5. 实用例子

5.5.1. 数据写入到区块链中

做一笔交易,并写入数据到区块链中

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 Test!!!"
};
let str = JSON.stringify(log);
console.log(str);
let data = Buffer.from(str).toString('hex');
data = '0x'+data;
console.log(data);

//将数据写入到交易中
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);
});

运行上面程序,会产生一个交易,记下hash值,然后启动挖矿。

> miner.start();
null
# 过一段时间后停止
> miner.stop();
true

然后查看这比交易

let Web3 = require("web3");
let fs = require("fs");
let web3 = new Web3("http://localhost:8545");
let address ="0xb15681eb4bdb6b9670d305fb341ebbc95d45c2ede0ea5034ef432b74f30b1b4f";
//从交易地址获取数据
web3.eth.getTransaction(address).then(console.log);

web3.eth.getTransaction(address,function(error, result){
    //console.log(result);
    inputData = result.input;
    res_str = Buffer.from(inputData.replace('0x',''),'hex').toString();
    res_json = JSON.parse(res_str);
    console.log(res_json);
});

结果输出

{ 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 Test!!!' } 就是保存在区块链中的数据。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Netkiller Blockchain 手札
    • Mr. Neo Chan, 陈景峯(BG7NYT)
      • 5.5. 实用例子
        • 5.5.1. 数据写入到区块链中
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档