区块链开发之生成12个助记词
我最近封裝了一個(gè)庫(kù),使用起來(lái)更簡(jiǎn)單,大家可以移步這里:Bip44確定性算法的android實(shí)現(xiàn)
Java版的庫(kù):Bip44確定性算法的Java實(shí)現(xiàn)庫(kù)(Android和java平臺(tái)都可以使用)
這里添加一下SecureRandomUtils類(lèi)的代碼,
注:如果你引入了web3j的庫(kù),就不需要自己在項(xiàng)目中添加此類(lèi),手動(dòng)添加此類(lèi),里面的LinuxSecureRandom類(lèi),可以使用bitcoinj庫(kù)下的,具體路徑為:org.bitcoinj.crypto.LinuxSecureRandom
這里使用bitcoinj庫(kù),來(lái)實(shí)現(xiàn)生成bip39的12個(gè)助記詞,引用庫(kù)
implementation ‘org.bitcoinj:bitcoinj-core:0.14.7’
填坑1
如果你直接引用庫(kù)之后,直接安裝運(yùn)行apk,會(huì)造成app崩潰,這是因?yàn)檫@個(gè)庫(kù)里面有一個(gè)libscrypt.dylib,這個(gè)庫(kù)是針對(duì)x86_64平臺(tái)的,并且沒(méi)有其他平臺(tái)的這個(gè)庫(kù),所以在arm cpu平臺(tái)的手機(jī)app會(huì)崩潰,解決方案就是在gradle的android節(jié)點(diǎn)下,加上以下配置
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個(gè)隨機(jī)的單詞,就要用到里面的MnemonicCode類(lèi),但是這個(gè)類(lèi),默認(rèn)會(huì)new一個(gè)實(shí)例出來(lái),并且默認(rèn)加載的單詞庫(kù)路徑方式是不支持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;}可以從代碼中看出來(lái),在android平臺(tái)此路徑是絕對(duì)不可用的,所以我們要手動(dòng)自己new這個(gè)對(duì)象,使用public MnemonicCode(InputStream wordstream, String wordListDigest) throws IOException, IllegalArgumentException這個(gè)構(gòu)造函數(shù)
具體實(shí)現(xiàn)代碼
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工具類(lèi)
import java.security.SecureRandom;/*** 如果你引入了web3j的庫(kù),就不需要自己在項(xiàng)目中添加此類(lèi),手動(dòng)添加此類(lèi),里面的LinuxSecureRandom類(lèi),可以使用bitcoinj庫(kù)下的,具體路徑為: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() { } }總結(jié)
以上是生活随笔為你收集整理的区块链开发之生成12个助记词的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 职场12年模拟沙盘心得及回顾
- 下一篇: 光电接收的TIA设计