-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
98 lines (76 loc) · 2.59 KB
/
main.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
const express = require('express')
const fetch = require('node-fetch')
const ScriptReader = require('script_reader')
const app = express()
function log (msg) {
console.log(new Date().toISOString().replace('T', ' '), msg)
}
async function getOPReturnData (txhash) {
const url = `https://api.whatsonchain.com/v1/bsv/main/tx/hash/${txhash}`
const response = await fetch(url)
const json = await response.json()
const txOuts = json.vout.map(out => Buffer.from(out.scriptPubKey.hex, 'hex'))
const opReturns = txOuts.filter(out => out[0] === 0x6a || (out[0] === 0x00 && out[1] === 0x6a))
if (opReturns.length === 0) {
throw new Error('no op_return transaction outputs')
}
const firstOpReturn = opReturns[0]
const reader = ScriptReader(firstOpReturn)
return reader.decodeParts()
}
app.get('/api/:tx', async (req, res) => {
try {
const oprParts = await getOPReturnData(req.params.tx)
// If this is the new OP_FALSE OP_RETURN format, remove the prepending OP_FALSE
if (oprParts[0] === 0x00 && oprParts[1] === 0x6a) {
oprParts.shift()
}
switch (oprParts[1].toString()) {
case 'meta':
// Metanet protocol
res.status(200)
if (oprParts[4].toString() === '19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut') {
res.contentType(oprParts[6].toString())
res.send(oprParts[5])
} else {
res.contentType('text/plain')
res.send(oprParts[4])
}
break
case '1ChDHzdd1H4wSjgGMHyndZm6qxEDGjqpJL':
// BCAT part
res.status(200)
res.contentType('text/plain')
res.send('This is a bcat part. Please find a parent transaction to display')
break
case '15DHFxWZJT58f9nhyGnsRBqrgwK4W6h4Up':
// BCAT transaction
let d = Buffer.alloc(0)
for (let i = 7; i < oprParts.length; i++) {
const txhash = oprParts[i].toString('hex')
const parts = await getOPReturnData(txhash)
d = Buffer.concat([d, parts[2]])
}
res.status(200)
res.setHeader('Content-Transfer-Encoding', 'binary')
res.contentType(oprParts[3].toString())
res.send(d)
break
case '19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut':
// B protocol
res.status(200)
res.contentType(oprParts[3].toString())
res.send(oprParts[2])
break
default:
throw new Error('Unrecognized op_return transaction')
}
} catch (err) {
log(err)
res.status(400).send({ error: 'Something failed!' })
}
})
const port = 4000
app.listen(port, () =>
log(`Example app listening on port ${port}`)
)