Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions cmd/migration/update-keyinfo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"fmt"
"log"
"strings"

"github.com/cryptoniumX/mpcium/pkg/config"
"github.com/cryptoniumX/mpcium/pkg/logger"
"github.com/hashicorp/consul/api"
)

// script to add key type prefix ecdsa for existing keys
func main() {
config.InitViperConfig()
logger.Init("production")
appConfig := config.LoadConfig()
logger.Info("App config", "config", appConfig)

consulClient := NewConsulClient(appConfig.Consul.Address)
kv := consulClient.KV()
// Get a handle to the KV API
// List all keys with the specified prefix
prefix := "threshold_keyinfo"
keys, _, err := kv.Keys(prefix, "", nil)
if err != nil {
log.Fatal(err)
}

// Print the keys
// fmt.Printf("Keys with prefix \"%s\":\n", prefix)
for _, key := range keys {
if !strings.Contains(key, "ecdsa") && !strings.Contains(key, "eddsa") {
pair, _, err := kv.Get(key, nil)
if err != nil {
log.Fatal(err)
}

// Define variables to store the extracted values
var walletID string

// Use fmt.Sscanf to extract the values
_, err = fmt.Sscanf(key, "threshold_keyinfo/%s", &walletID)
if err != nil {
fmt.Println("Error:", err)
return
}

if walletID != "" {
kv.Put(&api.KVPair{Key: fmt.Sprintf("threshold_keyinfo/ecdsa:%s", walletID), Value: pair.Value}, nil)
kv.Delete(key, nil)

}

}

fmt.Println(key)
}

}

func NewConsulClient(addr string) *api.Client {
// Create a new Consul client
consulConfig := api.DefaultConfig()
consulConfig.Address = addr
consulClient, err := api.NewClient(consulConfig)
if err != nil {
logger.Fatal("Failed to create consul client", err)
}
logger.Info("Connected to consul!")
return consulClient
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/avast/retry-go v3.0.0+incompatible
github.com/bnb-chain/tss-lib/v2 v2.0.2
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/decred/base58 v1.0.5
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.3
github.com/dgraph-io/badger/v4 v4.2.0
github.com/google/uuid v1.4.0
Expand All @@ -27,7 +26,6 @@ require (
github.com/btcsuite/btcutil v1.0.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/base58 v1.0.5 h1:hwcieUM3pfPnE/6p3J100zoRfGkQxBulZHo7GZfOqic=
github.com/decred/base58 v1.0.5/go.mod h1:s/8lukEHFA6bUQQb/v3rjUySJ2hu+RioCzLukAVkrfw=
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y=
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.3 h1:l/lhv2aJCUignzls81+wvga0TFlyoZx8QxRMQgXpZik=
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.3/go.mod h1:AKpV6+wZ2MfPRJnTbQ6NPgWrKzbe9RCIlCF/FKzMtM8=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
Expand Down
31 changes: 23 additions & 8 deletions pkg/eventconsumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,28 @@ func (ec *eventConsumer) consumeTxSigningEvent() error {

logger.Info("Received signing event", "waleltID", msg.WalletID, "tx", msg.Tx)
threshold := 1
session, err := ec.node.CreateSigningSession(
msg.WalletID,
msg.TxID,
msg.NetworkInternalCode,
threshold,
ec.signingSuccessQueue,
)

var session mpc.ISigningSession
switch msg.KeyType {
case KeyTypeSecp256k1:
session, err = ec.node.CreateSigningSession(
msg.WalletID,
msg.TxID,
msg.NetworkInternalCode,
threshold,
ec.signingSuccessQueue,
)
case KeyTypeEd25519:
session, err = ec.node.CreateEDDSASigningSession(
msg.WalletID,
msg.TxID,
msg.NetworkInternalCode,
threshold,
ec.signingSuccessQueue,
)

}

if err != nil {
logger.Error("Failed to create signing session", err)
return
Expand All @@ -192,7 +207,7 @@ func (ec *eventConsumer) consumeTxSigningEvent() error {
select {
case <-ctx.Done():
return
case err := <-session.ErrCh:
case err := <-session.ErrChan():
logger.Error("Signing session error", err)
}
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/eventconsumer/events.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package eventconsumer

type KeyType string

const (
KeyTypeSecp256k1 KeyType = "secp256k1"
KeyTypeEd25519 = "ed25519"
)

type SignTxMessage struct {
WalletID string `json:"wallet_id"`
NetworkInternalCode string `json:"network_internal_code"`
TxID string `json:"tx_id"`
Tx []byte `json:"tx"`
KeyType KeyType `json:"key_type"`
WalletID string `json:"wallet_id"`
NetworkInternalCode string `json:"network_internal_code"`
TxID string `json:"tx_id"`
Tx []byte `json:"tx"`
}
29 changes: 25 additions & 4 deletions pkg/mpc/signing_session.go → pkg/mpc/ecdsa_signing_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
SignSuccessTopic = "mpc.mpc_sign_success.completed"
)

// Ecdsa signing session
type SigningSession struct {
Session
endCh chan *common.SignatureData
Expand All @@ -31,13 +32,28 @@ type SigningSession struct {
networkInternalCode string
}

type ISession interface {
ErrChan() <-chan error
ListenToIncomingMessageAsync()
}

type ISigningSession interface {
ISession

Init(tx *big.Int) error
Sign(done func())
}

type SigningSuccessEvent struct {
NetworkInternalCode string `json:"network_internal_code"`
WalletID string `json:"wallet_id"`
TxID string `json:"tx_id"`
R []byte `json:"r"`
S []byte `json:"s"`
SignatureRecovery []byte `json:"signature_recovery"`

// TODO: define two separate events for eddsa and ecdsa
Signature []byte `json:"signature"`
}

func NewSigningSession(
Expand Down Expand Up @@ -71,12 +87,16 @@ func NewSigningSession(
keyinfoStore: keyinfoStore,
topicComposer: &TopicComposer{
ComposeBroadcastTopic: func() string {
return fmt.Sprintf("sign:broadcast:%s", walletID)
return fmt.Sprintf("sign:ecdsa:broadcast:%s", walletID)
},
ComposeDirectTopic: func(nodeID string) string {
return fmt.Sprintf("sign:direct:%s:%s", walletID, nodeID)
return fmt.Sprintf("sign:ecdsa:direct:%s:%s", walletID, nodeID)
},
},
composeKey: func(waleltID string) string {
return fmt.Sprintf("ecdsa:%s", waleltID)
},
getRoundFunc: GetEcdsaMsgRound,
successQueue: succesQueue,
},
endCh: make(chan *common.SignatureData),
Expand All @@ -90,12 +110,12 @@ func (s *SigningSession) Init(tx *big.Int) error {
ctx := tss.NewPeerContext(s.partyIDs)
params := tss.NewParameters(tss.S256(), ctx, s.selfPartyID, len(s.partyIDs), s.threshold)

keyData, err := s.kvstore.Get(s.walletID)
keyData, err := s.kvstore.Get(s.composeKey(s.walletID))
if err != nil {
return errors.Wrap(err, "Failed to get wallet data from KVStore")
}

keyInfo, err := s.keyinfoStore.Get(s.walletID)
keyInfo, err := s.keyinfoStore.Get(s.composeKey(s.walletID))
if err != nil {
return errors.Wrap(err, "Failed to get key info data")
}
Expand Down Expand Up @@ -176,6 +196,7 @@ func (s *SigningSession) Sign(done func()) {
})
if err != nil {
s.ErrCh <- errors.Wrap(err, "Failed to publish sign success message")

return
}

Expand Down
43 changes: 27 additions & 16 deletions pkg/mpc/eddsa_rounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mpc

import (
"github.com/bnb-chain/tss-lib/v2/eddsa/keygen"
"github.com/bnb-chain/tss-lib/v2/eddsa/signing"
"github.com/bnb-chain/tss-lib/v2/tss"
"github.com/cryptoniumX/mpcium/pkg/common/errors"
)
Expand All @@ -15,22 +16,14 @@ type RoundInfo struct {
}

const (
EDDSA_KEYGEN1 = "KGRound1Message"
EDDSA_KEYGEN2aUnicast = "KGRound2Message1"
EDDSA_KEYGEN2b = "KGRound2Message2"
// KEYGEN3 = "KGRound3Message"
// KEYSIGN1aUnicast = "SignRound1Message1"
// KEYSIGN1b = "SignRound1Message2"
// KEYSIGN2Unicast = "SignRound2Message"
// KEYSIGN3 = "SignRound3Message"
// KEYSIGN4 = "SignRound4Message"
// KEYSIGN5 = "SignRound5Message"
// KEYSIGN6 = "SignRound6Message"
// KEYSIGN7 = "SignRound7Message"
// KEYSIGN8 = "SignRound8Message"
// KEYSIGN9 = "SignRound9Message"
EDDSA_TSSKEYGENROUNDS = 3
// TSSKEYSIGNROUNDS = 10
EDDSA_KEYGEN1 = "KGRound1Message"
EDDSA_KEYGEN2aUnicast = "KGRound2Message1"
EDDSA_KEYGEN2b = "KGRound2Message2"
EDDSA_KEYSIGN1 = "SignRound1Message"
EDDSA_KEYSIGN2 = "SignRound2Message"
EDDSA_KEYSIGN3 = "SignRound3Message"
EDDSA_TSSKEYGENROUNDS = 3
EDDSA_TSSKEYSIGNROUNDS = 3
)

func GetEddsaMsgRound(msg []byte, partyID *tss.PartyID, isBroadcast bool) (RoundInfo, error) {
Expand All @@ -57,6 +50,24 @@ func GetEddsaMsgRound(msg []byte, partyID *tss.PartyID, isBroadcast bool) (Round
RoundMsg: EDDSA_KEYGEN2b,
}, nil

case *signing.SignRound1Message:
return RoundInfo{
Index: 0,
RoundMsg: EDDSA_KEYSIGN1,
}, nil

case *signing.SignRound2Message:
return RoundInfo{
Index: 0,
RoundMsg: EDDSA_KEYSIGN2,
}, nil

case *signing.SignRound3Message:
return RoundInfo{
Index: 0,
RoundMsg: EDDSA_KEYSIGN3,
}, nil

default:
return RoundInfo{}, errors.New("unknown round")
}
Expand Down
Loading