Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

imToken开源 核心代码分析

繁星如尘
99 0 0
##Token Core
- a( E7 l5 Z8 v; A9 y% OTokenCore 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.
1 J" m7 ^2 E0 ?/ V##Installation
0 c& `! \7 L, p0 }; vStep 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:, N) |! @3 C' b) t
allprojects {" e( d, \" E+ ?% j5 @
                repositories {
& B) K: D1 \" m% V/ F                        ...- d6 ^5 E  q- z% h. U: _- }
                        maven { url 'https://jitpack.io' }
- E/ c- T8 J+ @* E+ e4 ]( y; O                }
/ W  H6 \6 b! k, A% F        }
  s* a/ m6 A$ s9 q1 QStep 2. Add the dependency
/ M5 {+ y) L. c/ b: {, x, ~dependencies {" \7 B% }4 f# t' Z! R# K' h
        implementation 'com.github.consenlabs:token-core-android:v0.1'
8 j5 Y  ?' |2 C8 x& L- wStep 3. Add the JAVA8 support to your build.gradle3 `9 V7 b. U8 Y$ r, ?
android {4 n& v! w6 K1 r5 d/ Y
    ……3 h: a  B7 Y' h1 E0 L
    compileOptions {
7 p+ q  |2 c2 q        sourceCompatibility JavaVersion.VERSION_1_8
* Y- r* m& M* U+ Q! f        targetCompatibility JavaVersion.VERSION_1_8
6 w6 z5 p, G# ?/ [" Z1 \& Q: G) f. q    }
& ^1 x7 b6 }  k; E' t& I# R   ……7 e9 I+ e$ t$ J3 z
}0 h& }5 T7 n7 N4 K2 o$ H2 |- s
##Try the API
, h% A0 U/ Z( ^5 k0 u$ Y" }Init the storage to store the keystore file
3 c1 k  h7 {" vpublic class MainActivity extends AppCompatActivity implements KeystoreStorage {
" p3 u+ f# q5 @# f" d* V    @Override
  @0 K2 w7 e. M/ }# r: c    protected void onCreate(Bundle savedInstanceState) {  b- C3 y2 {5 s) \
        super.onCreate(savedInstanceState);& S" M# [: U+ a1 D( t/ i6 ?2 v% _6 @
        setContentView(R.layout.activity_main);
+ d) ^* V6 n6 J' x1 Z% r        WalletManager.storage = this;, P3 i% b' I' w- E
        WalletManager.scanWallets();
! N2 _6 T) N" f6 B8 d& k% s    }
: i1 z) D2 P8 D' R- G    public File getKeystoreDir() {4 v) `9 Y- a% r7 p
        return this.getFilesDir();
0 l; g7 y0 X; J& l, M4 G: ]$ i    }
+ t5 l8 U3 _. V- W}
: |5 ^+ F4 z6 ?) W! qCreate new Identity and derive the eth, btc wallets
/ p% _5 A  f  K8 X// You should create or recover Identity first before you create other wallets
+ j+ N$ r. E5 v// The last param, Metadata.P2WPKH means that the derived btc wallet is a SegWit wallet
- e/ X8 t9 o; @5 L( L, b0 KIdentity identity = Identity.createIdentity("MyFirstIdentity", SampleKey.PASSWORD, SampleKey.PASSWORD_HINT, Network.MAINNET, Metadata.P2WPKH);2 j, V8 {, _& S1 r/ u7 c0 v
Wallet ethereumWallet = identity.getWallets().get(0);
$ {% k4 W* L% k! z" YWallet bitcoinWallet = identity.getWallets().get(1);7 |; ]  ^0 k, |6 P) z
Export Wallet
# o, \8 O" N+ I  z8 \, _3 RString prvKey = WalletManager.exportPrivateKey(ethereumWallet.getId(), SampleKey.PASSWORD);
+ p; D6 O# k" a' k6 v" ^) TSystem.out.println(String.format("PrivateKey: %s", prvKey));
" ~6 V4 J9 O: i' w8 D5 iString mnemonic = WalletManager.exportMnemonic(ethereumWallet.getId(), SampleKey.PASSWORD).getMnemonic();" l6 z1 J" s2 r( h2 L( g
System.out.println(String.format("Mnemonic: %s", mnemonic));% o! c4 V6 u# j  ^
String json = WalletManager.exportKeystore(ethereumWallet.getId(), SampleKey.PASSWORD);
& v6 E. H9 P& S0 ]5 _2 ~* vSystem.out.println(String.format("Keystore: %s", json));
9 Y: z; \! F& u- ~, O// output:
7 M# ]! g6 S! u" J4 Q// PrivateKey: f653be3f639f45ea1ed3eb152829b6d881ce62257aa873891e06fa9569a8d9aa5 E3 O% y1 _) X- j1 T+ i
// Mnemonic: tide inmate cloud around wise bargain celery cement jungle melody galaxy grocery
! U# z: ~/ e# A  n; q4 G" Y// 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"}
; e# h" g% M8 r5 {$ d3 g0 _##Copyright and License
& |; j1 K0 N: z' o% v Copyright 2018 imToken PTE. LTD.
1 D! S; p& p3 F3 Q5 f, j' x4 W  Licensed under the Apache License, Version 2.0 (the "License");" a* F) l3 m/ U$ L
  you may not use this file except in compliance with the License." ?6 s5 Z" h) f% d$ e# X
  You may obtain a copy of the License at
8 d& S2 `1 Q* X    http://www.apache.org/licenses/LICENSE-2.0/ C- w+ ^+ i  Q5 ?: x; {  c
  Unless required by applicable law or agreed to in writing, software- H/ Z8 Y: a2 n. k% D- d
  distributed under the License is distributed on an "AS IS" BASIS,& r% u" z* h7 g. {, }# v
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
$ G% G3 r7 V8 Q- p# ]1 G  See the License for the specific language governing permissions and1 [: l* J/ N/ c' L" t4 e% f$ M3 O
  limitations under the License.0 Q) t# g8 v7 B" p# |8 U1 {
https://github.com/consenlabs/token-core-android#token-core
标签: imToken
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

繁星如尘 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    2