Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add endpoint to retrieve public key #36

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
signer sub module
* [#31](https://github.com/babylonlabs-io/covenant-emulator/pull/31/) Bump docker workflow
version, fix some Dockerfile issue
* [#36](https://github.com/babylonlabs-io/covenant-emulator/pull/36) Add public key
endpoint to the remote signer

## v0.8.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ func buildDataToSign(t *testing.T, covnenantPublicKey *btcec.PublicKey) signerap
}
}

func TestGetPublicKey(t *testing.T) {
tm := StartManager(t, 100)

pubKey, err := signerservice.GetPublicKey(context.Background(), tm.SigningServerUrl(), 10*time.Second)
require.NoError(t, err)
require.NotNil(t, pubKey)

pubKeyBytes := pubKey.SerializeCompressed()
require.Equal(t, hex.EncodeToString(pubKeyBytes), hex.EncodeToString(tm.covenantPrivKey.PubKey().SerializeCompressed()))
}

func TestSigningTransactions(t *testing.T) {
tm := StartManager(t, 100)

Expand Down
10 changes: 9 additions & 1 deletion covenant-signer/signerapp/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (s *SignerApp) SignTransactions(
ctx context.Context,
req *ParsedSigningRequest,
) (*ParsedSigningResponse, error) {

privKey, err := s.pkr.PrivKey(ctx)

defer func() {
Expand Down Expand Up @@ -77,7 +76,16 @@ func (s *SignerApp) SignTransactions(
UnbondingSig: unbondingSig,
SlashUnbondingAdaptorSigs: slashUnbondingSigs,
}, nil
}

func (s *SignerApp) PubKey(ctx context.Context) (*btcec.PublicKey, error) {
privKey, err := s.pkr.PrivKey(ctx)
if err != nil {
return nil, err
}
defer privKey.Zero()

return privKey.PubKey(), nil
}

func slashUnbondSig(
Expand Down
45 changes: 45 additions & 0 deletions covenant-signer/signerservice/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package signerservice
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerapp"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerservice/handlers"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerservice/types"
"github.com/btcsuite/btcd/btcec/v2"
)

const (
Expand Down Expand Up @@ -79,3 +81,46 @@ func RequestCovenantSignaure(

return types.ToParsedSigningResponse(&response.Data)
}

func GetPublicKey(ctx context.Context, signerUrl string, timeout time.Duration) (*btcec.PublicKey, error) {
route := fmt.Sprintf("%s/v1/public-key", signerUrl)

httpRequest, err := http.NewRequestWithContext(ctx, "GET", route, nil)

if err != nil {
return nil, err
}

client := http.Client{Timeout: timeout}
res, err := client.Do(httpRequest)

if err != nil {
return nil, err
}

defer res.Body.Close()

maxSizeReader := http.MaxBytesReader(nil, res.Body, maxResponseSize)

resBody, err := io.ReadAll(maxSizeReader)

if err != nil {
return nil, err
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("public key request failed. status code: %d, message: %s", res.StatusCode, string(resBody))
}

var response handlers.PublicResponse[types.GetPublicKeyResponse]
if err := json.Unmarshal(resBody, &response); err != nil {
return nil, err
}

pubKey, err := hex.DecodeString(response.Data.PublicKey)
if err != nil {
return nil, err
}

return btcec.ParsePubKey(pubKey)
}
14 changes: 14 additions & 0 deletions covenant-signer/signerservice/handlers/signtransactions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"encoding/hex"
"encoding/json"
"net/http"

Expand Down Expand Up @@ -40,3 +41,16 @@ func (h *Handler) SignTransactions(request *http.Request) (*Result, *types.Error

return NewResult(resp), nil
}

func (h *Handler) GetPublicKey(request *http.Request) (*Result, *types.Error) {
pubKey, err := h.s.PubKey(request.Context())
if err != nil {
return nil, types.NewErrorWithMsg(http.StatusInternalServerError, types.InternalServiceError, err.Error())
}

resp := &types.GetPublicKeyResponse{
PublicKey: hex.EncodeToString(pubKey.SerializeCompressed()),
}

return NewResult(resp), nil
}
1 change: 1 addition & 0 deletions covenant-signer/signerservice/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type SigningServer struct {
func (a *SigningServer) SetupRoutes(r *chi.Mux) {
handler := a.handler
r.Post("/v1/sign-transactions", registerHandler(handler.SignTransactions))
r.Get("/v1/public-key", registerHandler(handler.GetPublicKey))
}

func New(
Expand Down
6 changes: 6 additions & 0 deletions covenant-signer/signerservice/types/publickey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package types

type GetPublicKeyResponse struct {
// hex encoded 33 byte public key
PublicKey string `json:"public_key_hex"`
}