mirror of
https://github.com/luxfi/crypto.git
synced 2026-07-27 01:54:50 +00:00
66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
// Copyright (C) 2020-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package rpcsigner
|
|
|
|
import (
|
|
"context"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/luxfi/crypto/bls"
|
|
|
|
pb "github.com/luxfi/node/proto/pb/signer"
|
|
)
|
|
|
|
var _ bls.Signer = (*Client)(nil)
|
|
|
|
type Client struct {
|
|
client pb.SignerClient
|
|
pk *bls.PublicKey
|
|
}
|
|
|
|
func NewClient(ctx context.Context, conn *grpc.ClientConn) (*Client, error) {
|
|
client := pb.NewSignerClient(conn)
|
|
|
|
pubkeyResponse, err := client.PublicKey(ctx, &pb.PublicKeyRequest{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pkBytes := pubkeyResponse.GetPublicKey()
|
|
pk, err := bls.PublicKeyFromCompressedBytes(pkBytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Client{
|
|
client: client,
|
|
pk: pk,
|
|
}, nil
|
|
}
|
|
|
|
func (c *Client) PublicKey() *bls.PublicKey {
|
|
return c.pk
|
|
}
|
|
|
|
func (c *Client) Sign(message []byte) (*bls.Signature, error) {
|
|
resp, err := c.client.Sign(context.TODO(), &pb.SignRequest{Message: message})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
signature := resp.GetSignature()
|
|
|
|
return bls.SignatureFromBytes(signature)
|
|
}
|
|
|
|
func (c *Client) SignProofOfPossession(message []byte) (*bls.Signature, error) {
|
|
resp, err := c.client.SignProofOfPossession(context.TODO(), &pb.SignProofOfPossessionRequest{Message: message})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
signature := resp.GetSignature()
|
|
|
|
return bls.SignatureFromBytes(signature)
|
|
}
|