/
rpc.go
104 lines (88 loc) · 2.19 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
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
package plugin
import (
"net/rpc"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
)
var _ Signer = (*rpcClient)(nil)
type rpcServer struct {
impl Signer
}
// RPCInitArgs is exposed entirely to placate `net/rpc`.
type RPCInitArgs struct {
Config string
Roles []signature.SignerRole
}
// RPCLoadArgs is exposed entirely to placate `net/rpc`.
type RPCLoadArgs struct {
Role signature.SignerRole
MustGenerate bool
}
// RPCContextSignArgs is exposed entirely to placate `net/rpc`.
type RPCContextSignArgs struct {
Role signature.SignerRole
RawContext signature.Context
Message []byte
}
func (m *rpcServer) Initialize(args *RPCInitArgs, resp *interface{}) error {
return m.impl.Initialize(args.Config, args.Roles...)
}
func (m *rpcServer) Load(args *RPCLoadArgs, resp *interface{}) error {
return m.impl.Load(args.Role, args.MustGenerate)
}
func (m *rpcServer) Public(role signature.SignerRole, resp *signature.PublicKey) error {
pk, err := m.impl.Public(role)
*resp = pk
return err
}
func (m *rpcServer) ContextSign(args *RPCContextSignArgs, resp *[]byte) error {
sig, err := m.impl.ContextSign(args.Role, args.RawContext, args.Message)
*resp = sig
return err
}
type rpcClient struct {
client *rpc.Client
}
func (m *rpcClient) Initialize(config string, roles ...signature.SignerRole) error {
var resp interface{}
return m.client.Call(
"Plugin.Initialize",
&RPCInitArgs{
Config: config,
Roles: roles,
},
&resp,
)
}
func (m *rpcClient) Load(role signature.SignerRole, mustGenerate bool) error {
var resp interface{}
return m.client.Call(
"Plugin.Load",
&RPCLoadArgs{
Role: role,
MustGenerate: mustGenerate,
},
&resp,
)
}
func (m *rpcClient) Public(role signature.SignerRole) (signature.PublicKey, error) {
var resp signature.PublicKey
err := m.client.Call(
"Plugin.Public",
role,
&resp,
)
return resp, err
}
func (m *rpcClient) ContextSign(role signature.SignerRole, rawContext signature.Context, message []byte) ([]byte, error) {
var resp []byte
err := m.client.Call(
"Plugin.ContextSign",
&RPCContextSignArgs{
Role: role,
RawContext: rawContext,
Message: message,
},
&resp,
)
return resp, err
}