-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtransaction.js
54 lines (54 loc) · 1.49 KB
/
transaction.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
const axios = require('axios')
class Transaction {
constructor(o) {
if (o && o.url) {
this.url = o.url
this.headers = o.headers
this.statusPath = "/mapi/tx"
this.pushPath = "/mapi/tx"
}
this.validate = o.validate;
}
push(rawtx, options) {
if (this.url) {
let u = this.url + (this.pushPath ? this.pushPath : "")
return axios.post(u, { rawtx: rawtx }, {
headers: this.headers
}).then((res) => {
if (options && options.verbose) {
res.data.payload = JSON.parse(res.data.payload)
return res.data
} else {
return JSON.parse(res.data.payload)
}
})
} else {
throw new Error("Must instantiate with a miner merchant API root URL")
}
}
status(id, options) {
if (this.url) {
let u = this.url + (this.statusPath ? this.statusPath : "") + "/" + id
return axios.get(u, { headers: this.headers }).then((res) => {
let isvalid;
try {
isvalid = this.validate(res.data)
} catch (e) {
isvalid = false;
}
if (options && options.verbose) {
res.data.payload = JSON.parse(res.data.payload)
res.data.valid = isvalid
return res.data
} else {
let j = JSON.parse(res.data.payload)
j.valid = isvalid
return j
}
})
} else {
throw new Error("Must instantiate with a miner merchant API root URL")
}
}
}
module.exports = Transaction