nklave-cosmos 0.1.0

Cosmos/CometBFT remote signer protocol (Tendermint PrivValidator) for Nklave
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Cosmos signing service wrapper
//!
//! Wraps the core SigningService to provide Cosmos-specific signing operations

use crate::error::{CosmosError, RemoteSignerCode};
use crate::types::{canonical_proposal_sign_bytes, canonical_vote_sign_bytes, ProposalInfo, SignedMsgType, VoteInfo};
use nklave_core::policy::cosmos::CosmosPolicy;
use nklave_core::policy::types::PolicyDecision;
use nklave_core::state::validator::{ChainState, CosmosSignedMsgType, ValidatorState};
use nklave_core::{Ed25519Keypair, SigningService};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use tendermint_proto::types::{Proposal, Vote};
use tracing::{debug, info, warn};

/// Cosmos signing service that wraps the core SigningService
pub struct CosmosSigningService {
    /// Ed25519 keypairs indexed by public key (32 bytes)
    keypairs: Arc<RwLock<HashMap<[u8; 32], Ed25519Keypair>>>,

    /// Validator states indexed by public key (padded to 48 bytes for compatibility)
    validator_states: Arc<RwLock<HashMap<[u8; 48], ValidatorState>>>,

    /// The Cosmos slashing policy
    policy: CosmosPolicy,

    /// Expected chain ID
    chain_id: String,
}

impl CosmosSigningService {
    /// Create a new Cosmos signing service
    pub fn new(chain_id: String) -> Self {
        Self {
            keypairs: Arc::new(RwLock::new(HashMap::new())),
            validator_states: Arc::new(RwLock::new(HashMap::new())),
            policy: CosmosPolicy,
            chain_id,
        }
    }

    /// Create from an existing core SigningService (for integration)
    pub fn from_signing_service(signing_service: &SigningService, chain_id: String) -> Self {
        let service = Self::new(chain_id);
        // Copy validator states that are Cosmos type
        let states = signing_service.validator_states();
        for (pubkey, state) in states {
            if matches!(state.chain_state, Some(ChainState::Cosmos(_))) {
                service.validator_states.write().unwrap().insert(pubkey, state);
            }
        }
        service
    }

    /// Register a keypair with the service
    pub fn register_keypair(&self, keypair: Ed25519Keypair) {
        let pubkey_32 = keypair.public_key_bytes();
        let pubkey_48 = keypair.public_key_bytes_padded();

        // Initialize validator state if not exists
        {
            let mut states = self.validator_states.write().unwrap();
            states.entry(pubkey_48).or_insert_with(|| {
                ValidatorState::new_cosmos(pubkey_32)
            });
        }

        // Store keypair
        self.keypairs.write().unwrap().insert(pubkey_32, keypair);

        info!(
            pubkey = hex::encode(pubkey_32),
            "Registered Cosmos validator keypair"
        );
    }

    /// Get the public key for a registered validator
    pub fn get_public_key(&self, pubkey_32: &[u8; 32]) -> Option<[u8; 32]> {
        self.keypairs.read().unwrap().get(pubkey_32).map(|kp| kp.public_key_bytes())
    }

    /// Get the first registered public key (for single-validator setups)
    pub fn get_first_public_key(&self) -> Option<[u8; 32]> {
        self.keypairs.read().unwrap().keys().next().copied()
    }

    /// Get the chain ID
    pub fn chain_id(&self) -> &str {
        &self.chain_id
    }

    /// Verify chain ID matches
    pub fn verify_chain_id(&self, chain_id: &str) -> Result<(), CosmosError> {
        if chain_id != self.chain_id {
            return Err(CosmosError::ChainIdMismatch {
                expected: self.chain_id.clone(),
                actual: chain_id.to_string(),
            });
        }
        Ok(())
    }

    /// Sign a vote (prevote or precommit)
    pub fn sign_vote(
        &self,
        chain_id: &str,
        vote: &mut Vote,
    ) -> Result<Vec<u8>, (CosmosError, Option<RemoteSignerCode>)> {
        // Verify chain ID
        self.verify_chain_id(chain_id)
            .map_err(|e| (e, None))?;

        // Extract vote info
        let vote_info = VoteInfo::from_vote(vote)
            .map_err(|e| (e, None))?;

        // Find the keypair for this validator
        let keypairs = self.keypairs.read().unwrap();
        let keypair = self.find_keypair_by_address(&vote_info.validator_address, &keypairs)
            .ok_or_else(|| {
                let err = CosmosError::ValidatorNotFound {
                    pubkey_hex: hex::encode(&vote_info.validator_address),
                };
                (err, Some(RemoteSignerCode::NotFound))
            })?;

        let pubkey_48 = keypair.public_key_bytes_padded();

        // Check slashing policy
        let decision = {
            let states = self.validator_states.read().unwrap();
            let state = states.get(&pubkey_48).ok_or_else(|| {
                (CosmosError::ValidatorNotFound {
                    pubkey_hex: hex::encode(pubkey_48),
                }, Some(RemoteSignerCode::NotFound))
            })?;

            let cosmos_state = match &state.chain_state {
                Some(ChainState::Cosmos(cs)) => cs,
                _ => {
                    return Err((CosmosError::Internal("Validator is not Cosmos type".to_string()), None));
                }
            };

            match vote_info.msg_type {
                SignedMsgType::Prevote => {
                    self.policy.check_prevote(cosmos_state, vote_info.height, vote_info.round, vote_info.block_hash)
                }
                SignedMsgType::Precommit => {
                    self.policy.check_precommit(cosmos_state, vote_info.height, vote_info.round, vote_info.block_hash)
                }
                _ => {
                    return Err((CosmosError::InvalidVoteType(vote.r#type), None));
                }
            }
        };

        match decision {
            PolicyDecision::Allow => {
                debug!(
                    height = vote_info.height,
                    round = vote_info.round,
                    msg_type = ?vote_info.msg_type,
                    "Vote allowed by policy"
                );
            }
            PolicyDecision::Refuse(code) => {
                warn!(
                    height = vote_info.height,
                    round = vote_info.round,
                    msg_type = ?vote_info.msg_type,
                    refusal_code = ?code,
                    "Vote REFUSED by policy - double signing attempt"
                );
                return Err((
                    CosmosError::DoubleSigning {
                        height: vote_info.height,
                        round: vote_info.round,
                    },
                    Some(RemoteSignerCode::DoubleSign),
                ));
            }
        }

        // Create canonical sign bytes and sign
        let sign_bytes = canonical_vote_sign_bytes(chain_id, vote);
        let signature = keypair.sign(&sign_bytes);

        // Update state after successful signing
        {
            let mut states = self.validator_states.write().unwrap();
            if let Some(state) = states.get_mut(&pubkey_48) {
                if let Some(ChainState::Cosmos(ref mut cosmos_state)) = state.chain_state {
                    let msg_type = vote_info.msg_type.to_core_type()
                        .ok_or_else(|| (CosmosError::InvalidVoteType(vote.r#type), None))?;

                    cosmos_state.record_vote(
                        vote_info.height,
                        vote_info.round,
                        msg_type,
                        vote_info.block_hash,
                    );
                }
            }
        }

        let sig_bytes = signature.to_bytes().to_vec();
        vote.signature = sig_bytes.clone();

        info!(
            height = vote_info.height,
            round = vote_info.round,
            msg_type = ?vote_info.msg_type,
            "Vote signed successfully"
        );

        Ok(sig_bytes)
    }

    /// Sign a proposal
    pub fn sign_proposal(
        &self,
        chain_id: &str,
        proposal: &mut Proposal,
    ) -> Result<Vec<u8>, (CosmosError, Option<RemoteSignerCode>)> {
        // Verify chain ID
        self.verify_chain_id(chain_id)
            .map_err(|e| (e, None))?;

        // Extract proposal info
        let proposal_info = ProposalInfo::from_proposal(proposal)
            .map_err(|e| (e, None))?;

        // For proposals, we need to find the keypair differently
        // In a single-validator setup, use the first available keypair
        let keypairs = self.keypairs.read().unwrap();
        let keypair = keypairs.values().next()
            .ok_or_else(|| {
                (CosmosError::ValidatorNotFound {
                    pubkey_hex: "no validators registered".to_string(),
                }, Some(RemoteSignerCode::NotFound))
            })?;

        let pubkey_48 = keypair.public_key_bytes_padded();

        // Check slashing policy
        let decision = {
            let states = self.validator_states.read().unwrap();
            let state = states.get(&pubkey_48).ok_or_else(|| {
                (CosmosError::ValidatorNotFound {
                    pubkey_hex: hex::encode(pubkey_48),
                }, Some(RemoteSignerCode::NotFound))
            })?;

            let cosmos_state = match &state.chain_state {
                Some(ChainState::Cosmos(cs)) => cs,
                _ => {
                    return Err((CosmosError::Internal("Validator is not Cosmos type".to_string()), None));
                }
            };

            self.policy.check_proposal(cosmos_state, proposal_info.height, proposal_info.round, proposal_info.block_hash)
        };

        match decision {
            PolicyDecision::Allow => {
                debug!(
                    height = proposal_info.height,
                    round = proposal_info.round,
                    "Proposal allowed by policy"
                );
            }
            PolicyDecision::Refuse(code) => {
                warn!(
                    height = proposal_info.height,
                    round = proposal_info.round,
                    refusal_code = ?code,
                    "Proposal REFUSED by policy - double signing attempt"
                );
                return Err((
                    CosmosError::DoubleSigning {
                        height: proposal_info.height,
                        round: proposal_info.round,
                    },
                    Some(RemoteSignerCode::DoubleSign),
                ));
            }
        }

        // Create canonical sign bytes and sign
        let sign_bytes = canonical_proposal_sign_bytes(chain_id, proposal);
        let signature = keypair.sign(&sign_bytes);

        // Update state after successful signing
        {
            let mut states = self.validator_states.write().unwrap();
            if let Some(state) = states.get_mut(&pubkey_48) {
                if let Some(ChainState::Cosmos(ref mut cosmos_state)) = state.chain_state {
                    cosmos_state.record_vote(
                        proposal_info.height,
                        proposal_info.round,
                        CosmosSignedMsgType::Proposal,
                        Some(proposal_info.block_hash),
                    );
                }
            }
        }

        let sig_bytes = signature.to_bytes().to_vec();
        proposal.signature = sig_bytes.clone();

        info!(
            height = proposal_info.height,
            round = proposal_info.round,
            "Proposal signed successfully"
        );

        Ok(sig_bytes)
    }

    /// Find a keypair by validator address (first 20 bytes of SHA256 of public key)
    fn find_keypair_by_address<'a>(
        &self,
        address: &[u8],
        keypairs: &'a HashMap<[u8; 32], Ed25519Keypair>,
    ) -> Option<&'a Ed25519Keypair> {
        for keypair in keypairs.values() {
            let tendermint_addr = keypair.tendermint_address();
            if address.len() >= 20 && &tendermint_addr[..] == &address[..20] {
                return Some(keypair);
            }
        }
        None
    }

    /// Get all registered validator public keys
    pub fn registered_validators(&self) -> Vec<[u8; 32]> {
        self.keypairs.read().unwrap().keys().copied().collect()
    }

    /// Export validator states for checkpointing
    pub fn validator_states(&self) -> HashMap<[u8; 48], ValidatorState> {
        self.validator_states.read().unwrap().clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tendermint_proto::types::{BlockId, PartSetHeader};

    fn create_test_service() -> CosmosSigningService {
        let service = CosmosSigningService::new("test-chain".to_string());
        let keypair = Ed25519Keypair::random();
        service.register_keypair(keypair);
        service
    }

    #[test]
    fn test_service_creation() {
        let service = CosmosSigningService::new("test-chain".to_string());
        assert_eq!(service.chain_id(), "test-chain");
    }

    #[test]
    fn test_keypair_registration() {
        let service = CosmosSigningService::new("test-chain".to_string());
        let keypair = Ed25519Keypair::random();
        let pubkey = keypair.public_key_bytes();

        service.register_keypair(keypair);

        assert!(service.get_public_key(&pubkey).is_some());
        assert_eq!(service.registered_validators().len(), 1);
    }

    #[test]
    fn test_chain_id_verification() {
        let service = create_test_service();

        assert!(service.verify_chain_id("test-chain").is_ok());
        assert!(service.verify_chain_id("wrong-chain").is_err());
    }

    #[test]
    fn test_sign_vote_success() {
        let service = CosmosSigningService::new("test-chain".to_string());
        let keypair = Ed25519Keypair::random();
        let validator_address = keypair.tendermint_address().to_vec();
        service.register_keypair(keypair);

        let mut vote = Vote {
            r#type: 1, // Prevote
            height: 100,
            round: 0,
            block_id: Some(BlockId {
                hash: vec![1; 32],
                part_set_header: Some(PartSetHeader {
                    total: 1,
                    hash: vec![2; 32],
                }),
            }),
            timestamp: None,
            validator_address,
            validator_index: 0,
            signature: vec![],
            extension: vec![],
            extension_signature: vec![],
        };

        let result = service.sign_vote("test-chain", &mut vote);
        assert!(result.is_ok());
        assert!(!vote.signature.is_empty());
    }

    #[test]
    fn test_sign_vote_double_signing_refused() {
        let service = CosmosSigningService::new("test-chain".to_string());
        let keypair = Ed25519Keypair::random();
        let validator_address = keypair.tendermint_address().to_vec();
        service.register_keypair(keypair);

        // First vote
        let mut vote1 = Vote {
            r#type: 1,
            height: 100,
            round: 0,
            block_id: Some(BlockId {
                hash: vec![1; 32],
                part_set_header: None,
            }),
            timestamp: None,
            validator_address: validator_address.clone(),
            validator_index: 0,
            signature: vec![],
            extension: vec![],
            extension_signature: vec![],
        };
        assert!(service.sign_vote("test-chain", &mut vote1).is_ok());

        // Second vote at same height/round with DIFFERENT block - should be refused
        let mut vote2 = Vote {
            r#type: 1,
            height: 100,
            round: 0,
            block_id: Some(BlockId {
                hash: vec![2; 32], // Different block!
                part_set_header: None,
            }),
            timestamp: None,
            validator_address,
            validator_index: 0,
            signature: vec![],
            extension: vec![],
            extension_signature: vec![],
        };
        let result = service.sign_vote("test-chain", &mut vote2);
        assert!(result.is_err());

        if let Err((_, Some(code))) = result {
            assert_eq!(code, RemoteSignerCode::DoubleSign);
        } else {
            panic!("Expected DoubleSign error code");
        }
    }

    #[test]
    fn test_sign_vote_idempotent() {
        let service = CosmosSigningService::new("test-chain".to_string());
        let keypair = Ed25519Keypair::random();
        let validator_address = keypair.tendermint_address().to_vec();
        service.register_keypair(keypair);

        // Sign same vote twice - should succeed (idempotent)
        for _ in 0..2 {
            let mut vote = Vote {
                r#type: 1,
                height: 100,
                round: 0,
                block_id: Some(BlockId {
                    hash: vec![1; 32],
                    part_set_header: None,
                }),
                timestamp: None,
                validator_address: validator_address.clone(),
                validator_index: 0,
                signature: vec![],
                extension: vec![],
                extension_signature: vec![],
            };
            assert!(service.sign_vote("test-chain", &mut vote).is_ok());
        }
    }

    #[test]
    fn test_sign_proposal_success() {
        let service = CosmosSigningService::new("test-chain".to_string());
        let keypair = Ed25519Keypair::random();
        service.register_keypair(keypair);

        let mut proposal = Proposal {
            r#type: 32, // Proposal type
            height: 100,
            round: 0,
            pol_round: -1,
            block_id: Some(BlockId {
                hash: vec![1; 32],
                part_set_header: Some(PartSetHeader {
                    total: 1,
                    hash: vec![2; 32],
                }),
            }),
            timestamp: None,
            signature: vec![],
        };

        let result = service.sign_proposal("test-chain", &mut proposal);
        assert!(result.is_ok());
        assert!(!proposal.signature.is_empty());
    }

    #[test]
    fn test_wrong_chain_id_refused() {
        let service = create_test_service();

        let mut vote = Vote {
            r#type: 1,
            height: 100,
            round: 0,
            block_id: None,
            timestamp: None,
            validator_address: vec![0; 20],
            validator_index: 0,
            signature: vec![],
            extension: vec![],
            extension_signature: vec![],
        };

        let result = service.sign_vote("wrong-chain", &mut vote);
        assert!(result.is_err());
    }
}