Skip to content

Commit

Permalink
Return b58 encoding of public address as part of public address endpo…
Browse files Browse the repository at this point in the history
…int (#463)

* Return b58 encoding of public address as part of public address endpoint

* Returns b58 codes in JSON gateway

* fmt
  • Loading branch information
tsegaran authored Sep 22, 2020
1 parent b712f53 commit 610d411
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
5 changes: 2 additions & 3 deletions mobilecoind-json/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn public_address(
state: rocket::State<State>,
monitor_hex: String,
subaddress_index: u64,
) -> Result<Json<JsonPublicAddress>, String> {
) -> Result<Json<JsonPublicAddressResponse>, String> {
let monitor_id =
hex::decode(monitor_hex).map_err(|err| format!("Failed to decode monitor hex: {}", err))?;

Expand All @@ -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
Expand Down
37 changes: 37 additions & 0 deletions mobilecoind-json/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions mobilecoind/api/proto/mobilecoind_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ message GetPublicAddressRequest {
}
message GetPublicAddressResponse {
external.PublicAddress public_address = 1;
string b58_code = 2;
}

//
Expand Down
16 changes: 16 additions & 0 deletions mobilecoind/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,19 @@ impl<T: BlockchainConnection + UserTxConnection + 'static> ServiceApi<T> {
// 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)
}

Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 610d411

Please sign in to comment.