-
Notifications
You must be signed in to change notification settings - Fork 14
/
request.go
166 lines (155 loc) · 5.07 KB
/
request.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
package agent
import (
"bytes"
"crypto/sha256"
"github.com/aviate-labs/agent-go/certification/hashtree"
"github.com/aviate-labs/agent-go/identity"
"github.com/aviate-labs/agent-go/principal"
"github.com/aviate-labs/leb128"
"math/big"
"sort"
"github.com/fxamacker/cbor/v2"
)
var (
typeKey = sha256.Sum256([]byte("request_type"))
canisterIDKey = sha256.Sum256([]byte("canister_id"))
nonceKey = sha256.Sum256([]byte("nonce"))
methodNameKey = sha256.Sum256([]byte("method_name"))
argumentsKey = sha256.Sum256([]byte("arg"))
ingressExpiryKey = sha256.Sum256([]byte("ingress_expiry"))
senderKey = sha256.Sum256([]byte("sender"))
pathsKey = sha256.Sum256([]byte("paths"))
)
func hashPaths(paths [][]hashtree.Label) [32]byte {
var hash []byte
for _, path := range paths {
var rawPathHash []byte
for _, p := range path {
pathBytes := sha256.Sum256(p)
rawPathHash = append(rawPathHash, pathBytes[:]...)
}
pathHash := sha256.Sum256(rawPathHash)
hash = append(hash, pathHash[:]...)
}
return sha256.Sum256(hash)
}
// Request is the request to the agent.
// DOCS: https://smartcontracts.org/docs/interface-spec/index.html#http-call
type Request struct {
// The type of the request. This is used to distinguish between query, call and read_state requests.
Type RequestType
// The user who issued the request.
Sender principal.Principal
// Arbitrary user-provided data, typically randomly generated. This can be
// used to create distinct requests with otherwise identical fields.
Nonce []byte
// An upper limit on the validity of the request, expressed in nanoseconds
// since 1970-01-01 (like ic0.time()).
IngressExpiry uint64
// The principal of the canister to call.
CanisterID principal.Principal
// Name of the canister method to call.
MethodName string
// Argument to pass to the canister method.
Arguments []byte
// A list of paths, where a path is itself a sequence of blobs.
Paths [][]hashtree.Label
}
// MarshalCBOR implements the CBOR marshaler interface.
func (r *Request) MarshalCBOR() ([]byte, error) {
m := make(map[string]any)
if len(r.Type) != 0 {
m["request_type"] = r.Type
}
if r.CanisterID.Raw != nil {
m["canister_id"] = r.CanisterID.Raw
}
if len(r.MethodName) != 0 {
m["method_name"] = r.MethodName
}
if r.Arguments != nil {
// Some endpoints require the argument to be an empty array, not null.
// This is the case with the protobuf endpoints on the registry.
m["arg"] = r.Arguments
}
if len(r.Sender.Raw) != 0 {
m["sender"] = r.Sender.Raw
}
if r.IngressExpiry != 0 {
m["ingress_expiry"] = r.IngressExpiry
}
if len(r.Nonce) != 0 {
m["nonce"] = r.Nonce
}
if r.Paths != nil {
m["paths"] = r.Paths
}
return cbor.Marshal(m)
}
// RequestID is the request ID.
type RequestID [32]byte
// NewRequestID creates a new request ID.
// DOCS: https://smartcontracts.org/docs/interface-spec/index.html#request-id
func NewRequestID(req Request) RequestID {
var hashes [][]byte
if len(req.Type) != 0 {
typeHash := sha256.Sum256([]byte(req.Type))
hashes = append(hashes, append(typeKey[:], typeHash[:]...))
}
// NOTE: the canister ID may be the empty slice. The empty slice doesn't mean it's not
// set, it means it's the management canister (aaaaa-aa).
if req.CanisterID.Raw != nil {
canisterIDHash := sha256.Sum256(req.CanisterID.Raw)
hashes = append(hashes, append(canisterIDKey[:], canisterIDHash[:]...))
}
if len(req.MethodName) != 0 {
methodNameHash := sha256.Sum256([]byte(req.MethodName))
hashes = append(hashes, append(methodNameKey[:], methodNameHash[:]...))
}
if len(req.Arguments) != 0 {
argumentsHash := sha256.Sum256(req.Arguments)
hashes = append(hashes, append(argumentsKey[:], argumentsHash[:]...))
}
if len(req.Sender.Raw) != 0 {
senderHash := sha256.Sum256(req.Sender.Raw)
hashes = append(hashes, append(senderKey[:], senderHash[:]...))
}
if req.IngressExpiry != 0 {
bi := big.NewInt(int64(req.IngressExpiry))
e, _ := leb128.EncodeUnsigned(bi)
ingressExpiryHash := sha256.Sum256(e)
hashes = append(hashes, append(ingressExpiryKey[:], ingressExpiryHash[:]...))
}
if len(req.Nonce) != 0 {
nonceHash := sha256.Sum256(req.Nonce)
hashes = append(hashes, append(nonceKey[:], nonceHash[:]...))
}
if req.Paths != nil {
pathsHash := hashPaths(req.Paths)
hashes = append(hashes, append(pathsKey[:], pathsHash[:]...))
}
sort.Slice(hashes, func(i, j int) bool {
return bytes.Compare(hashes[i], hashes[j]) == -1
})
return sha256.Sum256(bytes.Join(hashes, nil))
}
// Sign signs the request ID with the given identity.
func (r RequestID) Sign(id identity.Identity) []byte {
message := append(
// \x0Aic-request
[]byte{0x0a, 0x69, 0x63, 0x2d, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74},
r[:]...,
)
signature := id.Sign(message)
return signature
}
// RequestType is the type of request.
type RequestType = string
const (
// RequestTypeCall is a call request.
RequestTypeCall RequestType = "call"
// RequestTypeQuery is a query request.
RequestTypeQuery RequestType = "query"
// RequestTypeReadState is a read state request.
RequestTypeReadState RequestType = "read_state"
)