From 771625869dfbcbfb151c77ad17eb7149f6b1b16d Mon Sep 17 00:00:00 2001 From: Alex Coats Date: Fri, 6 Oct 2023 08:21:21 -0400 Subject: [PATCH] PR suggestions and no_std --- sdk/src/types/block/address/restricted.rs | 36 +++++++++++++---------- sdk/src/wallet/account/mod.rs | 4 +-- sdk/tests/types/address/restricted.rs | 28 ++++++++++-------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/sdk/src/types/block/address/restricted.rs b/sdk/src/types/block/address/restricted.rs index 19696549be..1c6485e898 100644 --- a/sdk/src/types/block/address/restricted.rs +++ b/sdk/src/types/block/address/restricted.rs @@ -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}; @@ -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
+ Send) -> Result { + pub fn new(address: impl Into
) -> Result { let address = address.into(); if matches!(address, Address::Restricted(_)) { return Err(Error::InvalidAddressKind(Self::KIND)); @@ -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. @@ -84,11 +86,11 @@ 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; @@ -96,11 +98,11 @@ impl AddressCapabilityFlag { /// 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, @@ -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, @@ -124,11 +126,11 @@ impl AddressCapabilityFlag { /// Returns an iterator over all flags. pub fn all() -> impl Iterator { [ - Self::NativeTokens, - Self::Mana, + Self::OutputsWithNativeTokens, + Self::OutputsWithMana, Self::OutputsWithTimelock, Self::OutputsWithExpiration, - Self::OutputsWithStorageDeposit, + Self::OutputsWithStorageDepositReturn, Self::AccountOutputs, Self::NftOutputs, Self::DelegationOutputs, @@ -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::*; diff --git a/sdk/src/wallet/account/mod.rs b/sdk/src/wallet/account/mod.rs index e20bafcb1c..5da357fcd5 100644 --- a/sdk/src/wallet/account/mod.rs +++ b/sdk/src/wallet/account/mod.rs @@ -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 diff --git a/sdk/tests/types/address/restricted.rs b/sdk/tests/types/address/restricted.rs index 09ed599911..f9dc5defd9 100644 --- a/sdk/tests/types/address/restricted.rs +++ b/sdk/tests/types/address/restricted.rs @@ -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()); @@ -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] @@ -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" @@ -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" @@ -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"