Skip to content

Commit

Permalink
olympia -> babylon resource address derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Jun 9, 2023
1 parent d43caa6 commit 431e646
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
8 changes: 8 additions & 0 deletions native-json-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ pub mod native {
DeriveBabylonAddressFromOlympiaAddressHandler
as derive_babylon_address_from_olympia_address
);
export_handler!(
DeriveBabylonResourceAddressFromOlympiaResourceAddressHandler
as derive_babylon_resource_address_from_olympia_resource_address
);
export_handler!(DeriveVirtualAccountAddressHandler as derive_virtual_account_address);
export_handler!(DeriveVirtualIdentityAddressHandler as derive_virtual_identity_address);
export_handler!(
Expand Down Expand Up @@ -238,6 +242,10 @@ pub mod jni {
DeriveBabylonAddressFromOlympiaAddressHandler
as Java_com_radixdlt_toolkit_RadixEngineToolkitFFI_deriveBabylonAddressFromOlympiaAddress
);
export_handler!(
DeriveBabylonResourceAddressFromOlympiaResourceAddressHandler
as Java_com_radixdlt_toolkit_RadixEngineToolkitFFI_DeriveBabylonResourceAddressFromOlympiaResourceAddress
);
export_handler!(
DeriveVirtualAccountAddressHandler
as Java_com_radixdlt_toolkit_RadixEngineToolkitFFI_deriveVirtualAccountAddress
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use super::traits::Handler;
use crate::error::{Error, Result};
use crate::model::address::{EntityAddress, NetworkAwareResourceAddress};
use bech32::FromBase32;
use radix_engine_common::address::AddressError;
use toolkit_derive::serializable;

// =================
// Model Definition
// =================

/// Given an Olympia account address, this converts it from an Olympia account address to a Babylon
/// ECDSA Secp256k1 virtual account address and reveals the underlying public key of the Olympia
/// account.
#[serializable]
pub struct DeriveBabylonResourceAddressFromOlympiaResourceAddressRequest {
/// An unsigned 8 bit integer serialized as a string which represents the ID of the network
/// that the address will be used on. The primary use of this is for any Bech32m encoding
/// or decoding of addresses
#[schemars(with = "String")]
#[schemars(regex(pattern = "[0-9]+"))]
#[serde_as(as = "serde_with::DisplayFromStr")]
pub network_id: u8,

/// A string of the address on the Olympia network
pub olympia_resource_address: String,
}

/// The response form [`DeriveBabylonResourceAddressFromOlympiaResourceAddressRequest`] requests
#[serializable]
pub struct DeriveBabylonResourceAddressFromOlympiaResourceAddressResponse {
/// The Babylon account address associated with the Olympia address.
#[schemars(with = "EntityAddress")]
#[serde_as(as = "serde_with::TryFromInto<EntityAddress>")]
pub babylon_resource_address: NetworkAwareResourceAddress,
}

// ===============
// Implementation
// ===============

pub struct DeriveBabylonResourceAddressFromOlympiaResourceAddressHandler;

impl
Handler<
DeriveBabylonResourceAddressFromOlympiaResourceAddressRequest,
DeriveBabylonResourceAddressFromOlympiaResourceAddressResponse,
> for DeriveBabylonResourceAddressFromOlympiaResourceAddressHandler
{
fn pre_process(
request: DeriveBabylonResourceAddressFromOlympiaResourceAddressRequest,
) -> Result<DeriveBabylonResourceAddressFromOlympiaResourceAddressRequest> {
Ok(request)
}

fn handle(
request: &DeriveBabylonResourceAddressFromOlympiaResourceAddressRequest,
) -> Result<DeriveBabylonResourceAddressFromOlympiaResourceAddressResponse> {
// Bech32 decode the passed address. If the Bech32 variant is not Bech32, then this is not
// an Olympia address
let (_, data, variant) = bech32::decode(&request.olympia_resource_address)
.map_err(AddressError::Bech32mDecodingError)?;
if let bech32::Variant::Bech32 = variant {
Ok(())
} else {
Err(Error::NotAnOlympiaAddress {
address: request.olympia_resource_address.clone(),
})
}?;

// Convert from 5 bits to 8 bits.
let mut data = Vec::<u8>::from_base32(&data).map_err(AddressError::Bech32mDecodingError)?;

// Check the length of the data to ensure that it's valid.
if data.is_empty() {
Err(Error::NotAnOlympiaAddress {
address: request.olympia_resource_address.clone(),
})?;
}

let prefix = data.remove(0);
let length = data.len();

let resource_address = match (prefix, length) {
(0x01, 0) => Ok(scrypto::prelude::RADIX_TOKEN),
(0x03, 26) => Ok(scrypto::prelude::ResourceAddress::Fungible(
data.try_into().unwrap(),
)),
_ => Err(Error::NotAnOlympiaAddress {
address: request.olympia_resource_address.clone(),
}),
}?;

Ok(
DeriveBabylonResourceAddressFromOlympiaResourceAddressResponse {
babylon_resource_address: NetworkAwareResourceAddress {
address: resource_address,
network_id: request.network_id,
},
},
)
}

fn post_process(
_: &DeriveBabylonResourceAddressFromOlympiaResourceAddressRequest,
response: DeriveBabylonResourceAddressFromOlympiaResourceAddressResponse,
) -> Result<DeriveBabylonResourceAddressFromOlympiaResourceAddressResponse> {
Ok(response)
}
}
2 changes: 2 additions & 0 deletions radix-engine-toolkit/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod sbor_decode;
pub mod sbor_encode;

pub mod derive_babylon_address_from_olympia_address;
pub mod derive_babylon_resource_address_from_olympia_resource_address;
pub mod derive_non_fungible_global_id_from_public_key;
pub mod derive_virtual_account_address;
pub mod derive_virtual_identity_address;
Expand Down Expand Up @@ -74,6 +75,7 @@ pub use sbor_decode::*;
pub use sbor_encode::*;

pub use derive_babylon_address_from_olympia_address::*;
pub use derive_babylon_resource_address_from_olympia_resource_address::*;
pub use derive_non_fungible_global_id_from_public_key::*;
pub use derive_virtual_account_address::*;
pub use derive_virtual_identity_address::*;
Expand Down

0 comments on commit 431e646

Please sign in to comment.