-
Notifications
You must be signed in to change notification settings - Fork 28
add /assetid(to|from)hexbytes APIs #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
3b8ff6b
f029a61
b00e422
5323e54
f1e13c4
eef9f9b
55cdb6e
670ea46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,26 @@ | |
} | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub(crate) struct AssetIdToHexBytesRequest { | ||
pub(crate) asset_id: String, | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub(crate) struct AssetIdToHexBytesResponse { | ||
pub(crate) hex_bytes: String, | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub(crate) struct AssetIdFromHexBytesRequest { | ||
pub(crate) hex_bytes: String, | ||
} | ||
|
||
#[derive(Deserialize, Serialize)] | ||
pub(crate) struct AssetIdFromHexBytesResponse { | ||
pub(crate) asset_id: String, | ||
} | ||
|
||
Comment on lines
+144
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please sort these alphabetically |
||
#[derive(Deserialize, Serialize)] | ||
pub(crate) struct AssetMetadataRequest { | ||
pub(crate) asset_id: String, | ||
|
@@ -1137,7 +1157,7 @@ | |
} | ||
|
||
async fn check_locked( | ||
&self, | ||
Check warning on line 1160 in src/routes.rs
|
||
) -> Result<TokioMutexGuard<Option<Arc<UnlockedAppState>>>, APIError> { | ||
let unlocked_app_state = self.get_unlocked_app_state().await; | ||
if unlocked_app_state.is_some() { | ||
|
@@ -1149,7 +1169,7 @@ | |
} | ||
|
||
async fn check_unlocked( | ||
&self, | ||
Check warning on line 1172 in src/routes.rs
|
||
) -> Result<TokioMutexGuard<Option<Arc<UnlockedAppState>>>, APIError> { | ||
let unlocked_app_state = self.get_unlocked_app_state().await; | ||
if unlocked_app_state.is_none() { | ||
|
@@ -1224,6 +1244,29 @@ | |
})) | ||
} | ||
|
||
pub(crate) async fn asset_id_from_hex_bytes( | ||
WithRejection(Json(payload), _): WithRejection<Json<AssetIdFromHexBytesRequest>, APIError>, | ||
) -> Result<Json<AssetIdFromHexBytesResponse>, APIError> { | ||
let hex_bytes = hex_str_to_vec(&payload.hex_bytes) | ||
.ok_or_else(|| APIError::InvalidHexString(payload.hex_bytes))?; | ||
|
||
let contract_id = | ||
ContractId::copy_from_slice(&hex_bytes).map_err(|_| APIError::InvalidAssetIDBytes)?; | ||
let asset_id = contract_id.to_string(); | ||
|
||
Ok(Json(AssetIdFromHexBytesResponse { asset_id })) | ||
} | ||
|
||
pub(crate) async fn asset_id_to_hex_bytes( | ||
WithRejection(Json(payload), _): WithRejection<Json<AssetIdToHexBytesRequest>, APIError>, | ||
) -> Result<Json<AssetIdToHexBytesResponse>, APIError> { | ||
let contract_id = ContractId::from_str(&payload.asset_id) | ||
.map_err(|_| APIError::InvalidAssetID(payload.asset_id))?; | ||
let hex_bytes = hex_str(&contract_id.to_byte_array()); | ||
|
||
Ok(Json(AssetIdToHexBytesResponse { hex_bytes })) | ||
} | ||
|
||
pub(crate) async fn asset_metadata( | ||
State(state): State<Arc<AppState>>, | ||
WithRejection(Json(payload), _): WithRejection<Json<AssetMetadataRequest>, APIError>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use super::*; | ||
|
||
const TEST_DIR_BASE: &str = "tmp/asset_id_hex_bytes/"; | ||
|
||
#[serial_test::serial] | ||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] | ||
#[traced_test] | ||
async fn success() { | ||
initialize(); | ||
|
||
let test_dir_node1 = format!("{TEST_DIR_BASE}node1"); | ||
let (node1_addr, _) = start_node(&test_dir_node1, NODE1_PEER_PORT, false).await; | ||
|
||
fund_and_create_utxos(node1_addr, None).await; | ||
|
||
// issue assets | ||
let asset_cfa = issue_asset_cfa(node1_addr, None).await; | ||
let asset_nia = issue_asset_nia(node1_addr).await; | ||
let asset_uda = issue_asset_uda(node1_addr, None).await; | ||
|
||
// check | ||
let cfa_decoded_result = asset_id_to_hex_bytes(node1_addr, asset_cfa.asset_id.clone()).await; | ||
let nia_decoded_result = asset_id_to_hex_bytes(node1_addr, asset_nia.asset_id.clone()).await; | ||
let uda_decoded_result = asset_id_to_hex_bytes(node1_addr, asset_uda.asset_id.clone()).await; | ||
|
||
let cfa_encoded_result = | ||
asset_id_from_hex_bytes(node1_addr, cfa_decoded_result.hex_bytes.clone()).await; | ||
let nia_encoded_result = | ||
asset_id_from_hex_bytes(node1_addr, nia_decoded_result.hex_bytes.clone()).await; | ||
let uda_encoded_result = | ||
asset_id_from_hex_bytes(node1_addr, uda_decoded_result.hex_bytes.clone()).await; | ||
|
||
assert_eq!(cfa_encoded_result.asset_id, asset_cfa.asset_id); | ||
assert_eq!(nia_encoded_result.asset_id, asset_nia.asset_id); | ||
assert_eq!(uda_encoded_result.asset_id, asset_uda.asset_id); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.