11import validator from './validator' ;
2- import { addHexPrefix , clearHexPrefix , formatAddress , formatKey , toBuffer , toHex , toNumber } from '../common/formatter' ;
3- import { decryptKeystoreToPkey , pkeyToKeystore } from './keystore' ;
4- import { privateToAddress , privateToPublic , publicToAddress , sha3 , hashPersonalMessage , ecsign } from 'ethereumjs-util' ;
5- import { mnemonictoPrivatekey } from './mnemonic' ;
6- import { generateMnemonic } from 'bip39' ;
7- import { trimAll } from '../common/utils' ;
2+ import {
3+ addHexPrefix ,
4+ clearHexPrefix ,
5+ formatAddress ,
6+ formatKey ,
7+ toBuffer ,
8+ toHex ,
9+ toNumber
10+ } from '../common/formatter' ;
11+ import { decryptKeystoreToPkey , pkeyToKeystore } from './keystore' ;
12+ import {
13+ privateToAddress ,
14+ privateToPublic ,
15+ publicToAddress ,
16+ sha3 ,
17+ hashPersonalMessage ,
18+ ecsign
19+ } from 'ethereumjs-util' ;
20+ import { mnemonictoPrivatekey } from './mnemonic' ;
21+ import { generateMnemonic } from 'bip39' ;
22+ import { trimAll } from '../common/utils' ;
823import HDKey from 'hdkey' ;
924import EthTransaction from 'ethereumjs-tx' ;
10- import { getOrderHash } from '../relay/rpc/order' ;
25+ import { getOrderHash } from '../relay/rpc/order' ;
1126import * as Trezor from './trezor' ;
1227import * as Ledger from './ledger' ;
1328import * as MetaMask from './metaMask' ;
1429import Wallet from 'ethereumjs-wallet' ;
1530
1631const wallets = require ( '../config/wallets.json' ) ;
17- const LoopringWallet = wallets . find ( wallet => trimAll ( wallet . name ) . toLowerCase ( ) === 'loopringwallet' ) ;
32+ const LoopringWallet = wallets . find (
33+ wallet => trimAll ( wallet . name ) . toLowerCase ( ) === 'loopringwallet' ) ;
1834export const path = LoopringWallet . dpath ;
1935
2036export function createWallet ( )
@@ -73,8 +89,10 @@ export function getAddresses ({publicKey, chainCode, pageSize, pageNum})
7389{
7490 const addresses = [ ] ;
7591 const hdk = new HDKey ( ) ;
76- hdk . publicKey = publicKey instanceof Buffer ? publicKey : toBuffer ( addHexPrefix ( publicKey ) ) ;
77- hdk . chainCode = chainCode instanceof Buffer ? chainCode : toBuffer ( addHexPrefix ( chainCode ) ) ;
92+ hdk . publicKey = publicKey instanceof Buffer ? publicKey : toBuffer (
93+ addHexPrefix ( publicKey ) ) ;
94+ hdk . chainCode = chainCode instanceof Buffer ? chainCode : toBuffer (
95+ addHexPrefix ( chainCode ) ) ;
7896 for ( let i = 0 ; i < pageSize ; i ++ )
7997 {
8098 const dkey = hdk . derive ( `m/${ i + pageSize * pageNum } ` ) ;
@@ -144,6 +162,26 @@ export function fromKeystore (keystore, password)
144162 return fromPrivateKey ( privateKey ) ;
145163}
146164
165+ export async function fromLedger ( dpath )
166+ {
167+ const response = await Ledger . connect ( ) ;
168+ if ( response . result )
169+ {
170+ return new LedgerAccount ( response . result , dpath ) ;
171+ }
172+ throw new Error ( response . error . message ) ;
173+ }
174+
175+ export function fromTrezor ( dpath )
176+ {
177+ return new TrezorAccount ( dpath ) ;
178+ }
179+
180+ export function fromMetaMask ( web3 )
181+ {
182+ return new MetaMaskAccount ( web3 ) ;
183+ }
184+
147185/**
148186 * @description generate mnemonic
149187 * @param strength
@@ -394,7 +432,8 @@ export class LedgerAccount extends Account
394432 async signOrder ( order )
395433 {
396434 const hash = getOrderHash ( order ) ;
397- const result = await Ledger . signMessage ( this . dpath , clearHexPrefix ( toHex ( hash ) ) , this . ledger ) ;
435+ const result = await Ledger . signMessage ( this . dpath ,
436+ clearHexPrefix ( toHex ( hash ) ) , this . ledger ) ;
398437 if ( result . error )
399438 {
400439 throw new Error ( result . error . message ) ;
@@ -420,7 +459,8 @@ export class MetaMaskAccount extends Account
420459
421460 getAddress ( )
422461 {
423- if ( this . web3 && this . web3 . eth . accounts [ 0 ] ) return this . web3 . eth . accounts [ 0 ] ;
462+ if ( this . web3 &&
463+ this . web3 . eth . accounts [ 0 ] ) return this . web3 . eth . accounts [ 0 ] ;
424464 else throw new Error ( 'Not found MetaMask' ) ;
425465 }
426466
@@ -452,7 +492,8 @@ export class MetaMaskAccount extends Account
452492
453493 async signEthereumTx ( rawTx )
454494 {
455- const result = await MetaMask . signEthereumTx ( this . web3 , this . account , rawTx ) ;
495+ const result = await MetaMask . signEthereumTx ( this . web3 , this . account ,
496+ rawTx ) ;
456497 if ( ! result . error )
457498 {
458499 return result . result ;
0 commit comments