/
rpc.go
66 lines (55 loc) · 1.52 KB
/
rpc.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
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package modules
import (
"net/http"
)
var (
// UnsafeMethods is a list of all unsafe rpc methods of https://github.com/w3f/PSPs/blob/master/PSPs/drafts/psp-6.md
UnsafeMethods = []string{
"system_addReservedPeer",
"system_removeReservedPeer",
"author_submitExtrinsic",
"author_removeExtrinsic",
"author_insertKey",
"author_rotateKeys",
"state_getPairs",
"state_getKeysPaged",
"state_queryStorage",
"state_trie",
}
// AliasesMethods is a map that links the original methods to their aliases
AliasesMethods = map[string]string{
"chain_getHead": "chain_getBlockHash",
"account_nextIndex": "system_accountNextIndex",
"chain_getFinalisedHead": "chain_getFinalizedHead",
}
)
// RPCModule is a RPC module providing access to RPC methods
type RPCModule struct {
rPCAPI RPCAPI
}
// MethodsResponse struct representing methods
type MethodsResponse struct {
Methods []string `json:"methods"`
}
// NewRPCModule creates a new RPC api module
func NewRPCModule(rpcapi RPCAPI) *RPCModule {
return &RPCModule{
rPCAPI: rpcapi,
}
}
// Methods responds with list of methods available via RPC call
func (rm *RPCModule) Methods(r *http.Request, req *EmptyRequest, res *MethodsResponse) error {
res.Methods = rm.rPCAPI.Methods()
return nil
}
// IsUnsafe returns true if the `name` has the suffix
func IsUnsafe(name string) bool {
for _, unsafe := range UnsafeMethods {
if name == unsafe {
return true
}
}
return false
}