Skip to content
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

feat: add conversion from Address/ContractId types to Identity #1252

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions packages/fuels-core/src/types/core/identity.rs
Original file line number Diff line number Diff line change
@@ -1,3 1,4 @@
use crate::types::bech32::{Bech32Address, Bech32ContractId};
use fuel_tx::{Address, ContractId};
use fuels_macros::{Parameterize, Tokenizable, TryFrom};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -26,3 27,78 @@ impl AsRef<[u8]> for Identity {
}
}
}

impl From<&Address> for Identity {
fn from(address: &Address) -> Self {
Self::Address(*address)
}
}
impl From<Address> for Identity {
fn from(address: Address) -> Self {
Self::Address(address)
}
}

impl From<&ContractId> for Identity {
fn from(contract_id: &ContractId) -> Self {
Self::ContractId(*contract_id)
}
}
impl From<ContractId> for Identity {
fn from(contract_id: ContractId) -> Self {
Self::ContractId(contract_id)
}
}

impl From<&Bech32Address> for Identity {
fn from(data: &Bech32Address) -> Self {
Self::Address(data.into())
}
}
impl From<Bech32Address> for Identity {
fn from(data: Bech32Address) -> Self {
Self::Address(data.into())
}
}

impl From<&Bech32ContractId> for Identity {
fn from(data: &Bech32ContractId) -> Self {
Self::ContractId(data.into())
}
}
impl From<Bech32ContractId> for Identity {
fn from(data: Bech32ContractId) -> Self {
Self::ContractId(data.into())
}
}

#[cfg(test)]
mod test {
use super::*;
use std::str::FromStr;

#[test]
fn test_bech32() {
let b32_str = "fuel1dved7k25uxadatl7l5kql309jnw07dcn4t3a6x9hm9nxyjcpqqns50p7n2";
let bech32_contract_id = Bech32ContractId::from_str(b32_str).unwrap();

let bech32_contract_id_borrowed = &bech32_contract_id.clone();
let identity: Identity = bech32_contract_id_borrowed.into();
assert_eq!(
identity,
Identity::ContractId(bech32_contract_id.clone().into())
);

let identity: Identity = bech32_contract_id.clone().into();
assert_eq!(identity, Identity::ContractId(bech32_contract_id.into()));

let bech32_address = Bech32Address::from_str(b32_str).unwrap();

let bech32_address_borrowed = &bech32_address.clone();
let identity: Identity = bech32_address_borrowed.into();
assert_eq!(identity, Identity::Address(bech32_address.clone().into()));

let identity: Identity = bech32_address.clone().into();
assert_eq!(identity, Identity::Address(bech32_address.clone().into()));
}
}
Loading