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 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位

你可能感兴趣的:(bread-wallet开源钱包)