风险提示:理性看待区块链,提高风险意识!
简单使用以太坊Web3j
首页 > 币界资讯 > 区块链知识 2019-02-21 12:00:00

1、创建钱包

  1. 生成助记词
    import org.web3j.crypto.MnemonicUtils;
    import static org.web3j.crypto.SecureRandomUtils.secureRandom;
    
    private String generateMnemonics() {
        byte[] initialEntropy = new byte[16];
        secureRandom().nextBytes(initialEntropy);
        String mnemonic = MnemonicUtils.generateMnemonic(initialEntropy);
    }
    
  2. 生成种子
    //如果使用密码可能与大部分钱包不兼容
    byte[] seed = MnemonicUtils.generateSeed(mnemonic, password)
    
  3. 生成密钥对和地址
    import org.web3j.utils.Numeric;
    import static org.web3j.crypto.Hash.sha256;
    import org.web3j.crypto.Keys;
    
    ECKeyPair ecKeyPair = ECKeyPair.create(sha256(seed));
    String priKeyWithPrefix = Numeric.toHexStringWithPrefix(ecKeyPair.getPrivateKey());
    String pubKeyWithPrefix = Numeric.toHexStringWithPrefix(ecKeyPair.getPublicKey());
    
    //根据公钥或者ECKeyPair获取钱包地址
    String address = Keys.getAddress(ecKeyPair);
    String address = Keys.getAddress(pubKeyWithPrefix);
    
  4. 生成钱包文件
      WalletFile walletFile = Wallet.createLight(password, keyPair);
    

以上所有流程,在 Web3j 最新版本中被整合进一个方法中

Bip39Wallet wallet = WalletUtils.generateBip39Wallet(password, destinationDirectory)

Bip39Wallet 是一个纯 POJO 对象,包含了钱包文件和助记词。

在 BIP39 基础上,Web3j 同时提供了对 BIP44 的支持

Bip39Wallet wallet = Bip44WalletUtils.generateBip44Wallet(password, destinationDirectory)

2、恢复钱包

  • 私钥恢复
    ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(mPrivateKey,16));
    
  • 助记词恢复通过助记词得到种子 然后再得到公私钥,可参考前面 2-3步
  • 钱包文件恢复
    ECKeyPair ecKeyPair = Wallet.decrypt("密码", WalletFile );
    

3、交易

//Web3j web3b = Web3j.build(new HttpService("https://api.myetherapi.com/eth"));
Web3j web3b = Web3j.build(new HttpService());  // defaults to http://localhost:8545/

//Credentials credentials = WalletUtils.loadCredentials(password, "/path/to/walletfile");
Credentials credentials = WalletUtils.loadBip39Credentials(password, mnemonic)

TransactionReceipt transactionReceipt = Transfer.sendFunds(web3b, credentials, "0x<address>|<ensName>",BigDecimal.valueOf(1.0), Convert.Unit.ETHER).send();
上一篇: 学术向丨将大象压缩到忽略不计,FlyClient如何让区块链变得超轻
下一篇: MuSig : 比特币可用的Schnorr签名方案
推荐专栏
web3首席知识博主
一位相信价值投资的币圈KOL。稳定盈利的缠论野生交易员 #BTC行情分析师 #价值投资 #链上数据分析
爱Web 3,爱生活,爱科技,爱炒币的老韭菜
热门币种
更多
币种
价格
24H涨跌幅
BTC比特币
¥264,810.46
37,103.37 USDT
+0.1%
ETH以太坊
¥14,413.37
2,019.50 USDT
0%
USDT泰达币
¥7.20
1.01 USDT
0%
BNB币安币
¥1,629.54
228.32 USDT
+0.63%
XRP瑞波币
¥4.32
0.60480 USDT
+0.45%
USDC
¥7.14
1.00 USDT
+0.04%
SOLSolana
¥400.45
56.11 USDT
+1.59%
OKBOK币
¥399.37
55.96 USDT
-1.23%
ADA艾达币
¥2.67
0.37480 USDT
-1.32%
DOGE狗狗币
¥0.55330
0.07753 USDT
-1.01%
热搜币种
更多
币种
价格
24H涨跌幅
Terra Classic
¥0.00
9.481E-5 USDT
-18.15%
Gala
¥0.18
0.025383 USDT
-4.86%
dYdX
¥22.63
3.1984 USDT
-0.73%
比特股
¥0.05
0.006569 USDT
-1.93%
PancakeSwap
¥15.60
2.2054 USDT
-2.16%
Conflux
¥1.08
0.1531 USDT
-2.36%
Filecoin
¥31.55
4.4597 USDT
-0.5%
FTX Token
¥29.65
4.1911 USDT
+15.24%
Yield Guild Games
¥2.56
0.3615 USDT
-0.55%
Shiba Inu
¥0.00
8.15E-6 USDT
-2.16%
比特币
¥262,467.38
37103.37 USDT
+0.1%
比原链
¥0.07
0.010022 USDT
-4.68%
最新快讯
更多
汇丰、恒生、渣打、富邦华一四家外资银行入围首批“数字人民币”业务试点名单
2023-11-28 19:06:57
摩根大通和Apollo计划建立代币化“企业主网”
2023-11-28 19:03:57
Nansen2公测版本上线,新增链上数据异动、智能搜索等功能
2023-11-28 18:59:52
西班牙公民需在明年3月底前申报其海外平台上加密货币持仓
2023-11-28 18:53:43
Nansen2已公开测试
2023-11-28 18:53:38
dYdX基金会:主网启动以来超过1645万DYDX被质押
2023-11-28 18:52:07
NicCarter等比特币倡导者发文:比特币挖矿是清洁能源和平衡电网的关键工具
2023-11-28 18:47:58
下载币界网APP