|
| 1 | +# Attestation |
| 2 | + |
| 3 | +The `Attestation` contract manages decentralized public key updates through a quorum-based consensus mechanism. Multiple trusted attesters must agree on the same public keys before they are accepted. This contract is used by the `FirebaseGuard` to source its public keys in a trust-minimized manner. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Quorum-based public key attestation requiring multiple attesters to agree. |
| 8 | +- Role-based access control (DAO, Attester, CodeStager, CodeDeployer, DurationManager, PauseManager, UnpauseManager). |
| 9 | +- Pausable contract for emergency situations. |
| 10 | +- Upgradable contract with staged deployments. |
| 11 | +- Safe attester management that prevents quorum violations. |
| 12 | + |
| 13 | +## Contract State |
| 14 | + |
| 15 | +| Field | Type | Description | |
| 16 | +|-------|------|-------------| |
| 17 | +| `attestations` | `IterableMap<AccountId, Attestation>` | Current attestations from each attester | |
| 18 | +| `quorum` | `u32` | Number of matching attestations required to update keys | |
| 19 | +| `public_keys` | `Vector<PublicKey>` | Currently active public keys | |
| 20 | + |
| 21 | +### PublicKey Structure |
| 22 | + |
| 23 | +```rust |
| 24 | +pub struct PublicKey { |
| 25 | + n: Vec<u8>, // RSA modulus |
| 26 | + e: Vec<u8>, // RSA exponent |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### Attestation Structure |
| 31 | + |
| 32 | +```rust |
| 33 | +pub struct Attestation { |
| 34 | + hash: Vec<u8>, // SHA256 hash of the public keys |
| 35 | + public_keys: Vec<PublicKey>, // The attested public keys |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +## Roles |
| 40 | + |
| 41 | +| Role | Permissions | |
| 42 | +|------|-------------| |
| 43 | +| `DAO` | Full administrative access, can attest, manage quorum, manage attesters | |
| 44 | +| `Attester` | Can submit public key attestations | |
| 45 | +| `CodeStager` | Can stage contract code updates | |
| 46 | +| `CodeDeployer` | Can deploy staged contract updates | |
| 47 | +| `DurationManager` | Can manage upgrade duration settings | |
| 48 | +| `PauseManager` | Can pause the contract | |
| 49 | +| `UnpauseManager` | Can unpause the contract | |
| 50 | + |
| 51 | +## Initialization |
| 52 | + |
| 53 | +```rust |
| 54 | +#[init] |
| 55 | +pub fn new(quorum: u32, super_admins: Vec<AccountId>, attesters: Vec<AccountId>) -> Self |
| 56 | +``` |
| 57 | + |
| 58 | +- `quorum`: Number of attesters that must agree before keys are updated. |
| 59 | +- `super_admins`: Accounts with full administrative privileges (also granted DAO role). |
| 60 | +- `attesters`: Initial accounts authorized to submit attestations. |
| 61 | + |
| 62 | +**Validation**: |
| 63 | +- Quorum must be greater than 0. |
| 64 | +- At least one super admin is required. |
| 65 | +- Quorum cannot exceed the number of attesters. |
| 66 | + |
| 67 | +## Attestation Process |
| 68 | + |
| 69 | +### Submitting an Attestation |
| 70 | + |
| 71 | +Accounts with the `Attester` or `DAO` role can submit attestations: |
| 72 | + |
| 73 | +```rust |
| 74 | +#[pause] |
| 75 | +#[access_control_any(roles(Role::Attester, Role::DAO))] |
| 76 | +pub fn attest_public_keys(&mut self, public_keys: Vec<PublicKey>) |
| 77 | +``` |
| 78 | + |
| 79 | +**Process**: |
| 80 | +1. Validates that public keys are not empty and all components are valid. |
| 81 | +2. Computes the SHA256 hash of the concatenated public key data. |
| 82 | +3. Stores the attestation for the calling attester. |
| 83 | +4. Counts how many existing attestations have the same hash. |
| 84 | +5. If the count reaches quorum: |
| 85 | + - Updates the active public keys. |
| 86 | + - Clears all attestations to prepare for future updates. |
| 87 | + |
| 88 | +### Example Flow |
| 89 | + |
| 90 | +With a quorum of 2 and 3 attesters: |
| 91 | + |
| 92 | +1. **Attester A** submits public keys `[PK1, PK2]` → Attestation stored, count = 1. |
| 93 | +2. **Attester B** submits public keys `[PK1, PK2]` → Same hash, count = 2, quorum reached! |
| 94 | +3. Public keys updated to `[PK1, PK2]`, all attestations cleared. |
| 95 | + |
| 96 | +If **Attester C** had submitted different keys, the hashes wouldn't match and quorum wouldn't be reached. |
| 97 | + |
| 98 | +## Querying State |
| 99 | + |
| 100 | +### Get Public Keys |
| 101 | + |
| 102 | +```rust |
| 103 | +pub fn get_public_keys(&self) -> Vec<PublicKey> |
| 104 | +``` |
| 105 | + |
| 106 | +Returns the currently active public keys (empty until quorum is first reached). |
| 107 | + |
| 108 | +### Get Attestation |
| 109 | + |
| 110 | +```rust |
| 111 | +pub fn get_attestation(&self, account_id: AccountId) -> Option<Attestation> |
| 112 | +``` |
| 113 | + |
| 114 | +Returns the current attestation from a specific attester. |
| 115 | + |
| 116 | +### Get Quorum |
| 117 | + |
| 118 | +```rust |
| 119 | +pub fn get_quorum(&self) -> u32 |
| 120 | +``` |
| 121 | + |
| 122 | +### Get Attesters |
| 123 | + |
| 124 | +```rust |
| 125 | +pub fn get_attesters(&self, from_index: u64, limit: u64) -> Vec<AccountId> |
| 126 | +``` |
| 127 | + |
| 128 | +Paginated list of accounts with the `Attester` role. |
| 129 | + |
| 130 | +## Quorum Management |
| 131 | + |
| 132 | +Only accounts with the `DAO` role can update the quorum: |
| 133 | + |
| 134 | +```rust |
| 135 | +#[pause] |
| 136 | +#[access_control_any(roles(Role::DAO))] |
| 137 | +pub fn set_quorum(&mut self, quorum: u32) |
| 138 | +``` |
| 139 | + |
| 140 | +**Validation**: Quorum cannot exceed the current number of attesters. |
| 141 | + |
| 142 | +## Attester Management |
| 143 | + |
| 144 | +### Granting Attester Role |
| 145 | + |
| 146 | +```rust |
| 147 | +#[pause] |
| 148 | +#[access_control_any(roles(Role::DAO))] |
| 149 | +pub fn grant_attester(&mut self, account_id: AccountId) |
| 150 | +``` |
| 151 | + |
| 152 | +### Revoking Attester Role |
| 153 | + |
| 154 | +```rust |
| 155 | +#[pause] |
| 156 | +#[access_control_any(roles(Role::DAO))] |
| 157 | +pub fn revoke_attester(&mut self, account_id: AccountId) |
| 158 | +``` |
| 159 | + |
| 160 | +**Safety Check**: Cannot revoke an attester if doing so would make the quorum impossible to reach (i.e., if `quorum >= remaining_attesters`). |
| 161 | + |
| 162 | +## Pause Functionality |
| 163 | + |
| 164 | +The contract can be paused to prevent attestations during security incidents: |
| 165 | + |
| 166 | +- **Pause**: Accounts with `PauseManager` or `DAO` role. |
| 167 | +- **Unpause**: Accounts with `UnpauseManager` or `DAO` role. |
| 168 | + |
| 169 | +When paused, `attest_public_keys`, `set_quorum`, `grant_attester`, and `revoke_attester` are disabled. |
| 170 | + |
| 171 | +## Security Considerations |
| 172 | + |
| 173 | +### Hash Computation |
| 174 | + |
| 175 | +The hash of public keys is computed by concatenating all modulus (`n`) and exponent (`e`) bytes: |
| 176 | + |
| 177 | +```rust |
| 178 | +fn compute_public_keys_hash(&self, public_keys: &[PublicKey]) -> Vec<u8> { |
| 179 | + let mut data = Vec::new(); |
| 180 | + for pk in public_keys { |
| 181 | + data.extend_from_slice(&pk.n); |
| 182 | + data.extend_from_slice(&pk.e); |
| 183 | + } |
| 184 | + env::sha256(&data).to_vec() |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +This ensures: |
| 189 | +- Attesters must agree on the exact same keys in the same order. |
| 190 | +- Any difference in key data results in a different hash. |
| 191 | + |
| 192 | +### Quorum Safety |
| 193 | + |
| 194 | +The contract enforces several invariants: |
| 195 | +- Quorum can never exceed the number of attesters. |
| 196 | +- Revoking an attester is blocked if it would violate the quorum requirement. |
| 197 | +- These checks prevent the contract from entering an unrecoverable state. |
| 198 | + |
| 199 | +## Integration with FirebaseGuard |
| 200 | + |
| 201 | +The `FirebaseGuard` contract fetches its public keys from this contract: |
| 202 | + |
| 203 | +1. `FirebaseGuard` calls `attestation_contract.get_public_keys()`. |
| 204 | +2. The returned keys are validated and stored in `FirebaseGuard`. |
| 205 | +3. These keys are then used to verify Firebase JWT signatures. |
| 206 | + |
| 207 | +This design allows: |
| 208 | +- Decentralized key management without trusting a single party. |
| 209 | +- Key rotation through attester consensus rather than a single admin. |
| 210 | +- Separation of concerns between authentication and key management. |
0 commit comments