-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhooks.js
176 lines (157 loc) · 4.26 KB
/
webhooks.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
"use strict";
let request = require("./lib/request.js");
let Base58Check = require("@root/base58check").Base58Check;
let b58c = Base58Check.create();
let Hooks = module.exports;
Hooks.create = function ({ defaultWebhookTimeout = 5 * 1000, Db }) {
let hooks = {};
hooks.register = async function (req, res) {
let data = {
url: req.body.url,
address: req.body.address,
};
let webhookUrl = req.body.url;
// TODO
let account = req.account;
let url;
try {
url = new URL(data.url);
} catch (e) {
throw new Error(`BAD_REQUEST: invalid webhook url '${data.url}'`);
}
// TODO
if (!account.hostnames.includes(url.hostname)) {
throw new Error(`BAD_REQUEST: untrusted hostname '${url.hostname}'`);
}
if ("https:" !== url.protocol) {
throw new Error(`BAD_REQUEST: insecure webhook url '${url.protocol}'`);
}
let addr;
try {
addr = await b58c.verify(data.address);
} catch (e) {
throw new Error("BAD_REQUEST: invalid dash address");
}
// fn: test that valid auth succeeds
if (!url.username) {
url.username = "dwh";
}
if (!url.password) {
url.password = req.token;
}
await request({
timeout: defaultWebhookTimeout,
url: data.url,
auth: {
username: url.username,
password: url.password,
},
json: {
address: data.address,
satoshis: 0,
},
})
.then(function (resp) {
if (!resp.ok) {
throw new Error(
`BAD_REQUEST: webhook test did not respond with 2xx OK: ${resp.statusCode}`
);
}
if (0 !== resp.body.satoshis) {
throw new Error(
`BAD_REQUEST: webhook test did not respond with Content-Type: application/json and '{ "satoshis": 0 }'`
);
}
return resp;
})
.catch(function (e) {
if (e.message.startsWith("BAD_REQUEST:")) {
throw e;
}
throw new Error(
`BAD_REQUEST: webhook test failed network connection: ${e.message}`
);
});
// fn: test that invalid auth fails
await request({
timeout: defaultWebhookTimeout,
url: webhookUrl,
json: { address: data.address, satoshis: 0 },
}).then(function (resp) {
if (resp.ok) {
throw new Error(
"BAD_REQUEST: unauthenticated webhook test did not fail"
);
}
});
await Db.set({
ts: Date.now(),
address: data.address,
pubKeyHash: addr.pubKeyHash,
username: url.username,
password: url.password,
url: data.url,
});
let all = await Db.all();
console.log("DEBUG", addr.pubKeyHash, all);
if (url.password) {
let prefix = url.password.slice(0, 4);
let mask = "*".repeat(url.password.length - 6);
let last2 = url.password.slice(-2);
url.password = `${prefix}${mask}${last2}`;
}
res.json({
url: url.toString(),
address: data.address,
});
// TODO set this on an weak-ref interval?
await Db.cleanup();
};
//let msg = { event: evname, txid: tx.hash, satoshis: out.satoshis };
hooks.send = async function (payaddr, { event, txid, satoshis, p2pkh }) {
let hook;
if (p2pkh) {
hook = await Db.getByPubKeyHash(p2pkh);
} else {
hook = await Db.get(payaddr);
}
if (!hook) {
return;
}
let evname = event;
let out = {
satoshis: satoshis,
};
let tx = {
hash: txid,
};
console.info(`[${evname}] Target: ${out.satoshis} => ${payaddr}`);
//console.log("DEBUG", payaddr, { event, txid, satoshis, p2pkh });
//console.log("DEBUG hook", hook);
let req = {
timeout: defaultWebhookTimeout,
auth: {
username: hook.username,
password: hook.password,
},
url: hook.url,
json: {
txid: tx.hash,
event: evname,
instantsend: "txlock" === event,
address: hook.address,
// TODO duffs
satoshis: out.satoshis,
},
};
await request(req).then(function (resp) {
if (resp.ok) {
return resp;
}
console.error(`[${evname}] not OK:`);
console.error(resp.toJSON());
throw new Error("bad response from webhook");
});
};
return hooks;
};