fix(agent-identity): FIX-3 crypto — DID binding (anti-front-running) + derive_did domain-sep

Build + tests verde: agent-identity 18 (4 unit + 14 integration) + runtime 27.
Verificacion adversarial ultracode (3 skeptics): 2 confirmed + 1 partial/low,
todos los gaps PRE-EXISTENTES (no introducidos por FIX-3). Invariante "no
puedes registrar un public_key que no controlas" SE SOSTIENE.

FIX-3 (b) DID binding — V3-H1 anti-front-running:
- register_agent_did ahora exige signature: [u8;64] — una firma Ed25519 (por la
  private key de public_key) sobre AGENT_DID_REGISTER_DOMAIN || who.encode().
- Verificacion on-chain via sp_io::crypto::ed25519_verify (host function, no
  bloatea el wasm). Nuevo dep sp-io (no_std).
- Cierra el front-running: un atacante no puede registrar un public_key ajeno
  (no produce la firma); el who en el mensaje ata la firma a este caller (no
  replay-able a otra cuenta). Error InvalidSignature. Malleabilidad Ed25519
  rechazada por ed25519-zebra (S canonico).
- Una sola fuente de verdad del mensaje (agent_did_register_message) compartida
  entre verificador y firmante (evita divergencia tipo postcard-vs-SCALE).
- Orden: idempotency ANTES del verify (no gasta crypto en duplicados).

FIX-3 (c) derive_did domain-sep — V3-H2:
- blake3::derive_key("origin-chain/agent-did/v1", pk) en vez del concat ad-hoc
  BLAKE3(pk || version). KDF domain-separado. Inmutable #16 preservado.

Tests: mock con keypairs Ed25519 reales (sp_core::ed25519::Pair) + sign helper.
12 scenarios (scenario_6 reformulado a front-running -> InvalidSignature, +
scenario_11 firma invalida, scenario_12 DID collision genuino del key-holder).

NOTA: la (a) domain-sep del attestation signing payload (el otro tercio de
FIX-3) ya esta cubierta por el DC ORIGIN_ATTESTATION_SIGNING_PAYLOAD_V1 (helper
de Ergon attestation_signed_message), pendiente su ship Cap 45+.

FOLLOW-UPS (low, pre-existentes, no bloquean):
- (a) anadir genesis_hash/chain-id al mensaje de registro (hardening cross-chain
  replay) — ideal chain-wide junto al signing_payload de attestation.
- (b) DC para ratificar el modelo de autoridad Hybrid (quien firma) + si los
  co_authors requieren prueba de consentimiento (lineage hoy no autenticado).
- (c) weight benchmarking real que contabilice el ed25519_verify (chain-wide,
  post-testnet — ya en roadmap FIX-2 weights).
- creator = AccountId::new(public_key) (la cuenta del key-holder, no el caller):
  coherente con el binding, semantica documentada.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
davidherranandClaude Opus 4.8 committed 2026-06-09 23:18:22 -05:00
1 parent 8a1f779c32
commit 88e277e1fd
4 files changed
+241 -103

No files matched your search

+38 -6
View File
@@ -7,6 +7,7 @@
use crate as origin_chain_agent_identity;
use frame_support::derive_impl;
use sp_core::{ed25519, Pair};
use sp_runtime::BuildStorage;
/// Mock block type canonical para tests.
@@ -62,11 +63,42 @@ pub fn bob_account() -> sp_runtime::AccountId32 {
sp_runtime::AccountId32::new([2u8; 32])
}
/// Test pubkey canonical ALICE (deterministic).
pub const ALICE_PUBKEY: [u8; 32] = [11u8; 32];
// ─── Ed25519 keypairs canonical (FIX-3 V3-H1 DID binding) ──────────────
// Pairs reales desde seeds → pubkeys reales + firmas verificables. Reemplaza
// los pubkeys-bytes-arbitrarios previos (sin private key, incapaces de firmar el
// binding del registro).
/// Test pubkey canonical BOB (deterministic).
pub const BOB_PUBKEY: [u8; 32] = [22u8; 32];
/// Ed25519 pair canonical ALICE (agent identity key).
pub fn alice_pair() -> ed25519::Pair {
ed25519::Pair::from_seed(&[0x11u8; 32])
}
/// Ed25519 pair canonical BOB.
pub fn bob_pair() -> ed25519::Pair {
ed25519::Pair::from_seed(&[0x22u8; 32])
}
/// Ed25519 pair canonical CHARLIE (Hybrid co-author lineage).
pub fn charlie_pair() -> ed25519::Pair {
ed25519::Pair::from_seed(&[0x33u8; 32])
}
/// Test pubkey canonical CHARLIE (Hybrid co-author lineage testing).
pub const CHARLIE_PUBKEY: [u8; 32] = [33u8; 32];
/// Public key bytes canonical ALICE (derivado del pair real).
pub fn alice_pubkey() -> [u8; 32] {
alice_pair().public().0
}
/// Public key bytes canonical BOB.
pub fn bob_pubkey() -> [u8; 32] {
bob_pair().public().0
}
/// Public key bytes canonical CHARLIE.
pub fn charlie_pubkey() -> [u8; 32] {
charlie_pair().public().0
}
/// Firma de binding canonical (FIX-3 (b)): el holder de `pair` firma el mensaje
/// de registro `AGENT_DID_REGISTER_DOMAIN || who.encode()` → prueba control de la
/// key. Usa el MISMO builder de mensaje que el pallet (una fuente de verdad).
pub fn sign_registration(pair: &ed25519::Pair, who: &sp_runtime::AccountId32) -> [u8; 64] {
use parity_scale_codec::Encode;
let msg = origin_chain_agent_identity::agent_did_register_message(&who.encode());
pair.sign(&msg).0
}