Springboot operates Ethereum (eth), uses web3j, transfers, etc.

Develop Ethereum prc client:

This time I used the "MetaMask" wallet

 

1. Install node.js

2. Install ganache-cli and start the local web3j test service

Installation command: npm install -g ganache-cli

Start the local service command: ganache-cli -i 1 -h 0.0.0.0 -p 8545

The startup is successful and a test account is generated at the same time, each account is 100eth, as shown in the figure

Then the MetaMask wallet can add an account by entering the secret key, as shown in the figure

3. spring boot part:

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

If an error is reported (okhttp3.RequestBody.create(Ljava/lang/String; Lokhttp3/MediaType;) Lokhttp3/RequestBody), add pom

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

Create the web3j object:

 

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

4. If you already have a metamask wallet, but if you don’t have a metamask wallet, please apply for an account first

 

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. Use the code to create a wallet

create:

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();
		}
		
		
	}

transfer:

 

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();
		}
		
	}

 

Guess you like

Origin blog.csdn.net/qq_38158357/article/details/120964875