truthlinked-core 0.1.0

Core protocol primitives for the TruthLinked post-quantum blockchain.
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
//! Transaction and execution intent types for TruthLinked.
//!
//! This module owns the canonical transaction wire shape. Any change to these
//! structures can affect transaction hashes, signing payloads, storage indexing,
//! and client compatibility.

use crate::cells::StorageKeySpec;
use serde::{Deserialize, Serialize};

pub type AccountId = [u8; 32];

pub fn system_authority_id() -> AccountId {
    use sha2::{Digest, Sha256};
    let mut hasher = Sha256::new();
    hasher.update(b"truthlinked-system-authority-v1");
    let hash = hasher.finalize();
    let mut account_id = [0u8; 32];
    account_id.copy_from_slice(&hash);
    account_id
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchTransferEntry {
    pub recipient: AccountId,
    /// Omit for accounts already known on-chain (saves 1,952 bytes per recipient).
    pub recipient_pubkey: Option<Vec<u8>>,
    pub amount: u128,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NameTransferEntry {
    pub name: String,
    pub amount: u128,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CellCall {
    pub cell_id: AccountId,
    pub calldata: Vec<u8>,
    pub value: u128,
    pub use_result_from: Option<usize>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transaction {
    pub sender: AccountId,
    pub intent: TransactionIntent,
    pub signature: Vec<u8>,
    #[serde(default)]
    pub nonce: u64,
    pub timestamp: u64,
    pub genesis_fingerprint: [u8; 32],
    pub expiration_height: u64,
}

impl Transaction {
    /// Serialized payload size used for byte-weighted fees and mempool limits.
    pub fn byte_weight(&self) -> Result<usize, postcard::Error> {
        postcard::to_allocvec(self).map(|bytes| bytes.len())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(clippy::large_enum_variant)]
pub enum TransactionIntent {
    Transfer {
        recipient: AccountId,
        /// Omit for accounts already known on-chain (saves 1,952 bytes).
        recipient_pubkey: Option<Vec<u8>>,
        amount: u128,
    },
    TransferToName {
        name: String,
        amount: u128,
    },
    BatchTransfer {
        transfers: Vec<BatchTransferEntry>,
    },
    BatchTransferToName {
        transfers: Vec<NameTransferEntry>,
    },
    Claim {
        recipient: AccountId,
        /// Omit for accounts already known on-chain (saves 1,952 bytes).
        recipient_pubkey: Option<Vec<u8>>,
        amount: u128,
    },
    RotateKey {
        new_pubkey: Vec<u8>,
    },
    DepositCompute {
        amount: u128,
    },
    WithdrawCompute {
        amount: u128,
    },
    /// Wrap TRTH into wTRTH (1:1). Deducts TRTH, mints wTRTH token balance.
    WrapTRTH {
        amount: u128,
    },
    /// Unwrap wTRTH back to TRTH (1:1). Burns wTRTH token balance, credits TRTH.
    UnwrapTRTH {
        amount: u128,
    },
    Stake {
        amount: u128,
    },
    Unstake {
        amount: u128,
    },
    WithdrawStake,
    Unjail,
    MintNFT {
        nft_id: [u8; 32],
        name: String,
        metadata_uri: String,
        collection: Option<[u8; 32]>,
        royalty_bps: u16,
        royalty_recipient: Option<AccountId>,
    },
    TransferNFT {
        nft_id: [u8; 32],
        recipient: AccountId,
        /// Omit for accounts already known on-chain (saves 1,952 bytes).
        recipient_pubkey: Option<Vec<u8>>,
        sale_price: Option<u128>,
    },
    BurnNFT {
        nft_id: [u8; 32],
    },
    ApproveNFT {
        nft_id: [u8; 32],
        approved: Option<AccountId>,
    },
    DeployCell {
        cell_id: AccountId,
        bytecode: Vec<u8>,
        initial_balance: u128,
        declared_reads: Vec<[u8; 32]>,
        declared_writes: Vec<[u8; 32]>,
        commutative_keys: Vec<[u8; 32]>,
        storage_key_specs: Vec<StorageKeySpec>,
        oracle_schema_ids: Vec<[u8; 32]>,
    },
    DeployToken {
        cell_id: AccountId,
        name: String,
        symbol: String,
        decimals: u8,
        total_supply: u128,
        transfer_fee_bps: u16,
        transfer_fee_recipient: Option<AccountId>,
        non_transferable: bool,
    },
    CallCell {
        cell_id: AccountId,
        calldata: Vec<u8>,
        value: u128,
        gas_limit: u64,
    },
    CallCellChain {
        calls: Vec<CellCall>,
        gas_limit: u64,
    },
    UpgradeCell {
        cell_id: AccountId,
        new_bytecode: Vec<u8>,
        new_declared_reads: Vec<[u8; 32]>,
        new_declared_writes: Vec<[u8; 32]>,
        new_commutative_keys: Vec<[u8; 32]>,
        new_storage_key_specs: Vec<StorageKeySpec>,
        new_oracle_schema_ids: Vec<[u8; 32]>,
    },
    TransferOwnership {
        cell_id: AccountId,
        new_owner: AccountId,
    },
    AcceptOwnership {
        cell_id: AccountId,
    },
    MakeImmutable {
        cell_id: AccountId,
    },
    CloseCell {
        cell_id: AccountId,
    },
    ProposeCellUpgrade {
        cell_id: AccountId,
        new_bytecode: Vec<u8>,
        new_declared_reads: Vec<[u8; 32]>,
        new_declared_writes: Vec<[u8; 32]>,
        new_commutative_keys: Vec<[u8; 32]>,
        new_storage_key_specs: Vec<StorageKeySpec>,
        new_oracle_schema_ids: Vec<[u8; 32]>,
        timelock_blocks: u64,
    },
    ProposeCellOwnershipTransfer {
        cell_id: AccountId,
        new_owner: AccountId,
        timelock_blocks: u64,
    },
    ProposeCellMakeImmutable {
        cell_id: AccountId,
        timelock_blocks: u64,
    },
    VoteCellProposal {
        cell_id: AccountId,
        approve: bool,
    },
    ExecuteCellProposal {
        cell_id: AccountId,
    },
    TokenTransfer {
        token_cell: AccountId,
        recipient: AccountId,
        amount: u128,
    },
    TokenMint {
        token_cell: AccountId,
        recipient: AccountId,
        amount: u128,
    },
    TokenBurn {
        token_cell: AccountId,
        amount: u128,
    },
    TokenFreeze {
        token_cell: AccountId,
        account: AccountId,
    },
    TokenThaw {
        token_cell: AccountId,
        account: AccountId,
    },
    ProposeTokenAuthority {
        token_cell: AccountId,
        set_mint_authority: bool,
        new_mint_authority: AccountId,
        set_freeze_authority: bool,
        new_freeze_authority: AccountId,
        voting_period_blocks: u64,
    },
    VoteTokenAuthority {
        token_cell: AccountId,
        approve: bool,
    },
    CallSystem {
        controller: AccountId,
        calldata: Vec<u8>,
    },
    ProposeUrl {
        url_pattern: String,
        bond_amount: u128,
        voting_period_blocks: u64,
    },
    VoteUrl {
        url_pattern: String,
        approve: bool,
    },
    ReportMaliciousUrl {
        url_pattern: String,
        evidence: String,
    },
    SetCellVisibility {
        cell_id: AccountId,
        visibility: u8,
    },
    // MCP intents
    RegisterMcpTool {
        tool_id: AccountId,
        bytecode: Vec<u8>,
        name: String,
        input_schema_json: Vec<u8>,
        category: u8,
        declared_reads: Vec<[u8; 32]>,
        declared_writes: Vec<[u8; 32]>,
        commutative_keys: Vec<[u8; 32]>,
        oracle_schema_ids: Vec<[u8; 32]>,
        registry_id: AccountId,
    },
    RegisterMcpResource {
        resource_id: AccountId,
        bytecode: Vec<u8>,
        name: String,
        uri_scheme: String,
        mime_type: String,
        initial_data: Vec<(Vec<u8>, Vec<u8>)>,
        declared_reads: Vec<[u8; 32]>,
        declared_writes: Vec<[u8; 32]>,
        oracle_schema_ids: Vec<[u8; 32]>,
        registry_id: AccountId,
    },
    RegisterMcpPrompt {
        prompt_id: AccountId,
        name: String,
        template_bytes: Vec<u8>,
        arguments: Vec<(String, String, bool)>,
        registry_id: AccountId,
    },
    RegisterAgent {
        agent_id: AccountId,
        policy_cell_id: AccountId,
        agent_registry_id: AccountId,
    },
    SuspendAgent {
        agent_id: AccountId,
        agent_registry_id: AccountId,
        reason: String,
    },
    ReinstateAgent {
        agent_id: AccountId,
        agent_registry_id: AccountId,
    },
    McpToolCall {
        agent_id: AccountId,
        tool_id: AccountId,
        tool_calldata: Vec<u8>,
        value: u128,
        gas_limit: u64,
        policy_cell_id: AccountId,
        action_log_id: Option<AccountId>,
        timestamp: u64,
    },
    PrivateBalanceInit {
        cell_id: AccountId,
        agent_id: AccountId,
        encrypted_balance: Vec<u8>,
        commitment: [u8; 32],
        commit_nonce: [u8; 16],
    },
    PrivateBalanceDeposit {
        cell_id: AccountId,
        agent_id: AccountId,
        amount: u128,
        new_encrypted_balance: Vec<u8>,
        new_commitment: [u8; 32],
        new_commit_nonce: [u8; 16],
        old_commitment: [u8; 32],
    },
    PrivateBalanceWithdraw {
        cell_id: AccountId,
        agent_id: AccountId,
        amount: u128,
        recipient: AccountId,
        new_encrypted_balance: Vec<u8>,
        new_commitment: [u8; 32],
        new_commit_nonce: [u8; 16],
        old_commitment: [u8; 32],
    },
    PrivateBalanceConfidentialTransfer {
        sender_cell_id: AccountId,
        sender_agent_id: AccountId,
        recipient_cell_id: AccountId,
        amount_commitment: [u8; 32],
        stark_proof: Vec<u8>,
        sender_new_encrypted: Vec<u8>,
        sender_new_commitment: [u8; 32],
        sender_new_commit_nonce: [u8; 16],
        sender_old_commitment: [u8; 32],
        recipient_new_encrypted: Vec<u8>,
        recipient_new_commitment: [u8; 32],
        recipient_new_commit_nonce: [u8; 16],
        recipient_old_commitment: [u8; 32],
        proof_sender_old_balance: u64,
        proof_sender_new_balance: u64,
        proof_recipient_old_balance: u64,
        proof_recipient_new_balance: u64,
        proof_amount: u64,
    },
    // Oracle commit/reveal
    SubmitOracleCommit {
        request_id: [u8; 32],
        commit_hash: [u8; 32],
    },
    SubmitOracleReveal {
        request_id: [u8; 32],
        response_body: Vec<u8>,
        response_status: u16,
    },
}

pub fn system_cell_id(label: &str) -> AccountId {
    use sha2::{Digest, Sha256};
    let mut hasher = Sha256::new();
    hasher.update(label.as_bytes());
    hasher.finalize().into()
}

pub fn staking_system_cell_id() -> AccountId {
    system_cell_id("truthlinked-staking-v1")
}

pub fn treasury_system_cell_id() -> AccountId {
    system_cell_id("truthlinked-treasury-v1")
}

pub fn governance_system_cell_id() -> AccountId {
    system_cell_id("truthlinked-governance-v1")
}

pub fn name_registry_system_cell_id() -> AccountId {
    system_cell_id("truthlinked-name-registry-v1")
}

pub fn token_governance_system_cell_id() -> AccountId {
    system_cell_id("truthlinked-token-governance-v1")
}

pub fn oracle_governance_system_cell_id() -> AccountId {
    system_cell_id("truthlinked-oracle-governance-v1")
}

pub fn usdc_system_cell_id() -> AccountId {
    system_cell_id("truthlinked-usdc-controller-v1")
}

pub fn wtrth_system_cell_id() -> AccountId {
    system_cell_id("truthlinked-wtrth-v1")
}

pub fn usdt_system_cell_id() -> AccountId {
    system_cell_id("truthlinked-usdt-controller-v1")
}

impl Transaction {
    pub fn current_timestamp() -> u64 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
    }
}

impl TransactionIntent {
    pub fn cell_id_option(&self) -> Option<AccountId> {
        match self {
            TransactionIntent::DeployCell { cell_id, .. }
            | TransactionIntent::DeployToken { cell_id, .. }
            | TransactionIntent::CallCell { cell_id, .. }
            | TransactionIntent::UpgradeCell { cell_id, .. }
            | TransactionIntent::TransferOwnership { cell_id, .. }
            | TransactionIntent::AcceptOwnership { cell_id }
            | TransactionIntent::MakeImmutable { cell_id }
            | TransactionIntent::CloseCell { cell_id }
            | TransactionIntent::ProposeCellUpgrade { cell_id, .. }
            | TransactionIntent::ProposeCellOwnershipTransfer { cell_id, .. }
            | TransactionIntent::ProposeCellMakeImmutable { cell_id, .. }
            | TransactionIntent::VoteCellProposal { cell_id, .. }
            | TransactionIntent::ExecuteCellProposal { cell_id, .. }
            | TransactionIntent::TokenTransfer {
                token_cell: cell_id,
                ..
            }
            | TransactionIntent::TokenMint {
                token_cell: cell_id,
                ..
            }
            | TransactionIntent::TokenBurn {
                token_cell: cell_id,
                ..
            } => Some(*cell_id),
            TransactionIntent::CallCellChain { calls, .. } => calls.first().map(|c| c.cell_id),
            _ => None,
        }
    }
}