Skip to content

Commit

Permalink
handle token uri
Browse files Browse the repository at this point in the history
  • Loading branch information
PavitraAgarwal21 committed Sep 3, 2024
1 parent 1662009 commit ebcaae9
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 40 deletions.
3 changes: 1 addition & 2 deletions src/base/token_uris.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod follow_token_uri;
mod handle_token_uri;
mod profile_token_uri;
mod traits;
mod profile_svg;
mod traits;
52 changes: 49 additions & 3 deletions src/base/token_uris/handle_token_uri.cairo
Original file line number Diff line number Diff line change
@@ -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::<felt252>::new();
let mut attributespost = ArrayTrait::<felt252>::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 //


37 changes: 2 additions & 35 deletions src/base/token_uris/profile_token_uri.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<felt252> {
let mut svg = ArrayTrait::<felt252>::new();
Expand Down Expand Up @@ -55,39 +55,6 @@ pub mod ProfileTokenUri {
json
}

fn convert_into_byteArray(ref svg: Array<felt252>) -> 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::<u8>::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,';
Expand Down
1 change: 1 addition & 0 deletions src/base/token_uris/traits.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ mod color;
mod beard;
mod background;
mod cloth;
mod profile ;
File renamed without changes.
1 change: 1 addition & 0 deletions src/base/utils.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod byte_array_extra;
mod hubrestricted;
mod base64_extended ;
38 changes: 38 additions & 0 deletions src/base/utils/base64_extended.cairo
Original file line number Diff line number Diff line change
@@ -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::<u8>::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<felt252>) -> 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
}

0 comments on commit ebcaae9

Please sign in to comment.