-
Notifications
You must be signed in to change notification settings - Fork 0
/
receipt.go
130 lines (116 loc) · 3.23 KB
/
receipt.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
package veracity
import (
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"os"
"github.com/datatrails/go-datatrails-common/cbor"
"github.com/datatrails/go-datatrails-merklelog/massifs"
"github.com/urfave/cli/v2"
)
func NewReceiptCmd() *cli.Command {
return &cli.Command{
Name: "receipt",
Usage: "Generate a COSE Receipt of inclusion for any merklelog entry",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
},
&cli.Int64Flag{
Name: "mmrindex", Aliases: []string{"i"},
},
&cli.StringFlag{
Name: "format",
Usage: "override the output `FORMAT`. Supported formats: cbor (binary), hex, base64",
Aliases: []string{"f"},
Value: "hex",
Action: func(ctx *cli.Context, v string) error {
if v != "cbor" && v != "base64" && v != "hex" {
return fmt.Errorf("Unsupported format '%s'. Use one of: cbor, hex, base64", v)
}
return nil
},
},
},
Action: func(cCtx *cli.Context) error {
cmd := &CmdCtx{}
var err error
// This command uses the structured logger for all optional output.
// Output not explicitly printed is silenced by default.
if err = cfgLogging(cmd, cCtx); err != nil {
return err
}
log := func(m string, args ...any) {
cmd.log.Infof(m, args...)
}
dataUrl := cCtx.String("data-url")
reader, err := cfgReader(cmd, cCtx, dataUrl == "")
if err != nil {
return err
}
tenantIdentity := cCtx.String("tenant")
if tenantIdentity == "" {
return fmt.Errorf("tenant identity is required")
}
log("verifying for tenant: %s", tenantIdentity)
mmrIndex := cCtx.Uint64("mmrindex")
massifHeight := uint8(cCtx.Int64("height"))
// TODO: local replica receipts, its not a big lift, the local reader used by replicatelogs
// implements the necessary interface for NewReceipt.
var cborCodec cbor.CBORCodec
if cborCodec, err = massifs.NewRootSignerCodec(); err != nil {
return err
}
sealReader := massifs.NewSignedRootReader(cmd.log, reader, cborCodec)
massifReader := massifs.NewMassifReader(
cmd.log, reader,
massifs.WithSealGetter(&sealReader),
massifs.WithCBORCodec(cborCodec),
)
signedReceipt, err := massifs.NewReceipt(
context.Background(), massifHeight, tenantIdentity, mmrIndex, &massifReader,
)
if err != nil {
return err
}
cbor, err := signedReceipt.MarshalCBOR()
if err != nil {
return err
}
receipt := cbor
if cCtx.String("format") == "base64" {
receipt = make([]byte, base64.URLEncoding.EncodedLen(len(cbor)))
base64.URLEncoding.Encode(receipt, cbor)
} else if cCtx.String("format") == "hex" {
receipt = []byte(hex.EncodeToString(cbor))
}
if cCtx.String("output") == "" {
var n int
n, err = os.Stdout.Write(receipt)
if err != nil {
return err
}
if n != len(receipt) {
return fmt.Errorf("failed to write all bytes to stdout")
}
return nil
}
// Output to file requested
f, err := os.Create(cCtx.String("output"))
if err != nil {
return err
}
defer f.Close()
n, err := f.Write(receipt)
if err != nil {
return err
}
if n != len(receipt) {
return fmt.Errorf("failed to write all bytes to file")
}
return nil
},
}
}