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
-54
View File
@@ -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
}
-6
View File
@@ -1,6 +0,0 @@
package did
// Format is the formatter to use for DIDs
var Format = formatter{}
type formatter struct{}
-1
View File
@@ -1 +0,0 @@
package did
-9
View File
@@ -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"`
}
+1 -39
View File
@@ -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)
-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
}