springboot操作以太坊(eth),使用web3j,转账等

Posted 佛说今夜有雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot操作以太坊(eth),使用web3j,转账等相关的知识,希望对你有一定的参考价值。

开发以太坊prc客户端:

本次使用的是《MetaMask》钱包

 

1,安装node.js

2,安装ganache-cli,开启本地web3j的测试服务

安装命令:npm install -g ganache-cli

启动本地服务命令:ganache-cli -i 1 -h 0.0.0.0 -p 8545

启动成功同时生成是个测试账号,每个账号100eth,如图

然后再MetaMask钱包可以通过输入秘钥的方式添加账号,如图

3.springboot部分:

导入pom     <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>5.0.0</version>
        </dependency>

如果运行报错 (okhttp3.RequestBody.create(Ljava/lang/String;Lokhttp3/MediaType;)Lokhttp3/RequestBody)则添加pom

 <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.3.1</version>
        </dependency>

创建web3j对象:

 

Web3j web3 = Web3j.build(new HttpService("http://localhost:8545/"));

4,已有metamask钱包的情况,没有metamask钱包,请先申请账号

 

public static void trade(Web3j web3) {
		try {
			//根据私钥创建身份信息
			Credentials credentials = Credentials.create("0xa91514e0c5f1d5929b3052b756d07a0123ec5cf7b86188f4410033bdc99a85e7");
			//转账
			TransactionReceipt transactionReceipt = Transfer.sendFunds(
					web3, credentials, "0xb425cEA579C1c2F7A902ec1aFB5d2bEE04A611eA",
					BigDecimal.valueOf(1.0), Convert.Unit.ETHER).send();
			
			System.out.println(transactionReceipt.getBlockHash());
		} catch (IOException | CipherException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (TransactionException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	

5,使用代码创建钱包

创建:

public static void createWallet() {
		//存放钱包的位置,不需要文件名,会自动生成
		String filePath = "G:/eth_wallet";
		File f = new File(filePath);
		try {//"l.1061326670"你钱包的密码
			String fileName = WalletUtils.generateNewWalletFile("l.1061326670", f , false);
			System.out.println(fileName);
			Credentials credentials = WalletUtils.loadCredentials("l.1061326670", f);
			System.out.println(credentials.getAddress());//钱包地址
			System.out.println(credentials.getEcKeyPair().getPrivateKey());//私钥
			System.out.println(credentials.getEcKeyPair().getPublicKey());//公钥
		} catch (CipherException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidAlgorithmParameterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

转账:

 

public static void tradeByOnlineWallet(Web3j web3) {
		try {
			String filePath = "G:/eth_wallet/***.json";
			File f = new File(filePath);
			Credentials credentials = WalletUtils.loadCredentials("l.1061326670", f);
			TransactionReceipt transactionReceipt = Transfer.sendFunds(
					web3, credentials, "0xb425cEA579C1c2F3A024ec1aFB5d2bEE04A611eA",
					BigDecimal.valueOf(1.0), Convert.Unit.ETHER).send();
			
			System.out.println(transactionReceipt.getBlockHash());
		} catch (IOException | CipherException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (TransactionException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

 

以上是关于springboot操作以太坊(eth),使用web3j,转账等的主要内容,如果未能解决你的问题,请参考以下文章

以太坊账户操作语句

什么是以太币/以太坊ETH?

什么是以太币/以太坊ETH?

eth是啥意思

01 ETH-以太坊概述

ETH以太坊怎样进行一键发币?