-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
95 lines (83 loc) · 2.9 KB
/
index.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
import { remote } from 'electron';
import { utils } from 'ethers';
import LedgerTransport from '@ledgerhq/hw-transport-node-hid';
import Eth from '@ledgerhq/hw-app-eth';
function emitEvent(windowInstance, event) {
windowInstance.webContents.send('lns-status', event);
}
export function deviceEventListener() {
const mainWindow = remote.getGlobal('mainWindow');
const sub = LedgerTransport.listen({
next: (e) => emitEvent(mainWindow, e),
error: (e) => emitEvent(mainWindow, e),
complete: (e) => emitEvent(mainWindow, e),
});
process.on('exit', () => {
sub.unsubscribe();
});
}
export async function execWalletMethods(method, params) {
const { id, path } = params;
let result;
if (method === 'getaddress') {
result = await createEthTransportActivity(id, (ethTransport) => ethTransport.getAddress(path));
}
if (method === 'getpublickey') {
result = await createEthTransportActivity(id, (ethTransport) => ethTransport.getAddress(path, false, true));
}
if (method === 'signtx') {
const { rawTxHex } = params;
result = await createEthTransportActivity(id, (ethTransport) => ethTransport.signTransaction(path, rawTxHex));
}
if (method === 'signpersonalmessage') {
const { message } = params;
result = await createEthTransportActivity(id, (ethTransport) => ethTransport.signPersonalMessage(path, message));
}
if (result) {
return { ...result, id, path };
}
return null;
}
const createEthTransportActivity = async (descriptor, activityFn) => {
let transport;
try {
if (process.platform === 'win32') {
transport = await LedgerTransport.create();
} else {
transport = await LedgerTransport.open(descriptor);
}
const ethTransport = new Eth(transport);
const result = await activityFn(ethTransport);
return result;
} finally {
if (transport) {
await transport.close();
}
}
};
export const nahmiiSdkSignMessage = async (_message) => {
const transport = await LedgerTransport.create();
const eth = new Eth(transport);
let message = _message;
if (typeof message === 'string') {
message = utils.toUtf8Bytes(_message);
}
const messageHex = utils.hexlify(message).substring(2);
return eth.signPersonalMessage("m/44'/60'/0'/0", messageHex).then((_signature) => {
const signature = { ..._signature };
signature.r = `0x${signature.r}`;
signature.s = `0x${signature.s}`;
return utils.joinSignature(signature);
});
};
export const nahmiiSdkSignTransaction = async (unresolvedTx) => {
const transport = await LedgerTransport.create();
const eth = new Eth(transport);
const tx = await utils.resolveProperties(unresolvedTx);
const serializedTx = utils.serializeTransaction(tx);
const sig = await eth.signTransaction("m/44'/60'/0'/0", serializedTx.substring(2));
sig.r = `0x${sig.r}`;
sig.s = `0x${sig.s}`;
return utils.serializeTransaction(tx, sig);
};
export const PROTOCOL_NAME = 'lns';