Android 使用Web3j构建以太钱包

本文详细介绍使用Web3j库在Android环境下连接以太坊网络,包括配置私有链链接、创建及查询钱包、查看余额、创建钱包文件、查询钱包信息以及执行转账操作的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Web3j的引用直接通过咱们平时导入各个引用的方式

 implementation 'org.web3j:core:3.3.1-android'

这样导入还需要导入multidex 因为导入你会发现报65535所以还需要导入

  implementation 'com.android.support:multidex:1.+'

进下来创建以太的链接

//用**代替的可以选择ETH的链接地址 如

https://rinkeby.infura.io/zmd7VgRt9go0x6qlJ2Mk

当然也可以链接你们创建的私有链,如创建私有链,请进行百度不难只是时间问题。 

    //创建链接以太服务
        HttpService httpService = new HttpService("http://123.58.******");
        //创建Web3j
        web3j = Web3jFactory.build(httpService);
        //创建Admin
        build = AdminFactory.build(httpService);

链接是否成功我们可以所以版本进行查看。

private String getVersion() throws ExecutionException, InterruptedException {
        //链接
        //发送给请求获取版本   同步请求
        // result = web3j.web3ClientVersion().send().getResult();
        //发送异步请求
        String result = null;
            result = web3j.web3ClientVersion().sendAsync().get().getWeb3ClientVersion();
        return result;
    }

 以上为了更好的阅读我直接将异常捕获抛到上层,并且发送还分同步和异步。

同步 ----------------------------  send();

异步 ---------------------------- senAsync();

查看钱包余额

  BigInteger latest = web3j.ethGetBalance("需要查看的地址", DefaultBlockParameterName.LATEST).sendAsync().get().getBalance();

 创建钱包

  walletFileName = WalletUtils.generateNewWalletFile("设置你钱包的密码最哈记住因为转账时候会用到", new File(getFilesDir().getPath()), false);
  Log.e("TAS", walletFileName);

   创建钱包最好将你的钱包名字打印出来因为查询钱包的时候会用到

查询钱包信息

    String filePath = getFilesDir().getPath() + "钱包的名字咋们那会让打印的walletFileName字段.json";
        try {
            credentials = WalletUtils.loadCredentials("创建钱包的密码这边进行比对密码错无法查到", filePath);
            //获取创建的钱包地址
            String address = credentials.getAddress();
            //获取key
            BigInteger publicKey = credentials.getEcKeyPair().getPublicKey();
            //获取私有的key
            BigInteger privateKey = credentials.getEcKeyPair().getPrivateKey();

转账

Future<TransactionReceipt> transactionReceiptFuture = Transfer.sendFunds(web3j, credentials, to, BigDecimal.ONE, Convert.Unit.FINNEY).sendAsync();
//获取交易哈希
                    String transactionHash = transactionReceiptFuture.get().getTransactionHash();
  • web3j实体
  • Credentials 源账户
  • address 转出地址
  • value 数量
  • uint 单位

感兴趣的朋友可以看看只能合约

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值