Skip to content

Commit

Permalink
Update dependencies. (#245)
Browse files Browse the repository at this point in the history
* Update dependencies.
  • Loading branch information
ssuchichan authored Apr 6, 2024
1 parent 5a4769d commit 42b5ff1
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 92 deletions.
2 changes: 1 addition & 1 deletion explorer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
env_logger = "0.11.3"
log = "0.4.14"
base64 = "0.10"
base64 = "0.22.0"
bech32 = "0.7.2"
ruc = "1.0"
reqwest = { version = "0.12.3", features = ["json"] }
Expand Down
13 changes: 8 additions & 5 deletions explorer/src/service/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Error;
use base64::{engine, Engine};
use poem::http::StatusCode;
use ruc::{d, Result, RucResult};
use {
Expand All @@ -9,13 +10,14 @@ use {
#[allow(unused)]
#[inline(always)]
pub fn public_key_to_base64(key: &XfrPublicKey) -> String {
base64::encode_config(&ZeiFromToBytes::zei_to_bytes(key), base64::URL_SAFE)
engine::general_purpose::URL_SAFE.encode(ZeiFromToBytes::zei_to_bytes(key))
}

#[allow(unused)]
#[inline(always)]
pub fn public_key_from_base64(pk: &str) -> Result<XfrPublicKey> {
base64::decode_config(pk, base64::URL_SAFE)
engine::general_purpose::URL_SAFE
.decode(pk)
.c(d!())
.and_then(|bytes| XfrPublicKey::zei_from_bytes(&bytes).c(d!()))
}
Expand Down Expand Up @@ -65,7 +67,6 @@ pub fn handle_fetch_one_err(err: Error) -> poem::Error {
#[cfg(test)]
mod tests {
use super::*;
use base64::URL_SAFE;

#[tokio::test]
async fn test_public_key_to_bech32() -> Result<()> {
Expand All @@ -89,10 +90,12 @@ mod tests {
async fn test_asset_code() -> Result<()> {
let asset_addr = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
let asset_raw: Vec<u8> = vec![0u8; 32];
let res_code = base64::decode_config(asset_addr, URL_SAFE).unwrap();
let res_code = engine::general_purpose::URL_SAFE
.decode(asset_addr)
.unwrap();
assert_eq!(asset_raw, res_code);

let res_addr = base64::encode_config(&asset_raw, URL_SAFE);
let res_addr = engine::general_purpose::URL_SAFE.encode(&asset_raw);
assert_eq!(asset_addr, res_addr);
Ok(())
}
Expand Down
39 changes: 23 additions & 16 deletions explorer/src/service/v1/asset.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::service::api::Api;
use anyhow::Result;
use base64::{engine, Engine};
use module::utils::crypto::bech32enc;
use poem_openapi::{param::Path, param::Query, payload::Json, ApiResponse, Object};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -83,7 +84,7 @@ pub struct AssetRPCResult {

pub async fn get_asset(api: &Api, address: Path<String>) -> Result<AssetResponse> {
let mut conn = api.storage.lock().await.acquire().await?;
let code_res = base64::decode_config(&address.0, base64::URL_SAFE);
let code_res = engine::general_purpose::URL_SAFE.decode(&address.0);
let code = match code_res {
Ok(code) => code,
_ => {
Expand All @@ -95,7 +96,9 @@ pub async fn get_asset(api: &Api, address: Path<String>) -> Result<AssetResponse
}
};

let sql_query = "SELECT jsonb_path_query(value,'$.body.operations[*].DefineAsset.body.asset') AS asset,tx_hash,block_hash,height FROM transaction ORDER BY height DESC".to_string();
let sql_query = "SELECT jsonb_path_query(value,'$.body.operations[*].DefineAsset.body.asset') \
AS asset,tx_hash,block_hash,height FROM transaction ORDER BY height DESC"
.to_string();
let rows = sqlx::query(sql_query.as_str())
.fetch_all(&mut *conn)
.await?;
Expand All @@ -110,10 +113,10 @@ pub async fn get_asset(api: &Api, address: Path<String>) -> Result<AssetResponse
let block: String = row.try_get("block_hash")?;
let tx: String = row.try_get("tx_hash")?;

let pk_vec = base64::decode_config(&a.issuer.key, base64::URL_SAFE)?;
let pk_vec = engine::general_purpose::URL_SAFE.decode(&a.issuer.key)?;
let pk = XfrPublicKey::zei_from_bytes(&pk_vec).unwrap_or_default();
let issuer_addr = bech32enc(&XfrPublicKey::zei_to_bytes(&pk));
let asset_code = base64::encode_config(&a.code.val, base64::URL_SAFE);
let asset_code = engine::general_purpose::URL_SAFE.encode(&a.code.val);

assets.push(AssetDisplay {
issuer: issuer_addr,
Expand Down Expand Up @@ -144,10 +147,10 @@ pub async fn get_asset(api: &Api, address: Path<String>) -> Result<AssetResponse
.json::<AssetRPCResult>()
.await?;

let pk_vec = base64::decode_config(&resp.properties.issuer.key, base64::URL_SAFE)?;
let pk_vec = engine::general_purpose::URL_SAFE.decode(&resp.properties.issuer.key)?;
let pk = XfrPublicKey::zei_from_bytes(&pk_vec).unwrap_or_default();
let issuer_addr = bech32enc(&XfrPublicKey::zei_to_bytes(&pk));
let asset_code = base64::encode_config(&resp.properties.code.val, base64::URL_SAFE);
let asset_code = engine::general_purpose::URL_SAFE.encode(&resp.properties.code.val);

assets.push(AssetDisplay {
issuer: issuer_addr,
Expand Down Expand Up @@ -217,7 +220,8 @@ pub async fn get_asset_list(
let row = sqlx::query(sql_total).fetch_one(&mut *conn).await?;
let total: i64 = row.try_get("cnt")?;

let sql_query = format!("SELECT jsonb_path_query(value,'$.body.operations[*].DefineAsset.body.asset') AS asset,tx_hash,block_hash,height FROM transaction ORDER BY height DESC LIMIT {} OFFSET {}", page_size, page_size*(page-1));
let sql_query = format!("SELECT jsonb_path_query(value,'$.body.operations[*].DefineAsset.body.asset') \
AS asset,tx_hash,block_hash,height FROM transaction ORDER BY height DESC LIMIT {} OFFSET {}", page_size, page_size*(page-1));
let rows = sqlx::query(sql_query.as_str())
.fetch_all(&mut *conn)
.await?;
Expand All @@ -230,10 +234,10 @@ pub async fn get_asset_list(

let v: Value = row.try_get("asset").unwrap();
let a: Asset = serde_json::from_value(v).unwrap();
let pk_vec = base64::decode_config(&a.issuer.key, base64::URL_SAFE)?;
let pk_vec = engine::general_purpose::URL_SAFE.decode(&a.issuer.key)?;
let pk = XfrPublicKey::zei_from_bytes(&pk_vec).unwrap_or_default();
let issuer_addr = bech32enc(&XfrPublicKey::zei_to_bytes(&pk));
let asset_code = base64::encode_config(&a.code.val, base64::URL_SAFE);
let asset_code = engine::general_purpose::URL_SAFE.encode(&a.code.val);

assets.push(AssetDisplay {
issuer: issuer_addr,
Expand Down Expand Up @@ -333,7 +337,8 @@ pub async fn get_issued_asset_list(
let row = sqlx::query(sql_total).fetch_one(&mut *conn).await?;
let total: i64 = row.try_get("cnt")?;

let sql_query = format!("SELECT jsonb_path_query(value,'$.body.operations[*].IssueAsset') AS asset,block_hash,tx_hash,height FROM transaction ORDER BY height DESC LIMIT {} OFFSET {}", page_size, (page-1)*page_size);
let sql_query = format!("SELECT jsonb_path_query(value,'$.body.operations[*].IssueAsset') \
AS asset,block_hash,tx_hash,height FROM transaction ORDER BY height DESC LIMIT {} OFFSET {}", page_size, (page-1)*page_size);
let rows = sqlx::query(sql_query.as_str())
.fetch_all(&mut *conn)
.await?;
Expand All @@ -345,10 +350,10 @@ pub async fn get_issued_asset_list(

let v: Value = row.try_get("asset").unwrap();
let a: IssueAsset = serde_json::from_value(v).unwrap();
let pk_vec = base64::decode_config(&a.pubkey.key, base64::URL_SAFE)?;
let pk_vec = engine::general_purpose::URL_SAFE.decode(&a.pubkey.key)?;
let pk = XfrPublicKey::zei_from_bytes(&pk_vec).unwrap_or_default();
let issuer = bech32enc(&XfrPublicKey::zei_to_bytes(&pk));
let asset_code = base64::encode_config(&a.body.code.val, base64::URL_SAFE);
let asset_code = engine::general_purpose::URL_SAFE.encode(&a.body.code.val);

assets.push(IssueAssetData {
issuer,
Expand Down Expand Up @@ -397,7 +402,7 @@ pub async fn get_issued_asset(
) -> Result<SingleIssueAssetResponse> {
let mut conn = api.storage.lock().await.acquire().await?;

let code_res = base64::decode_config(&address.0, base64::URL_SAFE);
let code_res = engine::general_purpose::URL_SAFE.decode(&address.0);
let code = match code_res {
Ok(code) => code,
_ => {
Expand All @@ -411,7 +416,9 @@ pub async fn get_issued_asset(
}
};

let sql_query = "SELECT jsonb_path_query(value,'$.body.operations[*].IssueAsset') AS asset,block_hash,tx_hash,height FROM transaction ORDER BY height DESC".to_string();
let sql_query = "SELECT jsonb_path_query(value,'$.body.operations[*].IssueAsset') \
AS asset,block_hash,tx_hash,height FROM transaction ORDER BY height DESC"
.to_string();
let rows = sqlx::query(sql_query.as_str())
.fetch_all(&mut *conn)
.await?;
Expand All @@ -423,10 +430,10 @@ pub async fn get_issued_asset(
let block: String = row.try_get("block_hash")?;
let tx: String = row.try_get("tx_hash")?;
let height: i64 = row.try_get("height")?;
let pk_vec = base64::decode_config(&a.pubkey.key, base64::URL_SAFE)?;
let pk_vec = engine::general_purpose::URL_SAFE.decode(&a.pubkey.key)?;
let pk = XfrPublicKey::zei_from_bytes(&pk_vec).unwrap_or_default();
let issuer = bech32enc(&XfrPublicKey::zei_to_bytes(&pk));
let asset_code = base64::encode_config(&a.body.code.val, base64::URL_SAFE);
let asset_code = engine::general_purpose::URL_SAFE.encode(&a.body.code.val);

return Ok(SingleIssueAssetResponse::Ok(Json(SingleIssueAssetResult {
code: 200,
Expand Down
53 changes: 40 additions & 13 deletions explorer/src/service/v1/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::service::util::{public_key_from_bech32, public_key_to_base64};
use crate::Api;
use anyhow::Result;
use base64::{engine, Engine};
use ethereum_types::H256;
use module::schema::{
EvmTx, PrismTransaction, TransactionResponse, UnDelegationValue, ABAR_TO_ABAR, ABAR_TO_BAR,
Expand Down Expand Up @@ -214,7 +215,13 @@ pub async fn get_prism_received(
.await?;
let total: i64 = row_counts.try_get("cnt")?;

let sql_query = format!("SELECT tx_hash,block_hash,sender,receiver,asset,amount,decimal,height,timestamp,value FROM e2n WHERE receiver='{}' ORDER BY timestamp DESC LIMIT {} OFFSET {}", address.0, page_size, (page-1)*page_size);
let sql_query = format!(
"SELECT tx_hash,block_hash,sender,receiver,asset,amount,decimal,height,timestamp,value \
FROM e2n WHERE receiver='{}' ORDER BY timestamp DESC LIMIT {} OFFSET {}",
address.0,
page_size,
(page - 1) * page_size
);
let rows = sqlx::query(sql_query.as_str())
.fetch_all(&mut *conn)
.await?;
Expand Down Expand Up @@ -247,7 +254,7 @@ pub async fn get_prism_received(
decimal: decimal as i64,
height,
timestamp,
data: base64::encode(&result_bin),
data: engine::general_purpose::STANDARD.encode(&result_bin),
});
}

Expand Down Expand Up @@ -323,7 +330,9 @@ pub async fn get_prism_records_send(
.await?;
let total: i64 = row_counts.try_get("cnt")?;

let sql_query = format!("SELECT tx_hash,block_hash,height,timestamp,jsonb_path_query(value,'$.body.operations[*].ConvertAccount') AS ca FROM transaction WHERE value @? '$.body.operations[*].ConvertAccount.signer ? (@==\"{}\")' ORDER BY timestamp DESC LIMIT {} OFFSET {}", base64_addr, page_size, (page-1)*page_size);
let sql_query = format!("SELECT tx_hash,block_hash,height,timestamp,jsonb_path_query(value,'$.body.operations[*].ConvertAccount') \
AS ca FROM transaction WHERE value @? '$.body.operations[*].ConvertAccount.signer ? (@==\"{}\")' \
ORDER BY timestamp DESC LIMIT {} OFFSET {}", base64_addr, page_size, (page-1)*page_size);

let rows = sqlx::query(sql_query.as_str())
.fetch_all(&mut *conn)
Expand All @@ -338,7 +347,7 @@ pub async fn get_prism_records_send(
let ca: ConvertAccount = serde_json::from_value(ca_val)?;
let asset: String;
if let Some(asset_bin) = &ca.asset_type {
asset = base64::encode_config(asset_bin, base64::URL_SAFE);
asset = engine::general_purpose::URL_SAFE.encode(asset_bin);
} else {
asset = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=".to_string();
}
Expand All @@ -358,7 +367,7 @@ pub async fn get_prism_records_send(
decimal: 6,
height,
timestamp,
data: base64::encode(&ca_bin),
data: engine::general_purpose::STANDARD.encode(&ca_bin),
});
}

Expand Down Expand Up @@ -489,13 +498,16 @@ pub async fn get_txs_receive_from(
}
let pk = public_key_to_base64(&pk_res.unwrap());

let sql_total = format!("SELECT count(*) AS cnt FROM transaction WHERE value @? '$.body.operations[*].TransferAsset.body.transfer.inputs[*].public_key ? (@==\"{pk}\")'");
let sql_total = format!("SELECT count(*) AS cnt FROM transaction \
WHERE value @? '$.body.operations[*].TransferAsset.body.transfer.inputs[*].public_key ? (@==\"{pk}\")'");
let row = sqlx::query(sql_total.as_str())
.fetch_one(&mut *conn)
.await?;
let total = row.try_get("cnt")?;

let sql_query = format!("SELECT * FROM transaction WHERE value @? '$.body.operations[*].TransferAsset.body.transfer.inputs[*].public_key ? (@==\"{}\")' ORDER BY timestamp DESC LIMIT {} OFFSET {}", pk, page_size, page_size*(page-1));
let sql_query = format!("SELECT * FROM transaction WHERE \
value @? '$.body.operations[*].TransferAsset.body.transfer.inputs[*].public_key ? (@==\"{}\")' \
ORDER BY timestamp DESC LIMIT {} OFFSET {}", pk, page_size, page_size*(page-1));
let rows = sqlx::query(sql_query.as_str())
.fetch_all(&mut *conn)
.await?;
Expand Down Expand Up @@ -561,13 +573,16 @@ pub async fn get_txs_send_to(
}
let pk = public_key_to_base64(&pk_res.unwrap());

let sql_total = format!("SELECT count(*) AS cnt FROM transaction WHERE value @? '$.body.operations[*].TransferAsset.body.transfer.outputs[*].public_key ? (@==\"{pk}\")'");
let sql_total = format!("SELECT count(*) AS cnt FROM transaction \
WHERE value @? '$.body.operations[*].TransferAsset.body.transfer.outputs[*].public_key ? (@==\"{pk}\")'");
let row = sqlx::query(sql_total.as_str())
.fetch_one(&mut *conn)
.await?;
let total = row.try_get("cnt")?;

let sql_query = format!("SELECT * FROM transaction WHERE value @? '$.body.operations[*].TransferAsset.body.transfer.outputs[*].public_key ? (@==\"{}\")' ORDER BY timestamp DESC LIMIT {} OFFSET {}", pk, page_size, page_size*(page-1));
let sql_query = format!("SELECT * FROM transaction \
WHERE value @? '$.body.operations[*].TransferAsset.body.transfer.outputs[*].public_key ? (@==\"{}\")' \
ORDER BY timestamp DESC LIMIT {} OFFSET {}", pk, page_size, page_size*(page-1));
let rows = sqlx::query(sql_query.as_str())
.fetch_all(&mut *conn)
.await?;
Expand Down Expand Up @@ -780,7 +795,8 @@ pub async fn get_txs_raw(
let pk = pk.unwrap();
let native_addr = public_key_to_base64(&pk);
params.push(format!(
"((value @? '$.body.operations[*].TransferAsset.body.transfer.inputs[*].public_key ? (@==\"{native_addr}\")') OR (value @? '$.body.operations[*].TransferAsset.body.transfer.outputs[*].public_key ? (@==\"{native_addr}\")')) "
"((value @? '$.body.operations[*].TransferAsset.body.transfer.inputs[*].public_key ? (@==\"{native_addr}\")') \
OR (value @? '$.body.operations[*].TransferAsset.body.transfer.outputs[*].public_key ? (@==\"{native_addr}\")')) "
));
}

Expand Down Expand Up @@ -930,7 +946,8 @@ pub async fn get_triple_masking_txs(
}
}
} else if let Some(pk) = pub_key.0 {
params.push(format!("(value @? '$.body.operations[*].AbarToBar.note.body.output.public_key ? (@==\"{pk}\")') OR (value @? '$.body.operations[*].BarToAbar.note.body.output.commitment ? (@==\"{pk}\")') "));
params.push(format!("(value @? '$.body.operations[*].AbarToBar.note.body.output.public_key ? (@==\"{pk}\")') \
OR (value @? '$.body.operations[*].BarToAbar.note.body.output.commitment ? (@==\"{pk}\")') "));
}

if let Some(start_time) = start_time.0 {
Expand Down Expand Up @@ -1136,7 +1153,8 @@ pub async fn get_claims_amount(api: &Api, address: Path<String>) -> Result<Claim
}

let base64_address = public_key_to_base64(&pubkey_res.unwrap());
let sql_query = format!("SELECT jsonb_path_query(value, '$.body.operations[*].Claim.body.amount') AS amount FROM transaction WHERE value @? '$.body.operations[*].Claim.pubkey ? (@==\"{base64_address}\")'");
let sql_query = format!("SELECT jsonb_path_query(value, '$.body.operations[*].Claim.body.amount') \
AS amount FROM transaction WHERE value @? '$.body.operations[*].Claim.pubkey ? (@==\"{base64_address}\")'");
let rows = sqlx::query(sql_query.as_str())
.fetch_all(&mut *conn)
.await?;
Expand All @@ -1162,7 +1180,16 @@ pub async fn get_prism_tx(
page_size: Query<Option<i64>>,
) -> Result<PmtxsResponse> {
let mut conn = api.storage.lock().await.acquire().await?;
let mut sql = String::from("SELECT tx_hash,block_hash,ty,timestamp,CASE WHEN fnuc_code = '248,251,204,194' THEN '_consumeMint' WHEN fnuc_code = '242,38,15,112' THEN '_withdrawFRA' WHEN fnuc_code = '116,64,166,22' THEN '_withdrawFRC20' WHEN fnuc_code = '250,190,177,88' THEN 'adminSetAsset' WHEN fnuc_code = '185,50,44,225' THEN 'adminSetLedger' WHEN fnuc_code = '5,5,220,224' THEN 'asset_contract' WHEN fnuc_code = '82,79,49,152' THEN 'consumeMint' WHEN fnuc_code = '222,147,129,28' THEN 'depositFRA' WHEN fnuc_code = '230,242,112,109' THEN 'depositFRC20' WHEN fnuc_code = '4,78,219,111' THEN 'ledger_contract' WHEN fnuc_code = '253,253,93,76' THEN 'ops' WHEN fnuc_code = '141,165,203,91' THEN 'owner' WHEN fnuc_code = '216,78,128,56' THEN 'proxy_contract' WHEN fnuc_code = '113,80,24,166' THEN 'renounceOwnership' WHEN fnuc_code = '242,253,227,139' THEN 'transferOwnership' WHEN fnuc_code = '24,188,157,230' THEN 'withdrawFRA' WHEN fnuc_code = '82,119,153,176' THEN 'withdrawFRC20' ELSE 'unknown' END AS fnuc_name,value,code,log FROM(SELECT tx_hash,block_hash,ty,timestamp,concat(value -> 'function' -> 'Ethereum' -> 'Transact' -> 'input'->0, NULL, ',', value -> 'function' -> 'Ethereum' -> 'Transact' -> 'input'->1, NULL, ',', value -> 'function' -> 'Ethereum' -> 'Transact' -> 'input'->2, NULL, ',', value -> 'function' -> 'Ethereum' -> 'Transact' -> 'input'->3) AS fnuc_code,value,code,log FROM transaction WHERE ty = 1");
let mut sql = String::from("SELECT tx_hash,block_hash,ty,timestamp,CASE WHEN fnuc_code = '248,251,204,194' \
THEN '_consumeMint' WHEN fnuc_code = '242,38,15,112' THEN '_withdrawFRA' WHEN fnuc_code = '116,64,166,22' THEN '_withdrawFRC20' \
WHEN fnuc_code = '250,190,177,88' THEN 'adminSetAsset' WHEN fnuc_code = '185,50,44,225' THEN 'adminSetLedger' \
WHEN fnuc_code = '5,5,220,224' THEN 'asset_contract' WHEN fnuc_code = '82,79,49,152' THEN 'consumeMint' \
WHEN fnuc_code = '222,147,129,28' THEN 'depositFRA' WHEN fnuc_code = '230,242,112,109' THEN 'depositFRC20' \
WHEN fnuc_code = '4,78,219,111' THEN 'ledger_contract' WHEN fnuc_code = '253,253,93,76' THEN 'ops' \
WHEN fnuc_code = '141,165,203,91' THEN 'owner' WHEN fnuc_code = '216,78,128,56' THEN 'proxy_contract' \
WHEN fnuc_code = '113,80,24,166' THEN 'renounceOwnership' WHEN fnuc_code = '242,253,227,139' \
THEN 'transferOwnership' WHEN fnuc_code = '24,188,157,230' THEN 'withdrawFRA' \
WHEN fnuc_code = '82,119,153,176' THEN 'withdrawFRC20' ELSE 'unknown' END AS fnuc_name,value,code,log FROM(SELECT tx_hash,block_hash,ty,timestamp,concat(value -> 'function' -> 'Ethereum' -> 'Transact' -> 'input'->0, NULL, ',', value -> 'function' -> 'Ethereum' -> 'Transact' -> 'input'->1, NULL, ',', value -> 'function' -> 'Ethereum' -> 'Transact' -> 'input'->2, NULL, ',', value -> 'function' -> 'Ethereum' -> 'Transact' -> 'input'->3) AS fnuc_code,value,code,log FROM transaction WHERE ty = 1");
let mut sql_total = String::from("SELECT count(*) as total FROM transaction WHERE ty = 1");
let mut params: Vec<String> = vec![];
params.push(format!(
Expand Down
Loading

0 comments on commit 42b5ff1

Please sign in to comment.