Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

imToken开源 核心代码分析

繁星如尘
115 0 0
##Token Core7 `- t( }5 u# {$ K
TokenCore is a blockchain library. TokenCore provides the relatively consistent API that allows you to manage your wallets and sign transactions in BTC, ETH and EOS chains simultaneously. In addition, TokenCore introduces the concept of ‘identity’, you can use the same mnemonic to manage wallets on the three chains.
0 K2 n4 F- Y/ K& @##Installation3 h1 q( m1 f. x  j8 H
Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:
$ e1 V# [& G. p7 K" @allprojects {: j- w" F7 b9 B- f
                repositories {
( E0 ?3 a, x7 c- P* Q2 C" S: C* w                        ...6 A, B! l$ Y; d: o$ \# }9 d
                        maven { url 'https://jitpack.io' }
. t2 X  R0 W5 _5 y1 W* \                }
' O, q2 i/ z1 E" I- d, X        }8 N/ ?* W4 w" p4 t/ R: w- i
Step 2. Add the dependency: G9 ~3 [# `1 v. m) n, I0 h
dependencies {
# [& R: s$ D4 m: e% ~        implementation 'com.github.consenlabs:token-core-android:v0.1'
# a/ E( |& H2 o+ B# T+ q7 L9 o2 FStep 3. Add the JAVA8 support to your build.gradle+ `3 N8 A3 G/ ]. c
android {/ H& m  V1 O$ L! r- ]% X
    ……/ [. t+ L% u4 ^- t7 |
    compileOptions {
$ x& i- L  x1 T/ T5 R, |        sourceCompatibility JavaVersion.VERSION_1_84 l" M* \) d3 G( L% r
        targetCompatibility JavaVersion.VERSION_1_86 R" `' K+ @7 k, _# ?5 H, P
    }; A: W. E4 o5 T
   ……4 x& {) v0 ~& _. X* y
}8 O& M2 }. l, `7 F: P% ?1 N! M
##Try the API$ L1 x2 l( S- ^- G8 {% [* o4 q
Init the storage to store the keystore file
) H* N/ r: d0 U" d2 @5 a2 xpublic class MainActivity extends AppCompatActivity implements KeystoreStorage {* H+ p' u* c9 ^7 \
    @Override
. S* Q6 ~4 W# U% y1 e    protected void onCreate(Bundle savedInstanceState) {5 f& R( \+ s9 v
        super.onCreate(savedInstanceState);
6 A! w) S8 h3 a( @) ?" Q        setContentView(R.layout.activity_main);8 T! I4 i$ I) M5 W' R& ?
        WalletManager.storage = this;
- J  g$ p! @( }2 V0 Z- z1 o, e        WalletManager.scanWallets();
; |% t& ?1 ~2 P1 ?3 }    }
: O+ i: z; ^3 @3 \; S  H, V    public File getKeystoreDir() {
+ C/ G" V- e; Y1 l( ]        return this.getFilesDir();
) a$ M2 ^) v9 F2 s$ U6 h    }( H6 `; ]- }1 y$ g  g; G& H
}! d! B5 k- x& t; V7 M
Create new Identity and derive the eth, btc wallets
6 P; N3 r% j  A8 U, P2 g// You should create or recover Identity first before you create other wallets- P) K9 E& n8 @1 ?
// The last param, Metadata.P2WPKH means that the derived btc wallet is a SegWit wallet; {$ U. D0 e  W4 e; i. I
Identity identity = Identity.createIdentity("MyFirstIdentity", SampleKey.PASSWORD, SampleKey.PASSWORD_HINT, Network.MAINNET, Metadata.P2WPKH);
2 F5 f+ k$ n/ l2 A* k- ?Wallet ethereumWallet = identity.getWallets().get(0);
' w$ J. m, ^+ u9 ]Wallet bitcoinWallet = identity.getWallets().get(1);
  c1 ^* b4 P" R) [Export Wallet+ h2 |. \1 i1 E: o
String prvKey = WalletManager.exportPrivateKey(ethereumWallet.getId(), SampleKey.PASSWORD);
) _" W9 \( O' Y5 KSystem.out.println(String.format("PrivateKey: %s", prvKey));+ r3 P3 f1 w7 o6 `3 ^
String mnemonic = WalletManager.exportMnemonic(ethereumWallet.getId(), SampleKey.PASSWORD).getMnemonic();/ E4 K  y5 I/ T% r5 i: P' t
System.out.println(String.format("Mnemonic: %s", mnemonic));
% U  t4 B6 N+ _( Q6 gString json = WalletManager.exportKeystore(ethereumWallet.getId(), SampleKey.PASSWORD);. }" B/ E5 O; U& Y' n
System.out.println(String.format("Keystore: %s", json));: X* Z4 U5 ^8 R/ n
// output:! m3 O9 @+ d' \7 h0 K
// PrivateKey: f653be3f639f45ea1ed3eb152829b6d881ce62257aa873891e06fa9569a8d9aa8 W: ^) H3 L7 ?
// Mnemonic: tide inmate cloud around wise bargain celery cement jungle melody galaxy grocery" b% A2 [7 b3 @5 `8 G% |! a( z
// Keystore: {"id":"c7575eba-3ae3-4cc3-86ba-2eb9c6839cad","version":3,"crypto":{"ciphertext":"7083ba3dd5470ba4be4237604625e05fa6b668954d270beb848365cbf6933ec5","mac":"f4f9ea8d42ff348b11fc146c396da446cc975309b3538e08a58c0b218bddd15d","cipher":"aes-128-ctr","cipherparams":{"iv":"db3f523faf4da4f1c6edcd7bc1386879"},"kdf":"pbkdf2","kdfparams":{"dklen":32,"c":10240,"prf":"hmac-sha256","salt":"0ce830e9f888dfe33c31e6cfc444d6f588161c9d4128d4066ee5dfdcbc5d0079"}},"address":"4a1c2072ac67b616e5c578fd9e2a4d30e0158471"}
: E9 x  _; B6 N7 }4 G: f0 W6 {##Copyright and License
% H  C2 o2 \2 R8 r4 B) W/ `3 _ Copyright 2018 imToken PTE. LTD.
1 y- f* V% ^3 k" O" ^  Licensed under the Apache License, Version 2.0 (the "License");
7 c8 ~9 Q$ i7 X/ o/ i1 R  D, R  you may not use this file except in compliance with the License.
6 P( R( W( |- y  You may obtain a copy of the License at
! Y$ Z( z8 f; S3 K- G    http://www.apache.org/licenses/LICENSE-2.0
" d- O+ o5 o$ Y. m  Unless required by applicable law or agreed to in writing, software
9 ]) Z$ |( y; J2 J. _  distributed under the License is distributed on an "AS IS" BASIS,
/ ^" d: `( I& r) W/ P& A! d  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.' a: q  n8 E: x
  See the License for the specific language governing permissions and  R: T0 y$ e" e0 k
  limitations under the License.
; E6 Y( v& Z. shttps://github.com/consenlabs/token-core-android#token-core
标签: imToken
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

繁星如尘 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2