aboutsummaryrefslogtreecommitdiff
path: root/pkg/requests/requests.go
blob: 6b741840b5cc8ab43d16f720a4b45fa4cd2f6c12 (plain)
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
package requests

import (
	"fmt"
	"io"
	"strconv"
	"strings"

	"git.sigsum.org/sigsum-go/pkg/ascii"
	"git.sigsum.org/sigsum-go/pkg/hex"
	"git.sigsum.org/sigsum-go/pkg/merkle"
	"git.sigsum.org/sigsum-go/pkg/types"
)

type Leaf struct {
	ShardHint       uint64          `ascii:"shard_hint"`
	Message         merkle.Hash      `ascii:"message"`
	Signature       types.Signature `ascii:"signature"`
	PublicKey       types.PublicKey `ascii:"public_key"`
	DomainHint      string          `ascii:"domain_hint"`
}

type Leaves struct {
	StartSize uint64
	EndSize   uint64
}

type InclusionProof struct {
	TreeSize uint64
	LeafHash merkle.Hash
}

type ConsistencyProof struct {
	OldSize uint64
	NewSize uint64
}

type Cosignature struct {
	Cosignature types.Signature `ascii:"cosignature"`
	KeyHash     merkle.Hash      `ascii:"key_hash"`
}

func (req *Leaf) ToASCII(w io.Writer) error {
	return ascii.StdEncoding.Serialize(w, req)
}

// ToURL encodes request parameters at the end of a slash-terminated URL
func (req *Leaves) ToURL(url string) string {
	return url + fmt.Sprintf("%d/%d", req.StartSize, req.EndSize)
}

// ToURL encodes request parameters at the end of a slash-terminated URL
func (req *InclusionProof) ToURL(url string) string {
	return url + fmt.Sprintf("%d/%s", req.TreeSize, hex.Serialize(req.LeafHash[:]))
}

// ToURL encodes request parameters at the end of a slash-terminated URL
func (req *ConsistencyProof) ToURL(url string) string {
	return url + fmt.Sprintf("%d/%d", req.OldSize, req.NewSize)
}

func (req *Cosignature) ToASCII(w io.Writer) error {
	return ascii.StdEncoding.Serialize(w, req)
}

func (req *Leaf) FromASCII(r io.Reader) error {
	return ascii.StdEncoding.Deserialize(r, req)
}

// FromURL parses request parameters from a URL that is not slash-terminated
func (req *Leaves) FromURL(url string) (err error) {
	split := strings.Split(url, "/")
	if len(split) < 2 {
		return fmt.Errorf("not enough input")
	}
	startSize := split[len(split)-2]
	if req.StartSize, err = strconv.ParseUint(startSize, 10, 64); err != nil {
		return err
	}
	endSize := split[len(split)-1]
	if req.EndSize, err = strconv.ParseUint(endSize, 10, 64); err != nil {
		return err
	}
	return nil
}

// FromURL parses request parameters from a URL that is not slash-terminated
func (req *InclusionProof) FromURL(url string) (err error) {
	split := strings.Split(url, "/")
	if len(split) < 2 {
		return fmt.Errorf("not enough input")
	}
	treeSize := split[len(split)-2]
	if req.TreeSize, err = strconv.ParseUint(treeSize, 10, 64); err != nil {
		return err
	}
	b, err := hex.Deserialize(split[len(split)-1])
	if err != nil {
		return err
	}
	if n := len(b); n != merkle.HashSize {
		return fmt.Errorf("invalid hash size %d", n)
	}
	copy(req.LeafHash[:], b)
	return nil
}

// FromURL parses request parameters from a URL that is not slash-terminated
func (req *ConsistencyProof) FromURL(url string) (err error) {
	split := strings.Split(url, "/")
	if len(split) < 2 {
		return fmt.Errorf("not enough input")
	}
	oldSize := split[len(split)-2]
	if req.OldSize, err = strconv.ParseUint(oldSize, 10, 64); err != nil {
		return err
	}
	newSize := split[len(split)-1]
	if req.NewSize, err = strconv.ParseUint(newSize, 10, 64); err != nil {
		return err
	}
	return nil
}

func (req *Cosignature) FromASCII(r io.Reader) error {
	return ascii.StdEncoding.Deserialize(r, req)
}