区块链开发之生成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#L573private 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() { } }總結
以上是生活随笔為你收集整理的区块链开发之生成12个助记词的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 职场12年模拟沙盘心得及回顾
- 下一篇: 光电接收的TIA设计