feat: add DID method for each coin

This commit is contained in:
Prad Nukala
2024-08-31 17:53:17 -04:00
parent ec399eb275
commit dcf502773a
7 changed files with 42 additions and 146 deletions
+31
View File
@@ -0,0 +1,31 @@
package types
// Coin represents a cryptocurrency
type Coin interface {
// FormatAddress formats a public key into an address
FormatAddress(pubKey []byte) (string, error)
// GetIndex returns the coin type index
GetIndex() int64
// GetPath returns the coin component path
GetPath() uint32
// GetSymbol returns the coin symbol
GetSymbol() string
// GetMethod returns the coin DID method
GetMethod() string
// GetName returns the coin name
GetName() string
}
// CoinBTCType is the coin type for BTC
const CoinBTCType = int64(0)
// CoinETHType is the coin type for ETH
const CoinETHType = int64(60)
// CoinSNRType is the coin type for SNR
const CoinSNRType = int64(703)
-37
View File
@@ -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
}
+41
View File
@@ -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
}