geth 安装
下载地址
创建json文件
{
"nonce":"0x0000000000000042",
"mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x4000",
"alloc": {},
"coinbase":"0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00000000",
"gasLimit":"0xffffffff",
"config":{
"chainId": 666,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0
}
}
参数名 描述
identity 区块链的标示,用于标示目前网络的名字
init 指定创世块文件的位置,并创建创世块
datadir 当前区块数据存放的位置
port 网络监听端口,默认30303
rpc 启动rpc通信 [创世区块初始化]
rpcapi 设置允许连接rpc的客户端,一般为db,eth,net,web3
rpccorsdomain 指定什么url能连接到你的节点执行rpc定制端任务,如果输入的是“*”,则任何url都可以连接到你的rpc实例。
rpcaddr 连接rpc的地址,默认为localhost
rpcport 连接rpc的端口,默认为8545
networkid 设置当前区块链的网络ID,用于区分不同的网络,是一个数字
--rpcaddr : http-rpc服务器接口地址:默认“127.0.0.1”,
console 启动命令行模式,可以在Geth中执行命令
- ./geth --identity "MyNodeName" --rpc --rpcport "8545" --rpccorsdomain "*" --datadir "chaindata/" --port "30303" --nodiscover --rpcapi "db,eth,net,web3" --rpcaddr "0.0.0.0" --networkid 1006 console
开启本地私有链
- ./geth --datadir "chaindata" init geth.json 创世区块初始化
- miner.setEtherbase('0x2E3d5AEd4eCB0d0deD6c90733150701a3b746a3F') 设置挖矿存储地址
- personal.unlockAccount("0x01b5ecbcd8d46c1a9ee52e8b8a30bb6426dffb1b") 解锁账户
- eth.sendTransaction({from: "0x01b5ecbcd8d46c1a9ee52e8b8a30bb6426dffb1b", to: "0xe6e4e20c95abc11dca8b3e9c292a34725bf89930", value: web3.toWei(3)}) 转账
geth --dev creates a preconfigured private network, so --unlock and --password don't work.
If you want them to work, create a new private network and geth init it with a custom genesis.json.
- 使用geth attach命令进入控制台时,出现错误
Fatal: Unable to attach to remote geth: dial unix /home/lv/.ethereum/geth.ipc: connect: connection refused
解决方案:
Geth 1.3.6
$ geth attach rpc://localhost
Geth 1.4.0
$ geth attach http://localhost:8545
- 部署和约时,将编译好的代码拷贝直接放到控制台,出现Error: account is locked undefined错误
解决方案:
使用personal.unlockAccount(eth.accounts[0],'password')命令将用户解锁。Password为你创建账号的密码。 - 使用 Browser-solidity 在 Go-Ethereum上进行智能合约部署时,可能会出现Error: exceeds block gas limit undefined的报错
本地Eth项目地址>>>
使用
地址:0xD2311b861d5114630BaD401995A63100Da88bF36
私钥:6F9EFFAD92AEC3EFE713CD3496B6088E5EE51DB4ABC6064D56ADAF02CBC3B91E
地址:0x31cbEa7fF998E332D5e5a14b0269D2DCDD516F58
私钥:E6533EB5AD9FADC17832196A945732CC691DDA1039D1BC13AE209C1C66F4F133
依赖
org.web3j
core
3.4.0
package com.sskj.coin;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.web3j.crypto.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
public class EthMain {
//以太坊节点RPC地址
private static final String RPC_URL = "http://127.0.0.1:8545";
//使用http服务连接节点构建web3j对象
private static final Web3j web3 = Web3j.build(new HttpService(RPC_URL));
private static final String PRIVATE_KEY = "E6533EB5AD9FADC17832196A945732CC691DDA1039D1BC13AE209C1C66F4F133";
public static void main(String[] args) throws Exception {
Credentials credentials = loadCredentials(PRIVATE_KEY);
TransactionReceipt transactionReceipt = transferETH("0xD2311b861d5114630BaD401995A63100Da88bF36", new BigDecimal("1"), credentials);
System.out.println(transactionReceipt.toString());
System.out.println( getETHBalance("0xD2311b861d5114630BaD401995A63100Da88bF36"));
}
/**
* 加载钱包对象(Credentials)四种方式
* 1.通过私钥
* 2.通过密钥对
* 3.通过密码+keyStore路径
* 4.通过密码+keyStore字符串
*/
public static Credentials loadCredentials(String privateKey){
return Credentials.create(privateKey);
}
public static Credentials loadCredentials(ECKeyPair keyPair){
return Credentials.create(keyPair);
}
public static Credentials loadCredentials(String password, String keyStoreUrl) throws IOException, CipherException {
return WalletUtils.loadCredentials(password, keyStoreUrl);
}
public static Credentials loadCredentialsByString(String password, String keyStoreJson) throws IOException, CipherException {
WalletFile walletFile = new ObjectMapper().readValue(keyStoreJson, WalletFile.class);
return Credentials.create(Wallet.decrypt(password, walletFile));
}
/**
* 创建钱包
* @param password 生成的keyStore密码
* @param outPath 生成的keyStore文件存放路径
*/
public static String createWallet(String password, String outPath) throws Exception {
//创建新钱包KeyStore文件
String fileName = WalletUtils.generateFullNewWalletFile(password,new File(outPath));
//通过KeyStore文件加载钱包
Credentials credentials = WalletUtils.loadCredentials(password,"outPath\\"+fileName);
//通过钱包获取私钥
BigInteger privateKey = credentials.getEcKeyPair().getPrivateKey();
return privateKey.toString(16);
}
/**
* 查询以太币余额
* @Param address 需要查询的钱包地址
*/
public static String getETHBalance(String address) throws IOException {
BigDecimal banlance = new BigDecimal(web3.ethGetBalance(address, DefaultBlockParameter.valueOf("LATEST")).send().getBalance());
return banlance.divide(new BigDecimal("10").pow(18)).toPlainString();
}
/**
* ETH单笔转账
* @param address to地址
* @param count to数量
* @param credentials from钱包对象
*/
public static TransactionReceipt transferETH(String address, BigDecimal count, Credentials credentials) throws Exception {
return Transfer.sendFunds(web3, credentials, address, count, Convert.Unit.ETHER).send();
}
/**
* 获取当前nonce
* @param address 需要查询的钱包地址
*/
public static String getNonce(String address) throws IOException {
return web3.ethGetTransactionCount(address, DefaultBlockParameterName.LATEST).send().getTransactionCount().toString();
}
}