BlockDTO.ts 7.62 KB
Newer Older
1
import {TransactionDTO} from "./TransactionDTO"
2
import {CurrencyConfDTO} from "./ConfDTO"
3
import {hashf} from "../common"
4
import {Cloneable} from "./Cloneable";
5
6
7

const DEFAULT_DOCUMENT_VERSION = 10

8
9
10
11
12
export class BlockDTO implements Cloneable {

  clone(): any {
    return BlockDTO.fromJSONObject(this)
  }
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

  version: number
  number: number
  currency: string
  hash: string
  inner_hash: string
  previousHash: string
  issuer: string
  previousIssuer: string
  dividend: number
  time: number
  powMin: number
  unitbase: number
  membersCount: number
  issuersCount: number
  issuersFrame: number
  issuersFrameVar: number
30
31
32
33
34
35
36
37
  identities: string[] = []
  joiners: string[] = []
  actives: string[] = []
  leavers: string[] = []
  revoked: string[] = []
  excluded: string[] = []
  certifications: string[] = []
  transactions: TransactionDTO[] = []
38
39
40
41
42
  medianTime: number
  nonce: number
  fork: boolean
  parameters: string
  signature: string
Cédric Moreau's avatar
Cédric Moreau committed
43
44
  monetaryMass: number
  UDTime: number
45

46
47
  constructor() {
  }
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
  json() {
    return {
      version: this.version,
      nonce: this.nonce,
      number: this.number,
      powMin: this.powMin,
      time: this.time,
      medianTime: this.medianTime,
      membersCount: this.membersCount,
      monetaryMass: this.monetaryMass,
      unitbase: this.unitbase,
      issuersCount: this.issuersCount,
      issuersFrame: this.issuersFrame,
      issuersFrameVar: this.issuersFrameVar,
      len: this.len,
      currency: this.currency,
      issuer: this.issuer,
      signature: this.signature,
      hash: this.hash,
      parameters: this.parameters,
      previousHash: this.previousHash,
      previousIssuer: this.previousIssuer,
      inner_hash: this.inner_hash,
      dividend: this.dividend,
      identities: this.identities,
      joiners: this.joiners,
      actives: this.actives,
      leavers: this.leavers,
      revoked: this.revoked,
      excluded: this.excluded,
      certifications: this.certifications,
      transactions: this.transactions.map((tx) => {
        return {
          version: tx.version,
          currency: tx.currency,
          locktime: tx.locktime,
          blockstamp: tx.blockstamp,
          blockstampTime: tx.blockstampTime,
          issuers: tx.issuers,
          inputs: tx.inputs,
          outputs: tx.outputs,
          unlocks: tx.unlocks,
          signatures: tx.signatures,
          comment: tx.comment
        }
      })
    }
  }

  get len() {
    return this.identities.length +
      this.joiners.length +
      this.actives.length +
      this.leavers.length +
      this.revoked.length +
      this.certifications.length +
      this.transactions.reduce((sum, tx) => sum + tx.getLen(), 0)
  }

108
109
110
111
112
113
114
115
116
117
118
  getInlineIdentity(pubkey:string): string | null {
    let i = 0;
    let found = null;
    while (!found && i < this.identities.length) {
      if (this.identities[i].match(new RegExp('^' + pubkey)))
        found = this.identities[i];
      i++;
    }
    return found;
  }

119
120
121
122
  getRawUnSigned() {
    return this.getRawInnerPart() + this.getSignedPart()
  }

123
  getRawSigned() {
124
    return this.getRawUnSigned() + this.signature + "\n"
125
126
  }

127
128
129
130
131
  getSignedPart() {
    return "InnerHash: " + this.inner_hash + "\n" +
      "Nonce: " + this.nonce + "\n"
  }

132
133
134
135
  getSignedPartSigned() {
    return this.getSignedPart() + this.signature + "\n"
  }

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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  getRawInnerPart() {
    let raw = "";
    raw += "Version: " + this.version + "\n";
    raw += "Type: Block\n";
    raw += "Currency: " + this.currency + "\n";
    raw += "Number: " + this.number + "\n";
    raw += "PoWMin: " + this.powMin + "\n";
    raw += "Time: " + this.time + "\n";
    raw += "MedianTime: " + this.medianTime + "\n";
    if (this.dividend)
      raw += "UniversalDividend: " + this.dividend + "\n";
    raw += "UnitBase: " + this.unitbase + "\n";
    raw += "Issuer: " + this.issuer + "\n";
    raw += "IssuersFrame: " + this.issuersFrame + "\n";
    raw += "IssuersFrameVar: " + this.issuersFrameVar + "\n";
    raw += "DifferentIssuersCount: " + this.issuersCount + "\n";
    if(this.previousHash)
      raw += "PreviousHash: " + this.previousHash + "\n";
    if(this.previousIssuer)
      raw += "PreviousIssuer: " + this.previousIssuer + "\n";
    if(this.parameters)
      raw += "Parameters: " + this.parameters + "\n";
    raw += "MembersCount: " + this.membersCount + "\n";
    raw += "Identities:\n";
    for (const idty of (this.identities || [])){
      raw += idty + "\n";
    }
    raw += "Joiners:\n";
    for (const joiner of (this.joiners || [])){
      raw += joiner + "\n";
    }
    raw += "Actives:\n";
    for (const active of (this.actives || [])){
      raw += active + "\n";
    }
    raw += "Leavers:\n";
    for (const leaver of (this.leavers || [])){
      raw += leaver + "\n";
    }
    raw += "Revoked:\n";
    for (const revoked of (this.revoked || [])){
      raw += revoked + "\n";
    }
    raw += "Excluded:\n";
    for (const excluded of (this.excluded || [])){
      raw += excluded + "\n";
    }
    raw += "Certifications:\n";
    for (const cert of (this.certifications || [])){
      raw += cert + "\n";
    }
    raw += "Transactions:\n";
    for (const tx of (this.transactions || [])){
      raw += tx.getCompactVersion();
    }
    return raw
  }

194
195
196
197
  getHash() {
    return hashf(this.getSignedPartSigned())
  }

198
199
  static fromJSONObject(obj:any) {
    const dto = new BlockDTO()
200
    dto.version = parseInt(obj.version) || DEFAULT_DOCUMENT_VERSION
201
    dto.number = parseInt(obj.number)
202
203
    dto.currency = obj.currency || ""
    dto.hash = obj.hash || ""
204
205
    dto.inner_hash = obj.inner_hash
    dto.previousHash = obj.previousHash
206
    dto.issuer = obj.issuer || ""
207
208
209
210
    dto.previousIssuer = obj.previousIssuer
    dto.dividend = obj.dividend || null
    dto.time = parseInt(obj.time)
    dto.powMin = parseInt(obj.powMin)
211
    dto.monetaryMass = parseInt(obj.monetaryMass)
212
213
214
215
216
    dto.unitbase = parseInt(obj.unitbase)
    dto.membersCount = parseInt(obj.membersCount)
    dto.issuersCount = parseInt(obj.issuersCount)
    dto.issuersFrame = parseInt(obj.issuersFrame)
    dto.issuersFrameVar = parseInt(obj.issuersFrameVar)
217
218
219
220
221
222
223
    dto.identities = obj.identities || []
    dto.joiners = obj.joiners || []
    dto.actives = obj.actives || []
    dto.leavers = obj.leavers || []
    dto.revoked = obj.revoked || []
    dto.excluded = obj.excluded || []
    dto.certifications = obj.certifications || []
224
    dto.transactions = (obj.transactions || []).map((tx:any) => TransactionDTO.fromJSONObject(tx))
225
226
    dto.medianTime = parseInt(obj.medianTime)
    dto.fork = !!obj.fork
227
228
    dto.parameters = obj.parameters || ""
    dto.signature = obj.signature || ""
229
230
231
    dto.nonce = parseInt(obj.nonce)
    return dto
  }
232
233
234
235

  static getConf(block:BlockDTO): CurrencyConfDTO {
    const sp = block.parameters.split(':');
    return {
236
      currency: block.currency,
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
      c: parseFloat(sp[0]),
      dt: parseInt(sp[1]),
      ud0: parseInt(sp[2]),
      sigPeriod: parseInt(sp[3]),
      sigStock: parseInt(sp[4]),
      sigWindow: parseInt(sp[5]),
      sigValidity: parseInt(sp[6]),
      sigQty: parseInt(sp[7]),
      idtyWindow: parseInt(sp[8]),
      msWindow: parseInt(sp[9]),
      xpercent: parseFloat(sp[10]),
      msValidity: parseInt(sp[11]),
      stepMax: parseInt(sp[12]),
      medianTimeBlocks: parseInt(sp[13]),
      avgGenTime: parseInt(sp[14]),
      dtDiffEval: parseInt(sp[15]),
      percentRot: parseFloat(sp[16]),
      udTime0: parseInt(sp[17]),
      udReevalTime0: parseInt(sp[18]),
      dtReeval: parseInt(sp[19]),
      // New parameter, defaults to msWindow
      msPeriod: parseInt(sp[9])
    }
  }
261
262
263
264
265
266
267
268

  static getLen(block:any) {
    return BlockDTO.fromJSONObject(block).len
  }

  static getHash(block:any) {
    return BlockDTO.fromJSONObject(block).getHash()
  }
269
}