From 610d4117eeb627ca2a45eada59a8d0cab96aad5e Mon Sep 17 00:00:00 2001 From: tsegaran Date: Tue, 22 Sep 2020 14:59:21 -0700 Subject: [PATCH] Return b58 encoding of public address as part of public address endpoint (#463) * Return b58 encoding of public address as part of public address endpoint * Returns b58 codes in JSON gateway * fmt --- mobilecoind-json/src/bin/main.rs | 5 ++- mobilecoind-json/src/data_types.rs | 37 +++++++++++++++++++++ mobilecoind/api/proto/mobilecoind_api.proto | 1 + mobilecoind/src/service.rs | 16 +++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/mobilecoind-json/src/bin/main.rs b/mobilecoind-json/src/bin/main.rs index 3a22b59f21..dde89df2e6 100644 --- a/mobilecoind-json/src/bin/main.rs +++ b/mobilecoind-json/src/bin/main.rs @@ -198,7 +198,7 @@ fn public_address( state: rocket::State, monitor_hex: String, subaddress_index: u64, -) -> Result, String> { +) -> Result, String> { let monitor_id = hex::decode(monitor_hex).map_err(|err| format!("Failed to decode monitor hex: {}", err))?; @@ -212,8 +212,7 @@ fn public_address( .get_public_address(&req) .map_err(|err| format!("Failed getting public address: {}", err))?; - let public_address = resp.get_public_address(); - Ok(Json(JsonPublicAddress::from(public_address))) + Ok(Json(JsonPublicAddressResponse::from(&resp))) } /// Generates a request code with an optional value and memo diff --git a/mobilecoind-json/src/data_types.rs b/mobilecoind-json/src/data_types.rs index e0fa50e5d7..f5223fafca 100644 --- a/mobilecoind-json/src/data_types.rs +++ b/mobilecoind-json/src/data_types.rs @@ -235,6 +235,43 @@ impl From<&PublicAddress> for JsonPublicAddress { } } +#[derive(Deserialize, Serialize, Default)] +pub struct JsonPublicAddressResponse { + /// Hex encoded compressed ristretto bytes + pub view_public_key: String, + + /// Hex encoded compressed ristretto bytes + pub spend_public_key: String, + + /// Fog Report Server Url + pub fog_report_url: String, + + /// Hex encoded signature bytes + pub fog_authority_fingerprint_sig: String, + + /// String label for fog reports + pub fog_report_id: String, + + /// b58-encoded public address + pub b58_address_code: String, +} + +impl From<&mc_mobilecoind_api::GetPublicAddressResponse> for JsonPublicAddressResponse { + fn from(src: &mc_mobilecoind_api::GetPublicAddressResponse) -> Self { + let public_address = src.get_public_address(); + Self { + view_public_key: hex::encode(&public_address.get_view_public_key().get_data()), + spend_public_key: hex::encode(&public_address.get_spend_public_key().get_data()), + fog_report_url: String::from(public_address.get_fog_report_url()), + fog_report_id: String::from(public_address.get_fog_report_id()), + fog_authority_fingerprint_sig: hex::encode( + &public_address.get_fog_authority_fingerprint_sig(), + ), + b58_address_code: src.get_b58_code().to_string(), + } + } +} + // Helper conversion between json and protobuf impl TryFrom<&JsonPublicAddress> for PublicAddress { type Error = String; diff --git a/mobilecoind/api/proto/mobilecoind_api.proto b/mobilecoind/api/proto/mobilecoind_api.proto index 91b3c6111f..40f6bff61a 100644 --- a/mobilecoind/api/proto/mobilecoind_api.proto +++ b/mobilecoind/api/proto/mobilecoind_api.proto @@ -315,6 +315,7 @@ message GetPublicAddressRequest { } message GetPublicAddressResponse { external.PublicAddress public_address = 1; + string b58_code = 2; } // diff --git a/mobilecoind/src/service.rs b/mobilecoind/src/service.rs index cb7808c5c4..bff15fa4e9 100644 --- a/mobilecoind/src/service.rs +++ b/mobilecoind/src/service.rs @@ -345,9 +345,19 @@ impl ServiceApi { // Get the subaddress. let subaddress = data.account_key.subaddress(request.subaddress_index); + // Also build the b58 wrapper + let mut wrapper = mc_mobilecoind_api::printable::PrintableWrapper::new(); + wrapper.set_public_address((&subaddress).into()); + // Return response. let mut response = mc_mobilecoind_api::GetPublicAddressResponse::new(); response.set_public_address((&subaddress).into()); + response.set_b58_code( + wrapper + .b58_encode() + .map_err(|err| rpc_internal_error("b58_encode", err, &self.logger))?, + ); + Ok(response) } @@ -1909,6 +1919,12 @@ mod test { account_key.subaddress(10) ); + // Test that the b58 encoding is correct + let mut wrapper = mc_mobilecoind_api::printable::PrintableWrapper::new(); + wrapper.set_public_address((&account_key.subaddress(10)).into()); + let b58_code = wrapper.b58_encode().unwrap(); + assert_eq!(response.get_b58_code(), b58_code,); + // Subaddress that is out of index or an invalid monitor id should error. let request = mc_mobilecoind_api::GetPublicAddressRequest::new(); assert!(client.get_public_address(&request).is_err());