Skip to content

Commit 4206b60

Browse files
committed
Fixed comments
1 parent 6e77caf commit 4206b60

5 files changed

Lines changed: 47 additions & 25 deletions

File tree

cmd/mpcium-cli/generate-initiator.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111
"os/user"
1212
"path/filepath"
1313
"runtime"
14+
"slices"
1415
"time"
1516

1617
"filippo.io/age"
1718
"github.com/fystack/mpcium/pkg/common/pathutil"
1819
"github.com/fystack/mpcium/pkg/encryption"
20+
"github.com/fystack/mpcium/pkg/types"
1921
"github.com/urfave/cli/v3"
2022
)
2123

@@ -38,11 +40,18 @@ func generateInitiatorIdentity(ctx context.Context, c *cli.Command) error {
3840
algorithm := c.String("algorithm")
3941

4042
if algorithm == "" {
41-
algorithm = "ed25519"
43+
algorithm = string(types.KeyTypeEd25519)
4244
}
4345

44-
if algorithm != "ed25519" && algorithm != "p256" {
45-
return fmt.Errorf("invalid algorithm: %s. Must be 'ed25519' or 'p256'", algorithm)
46+
if !slices.Contains(
47+
[]string{string(types.KeyTypeEd25519), string(types.KeyTypeP256)},
48+
algorithm,
49+
) {
50+
return fmt.Errorf("invalid algorithm: %s. Must be %s or %s",
51+
algorithm,
52+
types.KeyTypeEd25519,
53+
types.KeyTypeP256,
54+
)
4655
}
4756

4857
// Create output directory if it doesn't exist
@@ -81,9 +90,9 @@ func generateInitiatorIdentity(ctx context.Context, c *cli.Command) error {
8190
var keyData encryption.KeyData
8291
var err error
8392

84-
if algorithm == "ed25519" {
93+
if algorithm == string(types.KeyTypeEd25519) {
8594
keyData, err = generateEd25519Keys()
86-
} else {
95+
} else if algorithm == string(types.KeyTypeP256) {
8796
keyData, err = encryption.GenerateP256Keys()
8897
}
8998

cmd/mpcium-cli/main.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ func main() {
128128
Value: "ed25519",
129129
Usage: "Algorithm to use for key generation (ed25519,p256)",
130130
},
131-
&cli.StringFlag{
132-
Name: "pubkey",
133-
Aliases: []string{"p"},
134-
Usage: "Path to PEM file containing P-256 public key (required when algorithm=p256)",
135-
},
136131
},
137132
Action: generateInitiatorIdentity,
138133
},

config.yaml.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ consul:
66
mpc_threshold: 2
77
environment: development
88
badger_password: "F))ysJp?E]ol&I;^"
9-
event_initiator_algorithm: "p256" # or "ed25519", default: ed25519
9+
event_initiator_algorithm: "ed25519" # or "ed25519", default: ed25519
1010
event_initiator_pubkey: "event_initiator_pubkey"
1111
db_path: "."
1212
backup_enabled: true

examples/reshare/main.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ func main() {
2121
config.InitViperConfig()
2222
logger.Init(environment, true)
2323

24+
algorithm := viper.GetString("event_initiator_algorithm")
25+
if algorithm == "" {
26+
algorithm = "ed25519"
27+
}
28+
29+
// Validate algorithm
30+
if algorithm != "ed25519" && algorithm != "p256" {
31+
logger.Fatal(
32+
"Invalid event_initiator_algorithm in config. Must be 'ed25519' or 'p256'",
33+
nil,
34+
)
35+
}
2436
natsURL := viper.GetString("nats.url")
2537
natsConn, err := nats.Connect(natsURL)
2638
if err != nil {
@@ -30,8 +42,9 @@ func main() {
3042
defer natsConn.Close()
3143

3244
mpcClient := client.NewMPCClient(client.Options{
33-
NatsConn: natsConn,
34-
KeyPath: "./event_initiator.key",
45+
Algorithm: algorithm,
46+
NatsConn: natsConn,
47+
KeyPath: "./event_initiator.key",
3548
})
3649

3750
// 3) Listen for signing results
@@ -46,14 +59,27 @@ func main() {
4659
if err != nil {
4760
logger.Fatal("Failed to subscribe to OnResharingResult", err)
4861
}
62+
// Determine key type based on algorithm
63+
var keyType types.KeyType
64+
switch algorithm {
65+
case "ed25519":
66+
keyType = types.KeyTypeEd25519
67+
case "p256":
68+
keyType = types.KeyTypeP256
69+
default:
70+
logger.Fatal("Unsupported algorithm", nil)
71+
}
4972

5073
resharingMsg := &types.ResharingMessage{
5174
SessionID: uuid.NewString(),
5275
WalletID: "506d2d40-483a-49f1-93c8-27dd4fe9740c",
53-
NodeIDs: []string{"c95c340e-5a18-472d-b9b0-5ac68218213a", "ac37e85f-caca-4bee-8a3a-49a0fe35abff"}, // new peer IDs
76+
NodeIDs: []string{
77+
"c95c340e-5a18-472d-b9b0-5ac68218213a",
78+
"ac37e85f-caca-4bee-8a3a-49a0fe35abff",
79+
}, // new peer IDs
5480

5581
NewThreshold: 1, // t+1 <= len(NodeIDs)
56-
KeyType: types.KeyTypeEd25519,
82+
KeyType: keyType,
5783
}
5884
err = mpcClient.Resharing(resharingMsg)
5985
if err != nil {

pkg/encryption/p256.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ func ParseP256PrivateKey(keyData []byte) (*ecdsa.PrivateKey, error) {
3333

3434
// Try to parse as hex string
3535
keyStr := strings.TrimSpace(string(keyData))
36-
if strings.HasPrefix(keyStr, "0x") {
37-
keyStr = keyStr[2:]
38-
}
36+
keyStr = strings.TrimPrefix(keyStr, "0x")
3937

4038
keyBytes, err := hex.DecodeString(keyStr)
4139
if err != nil {
@@ -144,12 +142,7 @@ func ParseP256PublicKeyFromBytes(keyBytes []byte) (*ecdsa.PublicKey, error) {
144142

145143
// ParseP256PublicKeyFromHex parses a P-256 public key from hex string
146144
func ParseP256PublicKeyFromHex(hexString string) (*ecdsa.PublicKey, error) {
147-
// Remove 0x prefix if present
148-
if strings.HasPrefix(hexString, "0x") {
149-
hexString = hexString[2:]
150-
}
151-
152-
// Decode hex
145+
hexString = strings.TrimPrefix(hexString, "0x")
153146
keyBytes, err := hex.DecodeString(hexString)
154147
if err != nil {
155148
return nil, fmt.Errorf("failed to decode hex string: %w", err)
@@ -160,7 +153,6 @@ func ParseP256PublicKeyFromHex(hexString string) (*ecdsa.PublicKey, error) {
160153

161154
// ParseP256PublicKeyFromBase64 parses a P-256 public key from base64 string
162155
func ParseP256PublicKeyFromBase64(base64String string) (*ecdsa.PublicKey, error) {
163-
// Decode base64
164156
keyBytes, err := base64.StdEncoding.DecodeString(base64String)
165157
if err != nil {
166158
return nil, fmt.Errorf("failed to decode base64 string: %w", err)

0 commit comments

Comments
 (0)