iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android 使用Web3j构建以太钱包
  • 559
分享到

Android 使用Web3j构建以太钱包

Web钱包Android 2022-06-06 12:06:06 559人浏览 独家记忆
摘要

Web3j的引用直接通过咱们平时导入各个引用的方式 implementation 'org.WEB3j:core:3.3.1-Android' 这样导入还需要导入multi

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

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


作者:景高旭


--结束END--

本文标题: Android 使用Web3j构建以太钱包

本文链接: https://www.lsjlt.com/news/28547.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作