mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-02 17:31:39 +00:00
* refactor: rename project from onsonr/sonr to sonr-io/snrd (#1249) * refactor: rename project from onsonr/sonr to sonr-io/snrd * refactor: update package paths to use sonr-io org --------- Co-authored-by: Prad N <prad@didao.xyz> * feat: introduce devbox for consistent development environment * feat: integrate Doppler for secrets management * feat: streamline release process with Devbox * <no value> * bump: version 0.6.3 → 0.6.4 * feat: enable commit-less version bumping * bump: version 0.6.4 → 0.6.5 * feat: streamline release process and update project metadata * feat: streamline build process and configuration * feat: streamline build process by removing release target * bump: version 0.6.5 → 0.6.6 * feat: upgrade to go 1.24 and align binary name * bump: version 0.6.6 → 0.6.7 * feat: streamline release process and update project metadata * bump: version 0.6.3 → 0.6.4 * feat: streamline development and release processes * feat: streamline development and release processes * refactor: streamline release process with Taskfile * feat: enhance goreleaser output with title for better UX * feat: consolidate release notes * bump: version 0.6.3 → 0.6.4 * feat: streamline release process with improved automation * feat: streamline development and release workflow * bump: version 0.6.3 → 0.6.4 * feat: streamline release process with Taskfile and conventional commits * bump: version 0.6.4 → 0.6.5 * feat: enhance Docker image publishing with piped credentials * chore: remove outdated changelog entries * bump: version 0.6.3 → 0.6.4 * feat: improve release process with interactive feedback * feat: enhance release process with real-time version feedback * bump: version 0.6.4 → 0.6.5 * feat: enhance release process with dynamic version display and streamlined automation * bump: version 0.6.3 → 0.6.4 * refactor: streamline release process by removing redundant version display * bump: version 0.6.4 → 0.6.5 * refactor: streamline release process and simplify dev environment * bump: version 0.6.3 → 0.6.4 * ci: simplify PR checks by removing redundant release validation * refactor: consolidate release workflows for improved maintainability * ci: enhance release workflow for automated version publishing --------- Co-authored-by: Prad N <prad@didao.xyz>
79 lines
2.5 KiB
Go
79 lines
2.5 KiB
Go
package accounts
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/sonr-io/snrd/internal/transaction"
|
|
)
|
|
|
|
// RegisterInitHandler registers an initialisation handler for a smart account that uses protobuf.
|
|
func RegisterInitHandler[
|
|
Req any, ProtoReq ProtoMsgG[Req], Resp any, ProtoResp ProtoMsgG[Resp],
|
|
](router *InitBuilder, handler func(ctx context.Context, req ProtoReq) (ProtoResp, error),
|
|
) {
|
|
reqName := MessageName(ProtoReq(new(Req)))
|
|
|
|
router.handler = func(ctx context.Context, initRequest transaction.Msg) (initResponse transaction.Msg, err error) {
|
|
concrete, ok := initRequest.(ProtoReq)
|
|
if !ok {
|
|
return nil, fmt.Errorf("%w: wanted %s, got %T", errInvalidMessage, reqName, initRequest)
|
|
}
|
|
return handler(ctx, concrete)
|
|
}
|
|
|
|
router.schema = HandlerSchema{
|
|
RequestSchema: *NewProtoMessageSchema[Req, ProtoReq](),
|
|
ResponseSchema: *NewProtoMessageSchema[Resp, ProtoResp](),
|
|
}
|
|
}
|
|
|
|
// RegisterExecuteHandler registers an execution handler for a smart account that uses protobuf.
|
|
func RegisterExecuteHandler[
|
|
Req any, ProtoReq ProtoMsgG[Req], Resp any, ProtoResp ProtoMsgG[Resp],
|
|
](router *ExecuteBuilder, handler func(ctx context.Context, req ProtoReq) (ProtoResp, error),
|
|
) {
|
|
reqName := MessageName(ProtoReq(new(Req)))
|
|
// check if not registered already
|
|
if _, ok := router.handlers[reqName]; ok {
|
|
router.err = fmt.Errorf("handler already registered for message %s", reqName)
|
|
return
|
|
}
|
|
|
|
router.handlers[reqName] = func(ctx context.Context, executeRequest transaction.Msg) (executeResponse transaction.Msg, err error) {
|
|
concrete, ok := executeRequest.(ProtoReq)
|
|
if !ok {
|
|
return nil, fmt.Errorf("%w: wanted %s, got %T", errInvalidMessage, reqName, executeRequest)
|
|
}
|
|
return handler(ctx, concrete)
|
|
}
|
|
|
|
router.handlersSchema[reqName] = HandlerSchema{
|
|
RequestSchema: *NewProtoMessageSchema[Req, ProtoReq](),
|
|
ResponseSchema: *NewProtoMessageSchema[Resp, ProtoResp](),
|
|
}
|
|
}
|
|
|
|
// RegisterQueryHandler registers a query handler for a smart account that uses protobuf.
|
|
func RegisterQueryHandler[
|
|
Req any, ProtoReq ProtoMsgG[Req], Resp any, ProtoResp ProtoMsgG[Resp],
|
|
](router *QueryBuilder, handler func(ctx context.Context, req ProtoReq) (ProtoResp, error),
|
|
) {
|
|
RegisterExecuteHandler(router.er, handler)
|
|
}
|
|
|
|
func NewProtoMessageSchema[T any, PT ProtoMsgG[T]]() *MessageSchema {
|
|
msg := PT(new(T))
|
|
if _, ok := (interface{}(msg)).(proto.Message); ok {
|
|
panic("protov2 messages are not supported")
|
|
}
|
|
return &MessageSchema{
|
|
Name: MessageName(msg),
|
|
New: func() transaction.Msg {
|
|
return PT(new(T))
|
|
},
|
|
}
|
|
}
|