日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/31 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 区块链开发之生成12个助记词 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我最近封裝了一個庫,使用起來更簡單,大家可以移步這里:Bip44確定性算法的android實現(xiàn)
Java版的庫:Bip44確定性算法的Java實現(xiàn)庫(Android和java平臺都可以使用)

這里添加一下SecureRandomUtils類的代碼,
注:如果你引入了web3j的庫,就不需要自己在項目中添加此類,手動添加此類,里面的LinuxSecureRandom類,可以使用bitcoinj庫下的,具體路徑為:org.bitcoinj.crypto.LinuxSecureRandom

這里使用bitcoinj庫,來實現(xiàn)生成bip39的12個助記詞,引用庫

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

填坑1

如果你直接引用庫之后,直接安裝運行apk,會造成app崩潰,這是因為這個庫里面有一個libscrypt.dylib,這個庫是針對x86_64平臺的,并且沒有其他平臺的這個庫,所以在arm cpu平臺的手機app會崩潰,解決方案就是在gradle的android節(jié)點下,加上以下配置

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這個構造函數(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工具類

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个助记词的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。