-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself_host.js
73 lines (61 loc) · 2.48 KB
/
self_host.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
import fetch from 'node-fetch';
import express from 'express';
import parser from 'body-parser';
const app = express();
app.use(parser.json());
const port = 80;
// NOTE: this example uses garlicblocks.com which doesn't support bech32/grlc1 addresses.
// In the app, you must write the IP starting with http:// e.g. "http://192.168.1.75".
// If you are hosting it on your PC you might have to disable Windows' firewall.
app.get('/', function (req, res) {
// deletes /#/ from the url to access txid on explorer
let response = Buffer.from('<script> const hash = window.location.hash;' +
'if (hash.length > 0 && hash.includes("#/")) {' +
'window.location.replace(window.location.href.replace("#/", ""));' +
'} </script >');
res.set('Content-Type', 'text/html');
res.send(response);
});
app.post('/api/GRLC/mainnet/tx/send', async function (req, res) {
// /api/GRLC/mainnet/tx/send
// POST {rawTx: "0200..."}
const tx = { 'rawtx': req.body.rawTx };
const response = await fetch('https://garlicblocks.com/api/tx/send', {
method: 'post',
body: JSON.stringify(tx),
headers: { 'Content-Type': 'application/json' }
});
let data = await response.text();
if (response.status == 200) {
data = JSON.parse(data);
}
res.statusCode = response.status;
res.send(data);
});
app.get('/GRLC/mainnet/tx/:txid', function (req, res) {
// /#/GRLC/mainnet/tx/ + txid
// Explorer link
res.redirect(`https://garlicblocks.com/tx/${req.params.txid}`);
});
app.get('/api/GRLC/mainnet/address/:address/balance', async function (req, res) {
// /api/GRLC/mainnet/address/ + address + /balance
// { confirmed, unconfirmed } (in sats)
const data = await fetch(`https://garlicblocks.com/api/addr/${req.params.address}`).then(res => res.json());
res.send({ confirmed: data.balanceSat, unconfirmed: data.unconfirmedBalanceSat });
});
app.get('/api/GRLC/mainnet/address/:address', async function (req, res) {
// /api/GRLC/mainnet/address/ + address + /?unspent=true
// GET {mintTxid, mintIndex, value} (in sats)
const utxos_api = await fetch(`https://garlicblocks.com/api/addr/${req.params.address}/utxo`).then(res => res.json());
const utxos = utxos_api.map(function (utxo) {
return {
mintTxid: utxo.txid,
mintIndex: utxo.vout,
value: utxo.satoshis
};
});
res.send(utxos);
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
})