2022-06-07 19:06:15 +02:00

32 lines
892 B
Go

package client
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/google/uuid"
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
"google.golang.org/grpc"
)
// Connect connects to the kaspawalletd server, and returns the client instance
func Connect(address string) (pb.KaspawalletdClient, func(), uuid.UUID, error) {
// Connection is local, so 1 second timeout is sufficient
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return nil, nil, uuid.UUID{}, errors.New("kaspawallet daemon is not running, start it with `kaspawallet start-daemon`")
}
return nil, nil, uuid.UUID{}, err
}
return pb.NewKaspawalletdClient(conn), func() {
conn.Close()
}, uuid.New(), nil
}