-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRpcRequest.go
More file actions
78 lines (62 loc) · 1.68 KB
/
RpcRequest.go
File metadata and controls
78 lines (62 loc) · 1.68 KB
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
package jrm1
import (
"encoding/json"
"fmt"
"io"
"github.com/vault-thirteen/auxie/errors"
)
const ProtocolNameM1 = "M1"
const (
ErrFUnsupportedProtocol = "unsupported protocol: %v"
ErrRpcRequestIsMalformed = "RPC request is malformed"
)
// RpcRequest is a raw RPC request.
// Parameters in this raw request are not parsed.
type RpcRequest struct {
// RPC protocol name.
ProtocolName *string `json:"jsonrpc"`
// Identifier of request.
Id *string `json:"id"`
// Name of the requested RPC function (method, procedure).
Method *string `json:"method"`
// Arguments for the requested RPC function (method, procedure).
Parameters *json.RawMessage `json:"params"`
}
// NewRpcRequest is a constructor of a raw RPC request.
// It takes an input stream of bytes and decodes it using JSON format.
func NewRpcRequest(input io.ReadCloser) (rr *RpcRequest, err error) {
defer func() {
derr := input.Close()
if derr != nil {
rr = nil
err = errors.Combine(err, derr)
}
}()
rr = new(RpcRequest)
err = json.NewDecoder(input).Decode(rr)
if err != nil {
return nil, err
}
return rr, nil
}
// HasAllRootFields tells if all the root fields are set, i.e. are not null
// pointers.
func (r *RpcRequest) HasAllRootFields() bool {
if (r.ProtocolName == nil) ||
(r.Id == nil) ||
(r.Method == nil) ||
(r.Parameters == nil) {
return false
}
return true
}
// CheckProtocolVersion tells if the protocol version is correct.
func (r *RpcRequest) CheckProtocolVersion() (err error) {
if r.ProtocolName == nil {
return fmt.Errorf(ErrFUnsupportedProtocol, r.ProtocolName)
}
if *r.ProtocolName != ProtocolNameM1 {
return fmt.Errorf(ErrFUnsupportedProtocol, *r.ProtocolName)
}
return nil
}