打包智能合约成Java代码

更新—2021/5/27

之前使用命令行转换solidity合约成Java形式的合约,非常复杂;通过学习了解,有一种更为便捷的方法,那就是使用插件;详见如下:

合约代码

先准备好一份合约代码如下:

合约的功能是实现简单的用户管理,包括

  • 注册功能
  • 登录功能
  • 修改密码
  • 获得用户信息
  • 获得系统用户总数
pragma solidity >= 0.5.0;
contract UserManagerment {
    // 用户结构体
    struct User {
        address ethAddr;
        string userName;
        bytes32 password;
    }
    // 合约部署者地址
    address host;
    // 保存所有用户
    User[] userInfos;
    // 用户名到是否注册的映射
    mapping(string => bool) registerPool;
    // 用户名到用户的映射
    mapping(string => User) userPool;
    // 构造函数 设置合约拥有者地址
    constructor() public {
        host = msg.sender;
    }
    // 登录功能
    function doLogin(string memory userName, string memory password)  public view returns (bool) {
        return userPool[userName].password == keccak256(abi.encode(password));
    }
    // 注册检测
    function checkRegister(string memory userName)  public view returns (bool) {
        return registerPool[userName];
    }
	// 用户注册
    function register(address ethAddr, string memory  userName, string memory password) public {
        // 检查用户是否注册 当 require() 中的条件值为假时抛出异常,异常信息为:“用户已经注册”
        require(!checkRegister(userName),"用户已经注册!");
        // 保存注册信息
		userPool[userName] = User(ethAddr, userName, keccak256(abi.encode(password)));
        // 设置用户名为已经注册
		registerPool[userName] = true;
        // 添加到用户数组中
        userInfos.push(userPool[userName]);
    }
    // 更新密码
    function updatePassword(string memory userName, string memory newPwd) public {
        // keccak256加密
		userPool[userName].password = keccak256(abi.encode(newPwd));
    }
    // 获得用户信息
    function getUserInfoByUserName(string memory userName) public view returns (address,string memory){
        require(registerPool[userName],"未查到该用户信息");
        User storage user = userPool[userName];
        return (user.ethAddr,user.userName);
    }
    // 获得所有用户信息
    function getAllUserInfos(uint index)public view returns (address,string memory){
        // 检查下表范围
        require(index < userInfos.length,"下标越界");
        // 检查用户权限,只有合约创建者才能执行该操作
        require(msg.sender == host,"非法访问");
        // 取出用户信息
        User storage user = userInfos[index];
        // 返回数据
        return (user.ethAddr,user.userName);
    }
    // 获得系统用户数
    function getTotalUserNum() public view returns (uint){
        // 检查用户权限,只有合约创建者才能执行该操作
        require(msg.sender == host,"非法访问");
        return userInfos.length;
    }
}

编译合约

由于使用的VS Code编辑器,安装的插件solidity0.0.72可以直接按F5编译
在这里插入图片描述
编译后,在文件夹根目录生成了bin文件夹。里头的内容如下:

在这里插入图片描述

java打包合约

  1. 下载web3j命令行工具

    下载连接:点击直达

    在这里插入图片描述

  2. 解压工具包

    在这里插入图片描述

    注:可以将bin文件夹路径添加到系统环境变量中,这样省得之后进入bin目录使用命令

  3. 进入文件夹bin目录

    输入如下命令:

    web3j solidity generate /path/to/<smart-contract>.bin /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name
    

    说明:

    • /path/to/.bin 智能合约编译后的生成物
    • /path/to/.abi 智能合约编译后的生成物
    • -o /path/to/src/main/java 输出路径
    • -p com.your.organisation.name 打包的包名

    举例:

    web3j solidity generate E:\BlockChain\Solidity\bin\UserManagerment.bin E:\BlockChain\Solidity\bin\UserManagerment.abi  -o E:\Web3j -p com.my.contract	
    

    在这里插入图片描述

    输出结果:

    在这里插入图片描述

修改Java代码

  1. 上述操作生成的UserManagerment.java内容如下:

    package com.my.contract;
    
    import java.math.BigInteger;
    import java.util.Arrays;
    import java.util.Collections;
    
    import org.web3j.abi.TypeReference;
    import org.web3j.abi.datatypes.Function;
    import org.web3j.abi.datatypes.Type;
    import org.web3j.crypto.Credentials;
    import org.web3j.protocol.Web3j;
    import org.web3j.protocol.core.RemoteCall;
    import org.web3j.protocol.core.methods.response.TransactionReceipt;
    import org.web3j.tx.Contract;
    import org.web3j.tx.TransactionManager;
    import org.web3j.tx.gas.ContractGasProvider;
    
    /**
     * <p>
     * Auto generated code.
     * <p>
     * <strong>Do not modify!</strong>
     * <p>
     * Please use the <a href="https://docs.web3j.io/command_line.html">web3j
     * command line tools</a>, or the
     * org.web3j.codegen.SolidityFunctionWrapperGenerator in the
     * <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen
     * module</a> to update.
     *
     * <p>
     * Generated with web3j version 3.6.0.
     */
    public class UserManagerment extends Contract {
    	private static final String BINARY = "608060405234801561001057600080fd5b50600080546001600160a01b03191633179055611004806100326000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c80638d59cc021161005b5780638d59cc021461030e578063a847666214610449578063a8917388146104ed578063cdfbb3511461050a5761007d565b8063787b50e8146100825780637fcee2761461009c57806382e2ec2a146101d1575b600080fd5b61008a610633565b60408051918252519081900360200190f35b610140600480360360208110156100b257600080fd5b810190602081018135600160201b8111156100cc57600080fd5b8201836020820111156100de57600080fd5b803590602001918460018302840111600160201b831117156100ff57600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061068a945050505050565b60405180836001600160a01b03166001600160a01b0316815260200180602001828103825283818151815260200191508051906020019080838360005b8381101561019557818101518382015260200161017d565b50505050905090810190601f1680156101c25780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b6102fa600480360360408110156101e757600080fd5b810190602081018135600160201b81111561020157600080fd5b82018360208201111561021357600080fd5b803590602001918460018302840111600160201b8311171561023457600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b81111561028657600080fd5b82018360208201111561029857600080fd5b803590602001918460018302840111600160201b831117156102b957600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610853945050505050565b604080519115158252519081900360200190f35b6104476004803603606081101561032457600080fd5b6001600160a01b038235169190810190604081016020820135600160201b81111561034e57600080fd5b82018360208201111561036057600080fd5b803590602001918460018302840111600160201b8311171561038157600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156103d357600080fd5b8201836020820111156103e557600080fd5b803590602001918460018302840111600160201b8311171561040657600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061094c945050505050565b005b6102fa6004803603602081101561045f57600080fd5b810190602081018135600160201b81111561047957600080fd5b82018360208201111561048b57600080fd5b803590602001918460018302840111600160201b831117156104ac57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610c36945050505050565b6101406004803603602081101561050357600080fd5b5035610ca1565b6104476004803603604081101561052057600080fd5b810190602081018135600160201b81111561053a57600080fd5b82018360208201111561054c57600080fd5b803590602001918460018302840111600160201b8311171561056d57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156105bf57600080fd5b8201836020820111156105d157600080fd5b803590602001918460018302840111600160201b831117156105f257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610dce945050505050565b600080546001600160a01b03163314610682576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b506001545b90565b600060606002836040518082805190602001908083835b602083106106c05780518252601f1990920191602091820191016106a1565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff1691506107449050576040805162461bcd60e51b815260206004820152601860248201527fe69caae69fa5e588b0e8afa5e794a8e688b7e4bfa1e681af0000000000000000604482015290519081900360640190fd5b60006003846040518082805190602001908083835b602083106107785780518252601f199092019160209182019101610759565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604080519788900382018820805460018281018054601f600293821615909a029097019096160496870184900484028a01840190925285895298506001600160a01b031696919550909350849291508301828280156108425780601f1061081757610100808354040283529160200191610842565b820191906000526020600020905b81548152906001019060200180831161082557829003601f168201915b505050505090509250925050915091565b6000816040516020018080602001828103825283818151815260200191508051906020019080838360005b8381101561089657818101518382015260200161087e565b50505050905090810190601f1680156108c35780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003846040518082805190602001908083835b602083106109105780518252601f1990920191602091820191016108f1565b51815160209384036101000a60001901801990921691161790529201948552506040519384900301909220600201549290921495945050505050565b61095582610c36565b1561099f576040805162461bcd60e51b8152602060048201526015602482015274e794a8e688b7e5b7b2e7bb8fe6b3a8e5868cefbc8160581b604482015290519081900360640190fd5b6040518060600160405280846001600160a01b03168152602001838152602001826040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610a005781810151838201526020016109e8565b50505050905090810190601f168015610a2d5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001208152506003836040518082805190602001908083835b60208310610a7d5780518252601f199092019160209182019101610a5e565b51815160209384036101000a6000190180199092169116179052920194855250604051938490038101909320845181546001600160a01b0319166001600160a01b039091161781558484015180519194610adf94506001860193500190610ec1565b506040820151816002015590505060016002836040518082805190602001908083835b60208310610b215780518252601f199092019160209182019101610b02565b51815160209384036101000a60001901801990921691161790529201948552506040519384900381018420805460ff19169515159590951790945550508351600192600392869290918291908401908083835b60208310610b935780518252601f199092019160209182019101610b74565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604051968790038101909620875460018181018a556000998a5297909820815460039099020180546001600160a01b0319166001600160a01b039099169890981788558087018054919897610c279750888101965090946002918316150290920116049050610f3f565b50600291820154910155505050565b60006002826040518082805190602001908083835b60208310610c6a5780518252601f199092019160209182019101610c4b565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff16949350505050565b6001546000906060908310610cec576040805162461bcd60e51b815260206004820152600c60248201526b392e22f9a821fa2da2b9e56360a21b604482015290519081900360640190fd5b6000546001600160a01b03163314610d3a576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b600060018481548110610d4957fe5b600091825260209182902060039091020180546001808301805460408051601f60026000199685161561010002969096019093169490940491820187900487028401870190528083529395506001600160a01b0390921693919290918391908301828280156108425780601f1061081757610100808354040283529160200191610842565b806040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610e0f578181015183820152602001610df7565b50505050905090810190601f168015610e3c5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003836040518082805190602001908083835b60208310610e895780518252601f199092019160209182019101610e6a565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092206002019290925550505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f0257805160ff1916838001178555610f2f565b82800160010185558215610f2f579182015b82811115610f2f578251825591602001919060010190610f14565b50610f3b929150610fb4565b5090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f785780548555610f2f565b82800160010185558215610f2f57600052602060002091601f016020900482015b82811115610f2f578254825591600101919060010190610f99565b61068791905b80821115610f3b5760008155600101610fba56fea2646970667358221220d58fd7f25a7952d20141a962115dcf426ac37274bb848f7a5f8374e3fce55e2d64736f6c63430006040033";
    
    	public static final String FUNC_CHECKREGISTER = "checkRegister";
    
    	public static final String FUNC_DOLOGIN = "doLogin";
    
    	public static final String FUNC_GETALLUSERINFOS = "getAllUserInfos";
    
    	public static final String FUNC_GETTOTALUSERNUM = "getTotalUserNum";
    
    	public static final String FUNC_GETUSERINFOBYUSERNAME = "getUserInfoByUserName";
    
    	public static final String FUNC_REGISTER = "register";
    
    	public static final String FUNC_UPDATEPASSWORD = "updatePassword";
    
    	@Deprecated
    	protected UserManagerment(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice,
    			BigInteger gasLimit) {
    		super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
    	}
    
    	protected UserManagerment(String contractAddress, Web3j web3j, Credentials credentials,
    			ContractGasProvider contractGasProvider) {
    		super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
    	}
    
    	@Deprecated
    	protected UserManagerment(String contractAddress, Web3j web3j, TransactionManager transactionManager,
    			BigInteger gasPrice, BigInteger gasLimit) {
    		super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    	}
    
    	protected UserManagerment(String contractAddress, Web3j web3j, TransactionManager transactionManager,
    			ContractGasProvider contractGasProvider) {
    		super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);
    	}
    
    	public static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials,
    			ContractGasProvider contractGasProvider) {
    		return deployRemoteCall(UserManagerment.class, web3j, credentials, contractGasProvider, BINARY, "");
    	}
    
    	public static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,
    			ContractGasProvider contractGasProvider) {
    		return deployRemoteCall(UserManagerment.class, web3j, transactionManager, contractGasProvider, BINARY, "");
    	}
    
    	@Deprecated
    	public static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice,
    			BigInteger gasLimit) {
    		return deployRemoteCall(UserManagerment.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
    	}
    
    	@Deprecated
    	public static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,
    			BigInteger gasPrice, BigInteger gasLimit) {
    		return deployRemoteCall(UserManagerment.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
    	}
    
    	public RemoteCall<TransactionReceipt> checkRegister(String userName) {
    		final Function function = new Function(FUNC_CHECKREGISTER,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    
    	public RemoteCall<TransactionReceipt> doLogin(String userName, String password) {
    		final Function function = new Function(FUNC_DOLOGIN,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),
    						new org.web3j.abi.datatypes.Utf8String(password)),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    
    	public RemoteCall<TransactionReceipt> getAllUserInfos(BigInteger index) {
    		final Function function = new Function(FUNC_GETALLUSERINFOS,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(index)),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    
    	public RemoteCall<TransactionReceipt> getTotalUserNum() {
    		final Function function = new Function(FUNC_GETTOTALUSERNUM, Arrays.<Type>asList(),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    
    	public RemoteCall<TransactionReceipt> getUserInfoByUserName(String userName) {
    		final Function function = new Function(FUNC_GETUSERINFOBYUSERNAME,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    
    	public RemoteCall<TransactionReceipt> register(String ethAddr, String userName, String password) {
    		final Function function = new Function(FUNC_REGISTER,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(ethAddr),
    						new org.web3j.abi.datatypes.Utf8String(userName),
    						new org.web3j.abi.datatypes.Utf8String(password)),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    
    	public RemoteCall<TransactionReceipt> updatePassword(String userName, String newPwd) {
    		final Function function = new Function(FUNC_UPDATEPASSWORD,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),
    						new org.web3j.abi.datatypes.Utf8String(newPwd)),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    
    	@Deprecated
    	public static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,
    			BigInteger gasPrice, BigInteger gasLimit) {
    		return new UserManagerment(contractAddress, web3j, credentials, gasPrice, gasLimit);
    	}
    
    	@Deprecated
    	public static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,
    			BigInteger gasPrice, BigInteger gasLimit) {
    		return new UserManagerment(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    	}
    
    	public static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,
    			ContractGasProvider contractGasProvider) {
    		return new UserManagerment(contractAddress, web3j, credentials, contractGasProvider);
    	}
    
    	public static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,
    			ContractGasProvider contractGasProvider) {
    		return new UserManagerment(contractAddress, web3j, transactionManager, contractGasProvider);
    	}
    }
     
    
  2. 代码内容介绍

    • 合约二进制串

      private static final String BINARY = "608060405234801561001057600080fd5b50600080546001600160a01b03191633179055611004806100326000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c80638d59cc021161005b5780638d59cc021461030e578063a847666214610449578063a8917388146104ed578063cdfbb3511461050a5761007d565b8063787b50e8146100825780637fcee2761461009c57806382e2ec2a146101d1575b600080fd5b61008a610633565b60408051918252519081900360200190f35b610140600480360360208110156100b257600080fd5b810190602081018135600160201b8111156100cc57600080fd5b8201836020820111156100de57600080fd5b803590602001918460018302840111600160201b831117156100ff57600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061068a945050505050565b60405180836001600160a01b03166001600160a01b0316815260200180602001828103825283818151815260200191508051906020019080838360005b8381101561019557818101518382015260200161017d565b50505050905090810190601f1680156101c25780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b6102fa600480360360408110156101e757600080fd5b810190602081018135600160201b81111561020157600080fd5b82018360208201111561021357600080fd5b803590602001918460018302840111600160201b8311171561023457600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b81111561028657600080fd5b82018360208201111561029857600080fd5b803590602001918460018302840111600160201b831117156102b957600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610853945050505050565b604080519115158252519081900360200190f35b6104476004803603606081101561032457600080fd5b6001600160a01b038235169190810190604081016020820135600160201b81111561034e57600080fd5b82018360208201111561036057600080fd5b803590602001918460018302840111600160201b8311171561038157600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156103d357600080fd5b8201836020820111156103e557600080fd5b803590602001918460018302840111600160201b8311171561040657600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061094c945050505050565b005b6102fa6004803603602081101561045f57600080fd5b810190602081018135600160201b81111561047957600080fd5b82018360208201111561048b57600080fd5b803590602001918460018302840111600160201b831117156104ac57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610c36945050505050565b6101406004803603602081101561050357600080fd5b5035610ca1565b6104476004803603604081101561052057600080fd5b810190602081018135600160201b81111561053a57600080fd5b82018360208201111561054c57600080fd5b803590602001918460018302840111600160201b8311171561056d57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156105bf57600080fd5b8201836020820111156105d157600080fd5b803590602001918460018302840111600160201b831117156105f257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610dce945050505050565b600080546001600160a01b03163314610682576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b506001545b90565b600060606002836040518082805190602001908083835b602083106106c05780518252601f1990920191602091820191016106a1565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff1691506107449050576040805162461bcd60e51b815260206004820152601860248201527fe69caae69fa5e588b0e8afa5e794a8e688b7e4bfa1e681af0000000000000000604482015290519081900360640190fd5b60006003846040518082805190602001908083835b602083106107785780518252601f199092019160209182019101610759565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604080519788900382018820805460018281018054601f600293821615909a029097019096160496870184900484028a01840190925285895298506001600160a01b031696919550909350849291508301828280156108425780601f1061081757610100808354040283529160200191610842565b820191906000526020600020905b81548152906001019060200180831161082557829003601f168201915b505050505090509250925050915091565b6000816040516020018080602001828103825283818151815260200191508051906020019080838360005b8381101561089657818101518382015260200161087e565b50505050905090810190601f1680156108c35780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003846040518082805190602001908083835b602083106109105780518252601f1990920191602091820191016108f1565b51815160209384036101000a60001901801990921691161790529201948552506040519384900301909220600201549290921495945050505050565b61095582610c36565b1561099f576040805162461bcd60e51b8152602060048201526015602482015274e794a8e688b7e5b7b2e7bb8fe6b3a8e5868cefbc8160581b604482015290519081900360640190fd5b6040518060600160405280846001600160a01b03168152602001838152602001826040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610a005781810151838201526020016109e8565b50505050905090810190601f168015610a2d5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001208152506003836040518082805190602001908083835b60208310610a7d5780518252601f199092019160209182019101610a5e565b51815160209384036101000a6000190180199092169116179052920194855250604051938490038101909320845181546001600160a01b0319166001600160a01b039091161781558484015180519194610adf94506001860193500190610ec1565b506040820151816002015590505060016002836040518082805190602001908083835b60208310610b215780518252601f199092019160209182019101610b02565b51815160209384036101000a60001901801990921691161790529201948552506040519384900381018420805460ff19169515159590951790945550508351600192600392869290918291908401908083835b60208310610b935780518252601f199092019160209182019101610b74565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604051968790038101909620875460018181018a556000998a5297909820815460039099020180546001600160a01b0319166001600160a01b039099169890981788558087018054919897610c279750888101965090946002918316150290920116049050610f3f565b50600291820154910155505050565b60006002826040518082805190602001908083835b60208310610c6a5780518252601f199092019160209182019101610c4b565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff16949350505050565b6001546000906060908310610cec576040805162461bcd60e51b815260206004820152600c60248201526b392e22f9a821fa2da2b9e56360a21b604482015290519081900360640190fd5b6000546001600160a01b03163314610d3a576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b600060018481548110610d4957fe5b600091825260209182902060039091020180546001808301805460408051601f60026000199685161561010002969096019093169490940491820187900487028401870190528083529395506001600160a01b0390921693919290918391908301828280156108425780601f1061081757610100808354040283529160200191610842565b806040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610e0f578181015183820152602001610df7565b50505050905090810190601f168015610e3c5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003836040518082805190602001908083835b60208310610e895780518252601f199092019160209182019101610e6a565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092206002019290925550505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f0257805160ff1916838001178555610f2f565b82800160010185558215610f2f579182015b82811115610f2f578251825591602001919060010190610f14565b50610f3b929150610fb4565b5090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f785780548555610f2f565b82800160010185558215610f2f57600052602060002091601f016020900482015b82811115610f2f578254825591600101919060010190610f99565b61068791905b80821115610f3b5760008155600101610fba56fea2646970667358221220d58fd7f25a7952d20141a962115dcf426ac37274bb848f7a5f8374e3fce55e2d64736f6c63430006040033";
      
    • 我们定义的function名称

      	public static final String FUNC_CHECKREGISTER = "checkRegister";
      
      	public static final String FUNC_DOLOGIN = "doLogin";
      
      	public static final String FUNC_GETALLUSERINFOS = "getAllUserInfos";
      
      	public static final String FUNC_GETTOTALUSERNUM = "getTotalUserNum";
      
      	public static final String FUNC_GETUSERINFOBYUSERNAME = "getUserInfoByUserName";
      
      	public static final String FUNC_REGISTER = "register";
      
      	public static final String FUNC_UPDATEPASSWORD = "updatePassword";
      
    • 合约部署函数

        public static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials,
      			ContractGasProvider contractGasProvider) {
      		return deployRemoteCall(UserManagerment.class, web3j, credentials, contractGasProvider, BINARY, "");
      	}
      
      	public static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,
      			ContractGasProvider contractGasProvider) {
      		return deployRemoteCall(UserManagerment.class, web3j, transactionManager, contractGasProvider, BINARY, "");
      	}
      
      	@Deprecated
      	public static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice,
      			BigInteger gasLimit) {
      		return deployRemoteCall(UserManagerment.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
      	}
      
      	@Deprecated
      	public static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,
      			BigInteger gasPrice, BigInteger gasLimit) {
      		return deployRemoteCall(UserManagerment.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
      	}
      
    • 合约加载函数

      	@Deprecated
      	public static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,
      			BigInteger gasPrice, BigInteger gasLimit) {
      		return new UserManagerment(contractAddress, web3j, credentials, gasPrice, gasLimit);
      	}
      
      	@Deprecated
      	public static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,
      			BigInteger gasPrice, BigInteger gasLimit) {
      		return new UserManagerment(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
      	}
      
      	public static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,
      			ContractGasProvider contractGasProvider) {
      		return new UserManagerment(contractAddress, web3j, credentials, contractGasProvider);
      	}
      
      	public static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,
      			ContractGasProvider contractGasProvider) {
      		return new UserManagerment(contractAddress, web3j, transactionManager, contractGasProvider);
      	}
      
    • 自定义的函数

      public RemoteCall<TransactionReceipt> checkRegister(String userName) {
      		final Function function = new Function(FUNC_CHECKREGISTER,
      				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),
      				Collections.<TypeReference<?>>emptyList());
      		return executeRemoteCallTransaction(function);
      	}
      
      	public RemoteCall<TransactionReceipt> doLogin(String userName, String password) {
      		final Function function = new Function(FUNC_DOLOGIN,
      				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),
      						new org.web3j.abi.datatypes.Utf8String(password)),
      				Collections.<TypeReference<?>>emptyList());
      		return executeRemoteCallTransaction(function);
      	}
      
      	public RemoteCall<TransactionReceipt> getAllUserInfos(BigInteger index) {
      		final Function function = new Function(FUNC_GETALLUSERINFOS,
      				Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(index)),
      				Collections.<TypeReference<?>>emptyList());
      		return executeRemoteCallTransaction(function);
      	}
      
      	public RemoteCall<TransactionReceipt> getTotalUserNum() {
      		final Function function = new Function(FUNC_GETTOTALUSERNUM, Arrays.<Type>asList(),
      				Collections.<TypeReference<?>>emptyList());
      		return executeRemoteCallTransaction(function);
      	}
      
      	public RemoteCall<TransactionReceipt> getUserInfoByUserName(String userName) {
      		final Function function = new Function(FUNC_GETUSERINFOBYUSERNAME,
      				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),
      				Collections.<TypeReference<?>>emptyList());
      		return executeRemoteCallTransaction(function);
      	}
      
      	public RemoteCall<TransactionReceipt> register(String ethAddr, String userName, String password) {
      		final Function function = new Function(FUNC_REGISTER,
      				Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(ethAddr),
      						new org.web3j.abi.datatypes.Utf8String(userName),
      						new org.web3j.abi.datatypes.Utf8String(password)),
      				Collections.<TypeReference<?>>emptyList());
      		return executeRemoteCallTransaction(function);
      	}
      
      	public RemoteCall<TransactionReceipt> updatePassword(String userName, String newPwd) {
      		final Function function = new Function(FUNC_UPDATEPASSWORD,
      				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),
      						new org.web3j.abi.datatypes.Utf8String(newPwd)),
      				Collections.<TypeReference<?>>emptyList());
      		return executeRemoteCallTransaction(function);
      	}
      
  3. 自定义的函数介绍

    举例说明:

    	public RemoteCall<TransactionReceipt> checkRegister(String userName) {
    		final Function function = new Function(FUNC_CHECKREGISTER,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    

    这是自动生成的Java代码,但是好像有些问题。因为我们在合约中定义的函数是有返回值的。

    • RemoteCall

      <> 中是函数的返回类型>

      TransactionReceipt 表示返回的是区块的信息

    • Function()中有三个参数

      函数名称输入参数返回值

    • return executeRemoteCallTransaction(function)

      表示执行远程调用,返回的是 TransactionReceipt

  4. 开始修改

    举例:

    	public RemoteCall<TransactionReceipt> checkRegister(String userName) {
    		final Function function = new Function(FUNC_CHECKREGISTER,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    

    合约中我们定义的是返回Bool值,所以要修改如下几处内容:

    在这里插入图片描述

    修改后内容:

    	public RemoteCall<Bool> checkRegister(String userName) {
    		final Function function = new Function(FUNC_CHECKREGISTER,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),
    				Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {
    				}));
    		return executeRemoteCallSingleValueReturn(function);
    	}
    
  5. 修改后完整代码

    package com.mao.contract;
    
    import java.math.BigInteger;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    import org.web3j.abi.TypeReference;
    import org.web3j.abi.datatypes.Address;
    import org.web3j.abi.datatypes.Bool;
    import org.web3j.abi.datatypes.Function;
    import org.web3j.abi.datatypes.Type;
    import org.web3j.abi.datatypes.Utf8String;
    import org.web3j.abi.datatypes.generated.Uint256;
    import org.web3j.crypto.Credentials;
    import org.web3j.protocol.Web3j;
    import org.web3j.protocol.core.RemoteCall;
    import org.web3j.protocol.core.methods.response.TransactionReceipt;
    import org.web3j.tx.Contract;
    import org.web3j.tx.TransactionManager;
    import org.web3j.tx.gas.ContractGasProvider;
    
    public class UserManagerment extends Contract {
    	private static final String BINARY = "608060405234801561001057600080fd5b50600080546001600160a01b03191633179055611004806100326000396000f3fe608060405234801561001057600080fd5b506004361061007d5760003560e01c80638d59cc021161005b5780638d59cc021461030e578063a847666214610449578063a8917388146104ed578063cdfbb3511461050a5761007d565b8063787b50e8146100825780637fcee2761461009c57806382e2ec2a146101d1575b600080fd5b61008a610633565b60408051918252519081900360200190f35b610140600480360360208110156100b257600080fd5b810190602081018135600160201b8111156100cc57600080fd5b8201836020820111156100de57600080fd5b803590602001918460018302840111600160201b831117156100ff57600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061068a945050505050565b60405180836001600160a01b03166001600160a01b0316815260200180602001828103825283818151815260200191508051906020019080838360005b8381101561019557818101518382015260200161017d565b50505050905090810190601f1680156101c25780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b6102fa600480360360408110156101e757600080fd5b810190602081018135600160201b81111561020157600080fd5b82018360208201111561021357600080fd5b803590602001918460018302840111600160201b8311171561023457600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b81111561028657600080fd5b82018360208201111561029857600080fd5b803590602001918460018302840111600160201b831117156102b957600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610853945050505050565b604080519115158252519081900360200190f35b6104476004803603606081101561032457600080fd5b6001600160a01b038235169190810190604081016020820135600160201b81111561034e57600080fd5b82018360208201111561036057600080fd5b803590602001918460018302840111600160201b8311171561038157600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156103d357600080fd5b8201836020820111156103e557600080fd5b803590602001918460018302840111600160201b8311171561040657600080fd5b91908080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525092955061094c945050505050565b005b6102fa6004803603602081101561045f57600080fd5b810190602081018135600160201b81111561047957600080fd5b82018360208201111561048b57600080fd5b803590602001918460018302840111600160201b831117156104ac57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610c36945050505050565b6101406004803603602081101561050357600080fd5b5035610ca1565b6104476004803603604081101561052057600080fd5b810190602081018135600160201b81111561053a57600080fd5b82018360208201111561054c57600080fd5b803590602001918460018302840111600160201b8311171561056d57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295949360208101935035915050600160201b8111156105bf57600080fd5b8201836020820111156105d157600080fd5b803590602001918460018302840111600160201b831117156105f257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600092019190915250929550610dce945050505050565b600080546001600160a01b03163314610682576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b506001545b90565b600060606002836040518082805190602001908083835b602083106106c05780518252601f1990920191602091820191016106a1565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff1691506107449050576040805162461bcd60e51b815260206004820152601860248201527fe69caae69fa5e588b0e8afa5e794a8e688b7e4bfa1e681af0000000000000000604482015290519081900360640190fd5b60006003846040518082805190602001908083835b602083106107785780518252601f199092019160209182019101610759565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604080519788900382018820805460018281018054601f600293821615909a029097019096160496870184900484028a01840190925285895298506001600160a01b031696919550909350849291508301828280156108425780601f1061081757610100808354040283529160200191610842565b820191906000526020600020905b81548152906001019060200180831161082557829003601f168201915b505050505090509250925050915091565b6000816040516020018080602001828103825283818151815260200191508051906020019080838360005b8381101561089657818101518382015260200161087e565b50505050905090810190601f1680156108c35780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003846040518082805190602001908083835b602083106109105780518252601f1990920191602091820191016108f1565b51815160209384036101000a60001901801990921691161790529201948552506040519384900301909220600201549290921495945050505050565b61095582610c36565b1561099f576040805162461bcd60e51b8152602060048201526015602482015274e794a8e688b7e5b7b2e7bb8fe6b3a8e5868cefbc8160581b604482015290519081900360640190fd5b6040518060600160405280846001600160a01b03168152602001838152602001826040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610a005781810151838201526020016109e8565b50505050905090810190601f168015610a2d5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001208152506003836040518082805190602001908083835b60208310610a7d5780518252601f199092019160209182019101610a5e565b51815160209384036101000a6000190180199092169116179052920194855250604051938490038101909320845181546001600160a01b0319166001600160a01b039091161781558484015180519194610adf94506001860193500190610ec1565b506040820151816002015590505060016002836040518082805190602001908083835b60208310610b215780518252601f199092019160209182019101610b02565b51815160209384036101000a60001901801990921691161790529201948552506040519384900381018420805460ff19169515159590951790945550508351600192600392869290918291908401908083835b60208310610b935780518252601f199092019160209182019101610b74565b518151600019602094850361010090810a82019283169219939093169190911790925294909201968752604051968790038101909620875460018181018a556000998a5297909820815460039099020180546001600160a01b0319166001600160a01b039099169890981788558087018054919897610c279750888101965090946002918316150290920116049050610f3f565b50600291820154910155505050565b60006002826040518082805190602001908083835b60208310610c6a5780518252601f199092019160209182019101610c4b565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092205460ff16949350505050565b6001546000906060908310610cec576040805162461bcd60e51b815260206004820152600c60248201526b392e22f9a821fa2da2b9e56360a21b604482015290519081900360640190fd5b6000546001600160a01b03163314610d3a576040805162461bcd60e51b815260206004820152600c60248201526b74cecf7359caf4575ff4cbd760a11b604482015290519081900360640190fd5b600060018481548110610d4957fe5b600091825260209182902060039091020180546001808301805460408051601f60026000199685161561010002969096019093169490940491820187900487028401870190528083529395506001600160a01b0390921693919290918391908301828280156108425780601f1061081757610100808354040283529160200191610842565b806040516020018080602001828103825283818151815260200191508051906020019080838360005b83811015610e0f578181015183820152602001610df7565b50505050905090810190601f168015610e3c5780820380516001836020036101000a031916815260200191505b5092505050604051602081830303815290604052805190602001206003836040518082805190602001908083835b60208310610e895780518252601f199092019160209182019101610e6a565b51815160209384036101000a600019018019909216911617905292019485525060405193849003019092206002019290925550505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f0257805160ff1916838001178555610f2f565b82800160010185558215610f2f579182015b82811115610f2f578251825591602001919060010190610f14565b50610f3b929150610fb4565b5090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10610f785780548555610f2f565b82800160010185558215610f2f57600052602060002091601f016020900482015b82811115610f2f578254825591600101919060010190610f99565b61068791905b80821115610f3b5760008155600101610fba56fea2646970667358221220d58fd7f25a7952d20141a962115dcf426ac37274bb848f7a5f8374e3fce55e2d64736f6c63430006040033";
    
    	public static final String FUNC_CHECKREGISTER = "checkRegister";
    
    	public static final String FUNC_DOLOGIN = "doLogin";
    
    	public static final String FUNC_GETALLUSERINFOS = "getAllUserInfos";
    
    	public static final String FUNC_GETTOTALUSERNUM = "getTotalUserNum";
    
    	public static final String FUNC_GETUSERINFOBYUSERNAME = "getUserInfoByUserName";
    
    	public static final String FUNC_REGISTER = "register";
    
    	public static final String FUNC_UPDATEPASSWORD = "updatePassword";
    
    	@Deprecated
    	protected UserManagerment(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice,
    			BigInteger gasLimit) {
    		super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
    	}
    
    	protected UserManagerment(String contractAddress, Web3j web3j, Credentials credentials,
    			ContractGasProvider contractGasProvider) {
    		super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
    	}
    
    	@Deprecated
    	protected UserManagerment(String contractAddress, Web3j web3j, TransactionManager transactionManager,
    			BigInteger gasPrice, BigInteger gasLimit) {
    		super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    	}
    
    	protected UserManagerment(String contractAddress, Web3j web3j, TransactionManager transactionManager,
    			ContractGasProvider contractGasProvider) {
    		super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);
    	}
    
    	public static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials,
    			ContractGasProvider contractGasProvider) {
    		return deployRemoteCall(UserManagerment.class, web3j, credentials, contractGasProvider, BINARY, "");
    	}
    
    	public static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,
    			ContractGasProvider contractGasProvider) {
    		return deployRemoteCall(UserManagerment.class, web3j, transactionManager, contractGasProvider, BINARY, "");
    	}
    
    	@Deprecated
    	public static RemoteCall<UserManagerment> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice,
    			BigInteger gasLimit) {
    		return deployRemoteCall(UserManagerment.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
    	}
    
    	@Deprecated
    	public static RemoteCall<UserManagerment> deploy(Web3j web3j, TransactionManager transactionManager,
    			BigInteger gasPrice, BigInteger gasLimit) {
    		return deployRemoteCall(UserManagerment.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
    	}
    	
    	/**************************************** 自定义的函数 ********************************************************/
    	public RemoteCall<Bool> checkRegister(String userName) {
    		final Function function = new Function(FUNC_CHECKREGISTER,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),
    				Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {
    				}));
    		return executeRemoteCallSingleValueReturn(function);
    	}
    
    	public RemoteCall<Bool> doLogin(String userName, String password) {
    		final Function function = new Function(FUNC_DOLOGIN,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),
    						new org.web3j.abi.datatypes.Utf8String(password)),
    				Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {
    				}));
    		return executeRemoteCallSingleValueReturn(function);
    	}
    
    	public RemoteCall<List<Type>> getAllUserInfos(int index) {
    		final Function function = new Function(FUNC_GETALLUSERINFOS,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(index)),
    				Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {
    				}, new TypeReference<Utf8String>() {
    				}));
    		return executeRemoteCallMultipleValueReturn(function);
    	}
    
    	public RemoteCall<Uint256> getTotalUserNum() {
    		final Function function = new Function(FUNC_GETTOTALUSERNUM, Arrays.<Type>asList(),
    				Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {
    				}));
    		return executeRemoteCallSingleValueReturn(function);
    	}
    
    	public RemoteCall<List<Type>> getUserInfoByUserName(String userName) {
    		final Function function = new Function(FUNC_GETUSERINFOBYUSERNAME,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName)),
    				Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {
    				}, new TypeReference<Utf8String>() {
    				}));
    		return executeRemoteCallMultipleValueReturn(function);
    	}
    
    	public RemoteCall<TransactionReceipt> register(String ethAddr, String userName, String password) {
    		final Function function = new Function(FUNC_REGISTER,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(ethAddr),
    						new org.web3j.abi.datatypes.Utf8String(userName),
    						new org.web3j.abi.datatypes.Utf8String(password)),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    
    	public RemoteCall<TransactionReceipt> updatePassword(String userName, String newPwd) {
    		final Function function = new Function(FUNC_UPDATEPASSWORD,
    				Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(userName),
    						new org.web3j.abi.datatypes.Utf8String(newPwd)),
    				Collections.<TypeReference<?>>emptyList());
    		return executeRemoteCallTransaction(function);
    	}
    	/**************************************** END ********************************************************/
    	
    	@Deprecated
    	public static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,
    			BigInteger gasPrice, BigInteger gasLimit) {
    		return new UserManagerment(contractAddress, web3j, credentials, gasPrice, gasLimit);
    	}
    
    	@Deprecated
    	public static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,
    			BigInteger gasPrice, BigInteger gasLimit) {
    		return new UserManagerment(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    	}
    
    	public static UserManagerment load(String contractAddress, Web3j web3j, Credentials credentials,
    			ContractGasProvider contractGasProvider) {
    		return new UserManagerment(contractAddress, web3j, credentials, contractGasProvider);
    	}
    
    	public static UserManagerment load(String contractAddress, Web3j web3j, TransactionManager transactionManager,
    			ContractGasProvider contractGasProvider) {
    		return new UserManagerment(contractAddress, web3j, transactionManager, contractGasProvider);
    	}
    }
    
    

部署和调用

启动私链

(不再介绍,看之前记录)

部署合约

在这里插入图片描述

Consts.java

public class Consts {
	// GAS价格
	public static BigInteger GAS_PRICE = BigInteger.valueOf(2000_000_000L);
	// GAS上限
	public static BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000L);
	// 交易费用
	public static BigInteger GAS_VALUE = BigInteger.valueOf(100_000L);;
	// 账户密码
	public static String PASSWORD = "123";
	// 账户文件路径
	public static String PATH = "E:/BlockChain/node5/data/keystore/UTC--2020-04-11T07-54-54.678177700Z--12d4e53d6f017a2a62807876ec41fc97a0f60a71";
	// 合约地址,第一次部署之后记录下来
	public static String ADDRESS = "0x2e2d63186093b543d9f84a3f8343c59a3047718f";
	// chain id,在创世区块中定义的
	public static byte CHAINID = (byte) 666;
}

DeployedContract.java

public class DeployedContract {

	public static void main(String[] args) throws Exception {
		// 1. 默认连接到 http://localhost:8545/
		Web3j web3j = Web3j.build(new HttpService());
		// 2. 获取凭证
		Credentials credentials = WalletUtils.loadCredentials(Consts.PASSWORD, Consts.PATH);
		// 3.部署合约
		UserManagerment contract = UserManagerment.deploy(web3j, credentials, Consts.GAS_PRICE, Consts.GAS_LIMIT).send();
		// 4.获得合约地址
		System.out.println(contract.getContractAddress());
	}

}

在这里插入图片描述

调用合约

Main.java

public class Main {
	public static void main(String[] args) throws Exception {
		// 1.默认连接到 http://localhost:8545/
		Web3j web3j = Web3j.build(new HttpService());
		// 2.获取凭证
		Credentials credentials = WalletUtils.loadCredentials(Consts.PASSWORD, Consts.PATH);
		// 3.加载合约
		UserManagerment contract = UserManagerment.load(Consts.ADDRESS, web3j, credentials, Consts.GAS_PRICE,
				Consts.GAS_LIMIT);
		// 4.调用合约
		// 检查注册
		Boolean isRegister = contract.checkRegister("张三").send().getValue();
		System.out.println("用户名‘张三’是否被注册:" + isRegister);
		// 注册
		contract.register("0x2e2d63186093b543d9f84a3f8343c59a3047718f", "张三", "123").send();
		isRegister = contract.checkRegister("张三").send().getValue();
		System.out.println("用户名‘张三’是否被注册:" + isRegister);
		// 登录
		Boolean canLogin = contract.doLogin("张三", "123").send().getValue();
		if (canLogin) {
			System.out.println("登录成功");
		} else {
			System.out.println("登录失败");
		}
		// 获得用户信息
		System.out.println("张三信息如下:");
		List<Type> userInfo = contract.getUserInfoByUserName("张三").send();
		for (Type info : userInfo) {
			System.out.println(info.toString());
		}
		// 系统用户人数
		int userNum = contract.getTotalUserNum().send().getValue().intValue();
		System.out.println("总共的用户数:" + userNum);
		// 输出所有用户信息
		for (int i = 0; i < userNum; i++) {
			List<Type> user = contract.getAllUserInfos(i).send();
			for (Type info : user) {
				System.out.println(info.toString());
			}
		}
		// 5.关闭连接
		web3j.shutdown();
	}
}

运行结果:

在这里插入图片描述

最后

以上内容纯属学习记录!!!

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
作为一名AI语言模型,我并不具备开发fabric智能合约的实践能力,但我可以给你一些基本信息。 fabric智能合约是一种用于Hyperledger Fabric平台的智能合约,它可以用于定义和管理各种资产,并提供了一个安全的、可编程的方式来管理这些资产的交易和状态。在fabric智能合约的开发中,需要使用一种称为链代码(chaincode)的程序来实现智能合约逻辑。链代码是一种独立运行的程序,它可以被安装在fabric网络的节点上,并与其他链代码共同协作完智能合约的执行。 在fabric智能合约开发中,需要使用一些工具和技术,如: 1. Hyperledger Fabric SDK:提供了一种编写、测试和部署fabric智能合约的API。 2. Docker:用于打包和部署链代码,以及构建fabric网络。 3. Go或Java:链代码可以用Go或Java等编程语言编写。 4. VS Code或其他编辑器:用于编写和调试链代码。 在开发fabric智能合约时,需要遵循一些最佳实践,如: 1. 在编写链代码时,需要考虑安全性和可靠性问题,以确保合约的正确执行。 2. 链代码应该尽可能简单,以便于维护和升级。 3. 在编写链代码时,需要考虑性能和可扩展性问题,以应对不断增长的交易量。 4. 在测试链代码时,需要尽可能模拟真实的环境,并采用自动化测试工具来提高测试效率。 总之,fabric智能合约开发需要掌握一些基本的工具和技术,同时需要遵循一些最佳实践,以确保合约的安全、可靠和高效执行。
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值