2024-12-02 14:27:18 -05:00
|
|
|
package mpc
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
|
2024-12-06 21:31:20 -05:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/bech32"
|
2024-12-02 14:27:18 -05:00
|
|
|
"github.com/onsonr/sonr/crypto/core/curves"
|
|
|
|
|
"github.com/onsonr/sonr/crypto/core/protocol"
|
|
|
|
|
"github.com/onsonr/sonr/crypto/tecdsa/dklsv1"
|
|
|
|
|
"golang.org/x/crypto/sha3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var ErrInvalidKeyshareRole = errors.New("invalid keyshare role")
|
|
|
|
|
|
|
|
|
|
type Role int
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
RoleUnknown Role = iota
|
|
|
|
|
RoleUser
|
|
|
|
|
RoleValidator
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (r Role) IsUser() bool {
|
|
|
|
|
return r == RoleUser
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r Role) IsValidator() bool {
|
|
|
|
|
return r == RoleValidator
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Message is the protocol.Message that is used for MPC
|
|
|
|
|
type Message *protocol.Message
|
|
|
|
|
|
|
|
|
|
type Signature *curves.EcdsaSignature
|
|
|
|
|
|
|
|
|
|
// RefreshFunc is the type for the refresh function
|
|
|
|
|
type RefreshFunc interface {
|
|
|
|
|
protocol.Iterator
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SignFunc is the type for the sign function
|
|
|
|
|
type SignFunc interface {
|
|
|
|
|
protocol.Iterator
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ValKeyshare struct {
|
2024-12-05 20:36:58 -05:00
|
|
|
BaseKeyshare
|
|
|
|
|
encoded string
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-06 21:31:20 -05:00
|
|
|
func computeSonrAddr(pk []byte) (string, error) {
|
|
|
|
|
sonrAddr, err := bech32.ConvertAndEncode("idx", pk)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return sonrAddr, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
func NewValKeyshare(msg *protocol.Message) (*ValKeyshare, error) {
|
2024-12-06 21:31:20 -05:00
|
|
|
vks := new(ValKeyshare)
|
2024-12-05 20:36:58 -05:00
|
|
|
encoded, err := protocol.EncodeMessage(msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-12-02 14:27:18 -05:00
|
|
|
valShare, err := dklsv1.DecodeAliceDkgResult(msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-12-06 21:31:20 -05:00
|
|
|
|
|
|
|
|
vks.BaseKeyshare = initFromAlice(valShare, msg)
|
|
|
|
|
vks.encoded = encoded
|
|
|
|
|
return vks, nil
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (v *ValKeyshare) RefreshFunc() (RefreshFunc, error) {
|
|
|
|
|
curve := curves.K256()
|
|
|
|
|
return dklsv1.NewAliceRefresh(curve, v.ExtractMessage(), protocol.Version1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (v *ValKeyshare) SignFunc(msg []byte) (SignFunc, error) {
|
|
|
|
|
curve := curves.K256()
|
|
|
|
|
return dklsv1.NewAliceSign(curve, sha3.New256(), msg, v.ExtractMessage(), protocol.Version1)
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
func (v *ValKeyshare) String() string {
|
|
|
|
|
return v.encoded
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PublicKey returns the uncompressed public key (65 bytes)
|
|
|
|
|
func (v *ValKeyshare) PublicKey() []byte {
|
2024-12-06 21:31:20 -05:00
|
|
|
return v.UncompressedPubKey
|
2024-12-05 20:36:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CompressedPublicKey returns the compressed public key (33 bytes)
|
|
|
|
|
func (v *ValKeyshare) CompressedPublicKey() []byte {
|
2024-12-06 21:31:20 -05:00
|
|
|
return v.CompressedPubKey
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UserKeyshare struct {
|
2024-12-05 20:36:58 -05:00
|
|
|
BaseKeyshare
|
|
|
|
|
encoded string
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
func NewUserKeyshare(msg *protocol.Message) (*UserKeyshare, error) {
|
2024-12-06 21:31:20 -05:00
|
|
|
uks := new(UserKeyshare)
|
2024-12-05 20:36:58 -05:00
|
|
|
encoded, err := protocol.EncodeMessage(msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-12-02 14:27:18 -05:00
|
|
|
out, err := dklsv1.DecodeBobDkgResult(msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-12-06 21:31:20 -05:00
|
|
|
|
|
|
|
|
uks.BaseKeyshare = initFromBob(out, msg)
|
|
|
|
|
uks.encoded = encoded
|
|
|
|
|
return uks, nil
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
func (u *UserKeyshare) RefreshFunc() (RefreshFunc, error) {
|
|
|
|
|
curve := curves.K256()
|
|
|
|
|
return dklsv1.NewBobRefresh(curve, u.ExtractMessage(), protocol.Version1)
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
func (u *UserKeyshare) SignFunc(msg []byte) (SignFunc, error) {
|
|
|
|
|
curve := curves.K256()
|
|
|
|
|
return dklsv1.NewBobSign(curve, sha3.New256(), msg, u.ExtractMessage(), protocol.Version1)
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
func (u *UserKeyshare) String() string {
|
|
|
|
|
return u.encoded
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
// PublicKey returns the uncompressed public key (65 bytes)
|
|
|
|
|
func (u *UserKeyshare) PublicKey() []byte {
|
2024-12-06 21:31:20 -05:00
|
|
|
return u.UncompressedPubKey
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
// CompressedPublicKey returns the compressed public key (33 bytes)
|
|
|
|
|
func (u *UserKeyshare) CompressedPublicKey() []byte {
|
2024-12-06 21:31:20 -05:00
|
|
|
return u.CompressedPubKey
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
func encodeMessage(m *protocol.Message) (string, error) {
|
|
|
|
|
return protocol.EncodeMessage(m)
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
func decodeMessage(s string) (*protocol.Message, error) {
|
|
|
|
|
return protocol.DecodeMessage(s)
|
2024-12-02 14:27:18 -05:00
|
|
|
}
|