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

开发以太坊prc客户端:

本次使用的是《MetaMask》钱包

springboot操作以太坊(eth),使用web3j,转账等_第1张图片

 

1,安装node.js

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

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

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

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

springboot操作以太坊(eth),使用web3j,转账等_第2张图片

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

springboot操作以太坊(eth),使用web3j,转账等_第3张图片

3.springboot部分:

导入pom     
            org.web3j
            core
            5.0.0
        

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

 
            com.squareup.okhttp3
            okhttp
            4.3.1
       

创建web3j对象:

 

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

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

springboot操作以太坊(eth),使用web3j,转账等_第4张图片

 

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,使用代码创建钱包

创建:

springboot操作以太坊(eth),使用web3j,转账等_第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();
		}
		
		
	}

转账:

springboot操作以太坊(eth),使用web3j,转账等_第6张图片

 

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,以太坊,以太坊,spring,boot,前端,区块链,智能合约)