-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add logging and tracing middleware and slight reorg (#10)
- Loading branch information
Showing
15 changed files
with
212 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,3 +100,4 @@ workflows: | |
only: | ||
- main | ||
- dev | ||
- logging-middleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package tracing | ||
|
||
import ( | ||
"context" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type TraceContextKey string | ||
|
||
const TraceInfoKey = TraceContextKey("requestTracingInfo") | ||
const TraceIdKey = TraceContextKey("requestTraceId") | ||
|
||
type SpanDetail struct { | ||
Name string | ||
Duration int64 | ||
} | ||
|
||
type TracingInfo struct { | ||
SpanDetails []SpanDetail | ||
} | ||
|
||
func AttachTracingIntoContext(ctx context.Context) context.Context { | ||
// Attach traceId into context | ||
traceID := uuid.New().String() | ||
ctx = context.WithValue(ctx, TraceIdKey, traceID) | ||
|
||
// Start tracingInfo | ||
return context.WithValue(ctx, TraceInfoKey, &TracingInfo{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
s "github.com/babylonchain/covenant-signer/signerapp" | ||
"net/http" | ||
) | ||
|
||
type Handler struct { | ||
s *s.SignerApp | ||
} | ||
|
||
type Result struct { | ||
Data interface{} | ||
Status int | ||
} | ||
|
||
type PublicResponse[T any] struct { | ||
Data T `json:"data"` | ||
} | ||
|
||
func NewResult[T any](data T) *Result { | ||
res := &PublicResponse[T]{Data: data} | ||
return &Result{Data: res, Status: http.StatusOK} | ||
} | ||
|
||
func NewHandler( | ||
_ context.Context, s *s.SignerApp, | ||
) (*Handler, error) { | ||
return &Handler{ | ||
s: s, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
"github.com/babylonchain/covenant-signer/signerservice/types" | ||
"github.com/babylonchain/covenant-signer/utils" | ||
"github.com/btcsuite/btcd/btcec/v2" | ||
"net/http" | ||
) | ||
|
||
func (h *Handler) SignUnbonding(request *http.Request) (*Result, *types.Error) { | ||
payload := &types.SignUnbondingTxRequest{} | ||
err := json.NewDecoder(request.Body).Decode(payload) | ||
if err != nil { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "invalid request payload") | ||
} | ||
|
||
pkScript, err := hex.DecodeString(payload.StakingOutputPkScriptHex) | ||
|
||
if err != nil { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "invalid staking output pk script") | ||
} | ||
|
||
covenantPublicKeyBytes, err := hex.DecodeString(payload.CovenantPublicKey) | ||
|
||
if err != nil { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "invalid covenant public key") | ||
} | ||
|
||
covenantPublicKey, err := btcec.ParsePubKey(covenantPublicKeyBytes) | ||
|
||
if err != nil { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "invalid covenant public key") | ||
} | ||
|
||
unbondingTx, _, err := utils.NewBTCTxFromHex(payload.UnbondingTxHex) | ||
|
||
if err != nil { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "invalid unbonding transaction") | ||
} | ||
|
||
sig, err := h.s.SignUnbondingTransaction( | ||
request.Context(), | ||
pkScript, | ||
unbondingTx, | ||
covenantPublicKey, | ||
) | ||
|
||
if err != nil { | ||
// TODO Properly translate errors between layers | ||
return nil, types.NewErrorWithMsg(http.StatusInternalServerError, types.InternalServiceError, err.Error()) | ||
} | ||
|
||
resp := types.SignUnbondingTxResponse{ | ||
SignatureHex: hex.EncodeToString(sig.Serialize()), | ||
} | ||
|
||
return NewResult(resp), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.