Ethereum_15_Web3j_1

2018/06/27 Ethereum
  1. maven集成
<dependency>
  <groupId>org.web3j</groupId>
  <artifactId>core</artifactId>
  <version>3.4.0</version>
</dependency>
  1. 本机(windows)安装node、jdk1.8版本以上

可直接下载安装程序,node-v8.11.3-x64,安装完成后查看版本

C:\Users\commando>node -v v8.11.3

C:\Users\commando>npm -v 5.6.0

安装solc

C:\Users\commando>npm install -g solc
提示安装完成
C:\Users\commando\AppData\Roaming\npm\solcjs -> C:\Users\commando\AppData\Roaming\npm\node_modules\solc\solcjs
+ solc@0.4.24
added 66 packages in 40.558s

  1. 生成bin和abi文件
C:\Users\commando>solcjs d:\\Data.sol --optimize --bin --abi  --output-dir d:\\

提示
Failed to write d:\d_\Data_sol_Data.bin: Error: ENOENT: no such file or directory, open 'd:\d_\Data_sol_Data.bin'
Failed to write d:\d_\Data_sol_Data.abi: Error: ENOENT: no such file or directory, open 'd:\d_\Data_sol_Data.abi'

在d盘下建立 d_ 子目录后生成成功

  1. 生成合约java文件

首先下载web3j,解压

C:\Users\commando>cd C:\Users\commando\Desktop\web3j-3.4.0

cmd窗口在其bin目录下执行生成文件命令

C:\Users\commando\Desktop\web3j-3.4.0\bin>web3j solidity generate d:\d_\Data_sol_Data.bin d:\d_\Data_sol_Data.abi -o d:\d_ -p com.euming.test

错误提示


成功提示

              _      _____ _     _
             | |    |____ (_)   (_)
__      _____| |__      / /_     _   ___
\ \ /\ / / _ \ '_ \     \ \ |   | | / _ \
 \ V  V /  __/ |_) |.___/ / | _ | || (_) |
  \_/\_/ \___|_.__/ \____/| |(_)|_| \___/
                         _/ |
                        |__/

Generating com.euming.test.Greeter_sol_greeter ... File written to d:\d_

注意abi和bin文件的顺序是一定的,倒过来是会出错的

  1. 完成

生成的java文件,对应着sol合约文件,可以放到java工程中,供web3j调用

  1. 其他 web3j接口中可以创建钱包,cmd命令行中也可以创建,效果是一样的,都是生成keystore文件
创建钱包:

C:\Users\commando\Desktop\web3j-3.4.0\bin>web3j wallet create

成功提示:

              _      _____ _     _
             | |    |____ (_)   (_)
__      _____| |__      / /_     _   ___
\ \ /\ / / _ \ '_ \     \ \ |   | | / _ \
 \ V  V /  __/ |_) |.___/ / | _ | || (_) |
  \_/\_/ \___|_.__/ \____/| |(_)|_| \___/
                         _/ |
                        |__/

Please enter a wallet file password:
Please re-enter the password:
Please enter a destination directory location [C:\Users\commando\AppData\Roaming\Ethereum\testnet\keystore]:
Creating directory: C:\Users\commando\AppData\Roaming\Ethereum\testnet\keystore ...complete
Wallet file UTC--2018-06-28T09-22-17.696000000Z--9c704efd3c1fc785b303cf4791db9e3811ef89e5.json successfully created in: C:\Users\commando\AppData\Roaming\Ethereum\testnet\keystore

钱包文件格式

{
	"address": "22149078fdf7178fd8f0ff89ce6a85d230e583d8",
	"crypto": {
		"cipher": "aes-128-ctr",
		"ciphertext": "afbc0f2667d76f36b14289942286df914ff61bc52c536313eea934dd164a115d",
		"cipherparams": {
			"iv": "7bf07e440112cbc67d2768c59c560a74"
		},
		"kdf": "scrypt",
		"kdfparams": {
			"dklen": 32,
			"n": 262144,
			"p": 1,
			"r": 8,
			"salt": "6031d4e7ae8df58998ce284b5781ac17fdddfb20f1855c538aef4ddb4417fd96"
		},
		"mac": "07c1625c32b11212eb7789a131bd84273e81f1c3c43646f05485baaacbab69ad"
	},
	"id": "781879b0-ea6c-4773-8bea-9e912b5bc5fc",
	"version": 3
}

cipher:加密算法,AES算法,用于加密以太坊私钥
cipherparams:cipher算法需要的参数,参数iv,是aes-128-ctr加密算法需要的初始化向量
ciphertext:加密后的密文,aes-128-ctr函数的加密输入密文;
kdf:秘钥生成函数,用于使用密码加密keystore文件
kdfparams:kdf算法所需要的参数
mac:验证密码的编码


  1. remix 如果使用remix
Not possible to connect to the Web3 provider. Make sure the provider is running and a connection is open (via IPC or RPC).


http://remix.ethereum.org 替换 https://remix.ethereum.org


remix deploy 显示 authentication needed:password or unlock

> personal.unlockAccount("0x22149078fdf7178fd8f0ff89ce6a85d230e583d8","huangl")

Search

    Table of Contents