bread-wallet开源钱包

出于安全考虑,有很多锁屏和输入pin码的操作,在7.0手机上总是异常,官方也推荐8.0手机及以上,所以特意从同事那里借来了8.1的vivo手机,能完整地跑起来了,我还是希望能关注到btc的逻辑,尤其是助记词部分,研究了半天发现很多逻辑都在BRKeyStore文件的getPhrase里,

public static byte[] getPhrase(final Context context, int requestCode) throws UserNotAuthenticatedException {
        Log.d("chendy","getPhrase开始生成助记词bytes开始  requestCode:"+requestCode);
        if (PostAuth.mAuthLoopBugHappened) {
            showLoopBugMessage(context);
            throw new UserNotAuthenticatedException();
        }
        AliasObject obj = ALIAS_OBJECT_MAP.get(PHRASE_ALIAS);
        byte[] result=getData(context, obj.mAlias, obj.mDatafileName, obj.mIvFileName, requestCode);
        Log.d("chendy","getPhrase开始生成助记词bytes结束");
        return result;
    }

但getData方法里大部分都是android的KeyStore、Cipher之类的系统加密接口,难道android封装好了这类加密接口,甚至助记词都有接口了吗?不应该吧,不是要用到bitcoinJ吗?发现它还是到本地保存的地方get去了,也就是在其它地方生成的,那么是在哪里生成的呢?

public static byte[] retrieveEncryptedData(Context ctx, String name) {
        SharedPreferences pref = ctx.getSharedPreferences(KEY_STORE_PREFS_NAME, Context.MODE_PRIVATE);
        String base64 = pref.getString(name, null);
        Log.d("chendy","retrieveEncryptedData 存在了SharedPreferences里 "+name+" base64:"+base64);
        return base64 == null ? null : Base64.decode(base64, Base64.DEFAULT);
    }

第一次启动,在滑动三张引导页之后点击"跳过",会调用如下方法,生成账户信息,并以base64保存起来

public void onCreateWalletAuth(final Context context, boolean authAsked, AuthenticationSuccessListener listener) {
        boolean success = WalletsMaster.getInstance().generateRandomSeed(context);
    }

整个的助记词逻辑都在下面的方法里了,generatePaperKey是jni方法,除了c代码高效还有能防止算法的反编译,但开源啊,jni这块不熟悉啊,另外只能生成12位,如何生成15位助记词呢?

 public synchronized boolean generateRandomSeed(final Context ctx) {
         SecureRandom sr = new SecureRandom();
        String languageCode = Locale.getDefault().getLanguage();
        Log.d("chendy", "PhraseUtils generateRandomSeed languageCode " + languageCode);//zh
        List<String> wordList = Bip39Reader.getBip39Words(ctx, "zh-Hant");

        final String[] words = wordList.toArray(new String[wordList.size()]);
        final byte[] randomSeed = sr.generateSeed(RANDOM_SEED_LENGTH);
        if (words.length != PHRASE_WORDS_LIST_LENGTH) {
            BRReportsManager.reportBug(new IllegalArgumentException("the list is wrong, size: " + words.length), true);
            return false;
        }
        if (randomSeed.length != RANDOM_SEED_LENGTH)
            throw new NullPointerException("failed to create the seed, seed length is not 128: " + randomSeed.length);
        byte[] paperKeyBytes = BRCoreMasterPubKey.generatePaperKey(randomSeed, words);
        if (paperKeyBytes == null || paperKeyBytes.length == 0) {
            BRReportsManager.reportBug(new NullPointerException("failed to encodeSeed"), true);
            return false;
        }
        for (int i = 0; i < paperKeyBytes.length; i++) {//这儿是字节
           // Log.d("chendy", "paperKeyBytes " + paperKeyBytes[i]);//zh
        }
        String[] splitPhrase = new String(paperKeyBytes).split(" ");//这儿就是12个助记词了
        if (splitPhrase.length != PHRASE_LENGTH) {
            BRReportsManager.reportBug(new NullPointerException("phrase does not have 12 words:" + splitPhrase.length + ", lang: " + languageCode), true);
            return false;
        }
        for (int i = 0; i < splitPhrase.length; i++) {//这儿就是12个助记词了
            Log.d("chendy", "splitPhrase " + splitPhrase[i]);//zh
        }
        Log.d("chendy", "Mnemonic " + WHITESPACE_SPLITTER.splitToList(Arrays.toString(splitPhrase)));//zh
        Log.d("chendy", "Mnemonic "+Arrays.toString(splitPhrase).replace(",",""));//zh
        return true;
    }

下一篇我封装了java方法,可以设置12位、15位

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、不安全的随机数生成,在CSRF TOKEN生成、password reset token生成等,会造成toke...
    nightmare丿阅读 9,157评论 0 1
  • 以OWASP移动应用安全认证标准和移动安全测试指导为基础,这个检测列表是为了设计、测试和发行安全的Android应...
    Ireliaaa阅读 4,758评论 0 1
  • 东西有点多,但是资源绝对nice,自己都全部亲身体验过了,大家可放心使用 github排名: https://gi...
    Rance935阅读 13,889评论 26 312
  • 好久没写文章了,最近也比较偷懒,今天继续讨论我实际开发中遇到的需求,那就是关于APP解锁,大家都知道。现在越来越多...
    青蛙要fly阅读 8,365评论 2 26
  • 梦想是什么? 对于小时候的我们来说,可能是自己想要的东西,想要享受的美好。比如想要像邻居家的哥哥或者姐姐一样,有好...
    唐家七妹阅读 1,430评论 0 2