Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 8a959a9

Browse files
authored
Merge pull request #109 from Loopring/v2.0.0
V2.0.0
2 parents 402749e + a4d2dc0 commit 8a959a9

9 files changed

Lines changed: 1697 additions & 2611 deletions

File tree

docs/chinese.md

Lines changed: 747 additions & 1251 deletions
Large diffs are not rendered by default.

docs/index.md

Lines changed: 838 additions & 1337 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "loopring.js",
3-
"version": "1.0.8",
3+
"version": "2.0.0",
44
"description": "Loopring javascript library",
55
"scripts": {
66
"lint": "eslint . --fix",

src/WalletUtils.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
privateKeytoAddress,
3+
publicKeytoAddress,
4+
privateKeytoPublic,
5+
createMnemonic,
6+
fromMnemonic,
7+
fromKeystore,
8+
fromPrivateKey,
9+
fromLedger,
10+
fromTrezor,
11+
fromMetaMask
12+
} from './ethereum/account';
13+
14+
import { mnemonictoPrivatekey, isValidateMnemonic } from './ethereum/mnemonic';
15+
import { decryptKeystoreToPkey, pkeyToKeystore } from './ethereum/keystore';
16+
17+
export default {
18+
privateKeytoPublic,
19+
publicKeytoAddress,
20+
privateKeytoAddress,
21+
createMnemonic,
22+
isValidateMnemonic,
23+
mnemonictoPrivatekey,
24+
decryptKeystoreToPkey,
25+
fromMnemonic,
26+
fromKeystore,
27+
fromPrivateKey,
28+
fromLedger,
29+
fromTrezor,
30+
fromMetaMask,
31+
pkeyToKeystore
32+
};

src/common/formatter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export function formatKey (mixed)
123123

124124
if (typeof mixed === 'string')
125125
{
126-
return mixed.startsWith('0x') ? mixed : mixed;
126+
return mixed.startsWith('0x') ? mixed.slice(2) : mixed;
127127
}
128128
throw new Error('Unsupported type');
129129
}

src/common/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {keccak, hashPersonalMessage} from 'ethereumjs-util';
2-
import {toHex, toBig} from './formatter';
2+
import formatter, {toHex, toBig} from './formatter';
33

44
/**
55
* trim head space and tail space
@@ -30,5 +30,10 @@ export function calculateGas (gasPrice, gasLimit)
3030
}
3131

3232
export default {
33-
hashPersonalMessage
33+
hashPersonalMessage,
34+
...formatter,
35+
trim,
36+
trimAll,
37+
keccakHash,
38+
calculateGas
3439
};

src/ethereum/account.js

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
import 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';
823
import HDKey from 'hdkey';
924
import EthTransaction from 'ethereumjs-tx';
10-
import {getOrderHash} from '../relay/rpc/order';
25+
import { getOrderHash } from '../relay/rpc/order';
1126
import * as Trezor from './trezor';
1227
import * as Ledger from './ledger';
1328
import * as MetaMask from './metaMask';
1429
import Wallet from 'ethereumjs-wallet';
1530

1631
const 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');
1834
export const path = LoopringWallet.dpath;
1935

2036
export 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;

src/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import common from './common';
22
import ethereum from './ethereum';
33
import relay from './relay';
4-
4+
import WalletUtils from './WalletUtils';
5+
import ContractUtils from './ethereum/contracts/Contracts';
6+
import EthRpcUtils from './ethereum/eth';
7+
import RelayRpcUitls from './relay/relay';
8+
import SocketUtils from './relay/socket';
9+
import Utils from './common/utils';
510
export {
611
common,
712
ethereum,
8-
relay
13+
relay,
14+
WalletUtils,
15+
ContractUtils,
16+
EthRpcUtils,
17+
RelayRpcUitls,
18+
SocketUtils,
19+
Utils
920
};

src/relay/rpc/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import account from './account';
2-
import market from './market';
3-
import order from './order';
4-
import ring from './ring';
1+
import * as account from './account';
2+
import * as market from './market';
3+
import * as order from './order';
4+
import * as ring from './ring';
55

66
export default {
77
account,

0 commit comments

Comments
 (0)