mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-03 18:01:39 +00:00
(no commit message provided)
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package kss
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/onsonr/hway/crypto"
|
||||
"github.com/onsonr/hway/crypto/core/protocol"
|
||||
)
|
||||
|
||||
// KssI is the interface for the keyshare set
|
||||
type Set interface {
|
||||
PublicKey() crypto.PublicKey
|
||||
Usr() User
|
||||
Val() Val
|
||||
}
|
||||
|
||||
// keyshares is the set of keyshares for the protocol
|
||||
type keyshares struct {
|
||||
val Val
|
||||
usr User
|
||||
|
||||
valBz []byte
|
||||
usrBz []byte
|
||||
}
|
||||
|
||||
// Usr returns the user keyshare
|
||||
func (ks *keyshares) Usr() User {
|
||||
return ks.usr
|
||||
}
|
||||
|
||||
// Val returns the validator keyshare
|
||||
func (ks *keyshares) Val() Val {
|
||||
return ks.val
|
||||
}
|
||||
|
||||
// PublicKey returns the public key for the keyshare set
|
||||
func (ks *keyshares) PublicKey() crypto.PublicKey {
|
||||
return ks.val.PublicKey()
|
||||
}
|
||||
|
||||
// NewKeyshareSet creates a new KeyshareSet
|
||||
func NewKeyshareSet(aliceResult *protocol.Message, bobResult *protocol.Message) (Set, error) {
|
||||
valBz, err := json.Marshal(aliceResult)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
usrBz, err := json.Marshal(bobResult)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &keyshares{
|
||||
val: createValidatorKeyshare(aliceResult),
|
||||
usr: createUserKeyshare(bobResult),
|
||||
valBz: valBz,
|
||||
usrBz: usrBz,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// LoadKeyshareSet loads bytes into a keyshare set
|
||||
func LoadKeyshareSet(valBz []byte, usrBz []byte) (Set, error) {
|
||||
val, err := ValidatorKeyshareFromBytes(valBz)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
usr, err := UserKeyshareFromBytes(usrBz)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &keyshares{
|
||||
val: val,
|
||||
usr: usr,
|
||||
valBz: valBz,
|
||||
usrBz: usrBz,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UserKeyshareFromBytes unmarshals a user keyshare from its bytes representation
|
||||
func UserKeyshareFromBytes(bz []byte) (User, error) {
|
||||
val := &protocol.Message{}
|
||||
err := json.Unmarshal(bz, val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return createUserKeyshare(val), nil
|
||||
}
|
||||
|
||||
// ValidatorKeyshareFromBytes unmarshals a validator keyshare from its bytes representation
|
||||
func ValidatorKeyshareFromBytes(bz []byte) (Val, error) {
|
||||
val := &protocol.Message{}
|
||||
err := json.Unmarshal(bz, val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return createValidatorKeyshare(val), nil
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package kss
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/onsonr/hway/crypto"
|
||||
"github.com/onsonr/hway/crypto/core/curves"
|
||||
"github.com/onsonr/hway/crypto/core/protocol"
|
||||
"github.com/onsonr/hway/crypto/tecdsa/dklsv1"
|
||||
"github.com/onsonr/hway/x/did/types"
|
||||
"golang.org/x/crypto/sha3"
|
||||
)
|
||||
|
||||
// RefreshFuncUser is the type for the user refresh function
|
||||
type RefreshFuncUser = *dklsv1.BobRefresh
|
||||
|
||||
// SignFuncUser is the type for the user sign function
|
||||
type SignFuncUser = *dklsv1.BobSign
|
||||
|
||||
// User is the interface for the user keyshare
|
||||
type User interface {
|
||||
GetSignFunc(msg []byte) (SignFuncUser, error)
|
||||
GetRefreshFunc() (RefreshFuncUser, error)
|
||||
PublicKey() crypto.PublicKey
|
||||
Marshal() ([]byte, error)
|
||||
}
|
||||
|
||||
// userKeyshare is the protocol result for the user keyshare
|
||||
type userKeyshare struct {
|
||||
usrKSS *protocol.Message
|
||||
}
|
||||
|
||||
// createUserKeyshare creates a new UserKeyshare and stores it into IPFS
|
||||
func createUserKeyshare(usrKSS *protocol.Message) User {
|
||||
return &userKeyshare{
|
||||
usrKSS: usrKSS,
|
||||
}
|
||||
}
|
||||
|
||||
// Bytes returns the bytes of the user keyshare
|
||||
func (u *userKeyshare) Marshal() ([]byte, error) {
|
||||
return json.Marshal(u.usrKSS)
|
||||
}
|
||||
|
||||
// GetSignFunc returns the sign function for the user keyshare
|
||||
func (u *userKeyshare) GetSignFunc(msg []byte) (SignFuncUser, error) {
|
||||
curve := curves.K256()
|
||||
bobSign, err := dklsv1.NewBobSign(curve, sha3.New256(), msg, u.usrKSS, protocol.Version1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bobSign, nil
|
||||
}
|
||||
|
||||
// GetRefreshFunc returns the refresh function for the user keyshare
|
||||
func (u *userKeyshare) GetRefreshFunc() (RefreshFuncUser, error) {
|
||||
curve := curves.K256()
|
||||
bobRefresh, err := dklsv1.NewBobRefresh(curve, u.usrKSS, protocol.Version1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bobRefresh, nil
|
||||
}
|
||||
|
||||
// PublicKey is the public key for the keyshare
|
||||
func (u *userKeyshare) PublicKey() crypto.PublicKey {
|
||||
bobOut, err := dklsv1.DecodeBobDkgResult(u.usrKSS)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
pub := &types.PublicKey{
|
||||
Key: bobOut.PublicKey.ToAffineUncompressed(),
|
||||
KeyType: "ecdsa-secp256k1",
|
||||
}
|
||||
return pub
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package kss
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/onsonr/hway/crypto"
|
||||
"github.com/onsonr/hway/crypto/core/curves"
|
||||
"github.com/onsonr/hway/crypto/core/protocol"
|
||||
"github.com/onsonr/hway/crypto/tecdsa/dklsv1"
|
||||
"github.com/onsonr/hway/x/did/types"
|
||||
"golang.org/x/crypto/sha3"
|
||||
)
|
||||
|
||||
// RefreshFuncVal is the type for the validator refresh function
|
||||
type RefreshFuncVal = *dklsv1.AliceRefresh
|
||||
|
||||
// SignFuncVal is the type for the validator sign function
|
||||
type SignFuncVal = *dklsv1.AliceSign
|
||||
|
||||
// Val is the interface for the validator keyshare
|
||||
type Val interface {
|
||||
GetSignFunc(msg []byte) (SignFuncVal, error)
|
||||
GetRefreshFunc() (RefreshFuncVal, error)
|
||||
PublicKey() crypto.PublicKey
|
||||
Marshal() ([]byte, error)
|
||||
}
|
||||
|
||||
// validatorKeyshare is the protocol result for the validator keyshare
|
||||
type validatorKeyshare struct {
|
||||
valKSS *protocol.Message
|
||||
}
|
||||
|
||||
// createValidatorKeyshare creates a new ValidatorKeyshare
|
||||
func createValidatorKeyshare(valKSS *protocol.Message) Val {
|
||||
return &validatorKeyshare{
|
||||
valKSS: valKSS,
|
||||
}
|
||||
}
|
||||
|
||||
// Bytes returns the bytes of the user keyshare
|
||||
func (u *validatorKeyshare) Marshal() ([]byte, error) {
|
||||
return json.Marshal(u.valKSS)
|
||||
}
|
||||
|
||||
// GetSignFunc returns the sign function for the validator keyshare
|
||||
func (v *validatorKeyshare) GetSignFunc(msg []byte) (SignFuncVal, error) {
|
||||
curve := curves.K256()
|
||||
aliceSign, err := dklsv1.NewAliceSign(curve, sha3.New256(), msg, v.valKSS, protocol.Version1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return aliceSign, nil
|
||||
}
|
||||
|
||||
// GetRefreshFunc returns the refresh function for the validator keyshare
|
||||
func (v *validatorKeyshare) GetRefreshFunc() (RefreshFuncVal, error) {
|
||||
curve := curves.K256()
|
||||
aliceRefresh, err := dklsv1.NewAliceRefresh(curve, v.valKSS, protocol.Version1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return aliceRefresh, nil
|
||||
}
|
||||
|
||||
// PublicKey is the public key for the keyshare
|
||||
func (u *validatorKeyshare) PublicKey() crypto.PublicKey {
|
||||
aliceOut, err := dklsv1.DecodeAliceDkgResult(u.valKSS)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
pub := &types.PublicKey{
|
||||
Key: aliceOut.PublicKey.ToAffineUncompressed(),
|
||||
KeyType: "ecdsa-secp256k1",
|
||||
}
|
||||
return pub
|
||||
}
|
||||
Reference in New Issue
Block a user