Hi 游客


比特池塘 区块链技术 正文

imToken开源 核心代码分析

89 0 0
##Token Core
1 |, P) R: M2 kTokenCore 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.
* L  ~& {, N6 h! C3 v; `9 L##Installation
2 |! b( g, ^. P" F6 c( W! HStep 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:
2 @- `5 r  r! g4 L4 Wallprojects {, k- T, _. @7 T% o; V7 E: S8 q
                repositories {
% @+ G4 f+ A) v& e0 [0 a$ a# d                        ...# ~" J9 N' Y# n* }# Q- s" E8 b* x6 s% l
                        maven { url 'https://jitpack.io' }- V  A, F. m; E9 H
- g( s. t, N8 }* Q! ~$ N5 c, e8 _        }
8 j( |& A" b% fStep 2. Add the dependency
0 V9 S* d, J5 y8 D; b6 q5 wdependencies {
3 t/ Z! a( W& F        implementation 'com.github.consenlabs:token-core-android:v0.1'  T4 y+ y' e" F5 F1 E( P
Step 3. Add the JAVA8 support to your build.gradle
# K; h5 F4 {8 iandroid {
* @# ?4 Z0 a3 l" z    ……% C+ Q) i1 J9 A
    compileOptions {( \  ~$ e- y; \6 ^9 O0 V" X
        sourceCompatibility JavaVersion.VERSION_1_8
8 [  m1 }" j5 h5 }  U: l' c6 {        targetCompatibility JavaVersion.VERSION_1_8
& R4 d* R4 e% I+ B    }
6 f* L, z$ r  c7 T9 r5 j7 T   ……
6 w. s3 ?1 u* ~. E$ x1 _+ f& p}7 l) u5 Z3 ^2 i1 \" o; u" J
##Try the API
5 K; x, `8 Y* ?2 T; C; MInit the storage to store the keystore file4 c6 }4 f' B# T1 v
public class MainActivity extends AppCompatActivity implements KeystoreStorage {
' i' |0 v* z, G2 l! v    @Override
, _' K! U& [0 i2 ]    protected void onCreate(Bundle savedInstanceState) {
7 j6 g8 Y6 m6 [) X& w/ x* c        super.onCreate(savedInstanceState);
2 m6 A# I. [* N        setContentView(R.layout.activity_main);) Y% l0 O9 i1 W5 p2 h; I
        WalletManager.storage = this;
" C0 E8 Z& i% \8 V8 o  G        WalletManager.scanWallets();
$ U1 h$ `. A5 ~% f! h/ w/ d1 j3 n  V    }
8 l( k, R0 c* o0 }: o0 n9 {- S) m    public File getKeystoreDir() {
8 T  F9 W( G4 D9 S; A        return this.getFilesDir();. c2 ]( |2 s) c3 }& T
1 O/ D& `& A7 ]3 j  P}
) x0 [1 M$ O2 E" h5 KCreate new Identity and derive the eth, btc wallets0 U% [$ t7 l  b
// You should create or recover Identity first before you create other wallets
1 d8 V  {3 Q. g1 B// The last param, Metadata.P2WPKH means that the derived btc wallet is a SegWit wallet) m" _) {; o* ]$ N1 x8 y) {
Identity identity = Identity.createIdentity("MyFirstIdentity", SampleKey.PASSWORD, SampleKey.PASSWORD_HINT, Network.MAINNET, Metadata.P2WPKH);
6 |* q: _& O2 }: I( }Wallet ethereumWallet = identity.getWallets().get(0);
! v4 Y  J( U' g% ]0 J9 R7 Q4 HWallet bitcoinWallet = identity.getWallets().get(1);, z+ R6 H$ @2 f& E1 h. y
Export Wallet
& z1 \, p) N9 MString prvKey = WalletManager.exportPrivateKey(ethereumWallet.getId(), SampleKey.PASSWORD);4 G) G4 C* N% M: U9 O, J
System.out.println(String.format("PrivateKey: %s", prvKey));( w: G+ D- z1 Z' t4 n! {  ~
String mnemonic = WalletManager.exportMnemonic(ethereumWallet.getId(), SampleKey.PASSWORD).getMnemonic();% q5 l' o3 j& h( O: f& e/ G
System.out.println(String.format("Mnemonic: %s", mnemonic));+ W' n* o. z7 U2 ]. j( U
String json = WalletManager.exportKeystore(ethereumWallet.getId(), SampleKey.PASSWORD);  \$ f2 }( j/ x: z# n# n
System.out.println(String.format("Keystore: %s", json));7 l% f) B+ b: R) G* q: {; G, q
// output:4 Q% M& |; j* E8 ?! U
// PrivateKey: f653be3f639f45ea1ed3eb152829b6d881ce62257aa873891e06fa9569a8d9aa0 e( ~: o# |2 P7 D+ V" U" y
// Mnemonic: tide inmate cloud around wise bargain celery cement jungle melody galaxy grocery8 ~% ?# ?0 [# p/ Y9 _0 `8 a
// 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"}: o' \9 c% D0 h1 P5 f" T
##Copyright and License
7 w4 f* W+ n. M. F2 P0 p1 ~8 t Copyright 2018 imToken PTE. LTD.3 l# l) @/ _; B5 \; K
  Licensed under the Apache License, Version 2.0 (the "License");  ?& X; F7 D. o' m
  you may not use this file except in compliance with the License.  G* `( o8 F8 q* N% p( Z6 ^
  You may obtain a copy of the License at$ T: |/ x0 F2 k5 h9 E6 l/ C& K
    http://www.apache.org/licenses/LICENSE-2.0; G3 q0 e, z5 `, @/ F6 A3 J6 {
  Unless required by applicable law or agreed to in writing, software4 ~: @- P3 m# x  z0 |
  distributed under the License is distributed on an "AS IS" BASIS,
, x) u+ I9 s* x! `! F2 g9 _% [* z  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.& e6 d* t* U! ?# l! c
  See the License for the specific language governing permissions and# t# b& |( c/ b9 W; u: Y
  limitations under the License.% E% _. S* Z7 V
标签: imToken
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
您需要登录后才可以回帖 登录 | 立即注册



繁星如尘 小学生
  • 粉丝


  • 关注


  • 主题