主页 > imtoken交易所下载 > 区块链开发的ETH钱包APP
区块链开发的ETH钱包APP
· 以太坊钱包支持
自动生成 Java 智能合约包装器以从原生 Java 代码创建、部署、处理和调用智能合约(支持 Solidity 和 Truffle 定义格式)
· 过滤器的反应式 API
· 以太坊名称服务 (ENS) 支持
· 支持 Parity 的 Personal 和 Geth 的 Personal 客户端 API
支持 Infura,因此您不必自己运行以太坊客户端
·支持ERC20和ERC721代币标准
· 彻底的集成测试,证明了以上的许多内容
·命令行工具
· 兼容安卓系统
通过 web3j-quorum 支持 JP Morgan 的 Quorum
依靠
Java 8:
compile('org.web3j:core:4.5.12')
安卓:
实现('org.web3j:core:4.2.0-android')
web3j的基本使用
1.同步
Web3j web3 = Web3j.build(new HttpService("节点地址"));//默认为:8545/
Web3ClientVersion web3ClientVersion = web3. web3ClientVersion()。 发送();
字符串版本 = web3ClientVersion。 getWeb3ClientVersion();
System.out.println("版本:" + 版本);
2.异步
Web3j web3 = Web3j.build(new HttpService("节点地址")); // 默认为:8545/
web3.web3ClientVersion().sendAsync().thenAccept(新消费者() {
@覆盖
公共无效接受(Web3ClientVersion web3ClientVersion){
System.out.println(web3ClientVersion.getWeb3ClientVersion());
}
}).exceptionally(新函数(){
@覆盖
public Void apply(Throwable throwable) {
可抛。 打印堆栈跟踪();
返回空值;
}
});
实际开发
生成账户
1.随机生成账号
String mnemonic = ChainUtil.genMnemonic();
ECKey ecKey = ChainUtil.genECKey(mnemonic, "m/44'/60'/0'/0/0", "");
ECKeyPair keyPair = ECKeyPair.create(ecKey.getPrivKeyBytes());
File file = new File("Keystore存放路径");
String walletFile = WalletUtils.generateWalletFile("你的密码", keyPair, file, false);
字符串密钥库 = FilesUtils。 readFileString(new File(file, walletFile).getAbsolutePath());
Credentials credentials = WalletUtils.loadCredentials("你的密码", new File(file, walletFile));
字符串地址 = 凭据。 获取地址();
System.out.println("助记词:" + mnemonic);
System.out.println("私钥:" + keyPair.getPrivateKey().toString(16));
System.out.println("公钥:" + keyPair.getPublicKey().toString(16));
System.out.println("密钥库:" + 密钥库);
System.out.println("地址:" + 地址);
2.导入私钥
String privateKey = "你的私钥";
凭证凭证 = 凭证。 创建(私钥);
字符串地址 = 凭据。 获取地址();
ECKeyPair keyPair = 凭据。 getEcKeyPair();
File file = new File("Keystore存放路径");
String walletFile = WalletUtils.generateWalletFile("你的密码",keyPair,file,false);
字符串密钥库 = FilesUtils。 readFileString(new File(file, walletFile).getAbsolutePath());
System.out.println("私钥:" + keyPair.getPrivateKey().toString(16));
System.out.println("公钥:" + keyPair.getPublicKey().toString(16));
System.out.println("密钥库:" + 密钥库);
System.out.println("地址:" + 地址);
//私钥无法计算助记词
3.KeyStore导入
String keystore = "你的密钥库";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
WalletFile walletFile = objectMapper. readValue(keystore, WalletFile.class);
ECKeyPair keyPair = EthWallet.decrypt("你的密码", walletFile);
字符串私有密钥 = 密钥对。 获取私钥()。 到字符串(16);
字符串公钥 = 密钥对。 获取公钥()。 到字符串(16);
凭证凭证 = 凭证。 创建(私钥,公钥);
字符串地址 = 凭据。 获取地址();
System.out.println("私钥:" + privateKey);
System.out.println("公钥:" + publicKey);
System.out.println("密钥库:" + 密钥库);
System.out.println("地址:" + 地址);
4.导入助记词
String mnemonic = "你的助记词,以逗号分隔";
ECKey ecKey = ChainUtil.genECKey(mnemonic, "m/44'/60'/0'/0/0", "");
ECKeyPair keyPair = ECKeyPair.create(ecKey.getPrivKeyBytes());
File file = new File("Keystore存放路径");
String walletFile = WalletUtils.generateWalletFile("你的密码", keyPair, file, false);
字符串密钥库 = FilesUtils。 readFileString(new File(file, walletFile).getAbsolutePath());
Credentials credentials = WalletUtils.loadCredentials("你的密码", new File(file, walletFile));
字符串地址 = 凭据。 获取地址();
System.out.println("助记词:" + mnemonic);
System.out.println("私钥:" + keyPair.getPrivateKey().toString(16));
System.out.println("公钥:" + keyPair.getPublicKey().toString(16));
System.out.println("密钥库:" + 密钥库);
System.out.println("地址:" + 地址);
转移
String from = "转账地址";
String to = "转账地址";
String privateKey = "你的私钥";
BigInteger value = "转出多少";
BigInteger gasPrice = "天然气价格";
BigInteger gasLimit = BigInteger.valueOf(21000); //单笔转账一般需要21000
Web3j web3 = Web3j.build(new HttpService("节点地址")); // 默认为:8545/
BigInteger nonce = web3.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send().getTransactionCount();
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, "");
ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(privateKey, 16));
凭证凭证 = 凭证。 创建(ecKeyPair);
byte[] signMessage = TransactionEncoder. signMessage(rawTransaction, credentials);
字符串 signData = 数字。 toHexString(signMessage);
EthSendTransaction ethSendTransaction = web3.ethSendRawTransaction(signData).send();
字符串 transactionHash = ethSendTransaction。 getTransactionHash();
System.out.println("哈希:" + transactionHash);
// 获取哈希查询交易状态。
代币转移
String from = "转账地址";
String to = "转账地址";
String privateKey = "你的私钥";
BigInteger value = "转出多少";
String contract = "";//合约地址
BigInteger gasPrice = "天然气价格";
BigInteger gasLimit = BigInteger.valueOf(60000); //代币转账一般需要60000
Web3j web3 = Web3j.build(new HttpService("节点地址")); // 默认为:8545/
BigInteger nonce = web3.ethGetTransactionCount(from, DefaultBlockParameterName.LATEST).send().getTransactionCount();
函数 function = new Function("transfer", Arrays.asList(new Address(to), new Uint256(value)),
收藏品。 singletonList(new TypeReference() {}));
字符串 encodedFunction = FunctionEncoder。 编码(函数);
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, contract, encodedFunction);
ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(privateKey, 16));
凭证凭证 = 凭证。 创建(ecKeyPair);
byte[] signMessage = TransactionEncoder. signMessage(rawTransaction, credentials);
字符串 signData = 数字。 toHexString(signMessage);
EthSendTransaction ethSendTransaction = web3.ethSendRawTransaction(signData).send();
字符串 transactionHash = ethSendTransaction。 getTransactionHash();
System.out.println("哈希:" + transactionHash);
// 获取哈希查询交易状态。
取得平衡
Web3j web3 = Web3j.build(new HttpService("节点地址")); // 默认为:8545/
String address = "获取余额的地址";
BigInteger balance = web3.ethGetBalance(address, DefaultBlockParameterName.LATEST).send().getBalance();
System.out.println("余额:" + 余额);
获取代币余额
· 参数:
来自:钱包地址
to:代币地址(智能合约地址)
数据:0x70a08231000000000000000000000000b60e8dd61c5d32be8058bb8eb970870f07233155
//注意数据数据格式:第一个“0x70a082310000000000000000000000000”是固定的,后面是钱包地址(没有“0x”前缀)
数量|标签,“最新”,“最早”或“待定”
代码示例
Web3j web3 = Web3j.build(new HttpService("节点地址")); // 默认为:8545/
String address = "获取余额的地址";
String contract = "合约地址";
字符串 tmpAddress = 地址;
如果 (tmpAddress.startsWith("0x")) {
tmpAddress = address.substring(2); //删除0x
}
字符串数据=“0x70a08231000000000000000000000000”+tmp地址;
交易 transaction = Transaction.createEthCallTransaction(address, contract, data);
String balance = web3.ethCall(transaction, DefaultBlockParameterName.LATEST).send().getValue();
System.out.println("余额:" + 余额);
总结
1.导入钱包踩坑
Android中使用web3j导入轻钱包解析失败解决Out Of Memory异常
解决方案
implementation 'com.lambdaworks:scrypt:1.4.0'//解决oom错误依赖
因为在导入keystore进行分析时,就是这个报错例程:ECKeyPair keyPair = ECKeyPair.create(privateKeyByte); 错误报告,
这个错误是因为无法通过解析轻钱包的方法解析出重钱包。 keyStore中的crypto.kdfparams.n:262144,这个值很大,正常的轻钱包n值在4000左右,还是一个四位数。
解决办法以太坊钱包下载,查了很多资料,终于找到了。 只需替换下面的代码。
//derivedKey = generateDerivedScryptKey(password.getBytes(Charset.forName("UTF-8")), salt, n, r, p, dklen);
derivedKey = com.lambdaworks.crypto.SCrypt.scrypt(password.getBytes(StandardCharsets.UTF_8), salt, n, r, p, dklen);
2. 实现以太坊钱包相对容易。 毕竟提供了相关的SDK,很容易实现。 主要是掌握web3j的API用法。 如果选择其他以太坊SDK以太坊钱包下载,也是类似的,熟悉API调用即可。
深圳城市微生活区块链公链开发、区块链浏览器开发、区块链白皮书编写、区块链去中心化交易系统搭建、区块链钱包+挖矿系统、DeFi模型开发、企业链变+溯源系统、区块链电商/商城代币购物系统以及区块链游戏等其他定制化开发服务。 选择都市微生活区块链开发,让您的企业快速迈向区块链+时代。返回搜狐查看更多