Skip to content

Commit 6e77caf

Browse files
committed
Sign worked. updated sign example. Testing reshare
1 parent 707833c commit 6e77caf

6 files changed

Lines changed: 612 additions & 90 deletions

File tree

examples/generate/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ func main() {
104104

105105
// STEP 3: Create wallets
106106
for _, walletID := range walletIDs {
107+
wg.Add(1) // Add to WaitGroup BEFORE attempting to create wallet
108+
107109
if err := mpcClient.CreateWallet(walletID); err != nil {
108110
logger.Error("CreateWallet failed", err)
109111
walletStartTimes.Delete(walletID)
110-
wg.Done()
112+
wg.Done() // Now this is safe since we added 1 above
111113
continue
112114
}
113-
wg.Add(1)
115+
114116
logger.Info("CreateWallet sent, awaiting result...", "walletID", walletID)
115117
}
116118

examples/sign/main.go

Lines changed: 28 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,17 +42,29 @@ 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
// 2) Once wallet exists, immediately fire a SignTransaction
3851
txID := uuid.New().String()
3952
dummyTx := []byte("deadbeef") // replace with real transaction bytes
4053

54+
// Determine key type based on algorithm
55+
var keyType types.KeyType
56+
switch algorithm {
57+
case "ed25519":
58+
keyType = types.KeyTypeEd25519
59+
case "p256":
60+
keyType = types.KeyTypeP256
61+
default:
62+
logger.Fatal("Unsupported algorithm", nil)
63+
}
64+
4165
txMsg := &types.SignTxMessage{
42-
KeyType: types.KeyTypeEd25519,
43-
WalletID: "c47cd6f4-8ef4-4d77-9d2b-37f9d062e615",
66+
KeyType: keyType,
67+
WalletID: "ad24f678-b04b-4149-bcf6-bf9c90df8e63", // Use the generated wallet ID
4468
NetworkInternalCode: "solana-devnet",
4569
TxID: txID,
4670
Tx: dummyTx,

pkg/encryption/p256.go

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/rand"
77
"crypto/sha256"
88
"crypto/x509"
9+
"encoding/base64"
910
"encoding/hex"
1011
"fmt"
1112
"strings"
@@ -55,16 +56,18 @@ func ParseP256PrivateKey(keyData []byte) (*ecdsa.PrivateKey, error) {
5556
return nil, fmt.Errorf("failed to parse P256 private key from DER or hex format")
5657
}
5758

58-
// SignWithP256 signs data using the P256 private key
59+
// SignWithP256 signs data using a P256 private key
5960
func SignWithP256(privateKey *ecdsa.PrivateKey, data []byte) ([]byte, error) {
61+
if privateKey == nil {
62+
return nil, fmt.Errorf("invalid private key: private key is nil")
63+
}
64+
6065
if privateKey.Curve == nil {
6166
return nil, fmt.Errorf("invalid private key: curve is nil")
6267
}
6368

6469
hash := sha256.Sum256(data)
6570

66-
// Use crypto/ecdsa.Sign to create a proper P256 signature
67-
// The signature will be in ASN.1 DER format
6871
signature, err := ecdsa.SignASN1(rand.Reader, privateKey, hash[:])
6972
if err != nil {
7073
return nil, fmt.Errorf("failed to sign data: %w", err)
@@ -117,3 +120,65 @@ func VerifyP256Signature(publicKey *ecdsa.PublicKey, data []byte, signature []by
117120

118121
return nil
119122
}
123+
func ParseP256PublicKeyFromBytes(keyBytes []byte) (*ecdsa.PublicKey, error) {
124+
// Try to parse as DER first
125+
if key, err := x509.ParsePKIXPublicKey(keyBytes); err == nil {
126+
if ecdsaKey, ok := key.(*ecdsa.PublicKey); ok {
127+
if ecdsaKey.Curve == elliptic.P256() {
128+
return ecdsaKey, nil
129+
}
130+
}
131+
}
132+
133+
// Try to parse as EC public key
134+
if key, err := x509.ParsePKIXPublicKey(keyBytes); err == nil {
135+
if ecdsaKey, ok := key.(*ecdsa.PublicKey); ok {
136+
if ecdsaKey.Curve == elliptic.P256() {
137+
return ecdsaKey, nil
138+
}
139+
}
140+
}
141+
142+
return nil, fmt.Errorf("failed to parse P-256 public key from bytes")
143+
}
144+
145+
// ParseP256PublicKeyFromHex parses a P-256 public key from hex string
146+
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
153+
keyBytes, err := hex.DecodeString(hexString)
154+
if err != nil {
155+
return nil, fmt.Errorf("failed to decode hex string: %w", err)
156+
}
157+
158+
return ParseP256PublicKeyFromBytes(keyBytes)
159+
}
160+
161+
// ParseP256PublicKeyFromBase64 parses a P-256 public key from base64 string
162+
func ParseP256PublicKeyFromBase64(base64String string) (*ecdsa.PublicKey, error) {
163+
// Decode base64
164+
keyBytes, err := base64.StdEncoding.DecodeString(base64String)
165+
if err != nil {
166+
return nil, fmt.Errorf("failed to decode base64 string: %w", err)
167+
}
168+
169+
return ParseP256PublicKeyFromBytes(keyBytes)
170+
}
171+
172+
// ValidateP256PublicKey validates that a public key is P-256
173+
func ValidateP256PublicKey(publicKey *ecdsa.PublicKey) error {
174+
if publicKey == nil {
175+
return fmt.Errorf("public key is nil")
176+
}
177+
if publicKey.Curve == nil {
178+
return fmt.Errorf("public key curve is nil")
179+
}
180+
if publicKey.Curve != elliptic.P256() {
181+
return fmt.Errorf("public key is not P-256 curve (got: %s)", publicKey.Curve.Params().Name)
182+
}
183+
return nil
184+
}

0 commit comments

Comments
 (0)