diff --git a/src/base/token_uris.cairo b/src/base/token_uris.cairo index 0321b91..d300aea 100644 --- a/src/base/token_uris.cairo +++ b/src/base/token_uris.cairo @@ -1,5 +1,4 @@ mod follow_token_uri; mod handle_token_uri; mod profile_token_uri; -mod traits; -mod profile_svg; +mod traits; \ No newline at end of file diff --git a/src/base/token_uris/handle_token_uri.cairo b/src/base/token_uris/handle_token_uri.cairo index 56fd220..b0412b9 100644 --- a/src/base/token_uris/handle_token_uri.cairo +++ b/src/base/token_uris/handle_token_uri.cairo @@ -1,10 +1,56 @@ // TODO: https://github.com/lens-protocol/core/blob/master/contracts/misc/token-uris/HandleTokenURI.sol pub mod HandleTokenUri { + use core::array::ArrayTrait; use alexandria_bytes::{Bytes, BytesTrait}; use karst::base::utils::byte_array_extra::FeltTryIntoByteArray; + use karst::base::utils::base64_extended::{convert_into_byteArray , get_base64_encode } ; + use karst::base::token_uris::traits::profile::ProfileSvg::gen_profile_svg ; + pub fn get_token_uri(token_id: u256, local_name: felt252, namespace: felt252 ) -> ByteArray { + let baseuri = 'data:image/svg+xml;base64,'; + /// TODO what are feaature include in the svg + // let mut svg_byte_array: ByteArray = get_svg(token_id, local_name , namespace); + let mut svg_byte_array = gen_profile_svg() ; + let mut svg_encoded: ByteArray = get_base64_encode(svg_byte_array); + let mut attribute_byte_array: ByteArray = get_attributes(token_id ,ref svg_encoded , local_name , namespace ); + let mut token_uri: ByteArray = baseuri.try_into().unwrap(); + token_uri.append(@attribute_byte_array); + token_uri + } - pub fn get_token_uri(token_id: u256, local_name: felt252, namespace: felt252) -> ByteArray { - "TODO" + fn get_attributes(token_id: u256,ref svg_encoded_byteArray : ByteArray ,local_name: felt252 , namespace : felt252 ) -> ByteArray { + let token_id_felt: felt252 = token_id.try_into().unwrap(); + let token_id_byte: ByteArray = token_id_felt.try_into().unwrap(); + let token_id_byte_len: felt252 = token_id_byte.len().try_into().unwrap(); + let mut attributespre = ArrayTrait::::new(); + let mut attributespost = ArrayTrait::::new() ; + attributespre.append( '{"name":"@'); + attributespre.append(local_name); + attributespre.append('","description":"Lens '); + attributespre.append('Protocol - Handle @') ; + attributespre.append(local_name) ; + attributespre.append('","image":"data:image'); + attributespre.append('/svg+xml;base64,'); + attributespost.append('","attributes":[{"display') ; + attributespost.append('_type":"number","trait_type'); + attributespost.append('":"ID","value":"'); + attributespost.append(token_id.try_into().unwrap()) ; + attributespost.append('"},{"trait_type":"NAMES'); + attributespost.append('PACE","value":"'); + attributespost.append(namespace) ; + attributespost.append('"},{"trait_type":"'); + attributespost.append('LENGTH","value":"') ; + attributespost.append('token_id_byte_len') ; + attributespost.append( '"}]}') ; + let mut attributespre_bytearray = convert_into_byteArray(ref attributespre) ; + let mut attributespost_bytearray = convert_into_byteArray(ref attributespost) ; + attributespre_bytearray.append(@svg_encoded_byteArray) ; + attributespre_bytearray.append(@attributespost_bytearray) ; + get_base64_encode( attributespre_bytearray) } -} +} + + + // cvonver the byteArray into the base64 encoded byteArray // + + \ No newline at end of file diff --git a/src/base/token_uris/profile_token_uri.cairo b/src/base/token_uris/profile_token_uri.cairo index e469882..28b6bbf 100644 --- a/src/base/token_uris/profile_token_uri.cairo +++ b/src/base/token_uris/profile_token_uri.cairo @@ -4,8 +4,8 @@ pub mod ProfileTokenUri { use core::array::ArrayTrait; use alexandria_bytes::{Bytes, BytesTrait}; use karst::base::utils::byte_array_extra::FeltTryIntoByteArray; - use alexandria_encoding::base64::{Base64UrlEncoder}; - + use karst::base::utils::base64_extended:: { get_base64_encode , convert_into_byteArray }; + use karst::base::token_uris::traits::profile::ProfileSvg::gen_profile_svg ; // get svg according to the token id and mint timestamp fn get_svg(token_id: u256, mint_timestamp: u64) -> Array { let mut svg = ArrayTrait::::new(); @@ -55,39 +55,6 @@ pub mod ProfileTokenUri { json } - fn convert_into_byteArray(ref svg: Array) -> ByteArray { - let mut res: ByteArray = Default::default(); - // converting felt252 array to byte array - while (!svg.is_empty()) { - let each_felt: felt252 = svg.pop_front().unwrap(); - let word: ByteArray = each_felt.try_into().unwrap(); - res.append(@word); - }; - res - } - - fn get_base64_encode(res: ByteArray) -> ByteArray { - let mut res_arr_u8 = ArrayTrait::::new(); - let mut i = 0; - while i < res - .len() { - let mut res_data = res.at(i); - res_arr_u8.append(res_data.unwrap()); - i += 1; - }; - // encoding the array of u8 to base64url - let mut encoded_val = Base64UrlEncoder::encode(res_arr_u8); - // converting array of u8 to byte array - let mut res_final: ByteArray = Default::default(); - let mut j = 0; - while j < encoded_val - .len() { - let encoded_val_data = encoded_val.at(j); - res_final.append_byte(*encoded_val_data); - j += 1; - }; - res_final - } pub fn get_token_uri(token_id: u256, mint_timestamp: u64) -> ByteArray { let baseuri = 'data:image/svg+xml;base64,'; diff --git a/src/base/token_uris/traits.cairo b/src/base/token_uris/traits.cairo index daa6b31..44d956c 100644 --- a/src/base/token_uris/traits.cairo +++ b/src/base/token_uris/traits.cairo @@ -4,3 +4,4 @@ mod color; mod beard; mod background; mod cloth; +mod profile ; \ No newline at end of file diff --git a/src/base/token_uris/profile_svg.cairo b/src/base/token_uris/traits/profile.cairo similarity index 100% rename from src/base/token_uris/profile_svg.cairo rename to src/base/token_uris/traits/profile.cairo diff --git a/src/base/utils.cairo b/src/base/utils.cairo index 8a90e4f..96e0a75 100644 --- a/src/base/utils.cairo +++ b/src/base/utils.cairo @@ -1,2 +1,3 @@ mod byte_array_extra; mod hubrestricted; +mod base64_extended ; \ No newline at end of file diff --git a/src/base/utils/base64_extended.cairo b/src/base/utils/base64_extended.cairo new file mode 100644 index 0000000..061bb83 --- /dev/null +++ b/src/base/utils/base64_extended.cairo @@ -0,0 +1,38 @@ +use core::serde::Serde; +use core::array::ArrayTrait; +use karst::base::utils::byte_array_extra::FeltTryIntoByteArray; +use alexandria_encoding::base64::{Base64UrlEncoder}; + +pub fn get_base64_encode(res: ByteArray) -> ByteArray { + let mut res_arr_u8 = ArrayTrait::::new(); + let mut i = 0; + while i < res + .len() { + let mut res_data = res.at(i); + res_arr_u8.append(res_data.unwrap()); + i += 1; + }; + // encoding the array of u8 to base64url + let mut encoded_val = Base64UrlEncoder::encode(res_arr_u8); + // converting array of u8 to byte array + let mut res_final: ByteArray = Default::default(); + let mut j = 0; + while j < encoded_val + .len() { + let encoded_val_data = encoded_val.at(j); + res_final.append_byte(*encoded_val_data); + j += 1; + }; + res_final + } + +pub fn convert_into_byteArray(ref svg: Array) -> ByteArray { + let mut res: ByteArray = Default::default(); + // converting felt252 array to byte array + while (!svg.is_empty()) { + let each_felt: felt252 = svg.pop_front().unwrap(); + let word: ByteArray = each_felt.try_into().unwrap(); + res.append(@word); + }; + res + } \ No newline at end of file