v0.3.12-pre.004
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-raw-transaction-lib/src/signature.rs
|
||||
// version: 3
|
||||
// version: 4
|
||||
|
||||
use base64::Engine; // rust-rules: trait-import
|
||||
|
||||
@@ -8,6 +8,35 @@ pub const MAX_RAW_TRANSACTION_SIGNATURE_TEXT_BYTES: usize = 88;
|
||||
/// Minimum UTF-8 byte length admitted for one textual Base58 Solana transaction signature.
|
||||
pub const MIN_RAW_TRANSACTION_SIGNATURE_TEXT_BYTES: usize = 64;
|
||||
|
||||
/// Encodes one canonical 64-byte Solana transaction signature as bounded Base58 text.
|
||||
#[must_use]
|
||||
pub fn format_raw_transaction_signature(signature: &ksp_store_api::RawTransactionSignature) -> std::string::String {
|
||||
const ALPHABET: &[u8; 58] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
||||
let bytes = signature.as_bytes();
|
||||
let leading_zeroes = bytes.iter().take_while(|byte| return **byte == 0).count();
|
||||
let mut digits = std::vec::Vec::with_capacity(crate::MAX_RAW_TRANSACTION_SIGNATURE_TEXT_BYTES);
|
||||
for byte in bytes {
|
||||
let mut carry = u32::from(*byte);
|
||||
for digit in &mut digits {
|
||||
let expanded = (u32::from(*digit) * 256) + carry;
|
||||
*digit = (expanded % 58) as u8;
|
||||
carry = expanded / 58;
|
||||
}
|
||||
while carry != 0 {
|
||||
digits.push((carry % 58) as u8);
|
||||
carry /= 58;
|
||||
}
|
||||
}
|
||||
let mut output = std::string::String::with_capacity(leading_zeroes + digits.len());
|
||||
for _ in 0..leading_zeroes {
|
||||
output.push('1');
|
||||
}
|
||||
for digit in digits.iter().rev() {
|
||||
output.push(char::from(ALPHABET[usize::from(*digit)]));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/// Parses one bounded Base58 Solana transaction signature to exactly 64 canonical bytes.
|
||||
pub fn parse_raw_transaction_signature(value: &str) -> ksp_core_lib::Result<ksp_store_api::RawTransactionSignature> {
|
||||
let text = value.as_bytes();
|
||||
|
||||
Reference in New Issue
Block a user