Skip to content

Commit

Permalink
starknetid
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Sep 6, 2023
1 parent 05f9feb commit d04b24d
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 5 deletions.
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ starknet = { git = "https://github.com/xJonathanLEI/starknet-rs" }
koit = "0.2.0"
twitter-v2 = "0.1.8"
serde_json = "1.0.105"
bigdecimal = "0.4.1"
152 changes: 151 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use bigdecimal::num_traits;
use dotenv::dotenv;
use num_traits::cast::ToPrimitive;
use reqwest::header::HeaderValue;
use reqwest::{header, Url};
use serde::{Deserialize, Serialize};
use starknet::core::types::{BlockId, EventFilter, FieldElement};
use starknet::core::types::{BlockId, BlockTag, EventFilter, FieldElement, FunctionCall};
use starknet::core::utils::get_selector_from_name;
use starknet::providers::Provider;
use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient};
Expand Down Expand Up @@ -125,6 +127,103 @@ pub async fn fetch_events(token: Token) -> Result<(), reqwest::Error> {
Ok(())
}

async fn address_to_domain(address: FieldElement, contract_addr: FieldElement) {
dotenv().ok();
let api_key = std::env::var(NODE_PROVIDER_API_KEY).expect("COINCAP_API_KEY must be set");

let rpc_url = format!("https://starknet-mainnet.infura.io/v3/{api_key}");
let rpc_client = JsonRpcClient::new(HttpTransport::new(Url::parse(&rpc_url).unwrap()));
let repsonse = rpc_client
.call(
FunctionCall {
contract_address: contract_addr,
entry_point_selector: get_selector_from_name("address_to_domain").unwrap(),
calldata: vec![address],
},
BlockId::Tag(BlockTag::Latest),
)
.await
.unwrap();

let mut domain = String::new();
repsonse.iter().skip(1).for_each(|value| {
domain.push_str(decode(*value).as_str());
domain.push('.');
});
domain.push_str("stark");
println!("DOMAIN FOUND {}", domain);
}

const BASIC_ALPHABET: &str = "abcdefghijklmnopqrstuvwxyz0123456789-";
const BIG_ALPHABET: &str = "这来";

pub fn decode(mut felt: FieldElement) -> String {
let mut decoded: String = String::new();
let basic_plus = FieldElement::from(BASIC_ALPHABET.chars().count() + 1);
let basic_len = FieldElement::from(BASIC_ALPHABET.chars().count());
let big_plus = FieldElement::from(BIG_ALPHABET.chars().count() + 1);
let big_len = FieldElement::from(BIG_ALPHABET.chars().count());
let last_big = BIG_ALPHABET.chars().last().unwrap();
while felt != FieldElement::ZERO {
let code = felt % basic_plus;
felt = felt.floor_div(basic_plus);
if code == basic_len {
let next_felt = felt.floor_div(big_plus);
if next_felt == FieldElement::ZERO {
let code2 = felt % big_plus;
felt = next_felt;
decoded.push(if code2 == FieldElement::ZERO {
BASIC_ALPHABET.chars().next().unwrap()
} else {
last_big
});
} else {
decoded.push(
BIG_ALPHABET
.chars()
.nth((felt % big_len).to_big_decimal(0).to_usize().unwrap())
.unwrap(),
);
felt = felt.floor_div(big_len);
}
} else {
decoded.push(
BASIC_ALPHABET
.chars()
.nth(code.to_big_decimal(0).to_usize().unwrap())
.unwrap(),
);
}
let (decoded_str, k) = extract_stars(decoded.as_str());
let mut decoded = String::from(decoded_str);
if k != 0 {
let star = last_big.to_string();
if k % 2 == 0 {
decoded.push_str(&str::repeat(&star, k / 2 - 1));
decoded.push(BIG_ALPHABET.chars().next().unwrap());
let mut basic_iter = BASIC_ALPHABET.chars();
basic_iter.next();
decoded.push(basic_iter.next().unwrap());
} else {
decoded.push_str(&str::repeat(&star, k / 2 + 1));
}
}
}
decoded
}

fn extract_stars(mut domain: &str) -> (&str, usize) {
let mut k = 0;
let last_char = BIG_ALPHABET.chars().last().unwrap();
while domain.ends_with(last_char) {
let mut chars = domain.chars();
chars.next_back();
domain = chars.as_str();
k += 1;
}
(domain, k)
}

#[cfg(test)]
mod tests {
use super::{fetch_coin, fetch_events, Token};
Expand Down Expand Up @@ -156,4 +255,55 @@ mod tests {
};
fetch_events(eth).await.unwrap();
}

#[tokio::test]
async fn test_starknet_id() {
// stark
super::address_to_domain(
FieldElement::from_hex_be(
"0x1f4055a52c859593e79988bfe998b536066805fe757522ece47945f46f6b6e7",
)
.unwrap(),
FieldElement::from_hex_be(
"0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678",
)
.unwrap(),
)
.await;

// eli
super::address_to_domain(
FieldElement::from_hex_be(
"0x48f24d0d0618fa31813db91a45d8be6c50749e5e19ec699092ce29abe809294",
)
.unwrap(),
FieldElement::from_hex_be(
"0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678",
)
.unwrap(),
)
.await;

// scott
super::address_to_domain(
FieldElement::from_hex_be(
"0x225bd17f4b4ede26c77673d8d40ec9805ec139a8167cae8d621bd295b260d13",
)
.unwrap(),
FieldElement::from_hex_be(
"0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678",
)
.unwrap(),
)
.await;

super::address_to_domain(
FieldElement::from_hex_be("0x225bd17f4b4ede26c77673d8d3").unwrap(),
FieldElement::from_hex_be(
"0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678",
)
.unwrap(),
)
.await;
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const TWITTER_OAUTH2_CLIENT_SECRET: &str = "TWITTER_OAUTH2_CLIENT_SECRET";
async fn main() -> Result<(), Box<dyn Error>> {
check_valid_env();

tweet("Someteazhing".to_string()).await;
tweet("Someteaeazzhing".to_string()).await;
Ok(())
}

Expand Down
3 changes: 1 addition & 2 deletions src/twitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ pub async fn tweet(text_to_tweet: String) {
);

let mut token = token.lock().await;

if oauth2_client
.refresh_token_if_expired(&mut token)
.await
.unwrap()
{
println!("Refreshing token");
serde_json::to_writer(
std::fs::File::create("./.token.json").expect(".token.json not found"),
std::fs::File::create("./token.json").expect(".token.json not found"),
token.deref(),
)
.expect("couldn't save token");
Expand Down

0 comments on commit d04b24d

Please sign in to comment.