feature/1110 abstract connected wallet operations (#1166)

- **refactor: refactor DID module types and move to controller package**
- **refactor: move controller creation and resolution logic to keeper**
- **refactor: update imports to reflect controller package move**
- **refactor: update protobuf definitions for DID module**
- **docs: update proto README to reflect changes**
- **refactor: move hway to gateway, update node modules, and refactor
pkl generation**
- **build: update pkl-gen task to use new pkl file paths**
- **refactor: refactor DWN WASM build and deployment process**
- **refactor: refactor DID controller implementation to use
account-based storage**
- **refactor: move DID controller interface to base file and update
implementation**
- **chore: migrate to google protobuf**
- **feat: Add v0.52.0 Interfaces for Acc Abstraction**
- **refactor: replace public_key with public_key_hex in Assertion
message**
- **refactor: remove unused PubKey, JSONWebKey, and RawKey message types
and related code**
This commit is contained in:
Prad Nukala
2024-11-18 19:04:10 -05:00
committed by GitHub
parent 01cb37e82e
commit bf94277b0f
190 changed files with 9345 additions and 14038 deletions
+73
View File
@@ -0,0 +1,73 @@
package controller
import (
"github.com/onsonr/crypto/mpc"
commonv1 "github.com/onsonr/sonr/pkg/common/types"
"github.com/onsonr/sonr/x/did/types"
)
type ControllerI interface {
ChainID() string
GetPubKey() *commonv1.PubKey
SonrAddress() string
RawPublicKey() []byte
}
func New(shares []mpc.Share) (ControllerI, error) {
var (
valKs = shares[0]
userKs = shares[1]
)
pb, err := valKs.PublicKey()
if err != nil {
return nil, err
}
sonrAddr, err := types.ComputeSonrAddr(pb)
if err != nil {
return nil, err
}
return &controller{
valKs: valKs,
userKs: userKs,
address: sonrAddr,
publicKey: pb,
}, nil
}
type controller struct {
userKs mpc.Share
valKs mpc.Share
address string
chainID string
publicKey []byte
did string
}
func (c *controller) ChainID() string {
return c.chainID
}
func (c *controller) GetPubKey() *commonv1.PubKey {
return &commonv1.PubKey{
KeyType: "ecdsa",
RawKey: &commonv1.RawKey{
Algorithm: "secp256k1",
Key: c.publicKey,
},
Role: "authentication",
}
}
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
}
+1
View File
@@ -0,0 +1 @@
package signer
+1
View File
@@ -0,0 +1 @@
package signer
+29
View File
@@ -0,0 +1,29 @@
package signer
import (
"context"
signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"
"cosmossdk.io/x/tx/signing"
"github.com/cosmos/cosmos-sdk/types/tx"
)
type directHandler struct{}
func (s directHandler) Mode() signingv1beta1.SignMode {
return signingv1beta1.SignMode_SIGN_MODE_DIRECT_AUX
}
func (s directHandler) GetSignBytes(
_ context.Context,
signerData signing.SignerData,
txData signing.TxData,
) ([]byte, error) {
txDoc := tx.SignDoc{
BodyBytes: txData.BodyBytes,
AuthInfoBytes: txData.AuthInfoBytes,
ChainId: signerData.ChainID,
AccountNumber: signerData.AccountNumber,
}
return txDoc.Marshal()
}
+20
View File
@@ -0,0 +1,20 @@
package controller
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common/hexutil"
didv1 "github.com/onsonr/sonr/api/did/v1"
)
func LoadFromTableEntry(ctx sdk.Context, entry *didv1.Controller) (ControllerI, error) {
k, err := hexutil.Decode(entry.PublicKeyHex)
if err != nil {
return nil, err
}
return &controller{
address: entry.Did,
chainID: ctx.ChainID(),
publicKey: k,
}, nil
}
+140
View File
@@ -0,0 +1,140 @@
package wallet
import (
"bytes"
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/crypto/types"
"google.golang.org/protobuf/proto"
commonv1 "github.com/onsonr/sonr/pkg/common/types"
)
type PubKeyI interface {
GetRole() string
GetKeyType() string
GetRawKey() *commonv1.RawKey
GetJwk() *commonv1.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
}
// 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 sdk.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
}
+1
View File
@@ -0,0 +1 @@
package wallet
+1
View File
@@ -0,0 +1 @@
package wallet