区块链开发之生成12个助记词

我最近封装了一个库,使用起来更简单,大家可以移步这里:Bip44确定性算法的android实现
Java版的库:Bip44确定性算法的Java实现库(Android和java平台都可以使用)

这里添加一下SecureRandomUtils类的代码,
注:如果你引入了web3j的库,就不需要自己在项目中添加此类,手动添加此类,里面的LinuxSecureRandom类,可以使用bitcoinj库下的,具体路径为:org.bitcoinj.crypto.LinuxSecureRandom

这里使用bitcoinj库,来实现生成bip39的12个助记词,引用库

implementation ‘org.bitcoinj:bitcoinj-core:0.14.7’

填坑1

如果你直接引用库之后,直接安装运行apk,会造成app崩溃,这是因为这个库里面有一个libscrypt.dylib,这个库是针对x86_64平台的,并且没有其他平台的这个库,所以在arm cpu平台的手机app会崩溃,解决方案就是在gradle的android节点下,加上以下配置

 packagingOptions {
        exclude 'lib/x86_64/darwin/libscrypt.dylib'
        exclude 'com/google/thirdparty/publicsuffix/PublicSuffixPatterns.gwt.xml'
        exclude 'com/google/thirdparty/publicsuffix/PublicSuffixType.gwt.xml'
        exclude 'org/bitcoinj/crypto/mnemonic/wordlist/english.txt'
        exclude 'org/bitcoinj/crypto/cacerts'
    }

填坑2

我们要得到12个随机的单词,就要用到里面的MnemonicCode类,但是这个类,默认会new一个实例出来,并且默认加载的单词库路径方式是不支持android的,官方代码如下

   static {
        try {
            INSTANCE = new MnemonicCode();
        } catch (FileNotFoundException e) {
            // We expect failure on Android. The developer has to set INSTANCE themselves.
            if (!Utils.isAndroidRuntime())
                log.error("Could not find word list", e);
        } catch (IOException e) {
            log.error("Failed to load word list", e);
        }
    }
 /** Initialise from the included word list. Won't work on Android. */
    public MnemonicCode() throws IOException {
        this(openDefaultWords(), BIP39_ENGLISH_SHA256);
    }

    private static InputStream openDefaultWords() throws IOException {
        InputStream stream = MnemonicCode.class.getResourceAsStream(BIP39_ENGLISH_RESOURCE_NAME);
        if (stream == null)
            throw new FileNotFoundException(BIP39_ENGLISH_RESOURCE_NAME);
        return stream;
    }

可以从代码中看出来,在android平台此路径是绝对不可用的,所以我们要手动自己new这个对象,使用public MnemonicCode(InputStream wordstream, String wordListDigest) throws IOException, IllegalArgumentException这个构造函数

具体实现代码

    public static void generateMnemonic(Context context) throws Exception {
        MnemonicCode mnemonicCode = new MnemonicCode(context.getAssets().open("english.txt"), null);

        SecureRandom secureRandom = SecureRandomUtils.secureRandom();
        byte[] initialEntropy = new byte[16];//算法需要,必须是被4整除
        secureRandom.nextBytes(initialEntropy);
        List<String> wd = mnemonicCode.toMnemonic(initialEntropy);
        if (wd == null || wd.size() != 12)
            throw new RuntimeException("generate word error");
        else {
            words.clear();
            words.addAll(wd);
            LogUtils.d(words.toString());
        }
    }

SecureRandomUtils工具类



import java.security.SecureRandom;

/**
 * 如果你引入了web3j的库,就不需要自己在项目中添加此类,手动添加此类,里面的LinuxSecureRandom类,可以使用bitcoinj库下的,具体路径为:org.bitcoinj.crypto.LinuxSecureRandom
 * Utility class for working with SecureRandom implementation.
 *
 * <p>This is to address issues with SecureRandom on Android. For more information, refer to the
 * following <a href="https://github.com/web3j/web3j/issues/146">issue</a>.
 */
final public class SecureRandomUtils {

    private static final SecureRandom SECURE_RANDOM;

    static {
        if (isAndroidRuntime()) {
            new LinuxSecureRandom();
        }
        SECURE_RANDOM = new SecureRandom();
    }

    public static SecureRandom secureRandom() {
        return SECURE_RANDOM;
    }

    // Taken from BitcoinJ implementation
    // https://github.com/bitcoinj/bitcoinj/blob/3cb1f6c6c589f84fe6e1fb56bf26d94cccc85429/core/src/main/java/org/bitcoinj/core/Utils.java#L573
    private static int isAndroid = -1;

    static boolean isAndroidRuntime() {
        if (isAndroid == -1) {
            final String runtime = System.getProperty("java.runtime.name");
            isAndroid = (runtime != null && runtime.equals("Android Runtime")) ? 1 : 0;
        }
        return isAndroid == 1;
    }

    private SecureRandomUtils() { }
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答1: 首先, 需要找到一个支持使用助记恢复钱包的钱包软件或网站. 然后, 按照钱包软件或网站的提示, 输入你的12助记. 根据助记, 你的钱包软件或网站就可以找到你的资产了. 需要注意的是, 助记需要按照正确的顺序输入, 不能有任何错误. 如果你输入的助记有误, 你可能会无法找到你的资产. 因此, 务必确保助记输入正确. ### 回答2: 通过12助记找到自己的资产是一种常见的钱包恢复方法,以下是具体步骤: 1. 准备好备份纸或电子设备,确保安全存储和保护。 2. 打开钱包应用或相应的区块链客户端,点击“恢复钱包”或“找回钱包”。 3. 输入钱包恢复页面,选择使用助记的方式恢复钱包。 4. 在助记输入框中,按照顺序依次填入备份的12助记。确保拼写准确并按照空格分隔。 5. 核对输入的助记是否正确,并点击“下一步”或“确认”。 6. 系统会根据助记生成相关的加密私钥和公钥。 7. 完成恢复后,钱包应用会自动同步账户余额和交易记录。 8. 检查钱包是否正确显示了您的资产和交易历史。如果没有正确显示,可能是助记输入错误或钱包选择问题。 9. 如有遗失助记或输入错误,需要找到备份的纸质或电子文件,仔细检查每个助记,并重新尝试恢复钱包的步骤。 10. 恢复完毕后,务必将助记保存在安全的地方,并且不要将其泄露给他人,以保护资产安全。 总之,通过记住和正确使用备份的12助记,您可以方便地恢复和找到自己的资产。但务必注意保存和保护助记的安全性,避免资产丢失或被盗。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值