1. compile ethereum
2. create network
./build/bin/geth --datadir data --networkid 20200107 --rpc --rpccorsdomain "*" init ./gochain/genesis.json
2.1
gensis.json
{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"ethash": {}
},
"difficulty": "1",
"gasLimit": "8000000",
"alloc": {
"b81d842a4395c34fc2ecd358015a4e7572b7a6ea": { "balance": "300000" },
"ba733e1d085933ae697c83cc4ae0cfe56c8c74e3": { "balance": "400000" }
}
}
3. start up network
../build/bin/geth --identity "myblockchain" --http --rpcaddr 127.0.0.1 --rpccorsdomain "*" --datadir data --rpcport 8546 --rpcapi "db,eth,net,personal,web3" --allow-insecure-unlock --networkid 20200107 console
4.
// in node.js
var Web3 = require('web3');
var web3 = new Web3(Web3.givenProvider || "http://localhost:8546");
web3.eth.getAccounts(console.log);
result:
null [ '0x41Bb6987c6C34813D6b00e331AC015c8027D69Bb', '0x792C9525699025dF598C00D5fD0B820315469171' ]
5.
// in node.js
var Web3 = require('web3');
let fs = require("fs");
var web3 = new Web3(Web3.givenProvider || "http://localhost:8546");
//web3.eth.getAccounts(console.log);
web3.eth.getAccounts().then(console.log);
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 = "0x41bb6987c6c34813d6b00e331ac015c8027d69bb"
console.log(coinbase)
let user1 = "0x792c9525699025df598c00d5fd0b820315469171";
web3.eth.personal.unlockAccount(coinbase, "123456").then(console.log);
let address = web3.eth.sendTransaction({
from:coinbase,
to:user1,
value:'0x00',
data:data
},function(error, hash){
console.log(hash);
});
6. get write info
var Web3 = require("web3");
let fs = require("fs");
let web3 = new Web3("http://localhost:8546");
let address ="0xdfc11dbe7a1110dd1d47c269587386b0549eb8eea776fc2942cea35454fd688f";
//从交易地址获取数据
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);
});