Skip to content

Commit

Permalink
fixup! go/common/crypto/signature/signers/remote: Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Yawning committed Feb 26, 2020
1 parent d43f735 commit 49bbf38
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions go/common/crypto/signature/signers/remote/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (

serviceDesc = grpc.ServiceDesc{
ServiceName: string(serviceName),
HandlerType: (*wrapper)(nil),
HandlerType: (*Backend)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: methodPublicKeys.ShortName(),
Expand All @@ -53,11 +53,17 @@ type SignRequest struct {
Message []byte `json:"message"`
}

// Backend is the remote signer backend interface.
type Backend interface {
PublicKeys(context.Context) ([]PublicKey, error)
Sign(context.Context, *SignRequest) ([]byte, error)
}

type wrapper struct {
signers map[signature.SignerRole]signature.Signer
}

func (w *wrapper) publicKeys(ctx context.Context) ([]PublicKey, error) {
func (w *wrapper) PublicKeys(ctx context.Context) ([]PublicKey, error) {
var resp []PublicKey
for _, v := range signature.SignerRoles { // Return in consistent order.
if signer := w.signers[v]; signer != nil {
Expand All @@ -70,7 +76,7 @@ func (w *wrapper) publicKeys(ctx context.Context) ([]PublicKey, error) {
return resp, nil
}

func (w *wrapper) sign(ctx context.Context, req *SignRequest) ([]byte, error) {
func (w *wrapper) Sign(ctx context.Context, req *SignRequest) ([]byte, error) {
signer, ok := w.signers[req.Role]
if !ok {
return nil, signature.ErrNotExist
Expand All @@ -85,14 +91,14 @@ func handlerPublicKeys( // nolint: golint
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
if interceptor == nil {
return srv.(*wrapper).publicKeys(ctx)
return srv.(Backend).PublicKeys(ctx)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodPublicKeys.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(*wrapper).publicKeys(ctx)
return srv.(Backend).PublicKeys(ctx)
}
return interceptor(ctx, nil, info, handler)
}
Expand All @@ -108,14 +114,14 @@ func handlerSign( // nolint: golint
return nil, err
}
if interceptor == nil {
return srv.(*wrapper).sign(ctx, &req)
return srv.(Backend).Sign(ctx, &req)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodSign.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(*wrapper).sign(ctx, req.(*SignRequest))
return srv.(Backend).Sign(ctx, req.(*SignRequest))
}
return interceptor(ctx, &req, info, handler)
}
Expand All @@ -137,6 +143,7 @@ func RegisterService(server *grpc.Server, signerFactory signature.SignerFactory)
w.signers[v] = signer
}
}

server.RegisterService(&serviceDesc, w)
}

Expand Down

0 comments on commit 49bbf38

Please sign in to comment.