diff --git a/onchain/src/social/bech32.cairo b/onchain/src/social/bech32.cairo index 81ea55de..5b322adf 100644 --- a/onchain/src/social/bech32.cairo +++ b/onchain/src/social/bech32.cairo @@ -10,7 +10,6 @@ use core::to_byte_array::FormatAsByteArray; use core::traits::{Into, TryInto}; use joyboy::utils::{shl, shr}; - //! bech32 encoding implementation //! Spec: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki //! Sample implementations: @@ -22,7 +21,6 @@ fn polymod(values: Array) -> u32 { 0x3b6a57b2_u32, 0x26508e6d_u32, 0x1ea119fa_u32, 0x3d4233dd_u32, 0x2a1462b3_u32 ]; let generator = generator.span(); - let mut chk = 1_u32; let len = values.len(); @@ -81,46 +79,32 @@ fn convert_bytes_to_5bit_chunks(bytes: @Array) -> Array { let len = bytes.len(); let mut i = 0; - let mut acc = 0_u8; let mut missing_bits = 5_u8; - // println!("bytes = {bytes:?}"); - loop { - if i == len { - break (); - } + while i < len { let mut byte: u8 = *bytes.at(i); let mut bits_left = 8_u8; - loop { + while bits_left > 0 { let chunk_size = min(missing_bits, bits_left); let chunk = shr(byte, 8 - chunk_size); - // println!( - // "byte: {}, acc: {}, chunk: {} -> {}", - // byte.format_as_byte_array(2), - // acc.format_as_byte_array(2), - // chunk.format_as_byte_array(2), - // (acc + chunk).format_as_byte_array(2) - // ); r.append(acc + chunk); byte = shl(byte, chunk_size); bits_left -= chunk_size; if bits_left < 5 { acc = shr(byte, 3); missing_bits = 5 - bits_left; - break (); + break; } else { acc = 0; - missing_bits = 5 + missing_bits = 5; } - }; + } i += 1; - }; + } if missing_bits < 5 { - // println!("-> {}", acc.format_as_byte_array(2)); r.append(acc); } - // println!("r = {r:?}"); r } @@ -162,11 +146,9 @@ fn checksum(hrp: @ByteArray, data: @Array) -> Array { } pub fn encode(hrp: @ByteArray, data: @ByteArray, limit: usize) -> ByteArray { - // change into an array and a const - let alphabet: ByteArray = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; - + + const ALPHABET: ByteArray = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; let data_5bits = convert_bytes_to_5bit_chunks(@data.into()); - let cs = checksum(hrp, @data_5bits); let mut combined = ArrayTrait::new(); @@ -174,13 +156,14 @@ pub fn encode(hrp: @ByteArray, data: @ByteArray, limit: usize) -> ByteArray { combined.append_span(cs.span()); let mut encoded: ByteArray = Default::default(); + encoded.append_byte(b'1'); let mut i = 0; let len = combined.len(); loop { if i == len { - break (); + break; } - encoded.append_byte(alphabet.at((*combined.at(i)).into()).unwrap()); + encoded.append_byte(ALPHABET.at((*combined.at(i)).into()).unwrap()); i += 1; };