/
model.go
394 lines (339 loc) · 11.8 KB
/
model.go
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package invoice
import (
"encoding/json"
"reflect"
"github.com/centrifuge/go-centrifuge/documents"
"github.com/centrifuge/centrifuge-protobufs/documenttypes"
"github.com/centrifuge/centrifuge-protobufs/gen/go/coredocument"
"github.com/centrifuge/centrifuge-protobufs/gen/go/invoice"
"github.com/centrifuge/go-centrifuge/coredocument"
"github.com/centrifuge/go-centrifuge/errors"
"github.com/centrifuge/go-centrifuge/identity"
clientinvoicepb "github.com/centrifuge/go-centrifuge/protobufs/gen/go/invoice"
"github.com/centrifuge/precise-proofs/proofs"
"github.com/centrifuge/precise-proofs/proofs/proto"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/any"
"github.com/golang/protobuf/ptypes/timestamp"
)
const prefix string = "invoice"
var compactPrefix = []byte{1, 0, 0, 0}
// Invoice implements the documents.Model keeps track of invoice related fields and state
type Invoice struct {
InvoiceNumber string // invoice number or reference number
SenderName string // name of the sender company
SenderStreet string // street and address details of the sender company
SenderCity string
SenderZipcode string // country ISO code of the sender of this invoice
SenderCountry string
RecipientName string // name of the recipient company
RecipientStreet string
RecipientCity string
RecipientZipcode string
RecipientCountry string // country ISO code of the recipient of this invoice
Currency string // country ISO code of the recipient of this invoice
GrossAmount int64 // invoice amount including tax
NetAmount int64 // invoice amount excluding tax
TaxAmount int64
TaxRate int64
Recipient *identity.CentID
Sender *identity.CentID
Payee *identity.CentID
Comment string
DueDate *timestamp.Timestamp
DateCreated *timestamp.Timestamp
ExtraData []byte
InvoiceSalts *proofs.Salts
CoreDocument *coredocumentpb.CoreDocument
}
// getClientData returns the client data from the invoice model
func (i *Invoice) getClientData() *clientinvoicepb.InvoiceData {
var recipient string
if i.Recipient != nil {
recipient = hexutil.Encode(i.Recipient[:])
}
var sender string
if i.Sender != nil {
sender = hexutil.Encode(i.Sender[:])
}
var payee string
if i.Payee != nil {
payee = hexutil.Encode(i.Payee[:])
}
var extraData string
if i.ExtraData != nil {
extraData = hexutil.Encode(i.ExtraData)
}
return &clientinvoicepb.InvoiceData{
InvoiceNumber: i.InvoiceNumber,
SenderName: i.SenderName,
SenderStreet: i.SenderStreet,
SenderCity: i.SenderCity,
SenderZipcode: i.SenderZipcode,
SenderCountry: i.SenderCountry,
RecipientName: i.RecipientName,
RecipientStreet: i.RecipientStreet,
RecipientCity: i.RecipientCity,
RecipientZipcode: i.RecipientZipcode,
RecipientCountry: i.RecipientCountry,
Currency: i.Currency,
GrossAmount: i.GrossAmount,
NetAmount: i.NetAmount,
TaxAmount: i.TaxAmount,
TaxRate: i.TaxRate,
Recipient: recipient,
Sender: sender,
Payee: payee,
Comment: i.Comment,
DueDate: i.DueDate,
DateCreated: i.DateCreated,
ExtraData: extraData,
}
}
// createP2PProtobuf returns centrifuge protobuf specific invoiceData
func (i *Invoice) createP2PProtobuf() *invoicepb.InvoiceData {
var recipient, sender, payee []byte
if i.Recipient != nil {
recipient = i.Recipient[:]
}
if i.Sender != nil {
sender = i.Sender[:]
}
if i.Payee != nil {
payee = i.Payee[:]
}
return &invoicepb.InvoiceData{
InvoiceNumber: i.InvoiceNumber,
SenderName: i.SenderName,
SenderStreet: i.SenderStreet,
SenderCity: i.SenderCity,
SenderZipcode: i.SenderZipcode,
SenderCountry: i.SenderCountry,
RecipientName: i.RecipientName,
RecipientStreet: i.RecipientStreet,
RecipientCity: i.RecipientCity,
RecipientZipcode: i.RecipientZipcode,
RecipientCountry: i.RecipientCountry,
Currency: i.Currency,
GrossAmount: i.GrossAmount,
NetAmount: i.NetAmount,
TaxAmount: i.TaxAmount,
TaxRate: i.TaxRate,
Recipient: recipient,
Sender: sender,
Payee: payee,
Comment: i.Comment,
DueDate: i.DueDate,
DateCreated: i.DateCreated,
ExtraData: i.ExtraData,
}
}
// InitInvoiceInput initialize the model based on the received parameters from the rest api call
func (i *Invoice) InitInvoiceInput(payload *clientinvoicepb.InvoiceCreatePayload, self string) error {
err := i.initInvoiceFromData(payload.Data)
if err != nil {
return err
}
collaborators := append([]string{self}, payload.Collaborators...)
i.CoreDocument, err = coredocument.NewWithCollaborators(collaborators)
if err != nil {
return errors.New("failed to init core document: %v", err)
}
return nil
}
// initInvoiceFromData initialises invoice from invoiceData
func (i *Invoice) initInvoiceFromData(data *clientinvoicepb.InvoiceData) error {
i.InvoiceNumber = data.InvoiceNumber
i.SenderName = data.SenderName
i.SenderStreet = data.SenderStreet
i.SenderCity = data.SenderCity
i.SenderZipcode = data.SenderZipcode
i.SenderCountry = data.SenderCountry
i.RecipientName = data.RecipientName
i.RecipientStreet = data.RecipientStreet
i.RecipientCity = data.RecipientCity
i.RecipientZipcode = data.RecipientZipcode
i.RecipientCountry = data.RecipientCountry
i.Currency = data.Currency
i.GrossAmount = data.GrossAmount
i.NetAmount = data.NetAmount
i.TaxAmount = data.TaxAmount
i.TaxRate = data.TaxRate
i.Comment = data.Comment
i.DueDate = data.DueDate
i.DateCreated = data.DateCreated
if recipient, err := identity.CentIDFromString(data.Recipient); err == nil {
i.Recipient = &recipient
}
if sender, err := identity.CentIDFromString(data.Sender); err == nil {
i.Sender = &sender
}
if payee, err := identity.CentIDFromString(data.Payee); err == nil {
i.Payee = &payee
}
if data.ExtraData != "" {
ed, err := hexutil.Decode(data.ExtraData)
if err != nil {
return errors.NewTypedError(err, errors.New("failed to decode extra data"))
}
i.ExtraData = ed
}
return nil
}
// loadFromP2PProtobuf loads the invoice from centrifuge protobuf invoice data
func (i *Invoice) loadFromP2PProtobuf(invoiceData *invoicepb.InvoiceData) {
i.InvoiceNumber = invoiceData.InvoiceNumber
i.SenderName = invoiceData.SenderName
i.SenderStreet = invoiceData.SenderStreet
i.SenderCity = invoiceData.SenderCity
i.SenderZipcode = invoiceData.SenderZipcode
i.SenderCountry = invoiceData.SenderCountry
i.RecipientName = invoiceData.RecipientName
i.RecipientStreet = invoiceData.RecipientStreet
i.RecipientCity = invoiceData.RecipientCity
i.RecipientZipcode = invoiceData.RecipientZipcode
i.RecipientCountry = invoiceData.RecipientCountry
i.Currency = invoiceData.Currency
i.GrossAmount = invoiceData.GrossAmount
i.NetAmount = invoiceData.NetAmount
i.TaxAmount = invoiceData.TaxAmount
i.TaxRate = invoiceData.TaxRate
if recipient, err := identity.ToCentID(invoiceData.Recipient); err == nil {
i.Recipient = &recipient
}
if sender, err := identity.ToCentID(invoiceData.Sender); err == nil {
i.Sender = &sender
}
if payee, err := identity.ToCentID(invoiceData.Payee); err == nil {
i.Payee = &payee
}
i.Comment = invoiceData.Comment
i.DueDate = invoiceData.DueDate
i.DateCreated = invoiceData.DateCreated
i.ExtraData = invoiceData.ExtraData
}
// getInvoiceSalts returns the invoice salts. Initialises if not present
func (i *Invoice) getInvoiceSalts(invoiceData *invoicepb.InvoiceData) (*proofs.Salts, error) {
if i.InvoiceSalts == nil {
invoiceSalts, err := documents.GenerateNewSalts(invoiceData, prefix, compactPrefix)
if err != nil {
return nil, errors.New("getInvoiceSalts error %v", err)
}
i.InvoiceSalts = invoiceSalts
}
return i.InvoiceSalts, nil
}
// ID returns document identifier.
// Note: this is not a unique identifier for each version of the document.
func (i *Invoice) ID() ([]byte, error) {
coreDoc, err := i.PackCoreDocument()
if err != nil {
return []byte{}, err
}
return coreDoc.DocumentIdentifier, nil
}
// PackCoreDocument packs the Invoice into a Core Document
// If the, Invoice is new, it creates a valid identifiers
func (i *Invoice) PackCoreDocument() (*coredocumentpb.CoreDocument, error) {
invoiceData := i.createP2PProtobuf()
serializedInvoice, err := proto.Marshal(invoiceData)
if err != nil {
return nil, errors.NewTypedError(err, errors.New("couldn't serialise InvoiceData"))
}
invoiceAny := any.Any{
TypeUrl: documenttypes.InvoiceDataTypeUrl,
Value: serializedInvoice,
}
invoiceSalts, err := i.getInvoiceSalts(invoiceData)
if err != nil {
return nil, errors.NewTypedError(err, errors.New("couldn't get InvoiceSalts"))
}
coreDoc := new(coredocumentpb.CoreDocument)
proto.Merge(coreDoc, i.CoreDocument)
coreDoc.EmbeddedData = &invoiceAny
coreDoc.EmbeddedDataSalts = documents.ConvertToProtoSalts(invoiceSalts)
return coreDoc, err
}
// UnpackCoreDocument unpacks the core document into Invoice
func (i *Invoice) UnpackCoreDocument(coreDoc *coredocumentpb.CoreDocument) error {
if coreDoc == nil {
return errors.New("core document provided is nil %v", coreDoc)
}
if coreDoc.EmbeddedData == nil ||
coreDoc.EmbeddedData.TypeUrl != documenttypes.InvoiceDataTypeUrl {
return errors.New("trying to convert document with incorrect schema")
}
invoiceData := &invoicepb.InvoiceData{}
err := proto.Unmarshal(coreDoc.EmbeddedData.Value, invoiceData)
if err != nil {
return err
}
i.loadFromP2PProtobuf(invoiceData)
if coreDoc.EmbeddedDataSalts == nil {
i.InvoiceSalts, err = i.getInvoiceSalts(invoiceData)
if err != nil {
return err
}
} else {
i.InvoiceSalts = documents.ConvertToProofSalts(coreDoc.EmbeddedDataSalts)
}
i.CoreDocument = new(coredocumentpb.CoreDocument)
proto.Merge(i.CoreDocument, coreDoc)
i.CoreDocument.EmbeddedDataSalts = nil
i.CoreDocument.EmbeddedData = nil
return err
}
// JSON marshals Invoice into a json bytes
func (i *Invoice) JSON() ([]byte, error) {
return json.Marshal(i)
}
// FromJSON unmarshals the json bytes into Invoice
func (i *Invoice) FromJSON(jsonData []byte) error {
return json.Unmarshal(jsonData, i)
}
// Type gives the Invoice type
func (i *Invoice) Type() reflect.Type {
return reflect.TypeOf(i)
}
// CalculateDataRoot calculates the data root and sets the root to core document
func (i *Invoice) CalculateDataRoot() ([]byte, error) {
t, err := i.getDocumentDataTree()
if err != nil {
return nil, errors.New("calculateDataRoot error %v", err)
}
return t.RootHash(), nil
}
// getDocumentDataTree creates precise-proofs data tree for the model
func (i *Invoice) getDocumentDataTree() (tree *proofs.DocumentTree, err error) {
invProto := i.createP2PProtobuf()
salts, err := i.getInvoiceSalts(invProto)
if err != nil {
return nil, err
}
t := documents.NewDefaultTreeWithPrefix(salts, prefix, compactPrefix)
err = t.AddLeavesFromDocument(invProto)
if err != nil {
return nil, errors.New("getDocumentDataTree error %v", err)
}
err = t.Generate()
if err != nil {
return nil, errors.New("getDocumentDataTree error %v", err)
}
return t, nil
}
// CreateProofs generates proofs for given fields
func (i *Invoice) CreateProofs(fields []string) (coreDoc *coredocumentpb.CoreDocument, proofs []*proofspb.Proof, err error) {
// There can be failure scenarios where the core doc for the particular document
// is still not saved with roots in db due to failures during getting signatures.
coreDoc, err = i.PackCoreDocument()
if err != nil {
return nil, nil, errors.New("createProofs error %v", err)
}
tree, err := i.getDocumentDataTree()
if err != nil {
return coreDoc, nil, errors.New("createProofs error %v", err)
}
proofs, err = coredocument.CreateProofs(tree, coreDoc, fields)
return coreDoc, proofs, err
}