From dcf502773a52c27d53b42d4eef0456d9f8738129 Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Sat, 31 Aug 2024 17:53:17 -0400 Subject: [PATCH] feat: add DID method for each coin --- pkg/coins/defaults.go | 54 ----------------------------- pkg/did/formatter.go | 6 ---- pkg/did/resolver.go | 1 - pkg/did/types.go | 9 ----- {pkg/coins => x/did/types}/coins.go | 40 +-------------------- x/did/types/keys.go | 37 -------------------- x/did/types/pub_key.go | 41 ++++++++++++++++++++++ 7 files changed, 42 insertions(+), 146 deletions(-) delete mode 100644 pkg/coins/defaults.go delete mode 100644 pkg/did/formatter.go delete mode 100644 pkg/did/resolver.go delete mode 100644 pkg/did/types.go rename {pkg/coins => x/did/types}/coins.go (54%) create mode 100644 x/did/types/pub_key.go diff --git a/pkg/coins/defaults.go b/pkg/coins/defaults.go deleted file mode 100644 index f85b7e151..000000000 --- a/pkg/coins/defaults.go +++ /dev/null @@ -1,54 +0,0 @@ -package coins - -import ( - "fmt" - - "github.com/cosmos/cosmos-sdk/types/bech32" -) - -type coin struct { - Name string `json:"name"` - Symbol string `json:"symbol"` - Hrp string `json:"hrp"` - Method string `json:"method"` - Index int64 `json:"index"` - Path uint32 `json:"path"` -} - -// FormatAddress formats the address based on the coin -func (c *coin) FormatAddress(pubKey []byte) (string, error) { - if c.Hrp != "" { - return bech32.ConvertAndEncode(c.Hrp, pubKey) - } - return "", fmt.Errorf("unsupported coin") -} - -// GetIndex returns the coin index -func (c *coin) GetIndex() int64 { - return c.Index -} - -// GetName returns the coin name -func (c *coin) GetName() string { - return c.Name -} - -// GetSymbol returns the coin symbol -func (c *coin) GetSymbol() string { - return c.Symbol -} - -// GetHrp returns the coin hrp -func (c *coin) GetHrp() string { - return c.Hrp -} - -// GetPath returns the coin path -func (c *coin) GetPath() uint32 { - return c.Path -} - -// GetMethod returns the DID method for the coin -func (c *coin) GetMethod() string { - return c.Method -} diff --git a/pkg/did/formatter.go b/pkg/did/formatter.go deleted file mode 100644 index 0cdc28307..000000000 --- a/pkg/did/formatter.go +++ /dev/null @@ -1,6 +0,0 @@ -package did - -// Format is the formatter to use for DIDs -var Format = formatter{} - -type formatter struct{} diff --git a/pkg/did/resolver.go b/pkg/did/resolver.go deleted file mode 100644 index 2bfcc86be..000000000 --- a/pkg/did/resolver.go +++ /dev/null @@ -1 +0,0 @@ -package did diff --git a/pkg/did/types.go b/pkg/did/types.go deleted file mode 100644 index 2c523a479..000000000 --- a/pkg/did/types.go +++ /dev/null @@ -1,9 +0,0 @@ -package did - -// VerificationMethod is an object that represents a verification method. -type VerificationMethod struct { - PublicKeyJwk map[string]interface{} `json:"publicKeyJwk"` - ID string `json:"id"` - Type string `json:"type"` - Controller string `json:"controller"` -} diff --git a/pkg/coins/coins.go b/x/did/types/coins.go similarity index 54% rename from pkg/coins/coins.go rename to x/did/types/coins.go index 73edd38e3..f5e03ab09 100644 --- a/pkg/coins/coins.go +++ b/x/did/types/coins.go @@ -1,4 +1,4 @@ -package coins +package types // Coin represents a cryptocurrency type Coin interface { @@ -21,44 +21,6 @@ type Coin interface { GetName() string } -// DefaultCoins is a list of default coins used in the vault -var DefaultCoins = []Coin{ - CoinBTC, - CoinETH, - CoinSNR, -} - -var ( - // Bitcoin mainnet - CoinBTC = &coin{ - Name: "Bitcoin", - Index: 0, - Path: 0x80000000, - Symbol: "BTC", - Hrp: "bc", - Method: "btcr", - } - - // Ethereum - CoinETH = &coin{ - Name: "Ethereum", - Index: 60, - Path: 0x8000003c, - Symbol: "ETH", - Method: "ethr", - } - - // Sonr - CoinSNR = &coin{ - Name: "Sonr", - Index: 703, - Path: 0x800002bf, - Symbol: "SNR", - Hrp: "idx", - Method: "sonr", - } -) - // CoinBTCType is the coin type for BTC const CoinBTCType = int64(0) diff --git a/x/did/types/keys.go b/x/did/types/keys.go index 1f1d7198b..024209038 100644 --- a/x/did/types/keys.go +++ b/x/did/types/keys.go @@ -1,13 +1,7 @@ package types import ( - fmt "fmt" - "math/big" - "cosmossdk.io/collections" - "github.com/onsonr/crypto/core/curves" - "github.com/onsonr/crypto/signatures/ecdsa" - "golang.org/x/crypto/sha3" ormv1alpha1 "cosmossdk.io/api/cosmos/orm/v1alpha1" ) @@ -29,34 +23,3 @@ var ORMModuleSchema = ormv1alpha1.ModuleSchemaDescriptor{ }, Prefix: []byte{0}, } - -// VerifySignature verifies the signature of a message -func VerifySignature(key []byte, msg []byte, sig []byte) bool { - pp, err := BuildEcPoint(key) - if err != nil { - return false - } - sigEd, err := ecdsa.DeserializeSecp256k1Signature(sig) - if err != nil { - return false - } - hash := sha3.New256() - _, err = hash.Write(msg) - if err != nil { - return false - } - digest := hash.Sum(nil) - return curves.VerifyEcdsa(pp, digest[:], sigEd) -} - -// BuildEcPoint builds an elliptic curve point from a compressed byte slice -func BuildEcPoint(pubKey []byte) (*curves.EcPoint, error) { - crv := curves.K256() - x := new(big.Int).SetBytes(pubKey[1:33]) - y := new(big.Int).SetBytes(pubKey[33:]) - ecCurve, err := crv.ToEllipticCurve() - if err != nil { - return nil, fmt.Errorf("error converting curve: %v", err) - } - return &curves.EcPoint{X: x, Y: y, Curve: ecCurve}, nil -} diff --git a/x/did/types/pub_key.go b/x/did/types/pub_key.go new file mode 100644 index 000000000..1ed57f2ce --- /dev/null +++ b/x/did/types/pub_key.go @@ -0,0 +1,41 @@ +package types + +import ( + fmt "fmt" + "math/big" + + "github.com/onsonr/crypto/core/curves" + "github.com/onsonr/crypto/signatures/ecdsa" + "golang.org/x/crypto/sha3" +) + +// VerifySignature verifies the signature of a message +func VerifySignature(key []byte, msg []byte, sig []byte) bool { + pp, err := BuildEcPoint(key) + if err != nil { + return false + } + sigEd, err := ecdsa.DeserializeSecp256k1Signature(sig) + if err != nil { + return false + } + hash := sha3.New256() + _, err = hash.Write(msg) + if err != nil { + return false + } + digest := hash.Sum(nil) + return curves.VerifyEcdsa(pp, digest[:], sigEd) +} + +// BuildEcPoint builds an elliptic curve point from a compressed byte slice +func BuildEcPoint(pubKey []byte) (*curves.EcPoint, error) { + crv := curves.K256() + x := new(big.Int).SetBytes(pubKey[1:33]) + y := new(big.Int).SetBytes(pubKey[33:]) + ecCurve, err := crv.ToEllipticCurve() + if err != nil { + return nil, fmt.Errorf("error converting curve: %v", err) + } + return &curves.EcPoint{X: x, Y: y, Curve: ecCurve}, nil +}