use std::rc::Rc;
use std::any::Any;
use std::cell::Cell;
use basex_rs::{BaseX, SKYWELL, Decode};
use crate::base::wallet::keypair::{Keypair, KeypairBuilder};
use crate::WalletType;
use crate::message::common::amount::Amount;
use crate::base::base_config::*;
use crate::base::wallet::address::{WalletAddress};
use crate::base::wallet::seed::Seed;
use cast_rs::hex;
pub fn get_keypair_from_secret(secret: &String) -> Result<Keypair, &'static str> {
let wtype = fetch_wtype_from_secret(&secret);
KeypairBuilder::new(&secret, &wtype).build()
}
pub fn fetch_wtype_from_secret(_secret: &String) -> WalletType {
WalletType::SECP256K1
}
pub fn check_secret(secret: &String) -> Option<bool> {
Seed::check_secret(secret)
}
pub fn check_address(address: &String) -> Option<bool> {
WalletAddress::check_address(address)
}
pub fn check_currency(cur: &Option<String>) -> bool {
let mut f = true;
if let Some(x) = cur {
if x.len() != 3 {
f = false;
}
for c in x.chars() {
if c >= 'A' && c <= 'Z' {
} else {
f = false;
break;
}
}
}
f
}
pub fn check_amount(amount: &Amount) -> bool {
let value = &amount.value;
let len = value.len();
if len == 1 && value == "." {
return false;
}
for c in value.chars() {
if c >= '0' && c <= '9' || c == '.' {
} else {
return false;
}
}
if check_currency(&amount.currency) == false {
return false;
}
if amount.currency == Some(CURRENCY.to_string()) && amount.issuer.is_some() {
return false;
}
let mut is_issuer = None;
if let Some(ref x) = amount.issuer {
is_issuer = check_address(&x);
}
if amount.currency != Some(CURRENCY.to_string()) && is_issuer.is_none() {
return false;
}
true
}
pub fn decode_j_address(address: String) -> Option<Vec<u8>> {
decode_address(address)
}
pub fn decode_address(address: String) -> Option<Vec<u8>> {
if true { return decode_versioned(address);
}
None
}
pub fn decode_versioned(address: String) -> Option<Vec<u8>> {
decode_multi_versioned(address)
}
pub fn decode_multi_versioned(address: String) -> Option<Vec<u8>> {
let x = decode_checked(address);
x
}
pub fn decode_checked(encoded: String) -> Option<Vec<u8>> {
let buf = decode_raw(encoded).unwrap();
Some(buf[1..21].to_vec())
}
pub fn decode_raw(encoded: String) -> Option<Vec<u8>> {
BaseX::new(SKYWELL).decode(encoded)
}
pub fn downcast_to_string(value: Rc<dyn Any>) -> String {
match value.downcast::<Cell<String>>() {
Ok(string) => {
return string.take();
},
Err(_) => { "None".to_string() }
}
}
pub fn downcast_to_usize(value: Rc<dyn Any>) -> u32 {
let mut us: u64 = 0;
if let Ok(u) = value.downcast::<Cell<u64>>() {
us = u.take();
}
us as u32
}
pub fn check_value(p: i32) -> String {
let mut hex_string = format!("{:x}", p);
while hex_string.len() < 64 {
hex_string.insert(0, '0');
}
return hex::encode(hex_string);
}
pub fn check_string(p: String) -> String {
let mut hex_string = hex::encode(p);
while hex_string.len() < 64 {
hex_string.push('0');
}
return hex::encode(hex_string);
}
pub fn check(p: String) -> String {
let hex_string: String = p;
if let Ok(x) = hex_string.parse::<i32>() {
return check_value(x);
}
return check_string(hex_string);
}