From 6da6f2b97bd86be3047731ee232805c8627a4745 Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Fri, 18 Oct 2024 13:45:57 -0400 Subject: [PATCH] feat: add custom secp256k1 pubkey --- x/did/types/controller.go | 11 ++++- x/did/types/formatter.go | 1 + x/did/types/mpc/secp256k1_pk.go | 68 +++++++++++++++++++++++++++++ x/did/types/pubkey/pubkey.go | 76 +++++++++++++++++++++++++++++++++ x/macaroon/types/formatter.go | 1 + x/service/types/formatter.go | 1 + x/vault/types/formatter.go | 1 + 7 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 x/did/types/formatter.go create mode 100644 x/did/types/mpc/secp256k1_pk.go create mode 100644 x/did/types/pubkey/pubkey.go create mode 100644 x/macaroon/types/formatter.go create mode 100644 x/service/types/formatter.go create mode 100644 x/vault/types/formatter.go diff --git a/x/did/types/controller.go b/x/did/types/controller.go index e4d72dc9e..bffd7a0b9 100644 --- a/x/did/types/controller.go +++ b/x/did/types/controller.go @@ -5,6 +5,7 @@ import ( "github.com/onsonr/crypto/mpc" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" didv1 "github.com/onsonr/sonr/api/did/v1" ) @@ -15,7 +16,8 @@ type ControllerI interface { SonrAddress() string EthAddress() string BtcAddress() string - PublicKey() []byte + // RawPublicKey() []byte + StdPublicKey() cryptotypes.PubKey GetTableEntry() (*didv1.Controller, error) ExportUserKs() (string, error) } @@ -117,10 +119,15 @@ func (c *controller) GetTableEntry() (*didv1.Controller, error) { }, nil } -func (c *controller) PublicKey() []byte { +func (c *controller) RawPublicKey() []byte { return c.publicKey } +// +// func (c *controller) StdPublicKey() cryptotypes.PubKey { +// return c.stdPubKey +// } + func (c *controller) SonrAddress() string { return c.address } diff --git a/x/did/types/formatter.go b/x/did/types/formatter.go new file mode 100644 index 000000000..ab1254f4c --- /dev/null +++ b/x/did/types/formatter.go @@ -0,0 +1 @@ +package types diff --git a/x/did/types/mpc/secp256k1_pk.go b/x/did/types/mpc/secp256k1_pk.go new file mode 100644 index 000000000..dbebfa45e --- /dev/null +++ b/x/did/types/mpc/secp256k1_pk.go @@ -0,0 +1,68 @@ +package mpc + +import ( + "bytes" + "fmt" + + "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/golang/protobuf/proto" +) + +// CustomPubKey represents a custom secp256k1 public key. +type CustomPubKey struct { + proto.Message + + Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` +} + +// NewCustomPubKeyFromRawBytes creates a new CustomPubKey from raw bytes. +func NewCustomPubKeyFromRawBytes(key []byte) (*CustomPubKey, error) { + // Validate the key length and format + if len(key) != 33 { + return nil, fmt.Errorf("invalid key length; expected 33 bytes, got %d", len(key)) + } + if key[0] != 0x02 && key[0] != 0x03 { + return nil, fmt.Errorf("invalid key format; expected 0x02 or 0x03 as the first byte, got 0x%02x", key[0]) + } + + return &CustomPubKey{Key: key}, nil +} + +// Bytes returns the byte representation of the public key. +func (pk *CustomPubKey) Bytes() []byte { + return pk.Key +} + +// Equals checks if two public keys are equal. +func (pk *CustomPubKey) Equals(other types.PubKey) bool { + return bytes.EqualFold(pk.Bytes(), other.Bytes()) +} + +// Type returns the type of the public key. +func (pk *CustomPubKey) Type() string { + return "custom-secp256k1" +} + +// Marshal implements the proto.Message interface. +func (pk *CustomPubKey) Marshal() ([]byte, error) { + return proto.Marshal(pk) +} + +// Unmarshal implements the proto.Message interface. +func (pk *CustomPubKey) Unmarshal(data []byte) error { + return proto.Unmarshal(data, pk) +} + +// Address returns the address derived from the public key. +func (pk *CustomPubKey) Address() []byte { + // Implement address derivation logic here + // For simplicity, this example uses a placeholder + return []byte("derived-address") +} + +// VerifySignature verifies a signature using the public key. +func (pk *CustomPubKey) VerifySignature(msg []byte, sig []byte) bool { + // Implement signature verification logic here + // For simplicity, this example uses a placeholder + return true +} diff --git a/x/did/types/pubkey/pubkey.go b/x/did/types/pubkey/pubkey.go new file mode 100644 index 000000000..2cfd2866e --- /dev/null +++ b/x/did/types/pubkey/pubkey.go @@ -0,0 +1,76 @@ +package pubkey + +import ( + "strings" + + didv1 "github.com/onsonr/sonr/api/did/v1" +) + +type PubKeyI interface { + GetRole() string + GetKeyType() string + GetRawKey() *didv1.RawKey + GetJwk() *didv1.JSONWebKey +} + +// PubKey defines a generic pubkey. +type PublicKey interface { + VerifySignature(msg, sig []byte) bool +} + +type PubKeyG[T any] interface { + *T + PublicKey +} + +type pubKeyImpl struct { + decode func(b []byte) (PublicKey, error) + validate func(key PublicKey) error +} + +// func WithSecp256K1PubKey() Option { +// return WithPubKeyWithValidationFunc(func(pt *secp256k1.PubKey) error { +// _, err := dcrd_secp256k1.ParsePubKey(pt.Key) +// return err +// }) +// } +// +// func WithPubKey[T any, PT PubKeyG[T]]() Option { +// return WithPubKeyWithValidationFunc[T, PT](func(_ PT) error { +// return nil +// }) +// } +// +// func WithPubKeyWithValidationFunc[T any, PT PubKeyG[T]](validateFn func(PT) error) Option { +// pkImpl := pubKeyImpl{ +// decode: func(b []byte) (PublicKey, error) { +// key := PT(new(T)) +// err := gogoproto.Unmarshal(b, key) +// if err != nil { +// return nil, err +// } +// return key, nil +// }, +// validate: func(k PublicKey) error { +// concrete, ok := k.(PT) +// if !ok { +// return fmt.Errorf( +// "invalid pubkey type passed for validation, wanted: %T, got: %T", +// concrete, +// k, +// ) +// } +// return validateFn(concrete) +// }, +// } +// return func(a *Account) { +// a.supportedPubKeys[gogoproto.MessageName(PT(new(T)))] = pkImpl +// } +// } +func nameFromTypeURL(url string) string { + name := url + if i := strings.LastIndexByte(url, '/'); i >= 0 { + name = name[i+len("/"):] + } + return name +} diff --git a/x/macaroon/types/formatter.go b/x/macaroon/types/formatter.go new file mode 100644 index 000000000..ab1254f4c --- /dev/null +++ b/x/macaroon/types/formatter.go @@ -0,0 +1 @@ +package types diff --git a/x/service/types/formatter.go b/x/service/types/formatter.go new file mode 100644 index 000000000..ab1254f4c --- /dev/null +++ b/x/service/types/formatter.go @@ -0,0 +1 @@ +package types diff --git a/x/vault/types/formatter.go b/x/vault/types/formatter.go new file mode 100644 index 000000000..ab1254f4c --- /dev/null +++ b/x/vault/types/formatter.go @@ -0,0 +1 @@ +package types