1、geth安装
https://geth.ethereum.org/downloads/
2、web3.js下载
https://github.com/ethereum/web3.js
或在前端直接引用js
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
3、创建geth初始化文件 genesis.json
{
"nonce": "0x0000000000000042",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x8000000",
"difficulty": "0x0400",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
},
"config": {
"chainId": 3141,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
}
}
4、开始布置节点
geth --datadir "./" init genesis.json
常遇到的几个错误:
Fatal: invalid genesis file: missing 0x prefix for hex data:这个错误信息意思很明白,就是你的json文件中,对于16进制数据,需要加上0x前缀
Fatal: invalid genesis file: hex string has odd length: 从v1.6开始,设置的十六进制数值,不能是奇数位, 比如不能是0x0,而应该是0x00。
Fatal: failed to write genesis block: genesis has no chain configuration :这个错误信息,就是说,你的json文件中,缺少config部分。看到这个信息,我们不需要把geth退回到v1.5版本,而是需要加上config部分。
Error: invalid sender undefined: 这个错误不会导致初始化失败,但是会在以后的转账(eth.sendTransaction),或者部署智能合约的时候产生。解决方法就是chainId 不能设置为0。 如果你完全按照github上给的官方配置文件,就会产生这个错误。一些参数如下:
该区域内容来自转载
5、启动geth私链
geth --datadir "./" --networkid 4225 --rpc --rpcport 8545 --port 30303 --rpccorsdomain="*" -rpcapi eth,web3,personal,net console 2>log.txt
datadir 这是你私链的地址 这里就是D盘blockchain下面
targetgaslimit –每个区块能承载gas上限,这里可以暂时理解为容量
rpc –启动rpc通信,可以进行智能合约的部署和调试
rpcaddr –rpc接口的地址
rpcport –rpc接口的端口号
port –网络监听端口,用于节点之间通信
rpcapi –设置rpc的范围,暂时开启eth,web3,personal足够
networkid –设置当前区块链的网络ID,是一个数字,可以随便写(尽可能大于10)
mine 允许挖矿
minerthreads 挖矿启动的线程数量,默认是4个。
console –启动命令行模式
6、geth命令行操作
personal.newAccount("输入密码");//创建用户
eth.accounts;//查询用户
eth.getBalance("输入账号");//查询余额
miner.start();//开始挖矿
miner.stop();//停止挖矿
personal.unlockAccount("用户地址","密码");//解锁用户
eth.sendTransaction({from: user1,to: user2,value: web3.toWei(3,"ether")});//转账
7、使用web3.js 操作钱包
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
<script>
var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
var ethobj = {
//创建账号
createAccount:function(password){
web3.personal.newAccount(password,function(error,result){
console.log(result);
});
},
//获取账号
getAccounts:function () {
web3.eth.getAccounts(function(error,result){
console.log(result);
});
},
//余额查询
getBalance:function (addr) {
//web3.eth.getBalance(addr).then(console.log);
web3.eth.getBalance(addr,function (error,result) {
console.log(result);
})
},
//解锁账号
unlockAccount:function (addr,password) {
web3.personal.unlockAccount(addr,password,function(error,result){
console.log(result);
})
},
//转账
transfer:function (_from,_to,balance) {
web3.eth.sendTransaction({
from:_from,
to:_to,
value:balance
},function(error,result){
console.log(result);
})
}
}
var addr1 = "";
var addr2 = "";
var password = "";
var balance = "";
//ethobj.createAccount(password);
//ethobj.getAccounts();
//ethobj.getBalance(addr2);
//ethobj.unlockAccount(addr1,password);
//ethobj.transfer(addr1,addr2,balance);
</script>
</body>
</html>
8、私钥 keystore 助记词
https://github.com/ethereumjs/keythereum