-
Notifications
You must be signed in to change notification settings - Fork 26
/
leadger-service.js
57 lines (53 loc) · 1.64 KB
/
leadger-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
import 'babel-polyfill';
import { timeout } from 'promise-timeout';
import Web3 from 'web3';
import ProviderEngine from 'web3-provider-engine';
import FetchSubprovider from 'web3-provider-engine/subproviders/fetch';
import HWTransportNodeHid from '@ledgerhq/hw-transport-node-hid';
import Web3SubProvider from '@ledgerhq/web3-subprovider';
import { chainId } from 'common/config';
import { SELECTED_SERVER_URL } from './web3-service';
export class LedgerService {
constructor({ web3Service }) {
this.web3Service = web3Service;
}
createWeb3(accountsOffset, accountsQuantity) {
accountsOffset = accountsOffset || 0;
accountsQuantity = accountsQuantity || 6;
const engine = new ProviderEngine();
const getTransport = () => HWTransportNodeHid.create();
const ledger = Web3SubProvider(getTransport, {
networkId: chainId,
accountsLength: accountsQuantity,
accountsOffset
});
engine.addProvider(ledger);
engine.addProvider(new FetchSubprovider({ rpcUrl: SELECTED_SERVER_URL }));
engine.start();
return new Web3(engine);
}
signTransaction(args) {
args.dataToSign.from = args.address;
let signPromise = this.web3Service.waitForTicket({
method: 'signTransaction',
args: [args.dataToSign],
contractAddress: null,
contractMethod: null,
onceListenerName: null,
customWeb3: this.createWeb3()
});
return timeout(signPromise, 30000);
}
getAccounts(args) {
return this.web3Service.waitForTicket({
method: 'getAccounts',
args: [],
contractAddress: null,
contractMethod: null,
onceListenerName: null,
customWeb3: this.createWeb3(args.start, args.quantity)
});
}
}
export default LedgerService;