Skip to main content

thru_base/
lib.rs

1pub mod compression;
2pub mod bintrie;
3pub mod bintrie_error;
4pub mod bintrie_proof;
5pub mod bintrie_types;
6pub mod block_parser;
7pub mod crypto_utils;
8pub mod bootstrap_addresses;
9pub mod ratelimit;
10pub mod rpc_types;
11pub mod tn_account;
12pub mod tn_block_packet;
13pub mod tn_public_address;
14pub mod tn_runtime_utils;
15pub mod signer;
16pub mod tn_signature;
17pub mod tn_signature_encoding;
18pub mod tn_signature_legacy;
19pub mod tn_state_proof;
20pub mod tn_tools;
21pub mod txn_lib;
22pub mod txn_tools;
23pub mod version;
24
25#[cfg(test)]
26mod txn_tools_test;
27
28#[cfg(test)]
29mod bintrie_tests;
30
31pub const TN_VM_ERROR_REVERT: i32 = -765; /* TN_RUNTIME_TXN_VM_REVERT fffffd03 */
32pub const TN_VM_ERROR_FEE_PAYER_ACCOUNT_DOES_NOT_EXIST: i32 = -508; /* TN_RUNTIME_TXN_ERR_FEE_PAYER_ACCOUNT_DOES_NOT_EXIST fffffe04 */
33
34pub const TN_EXECUTION_RESULT: u64 = 0xfffffffffffffffc; /* TN_VM_ERR_SIGFAULT  */
35pub const TN_USER_ERROR_CODE_SYSCALL_INSUFFICIENT_BALANCE: u64 = 0xffffffffffffffdc; /* TN_VM_ERR_SYSCALL_INSUFFICIENT_BALANCE */
36
37// re-export types
38pub use bintrie::{BinTrie, BinTriePair};
39pub use tn_state_proof::{StateProof, StateProofBody, StateProofHeader, StateProofType};
40pub use tn_tools::KeyPair;
41pub use tn_tools::Pubkey;
42pub use tn_tools::Signature;
43pub use txn_lib::Transaction;
44pub use txn_tools::TransactionBuilder;
45
46// re-export crypto utilities
47pub use crypto_utils::{derive_manager_program_accounts, derive_uploader_program_accounts};
48
49// re-export public address utilities
50pub use tn_public_address::{
51    create_program_defined_account_address, create_program_defined_account_address_string,
52    pack_seed, tn_pubkey_to_address_string, tn_public_address_decode, tn_public_address_encode,
53};
54
55// re-export runtime utilities
56pub use bintrie_error::BinTrieError;
57pub use bintrie_proof::{NonExistenceProof, Proof};
58pub use bintrie_types::{Hash as BinTrieHash, Pubkey as BinTriePubkey};
59pub use tn_runtime_utils::tn_vm_error_str;
60
61/// Checks if the given byte slice is a valid C-style null-terminated string containing only printable ASCII characters.
62pub fn is_c_printable_ascii_null_terminated(bytes: &[u8]) -> bool {
63    // Must contain at least one null byte
64    let Some(pos) = bytes.iter().position(|&b| b == 0) else {
65        return false;
66    };
67    // All bytes before the first null must be printable ASCII (0x20..=0x7E)
68    for &b in &bytes[..pos] {
69        if !(b >= 0x20 && b <= 0x7E) {
70            return false;
71        }
72    }
73    true
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_is_c_printable_ascii_null_terminated() {
82        // Valid cases
83        assert!(is_c_printable_ascii_null_terminated(b"hello\0"));
84        assert!(is_c_printable_ascii_null_terminated(b"!@#$%^&*()_+\0"));
85        assert!(is_c_printable_ascii_null_terminated(b" \0")); // space is printable
86        assert!(is_c_printable_ascii_null_terminated(b"~\0")); // tilde is printable
87        assert!(is_c_printable_ascii_null_terminated(b"\0")); // empty string
88        // Valid: data after null is ignored
89        assert!(is_c_printable_ascii_null_terminated(
90            b"hello\0not_printed\x01\x02"
91        ));
92
93        // Invalid: not null-terminated
94        assert!(!is_c_printable_ascii_null_terminated(b"hello"));
95        // Invalid: contains non-printable ASCII (tab)
96        assert!(!is_c_printable_ascii_null_terminated(b"hel\tlo\0"));
97        // Invalid: contains non-printable ASCII (DEL)
98        assert!(!is_c_printable_ascii_null_terminated(b"hel\x7Flo\0"));
99        // Invalid: empty slice
100        assert!(!is_c_printable_ascii_null_terminated(b""));
101        // Invalid: non-printable before null
102        assert!(!is_c_printable_ascii_null_terminated(b"foo\x19\0"));
103    }
104}