-
Notifications
You must be signed in to change notification settings - Fork 97
/
ledger.js
132 lines (127 loc) · 4.41 KB
/
ledger.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { app, ipcMain } from 'electron'; // eslint-disable-line import/no-extraneous-dependencies
import Lisk from 'lisk-elements'; // eslint-disable-line import/no-extraneous-dependencies
import { LedgerAccount, SupportedCoin, DposLedger } from 'dpos-ledger-api'; // eslint-disable-line import/no-extraneous-dependencies
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid'; // eslint-disable-line import/no-extraneous-dependencies
import win from './modules/win';
// mock transportnodehid, todo fix this to work also on windows and linux
// const TransportNodeHid = {
// open: () => {},
// listen: () => {},
// setListenDevicesDebounce: () => {},
// setListenDevicesPollingSkip: () => {},
// };
// TransportNodeHid.setListenDevicesDebounce(200);
/*
TransportNodeHid.setListenDevicesDebug((msg, ...args) => {
console.log(msg);
console.log({
type: 'listenDevices',
args,
});
});
*/
let busy = false;
TransportNodeHid.setListenDevicesPollingSkip(() => busy);
let ledgerPath = null;
const getLedgerAccount = (index = 0) => {
const ledgerAccount = new LedgerAccount();
ledgerAccount.coinIndex(SupportedCoin.LISK);
ledgerAccount.account(index);
return ledgerAccount;
};
const isValidAddress = address => address.length > 2 && address.length < 22 && address[address.length - 1] === 'L';
const isInsideLedgerApp = async (path) => {
try {
const transport = await TransportNodeHid.open(path);
const liskLedger = new DposLedger(transport);
const ledgerAccount = getLedgerAccount(0);
const liskAccount = await liskLedger.getPubKey(ledgerAccount);
transport.close();
return isValidAddress(liskAccount.address);
} catch (e) {
return false;
}
};
const ledgerObserver = {
next: async ({ device, type }) => {
if (device) {
if (type === 'add') {
if (process.platform !== 'linux' || await isInsideLedgerApp(device.path)) {
ledgerPath = device.path;
win.send({ event: 'ledgerConnected', value: null });
}
} else if (type === 'remove') {
if (ledgerPath) {
ledgerPath = null;
win.send({ event: 'ledgerDisconnected', value: null });
}
}
}
},
};
let observableListen = null;
const syncDevices = () => {
try {
observableListen = TransportNodeHid.listen(ledgerObserver);
} catch (e) {
ledgerPath = null;
syncDevices();
}
};
syncDevices();
app.on('will-quit', () => {
if (observableListen) {
observableListen.unsubscribe();
observableListen = null;
}
});
const getBufferToHex = buffer => Lisk.cryptography.bufferToHex(buffer);
const createCommand = (k, fn) => {
ipcMain.on(`${k}.request`, (event, ...args) => {
fn(...args)
.then(r => ({ success: true, data: r }))
.catch(e => ({ success: false, errorKey: e }))
.then(r => event.sender.send(`${k}.result`, r));
});
};
// eslint-disable-next-line arrow-body-style
createCommand('ledgerCommand', (command) => {
if (ledgerPath) {
return TransportNodeHid.open(ledgerPath)
.then(async (transport) => { // eslint-disable-line max-statements
busy = true;
try {
const liskLedger = new DposLedger(transport);
const ledgerAccount = getLedgerAccount(command.data.index);
let commandResult;
if (command.action === 'GET_ACCOUNT') {
commandResult = await liskLedger.getPubKey(ledgerAccount);
}
if (command.action === 'SIGN_MSG') {
const signature = await liskLedger.signMSG(ledgerAccount, command.data.message);
commandResult = getBufferToHex(signature.slice(0, 64));
}
if (command.action === 'SIGN_TX') {
commandResult = await liskLedger.signTX(ledgerAccount, command.data.tx, false);
}
transport.close();
busy = false;
return Promise.resolve(commandResult);
} catch (err) {
transport.close();
busy = false;
if (err.statusText && err.statusText === 'CONDITIONS_OF_USE_NOT_SATISFIED') {
return Promise.reject(new Error('LEDGER_ACTION_DENIED_BY_USER'));
}
return Promise.reject(new Error('LEDGER_ERR_DURING_CONNECTION'));
}
})
.catch((e) => {
if (typeof e === 'string') {
return Promise.reject(e);
}
return Promise.reject(new Error('LEDGER_ERR_DURING_CONNECTION'));
});
}
return Promise.reject(new Error('LEDGER_IS_NOT_CONNECTED'));
});