-
Notifications
You must be signed in to change notification settings - Fork 3
/
waardepapieren-service.js
112 lines (87 loc) · 4.07 KB
/
waardepapieren-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
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
import { AbundanceService } from '@discipl/abundance-service'
// Specifically importing the server, because the server is not in the index to ensure browser compatibility
import EphemeralServer from '@discipl/core-ephemeral/dist/EphemeralServer'
import { take } from 'rxjs/operators'
import { w3cwebsocket } from 'websocket'
import jp from 'jsonpath'
import fs from 'fs'
import * as log from 'loglevel'
class WaardenpapierenService {
constructor (core) {
this.core = core
}
async start (configuration) {
this.logger = log.getLogger('WaardepapierenService')
this.configuration = configuration
if (configuration.LOG_LEVEL) {
this.logger.setLevel(configuration.LOG_LEVEL)
log.getLogger('EphemeralConnector').setLevel(configuration.LOG_LEVEL)
}
// Setup server
this.ephemeralServer = new EphemeralServer(3232, configuration.EPHEMERAL_CERT, configuration.EPHEMERAL_KEY, configuration.EPHEMERAL_RETENTION_TIME)
this.ephemeralServer.start()
this.abundance = new AbundanceService(this.core)
const core = this.abundance.getCoreAPI()
const ephemeralConnector = await core.getConnector('ephemeral')
ephemeralConnector.configure(this.configuration.EPHEMERAL_ENDPOINT, this.configuration.EPHEMERAL_WEBSOCKET_ENDPOINT, w3cwebsocket)
let nlxCert = fs.readFileSync(this.configuration.NLX_CERT).toString()
let nlxKey = fs.readFileSync(this.configuration.NLX_KEY).toString()
console.log('NLX_CERT', nlxCert)
let nlxEphemeralIdentity = await ephemeralConnector.newIdentity({ 'cert': nlxCert, 'privkey': nlxKey })
const nlxConnector = await core.getConnector('nlx')
nlxConnector.configure(this.configuration.NLX_OUTWAY_ENDPOINT)
let attendResult = await this.abundance.attendTo('ephemeral', this.configuration.PRODUCT_NEED, [this.configuration.SOURCE_ARGUMENT])
// TODO: Refactor to observableResult.subscribe when fix from core propagates
await attendResult.observableResult._observable.subscribe(async (need) => {
await this.serveNeed(need, nlxEphemeralIdentity)
}, (e) => {
// If connection is dropped by remote peer, this is fine
if (e.code !== 1006) {
this.logger.error('Error while listening to need')
this.logger.error(e)
}
else {
this.logger.info('Websocket Connection broke')
}
})
await attendResult.observableResult._readyPromise
this.logger.info('Serving needs')
}
async serveNeed (need, nlxIdentity) {
let logId = Math.floor(Math.random()*10000);
this.logger.info('Serving need ', logId)
let core = this.abundance.getCoreAPI()
let needDetails = await need
let argumentClaim = await needDetails.informationPromise
this.logger.info('Received BSN ', logId)
let srcarg = argumentClaim['claim']['data'][this.configuration.SOURCE_ARGUMENT]
const nlxConnector = await core.getConnector('nlx')
let nlxpath = this.configuration.SOURCE_NLX_PATH.replace('{'+this.configuration.SOURCE_ARGUMENT+'}', srcarg)
let identifier = await nlxConnector.claim(null, { 'path': nlxpath, 'params': {[this.configuration.SOURCE_ARGUMENT]:srcarg} })
let result = await nlxConnector.get(identifier)
this.logger.info('Retrieved NLX data', logId)
let resultArray = [{'Doel':this.configuration.PRODUCT_PURPOSE}]
for (let field in this.configuration.SOURCE_DATA_SELECTION) {
let key = Object.keys(this.configuration.SOURCE_DATA_SELECTION[field])[0]
let path = this.configuration.SOURCE_DATA_SELECTION[field][key]
let value = jp.query(result, path)
resultArray.push({ [key]: value[0] })
}
let productClaim = await core.claim(nlxIdentity, resultArray)
await core.allow(nlxIdentity, productClaim, needDetails.theirPrivateDid)
await this.abundance.offer(needDetails.myPrivateSsid, productClaim)
this.logger.info('Served need', logId)
}
async stop () {
try {
await this.ephemeralServer.close()
} catch (e) {
console.log('Error while closing server')
console.log(e)
}
}
getCoreAPI () {
return this.abundance.getCoreAPI()
}
}
export default WaardenpapierenService