-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
executable file
·357 lines (352 loc) · 10.6 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
const express = require('express')
const fs = require('fs')
const path = require('path')
const bsv = require('bsv')
const bpu = require('bpu');
const Dat = require('dat-node')
const readline = require('readline')
const Stream = require('stream')
const Tail = require('./tail')
class Overpool {
constructor(o) {
this.path = (o && o.path ? o.path + "/overpool" : process.cwd() + "/overpool")
this.subscribed = [];
this.filters = {}
this.dats = {};
this.tails = {};
}
join(store, key) {
return new Promise((resolve, reject) => {
if (key) {
if (!fs.existsSync(store)) {
fs.mkdirSync(store, { recursive: true })
}
Dat(store, key, (err, dat) => {
if (err) throw err
dat.joinNetwork()
dat.network.on('connection', () => {
console.log('connected', key)
})
dat.network.on('connection-closed', () => {
console.log('connection closed')
})
this.dats[key] = dat;
resolve(dat.key.toString('hex'))
})
} else {
Dat(store, (err, dat) => {
if (err) throw err
console.log("import")
dat.importFiles({watch: true})
dat.joinNetwork()
dat.network.on('connection', () => {
console.log("connected", dat.key.toString("hex"))
})
dat.network.on('connection-closed', () => {
console.log('connection closed')
})
this.dats[key] = dat;
resolve(dat.key.toString('hex'))
})
}
})
}
async pub(o) {
if (o && o.path) {
let discoveryKey = await this.join(this.path + "/" + o.path)
this.publicKey = discoveryKey
return discoveryKey;
} else {
throw new Error("Must specify path")
}
}
async sub(o) {
if (o && o.path) {
let discoveryKey = await this.join(this.path + "/" + o.path, { key: o.path })
this.subscribed.push({
key: o.path,
path: this.path + "/" + o.path
})
return discoveryKey;
} else {
throw new Error("Must specify path")
}
}
create (o) {
return new Promise((resolve, reject) => {
let port = (o && o.port ? o.port : 3000);
let poolPath = (o && o.path ? o.path : "localhost")
if (o && o.filter) this.filters[poolPath] = o.filter;
if (!fs.existsSync(this.path + "/" + poolPath)) {
fs.mkdirSync(this.path + "/" + poolPath, { recursive: true })
}
this.app = express()
this.app.use(express.json())
this.app.use(express.urlencoded({ extended: true }))
this.app.post("/" + poolPath, (req, res) => {
let payment = req.body;
this.post({ payment: payment, path: poolPath })
.then((response) => {
res.json(response)
})
.catch((error) => {
res.json(error)
})
})
this.subscribed.push({
key: poolPath,
path: this.path + "/" + poolPath
})
this.app.listen(port, () => {
console.log("overpool", `listening to port:${port}!`)
resolve();
})
})
}
post (o) {
let payment = o.payment;
let type = o.path;
return new Promise((resolve, reject) => {
/****************************************************
BIP270 Format
Payment {
merchantData // string. optional.
transaction // a hex-formatted (and fully-signed and valid) transaction. required.
refundTo // string. paymail to send a refund to. optional.
memo // string. optional.
}
PaymentACK {
payment // Payment. required.
memo // string. optional.
error // number. optional.
}
****************************************************/
if (!payment.transaction) {
reject({ payment: payment, error: "Must include hex-formatted transaction" })
} else {
bpu.parse({
tx: { r: payment.transaction },
transform: (o, c) => {
if (c.buf && c.buf.byteLength > 512) {
o.ls = o.s
o.lb = o.b
delete o.s
delete o.b
}
return o
},
split: [
{ token: { s: "|" }, },
{ token: { op: 106 }, include: "l" }
]
})
.then((parsed) => {
if (parsed) {
let hash = parsed.tx.h;
if (this.filters[type]) {
let payload = payment
payload.parsed = parsed
this.filters[type](payload, (err, valid) => {
if (err) {
reject({ payment: payment, error: "Unsupported transaction type" })
} else {
try {
this._post({ hash: hash, payment: payment, path: type }).then((response) => {
resolve({ payment: payment, memo: response })
})
} catch (e) {
reject({ payment: payment, error: e })
}
}
})
} else {
this._post({ hash: hash, payment: payment, path: type }).then((response) => {
resolve({ payment: payment, memo: response })
})
}
} else {
reject({ payment: payment, error: "Invalid Transaction" })
}
})
.catch((e) => {
reject({ payment: payment, error: e })
})
}
})
}
async _post (o) {
/**************************************
*
* o := {
* hash: <transaction hash>,
* payment: <payment object>,
* path: <overpool path>
* }
*
**************************************/
// generate hash
if (o.hash && o.payment && o.path) {
let poolPath = path.resolve(this.path, o.path)
let filePath = path.resolve(poolPath, o.hash)
await fs.promises.writeFile(filePath, JSON.stringify(o.payment))
let tapePath = path.resolve(poolPath, "tape.txt")
let line = "OVERPOOL " + o.hash + " " + Date.now() + "\n"
await fs.promises.appendFile(tapePath, line);
} else {
throw new Error("The post object must contain three attributes: hash, payment, and path")
}
}
prune(id) {
return new Promise((resolve, reject) => {
// move the tape.txt to "tape-<timestamp>.txt"
// create a new tape.txt
if (id) {
fs.rename(
this.path + "/" + id + "/tape.txt",
this.path + "/" + id + "/tape-" + Date.now() + ".txt",
(err) => {
if (err) {
reject(err);
} else {
fs.closeSync(fs.openSync(this.path + "/" + id + "/tape.txt", 'w'));
resolve();
}
}
)
} else {
reject(new Error("Must specify the path"))
}
})
}
on (e, handler) {
if (e === 'tx') {
this.subscribed.forEach((p) => {
if (!this.tails[p.path]) {
let tail;
try {
if (!fs.existsSync(p.path + "/tape.txt")) {
fs.closeSync(fs.openSync(p.path + "/tape.txt", 'w'));
}
tail = new Tail(p.path + "/tape.txt")
} catch (e) {
fs.closeSync(fs.openSync(p.path + "/tape.txt", 'w'));
tail = new Tail(p.path + "/tape.txt")
}
this.tails[p.path] = tail
tail.on("line", async (data) => {
let chunks = data.split(" ")
let type = chunks[0];
let hash = chunks[1];
this.read(p, hash, handler)
});
tail.on("close", () => {
console.log("watch closed");
})
tail.on("error", (error) => {
console.log("OVERPOOL", 'Tail error', error);
});
tail.watch();
}
})
}
return this;
}
read (p, hash, handler) {
if (fs.existsSync(p.path + "/" + hash)) {
fs.readFile(p.path + "/" + hash, "utf8", async (err, content) => {
let payment = JSON.parse(content);
let parsed = await bpu.parse({
tx: { r: payment.transaction },
transform: (o, c) => {
if (c.buf && c.buf.byteLength > 512) {
o.ls = o.s
o.lb = o.b
delete o.s
delete o.b
}
return o
},
split: [
{ token: { s: "|" }, },
{ token: { op: 106 }, include: "l" }
]
})
handler({
path: p.key,
key: p.key,
hash: hash,
payment: JSON.parse(content),
parsed: parsed
})
})
} else {
setTimeout(() => {
this.read(p, hash, handler)
}, 1000)
}
}
readPromise(o, txid) {
return new Promise((resolve, reject) => {
this.read ({ key: o.key, path: o.path }, txid, (result) => {
resolve(result)
})
})
}
tail (o) {
return new Promise((resolve, reject) => {
if (o && o.path && o.size) {
let poolPath = path.resolve(this.path, o.path)
let filePath = path.resolve(poolPath, "tape.txt")
let readStream = fs.createReadStream(filePath);
let rl = readline.createInterface(readStream, new Stream);
let cache = []
rl.on('close', () => {
// use currentLine here
let promises = cache.map((c) => {
return new Promise((_resolve, _reject) => {
let chunks = c.split(" ");
let txid = chunks[1];
let res = this.readPromise({ key: o.path, path: poolPath }, txid)
_resolve(res)
})
})
Promise.all(promises).then(resolve)
});
rl.on('line', (line) => {
cache.push(line);
if (cache.length > o.size) {
cache.shift();
}
});
} else {
throw new Error("The head query must contain 'path' and 'size' attributes")
}
})
}
get (o) {
return new Promise((resolve, reject) => {
/**************************************
*
* o := {
* path: <overpool path>,
* hash: <transaction id>
* }
*
**************************************/
if (o && o.hash && o.path) {
let poolPath = path.resolve(this.path, o.path)
let filePath = path.resolve(poolPath, o.hash)
try {
this.read ({ key: o.path, path: poolPath }, o.hash, (result) => {
resolve(result)
})
} catch (e) {
reject("The file doesn't exist")
}
} else {
throw new Error("The get query must contain 'hash' and 'path' attributes")
}
})
}
}
module.exports = Overpool