Skip to content

Commit

Permalink
PR suggestions and no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Coats committed Oct 6, 2023
1 parent 91b7ffe commit 7716258
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
36 changes: 20 additions & 16 deletions sdk/src/types/block/address/restricted.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use alloc::boxed::Box;

use derive_more::Deref;
use getset::Getters;
use packable::{error::UnpackErrorExt, prefix::BoxedSlicePrefix, Packable, PackableExt};
Expand All @@ -21,7 +23,7 @@ impl RestrictedAddress {

/// Creates a new [`RestrictedAddress`] address from an [`Address`] with default allowed capabilities.
#[inline(always)]
pub fn new(address: impl Into<Address> + Send) -> Result<Self, Error> {
pub fn new(address: impl Into<Address>) -> Result<Self, Error> {
let address = address.into();
if matches!(address, Address::Restricted(_)) {
return Err(Error::InvalidAddressKind(Self::KIND));
Expand Down Expand Up @@ -66,15 +68,15 @@ impl core::fmt::Display for RestrictedAddress {
#[non_exhaustive]
pub enum AddressCapabilityFlag {
/// Can receive Outputs with Native Tokens.
NativeTokens,
OutputsWithNativeTokens,
/// Can receive Outputs with Mana.
Mana,
OutputsWithMana,
/// Can receive Outputs with a Timelock Unlock Condition.
OutputsWithTimelock,
/// Can receive Outputs with an Expiration Unlock Condition.
OutputsWithExpiration,
/// Can receive Outputs with a Storage Deposit Return Unlock Condition.
OutputsWithStorageDeposit,
OutputsWithStorageDepositReturn,
/// Can receive Account Outputs.
AccountOutputs,
/// Can receive NFT Outputs.
Expand All @@ -84,23 +86,23 @@ pub enum AddressCapabilityFlag {
}

impl AddressCapabilityFlag {
const NATIVE_TOKENS: u8 = 0b00000001;
const MANA: u8 = 0b00000010;
const OUTPUTS_WITH_NATIVE_TOKENS: u8 = 0b00000001;
const OUTPUTS_WITH_MANA: u8 = 0b00000010;
const OUTPUTS_WITH_TIMELOCK: u8 = 0b00000100;
const OUTPUTS_WITH_EXPIRATION: u8 = 0b00001000;
const OUTPUTS_WITH_STORAGE_DEPOSIT: u8 = 0b00010000;
const OUTPUTS_WITH_STORAGE_DEPOSIT_RETURN: u8 = 0b00010000;
const ACCOUNT_OUTPUTS: u8 = 0b00100000;
const NFT_OUTPUTS: u8 = 0b01000000;
const DELEGATION_OUTPUTS: u8 = 0b10000000;

/// Converts the flag into the byte representation.
pub fn as_byte(&self) -> u8 {
match self {
Self::NativeTokens => Self::NATIVE_TOKENS,
Self::Mana => Self::MANA,
Self::OutputsWithNativeTokens => Self::OUTPUTS_WITH_NATIVE_TOKENS,
Self::OutputsWithMana => Self::OUTPUTS_WITH_MANA,
Self::OutputsWithTimelock => Self::OUTPUTS_WITH_TIMELOCK,
Self::OutputsWithExpiration => Self::OUTPUTS_WITH_EXPIRATION,
Self::OutputsWithStorageDeposit => Self::OUTPUTS_WITH_STORAGE_DEPOSIT,
Self::OutputsWithStorageDepositReturn => Self::OUTPUTS_WITH_STORAGE_DEPOSIT_RETURN,
Self::AccountOutputs => Self::ACCOUNT_OUTPUTS,
Self::NftOutputs => Self::NFT_OUTPUTS,
Self::DelegationOutputs => Self::DELEGATION_OUTPUTS,
Expand All @@ -110,11 +112,11 @@ impl AddressCapabilityFlag {
/// Returns the index in [`AddressCapabilities`] to which this flag is applied.
pub fn index(&self) -> usize {
match self {
Self::NativeTokens
| Self::Mana
Self::OutputsWithNativeTokens
| Self::OutputsWithMana
| Self::OutputsWithTimelock
| Self::OutputsWithExpiration
| Self::OutputsWithStorageDeposit
| Self::OutputsWithStorageDepositReturn
| Self::AccountOutputs
| Self::NftOutputs
| Self::DelegationOutputs => 0,
Expand All @@ -124,11 +126,11 @@ impl AddressCapabilityFlag {
/// Returns an iterator over all flags.
pub fn all() -> impl Iterator<Item = Self> {
[
Self::NativeTokens,
Self::Mana,
Self::OutputsWithNativeTokens,
Self::OutputsWithMana,
Self::OutputsWithTimelock,
Self::OutputsWithExpiration,
Self::OutputsWithStorageDeposit,
Self::OutputsWithStorageDepositReturn,
Self::AccountOutputs,
Self::NftOutputs,
Self::DelegationOutputs,
Expand Down Expand Up @@ -273,6 +275,8 @@ impl Packable for AddressCapabilities {

#[cfg(feature = "serde")]
pub(crate) mod dto {
use alloc::boxed::Box;

use serde::{Deserialize, Serialize};

use super::*;
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/wallet/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ impl AccountInner {

/// Returns the first address of the account as bech32
pub async fn first_address_bech32(&self) -> Bech32Address {
// PANIC: indexing is fine as one address is always generated during account creation.
self.addresses().await[0].clone().into_bech32()
// PANIC: unwrap is fine as one address is always generated during account creation.
self.addresses().await.into_iter().next().unwrap().into_bech32()
}

/// Returns all public addresses of the account
Expand Down
28 changes: 16 additions & 12 deletions sdk/tests/types/address/restricted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fn capabilities() {
let address = RestrictedAddress::new(rand_ed25519_address()).unwrap();
let mut capabilities = address.allowed_capabilities().clone();

assert!(!capabilities.has_capability(Flag::NativeTokens));
capabilities.add_capability(Flag::NativeTokens);
assert!(capabilities.has_capabilities([Flag::NativeTokens]));
assert!(!capabilities.has_capability(Flag::OutputsWithNativeTokens));
capabilities.add_capability(Flag::OutputsWithNativeTokens);
assert!(capabilities.has_capabilities([Flag::OutputsWithNativeTokens]));
assert!(!capabilities.has_capabilities(AddressCapabilities::all().capabilities_iter()));
assert!(!capabilities.is_none());
assert!(!capabilities.is_all());
Expand All @@ -24,12 +24,16 @@ fn capabilities() {
assert!(capabilities.has_capabilities(Flag::all()));
capabilities.set_none();

assert!(!capabilities.has_capability(Flag::Mana));
capabilities.set_capabilities([Flag::Mana, Flag::DelegationOutputs]);
assert!(capabilities.has_capability(Flag::Mana));
assert!(capabilities.has_capabilities([Flag::Mana, Flag::DelegationOutputs]));
assert!(!capabilities.has_capability(Flag::NativeTokens));
assert!(!capabilities.has_capabilities([Flag::Mana, Flag::DelegationOutputs, Flag::NativeTokens]));
assert!(!capabilities.has_capability(Flag::OutputsWithMana));
capabilities.set_capabilities([Flag::OutputsWithMana, Flag::DelegationOutputs]);
assert!(capabilities.has_capability(Flag::OutputsWithMana));
assert!(capabilities.has_capabilities([Flag::OutputsWithMana, Flag::DelegationOutputs]));
assert!(!capabilities.has_capability(Flag::OutputsWithNativeTokens));
assert!(!capabilities.has_capabilities([
Flag::OutputsWithMana,
Flag::DelegationOutputs,
Flag::OutputsWithNativeTokens
]));
}

#[test]
Expand Down Expand Up @@ -73,7 +77,7 @@ fn restricted_ed25519() {
);

// Restricted Ed25519 Address (Can receive Native Tokens)
address.set_allowed_capabilities([AddressCapabilityFlag::NativeTokens]);
address.set_allowed_capabilities([AddressCapabilityFlag::OutputsWithNativeTokens]);
assert_eq!(
address.clone().to_bech32_unchecked("iota"),
"iota19qqwlhq39mlzv2esf08n0xexcvd66q5lv9hw8mz25c695dnwfj0y8gcpqytmqxr4"
Expand Down Expand Up @@ -115,7 +119,7 @@ fn restricted_account() {
);

// Restricted Account Address (Can receive Native Tokens)
address.set_allowed_capabilities([AddressCapabilityFlag::NativeTokens]);
address.set_allowed_capabilities([AddressCapabilityFlag::OutputsWithNativeTokens]);
assert_eq!(
address.clone().to_bech32_unchecked("iota"),
"iota19qy0rsq3ld2d7jjwtvr5vffklwkvw7dlsrxytcpmcdqssdjc0d80exqpqyfjata7"
Expand Down Expand Up @@ -157,7 +161,7 @@ fn restricted_nft() {
);

// Restricted NFT Address (Can receive Native Tokens)
address.set_allowed_capabilities([AddressCapabilityFlag::NativeTokens]);
address.set_allowed_capabilities([AddressCapabilityFlag::OutputsWithNativeTokens]);
assert_eq!(
address.clone().to_bech32_unchecked("iota"),
"iota19qgvw2n94efawzue54lhycmml5w4afa6526t5z2unzdkvlfc2kqg0kcpqyp0nq2r"
Expand Down

0 comments on commit 7716258

Please sign in to comment.