-
Notifications
You must be signed in to change notification settings - Fork 3
/
classes.js
85 lines (76 loc) · 1.9 KB
/
classes.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
// recursive function to convert value(s) to Sathoshi; !NOTE! assumes standard ratio of 1:100,000,000
const convertToSatoshis = (values) => {
if (Array.isArray(values)) {
for (let i = 0; i < values.length; i++) {
values[i].value = convertToSatoshis(values[i].value)
}
} else {
return Math.round(1e8 * values)
}
return values
}
// converts a given epochTime to standard ISO date string - if epochTime is not a number do not attempt transform
const convertEpochToIso = (epochTime) => {
if (typeof(epochTime) !== 'number') {
return epochTime
} else {
let date = new Date(epochTime * 1000)
return date.toISOString()
}
}
class Data {
constructor() {
this.hash = '',
this.time = undefined,
this.timeConfirmed = undefined,
this.timeReceived = undefined,
this.timeMedian = undefined
}
timesToISO() {
this.time = convertEpochToIso(this.time)
this.timeMedian = convertEpochToIso(this.timeMedian)
this.timeConfirmed = convertEpochToIso(this.timeConfirmed)
this.timeReceived = convertEpochToIso(this.timeReceived)
}
}
class Block extends Data {
constructor() {
super()
this.height = '',
this.transactions = []
this.totalTransactions = undefined
}
}
class Transaction extends Data {
constructor() {
super()
this.total = undefined,
this.fee = undefined,
this.inputs = [],
this.outputs = []
}
valuesToSatoshis() {
this.total = convertToSatoshis(this.total)
this.fee = convertToSatoshis(this.fee)
this.outputs = convertToSatoshis(this.outputs)
}
}
class TransactionInput {
constructor(hash, inputIndex, value) {
this.hash = hash,
this.index = inputIndex,
this.value = value
}
}
class TransactionOutput {
constructor(outputIndex, value) {
this.index = outputIndex,
this.value = value
}
}
module.exports = {
Block,
Transaction,
TransactionInput,
TransactionOutput
}