Skip to content

Commit 3ecd016

Browse files
committed
Udpate documentation
1 parent 2c5c731 commit 3ecd016

1 file changed

Lines changed: 245 additions & 25 deletions

File tree

README.md

Lines changed: 245 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -153,41 +153,261 @@ $ mpcium start -n node2
153153
- **Go**: Available in the `pkg/client` directory. Check the `examples` folder for usage samples.
154154
- **TypeScript**: Available at [github.com/fystack/mpcium-client-ts](https://github.com/fystack/mpcium-client-ts)
155155

156-
### Client
156+
### Client Usage
157157

158-
```go
158+
Mpcium supports flexible client authentication through a signer interface, allowing you to use either local keys or AWS KMS for signing operations.
159+
160+
#### Local Signer (Ed25519)
159161

162+
```go
160163
import (
161-
"github.com/fystack/mpcium/client"
164+
"github.com/fystack/mpcium/pkg/client"
165+
"github.com/fystack/mpcium/pkg/event"
166+
"github.com/fystack/mpcium/pkg/types"
167+
"github.com/google/uuid"
162168
"github.com/nats-io/nats.go"
163169
)
164170

171+
func main() {
172+
// Connect to NATS
173+
natsConn, err := nats.Connect(natsURL)
174+
if err != nil {
175+
logger.Fatal("Failed to connect to NATS", err)
176+
}
177+
defer natsConn.Close()
178+
179+
// Create local signer with Ed25519 key
180+
localSigner, err := client.NewLocalSigner(types.EventInitiatorKeyTypeEd25519, client.LocalSignerOptions{
181+
KeyPath: "./event_initiator.key",
182+
})
183+
if err != nil {
184+
logger.Fatal("Failed to create local signer", err)
185+
}
186+
187+
// Create MPC client with signer
188+
mpcClient := client.NewMPCClient(client.Options{
189+
NatsConn: natsConn,
190+
Signer: localSigner,
191+
})
192+
193+
// Handle wallet creation results
194+
err = mpcClient.OnWalletCreationResult(func(event event.KeygenResultEvent) {
195+
logger.Info("Received wallet creation result", "event", event)
196+
})
197+
if err != nil {
198+
logger.Fatal("Failed to subscribe to wallet-creation results", err)
199+
}
200+
201+
// Create a wallet
202+
walletID := uuid.New().String()
203+
if err := mpcClient.CreateWallet(walletID); err != nil {
204+
logger.Fatal("CreateWallet failed", err)
205+
}
206+
logger.Info("CreateWallet sent, awaiting result...", "walletID", walletID)
207+
}
208+
```
209+
210+
#### Local Signer (P256 with encrypted key)
211+
212+
```go
213+
// Create local signer with P256 key (encrypted with age)
214+
localSigner, err := client.NewLocalSigner(types.EventInitiatorKeyTypeP256, client.LocalSignerOptions{
215+
KeyPath: "./event_initiator_p256.key.age",
216+
Encrypted: true,
217+
Password: "your-encryption-password",
218+
})
219+
```
220+
221+
#### AWS KMS Signer
222+
223+
##### Production (IAM Role-based Authentication)
224+
225+
For production environments using IAM roles (recommended):
226+
227+
```go
228+
import (
229+
"github.com/fystack/mpcium/pkg/client"
230+
"github.com/fystack/mpcium/pkg/types"
231+
)
232+
233+
func main() {
234+
// KMS signer with role-based authentication (no static credentials)
235+
kmsSigner, err := client.NewKMSSigner(types.EventInitiatorKeyTypeP256, client.KMSSignerOptions{
236+
Region: "us-east-1",
237+
KeyID: "arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012",
238+
// No AccessKeyID/SecretAccessKey - uses IAM role
239+
})
240+
if err != nil {
241+
logger.Fatal("Failed to create KMS signer", err)
242+
}
243+
244+
mpcClient := client.NewMPCClient(client.Options{
245+
NatsConn: natsConn,
246+
Signer: kmsSigner,
247+
})
248+
// ... rest of the client code
249+
}
250+
```
251+
252+
##### Development with Static Credentials
253+
254+
```go
255+
// KMS signer with static credentials (development only)
256+
kmsSigner, err := client.NewKMSSigner(types.EventInitiatorKeyTypeP256, client.KMSSignerOptions{
257+
Region: "us-west-2",
258+
KeyID: "12345678-1234-1234-1234-123456789012",
259+
AccessKeyID: "AKIA...",
260+
SecretAccessKey: "...",
261+
})
262+
```
263+
264+
##### LocalStack Development
265+
266+
```go
267+
// KMS signer with LocalStack for local development
268+
kmsSigner, err := client.NewKMSSigner(types.EventInitiatorKeyTypeP256, client.KMSSignerOptions{
269+
Region: "us-east-1",
270+
KeyID: "48e76117-fd08-4dc0-bd10-b1c7d01de748",
271+
EndpointURL: "http://localhost:4566", // LocalStack endpoint
272+
AccessKeyID: "test", // LocalStack dummy credentials
273+
SecretAccessKey: "test",
274+
})
275+
```
276+
277+
##### AWS Cloud Config Variations
165278

166-
func main () {
167-
natsConn, err := nats.Connect(natsURL)
168-
if err != nil {
169-
logger.Fatal("Failed to connect to NATS", err)
170-
}
171-
defer natsConn.Close()
172-
mpcClient := client.NewMPCClient(client.Options{
173-
NatsConn: natsConn,
174-
KeyPath: "./event_initiator.key",
175-
})
176-
err = mpcClient.OnWalletCreationResult(func(event event.KeygenSuccessEvent) {
177-
logger.Info("Received wallet creation result", "event", event)
178-
})
179-
if err != nil {
180-
logger.Fatal("Failed to subscribe to wallet-creation results", err)
181-
}
182-
183-
walletID := uuid.New().String()
184-
if err := mpcClient.CreateWallet(walletID); err != nil {
185-
logger.Fatal("CreateWallet failed", err)
186-
}
187-
logger.Info("CreateWallet sent, awaiting result...", "walletID", walletID)
279+
```go
280+
// Different regions and key formats
281+
configs := []client.KMSSignerOptions{
282+
// Key ID only
283+
{
284+
Region: "eu-west-1",
285+
KeyID: "12345678-1234-1234-1234-123456789012",
286+
},
287+
// Full ARN
288+
{
289+
Region: "ap-southeast-1",
290+
KeyID: "arn:aws:kms:ap-southeast-1:123456789012:key/12345678-1234-1234-1234-123456789012",
291+
},
292+
// Key alias
293+
{
294+
Region: "us-east-2",
295+
KeyID: "alias/mpcium-signing-key",
296+
},
188297
}
189298
```
190299

300+
**Note**: AWS KMS only supports P256 (ECDSA) keys, not Ed25519. If you need Ed25519, use the local signer.
301+
302+
## Test with AWS KMS (LocalStack)
303+
304+
For local development and testing with AWS KMS functionality, you can use LocalStack to simulate AWS KMS services.
305+
306+
### Setup LocalStack
307+
308+
1. **Install and start LocalStack:**
309+
```bash
310+
# Using Docker
311+
docker run -d \
312+
-p 4566:4566 \
313+
-p 4510-4559:4510-4559 \
314+
localstack/localstack
315+
316+
# Or using LocalStack CLI
317+
pip install localstack
318+
localstack start
319+
```
320+
321+
2. **Configure AWS CLI for LocalStack:**
322+
```bash
323+
aws configure set aws_access_key_id test
324+
aws configure set aws_secret_access_key test
325+
aws configure set region us-east-1
326+
```
327+
328+
### Create P256 Key in LocalStack
329+
330+
1. **Create a P256 keypair in AWS KMS:**
331+
```bash
332+
aws kms create-key \
333+
--endpoint-url=http://localhost:4566 \
334+
--description "Test P-256 keypair for Mpcium" \
335+
--key-usage SIGN_VERIFY \
336+
--customer-master-key-spec ECC_NIST_P256
337+
```
338+
339+
Expected response:
340+
```json
341+
{
342+
"KeyMetadata": {
343+
"AWSAccountId": "000000000000",
344+
"KeyId": "330a9df7-4fd9-4e86-bfc5-f360b4c4be39",
345+
"Arn": "arn:aws:kms:us-east-1:000000000000:key/330a9df7-4fd9-4e86-bfc5-f360b4c4be39",
346+
"CreationDate": "2025-08-28T16:42:18.487655+07:00",
347+
"Enabled": true,
348+
"Description": "Test P-256 keypair for Mpcium",
349+
"KeyUsage": "SIGN_VERIFY",
350+
"KeyState": "Enabled",
351+
"Origin": "AWS_KMS",
352+
"KeyManager": "CUSTOMER",
353+
"CustomerMasterKeySpec": "ECC_NIST_P256",
354+
"KeySpec": "ECC_NIST_P256",
355+
"SigningAlgorithms": [
356+
"ECDSA_SHA_256"
357+
],
358+
"MultiRegion": false
359+
}
360+
}
361+
```
362+
363+
2. **Get the public key (save the KeyId from step 1):**
364+
```bash
365+
export KMS_KEY_ID="330a9df7-4fd9-4e86-bfc5-f360b4c4be39" # Replace with your KeyId
366+
367+
aws kms get-public-key \
368+
--endpoint-url=http://localhost:4566 \
369+
--key-id $KMS_KEY_ID \
370+
--query PublicKey \
371+
--output text | base64 -d | xxd -p -c 256
372+
```
373+
374+
Expected response (hex-encoded public key):
375+
```
376+
3059301306072a8648ce3d020106082a8648ce3d030107034200042b7539fc51123c3ba53c71e244be71d2d3138cbed4909fa259b924b56c92148cadd410cf98b789269d7f672c3ba978e99fc1f01c87daee97292d3666357738fd
377+
```
378+
379+
### Configure Mpcium for LocalStack KMS
380+
381+
Update your `config.yaml` file with the KMS public key and algorithm:
382+
383+
```yaml
384+
# MPC Configuration
385+
mpc_threshold: 2
386+
event_initiator_pubkey: "3059301306072a8648ce3d020106082a8648ce3d030107034200042b7539fc51123c3ba53c71e244be71d2d3138cbed4909fa259b924b56c92148cadd410cf98b789269d7f672c3ba978e99fc1f01c87daee97292d3666357738fd"
387+
event_initiator_algorithm: "p256"
388+
389+
# Other configuration...
390+
nats:
391+
url: "nats://localhost:4222"
392+
consul:
393+
address: "localhost:8500"
394+
```
395+
396+
### Test KMS Integration
397+
398+
Run the KMS example:
399+
400+
```bash
401+
# Run the KMS example directly
402+
go run examples/generate/kms/main.go -n 1
403+
```
404+
405+
The example will:
406+
1. Connect to LocalStack KMS endpoint
407+
2. Load the P256 public key from KMS
408+
3. Use KMS for signing wallet creation events
409+
4. Generate wallets using the MPC cluster
410+
191411
### Testing
192412

193413
## 1. Unit tests

0 commit comments

Comments
 (0)