forked from KhalilBellakrid/ledger-test-library-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (79 loc) · 2.89 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
96
97
98
99
100
101
const axios = require('axios')
let binding = null
function loadBinding() {
if (!binding) {
binding = require('bindings')('ledgerapp_nodejs') // eslint-disable-line global-require
}
}
loadBinding()
// -----------------------------------------------------------------------------
// NODEJS IMPLEMENTATIONS
// -----------------------------------------------------------------------------
const NJSHttpImpl = {}
NJSHttpImpl.get = (url, headers, callback) => {
const header = { method: 'get', url }
if (headers.length > 0) {
const tokenHeader = {}
headers.forEach(h => (tokenHeader[h.field] = h.value))
header.headers = tokenHeader
}
axios(header)
.then(response => callback.on_success(response.status, JSON.stringify(response.data)))
.catch(err => callback.on_network_error(err))
}
/*
NJSItfExecutionContext Implementation
*/
const NJSContextImpl = {}
NJSContextImpl.execute = runnable => runnable.run()
/*
NJSItfThreadDispatcher Implementation
*/
const NJSThreadDispatcherImpl = {
contexts: {},
}
NJSThreadDispatcherImpl.getSerialExecutionContext = name => {
let currentContext = NJSThreadDispatcherImpl.contexts[name]
if (currentContext === undefined) {
currentContext = new binding.NJSItfExecutionContext(NJSContextImpl)
NJSThreadDispatcherImpl.contexts[name] = currentContext
}
return currentContext
}
NJSThreadDispatcherImpl.getThreadPoolExecutionContext = name =>
NJSThreadDispatcherImpl.getSerialExecutionContext(name)
NJSThreadDispatcherImpl.getThreadPoolExecutionContext = name =>
NJSThreadDispatcherImpl.getMainExecutionContext(name)
NJSThreadDispatcherImpl.newLock = () => {
console.log('Not implemented') // eslint-disable-line no-console
}
const NJSTransactionListVmObserverImpl = {}
NJSTransactionListVmObserverImpl.on_update = () => {}
const makeApi = () => {
const LGHttp = new binding.NJSItfHttp(NJSHttpImpl)
const LGThreadDispatcher = new binding.NJSItfThreadDispatcher(NJSThreadDispatcherImpl)
return new binding.NJSItfApi(LGHttp, LGThreadDispatcher)
}
// -----------------------------------------------------------------------------
// EXPORTS
// -----------------------------------------------------------------------------
exports.getTransactions = function getTransactions(addresses, currency) {
const api = makeApi()
const observer = new binding.NJSItfTransactionListVmObserver(NJSTransactionListVmObserverImpl)
const handle = api.observer_transaction_list()
return new Promise((resolve, reject) => {
const NJSItfHandleResponseImpl = {}
NJSItfHandleResponseImpl.respond = response => {
if (response.error) {
return reject(response.error)
}
return resolve(JSON.parse(response.result))
}
handle.start(
observer,
addresses,
currency,
new binding.NJSItfHandleResponse(NJSItfHandleResponseImpl),
)
})
}