如何使用Web3J创建、导入以太坊钱包

2018-12-11  本文已影响0人  NAH丶

1. 依赖导入

        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <!--<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/core-3.4.0.jar</systemPath>-->
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>crypto</artifactId>
            <version>RELEASE</version>
        </dependency>
        <!-- 用于助记词加密工具 -->
        <dependency>
            <groupId>io.github.novacrypto</groupId>
            <artifactId>BIP32derivation</artifactId>
            <version>0.0.2</version>
        </dependency>

        <dependency>
            <groupId>io.github.novacrypto</groupId>
            <artifactId>BIP39</artifactId>
            <version>0.1.9</version>
        </dependency>

        <dependency>
            <groupId>io.github.novacrypto</groupId>
            <artifactId>BIP44</artifactId>
            <version>0.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.bitcoinj</groupId>
            <artifactId>bitcoinj-core</artifactId>
            <version>0.14.6</version>
            <scope>compile</scope>
        </dependency>

2.创建钱包

钱包可以创建助记词或非助记词钱包;钱包创建在以太坊地址上会生成八个路径上的不同钱包地址;这里选择"m/44'/60'/0'/0/0";也是市面上大部分钱包所选择的:

    /**
    *生成的keyStore文本的存储位置
    */
    @Value("${wallet.keyStore.path}")
    private String filePath;
    /**
     * 通用的以太坊基于bip44协议的助记词路径 (imtoken jaxx Metamask myetherwallet)
     */
    private static String ETH_TYPE = "m/44'/60'/0'/0/0";

    private static SecureRandom secureRandom = new SecureRandom();

生成无助记词钱包:

     /**
     * 生成eth钱包 保存对应的keyStore(无助记词方式)
     * @param password
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchProviderException
     * @throws InvalidAlgorithmParameterException
     * @throws CipherException
     * @throws IOException
     */
    public String createWallet(String password) throws NoSuchAlgorithmException, NoSuchProviderException,
            InvalidAlgorithmParameterException, CipherException, IOException {
        File file = new File(filePath);
        String fileName = WalletUtils.generateFullNewWalletFile(password, file);
        System.out.println(fileName);
        return  fileName;
    }

阅读更多

上一篇下一篇

猜你喜欢

热点阅读