Skip to content

Commit

Permalink
Merge branch '2.0' into query-parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Sep 25, 2023
2 parents dfb3d02 + 50cc23c commit fc390b5
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 156 deletions.
30 changes: 27 additions & 3 deletions sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use crate::types::{
unlock_condition::{
verify_allowed_unlock_conditions, UnlockCondition, UnlockConditionFlags, UnlockConditions,
},
verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, AccountId, ChainId,
NativeToken, NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure,
StateTransitionError, StateTransitionVerifier,
verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, ChainId, NativeToken,
NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError,
StateTransitionVerifier,
},
protocol::ProtocolParameters,
semantic::{TransactionFailureReason, ValidationContext},
Expand All @@ -33,6 +33,30 @@ use crate::types::{
ValidationParams,
};

impl_id!(pub AccountId, 32, "Unique identifier of an account, which is the BLAKE2b-256 hash of the Output ID that created it.");

#[cfg(feature = "serde")]
string_serde_impl!(AccountId);

impl From<&OutputId> for AccountId {
fn from(output_id: &OutputId) -> Self {
Self::from(output_id.hash())
}
}

impl AccountId {
///
pub fn or_from_output_id(self, output_id: &OutputId) -> Self {
if self.is_null() { Self::from(output_id) } else { self }
}
}

impl From<AccountId> for Address {
fn from(value: AccountId) -> Self {
Self::Account(AccountAddress::new(value))
}
}

/// Types of account transition.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down
31 changes: 0 additions & 31 deletions sdk/src/types/block/output/account_id.rs

This file was deleted.

2 changes: 1 addition & 1 deletion sdk/src/types/block/output/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::types::{
block::{
address::Address,
output::{
account_id::AccountId,
account::AccountId,
chain_id::ChainId,
feature::FeatureFlags,
unlock_condition::{
Expand Down
57 changes: 53 additions & 4 deletions sdk/src/types/block/output/foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::cmp::Ordering;

use packable::{
error::{UnpackError, UnpackErrorExt},
packer::Packer,
packer::{Packer, SlicePacker},
unpacker::Unpacker,
Packable,
};
Expand All @@ -16,13 +16,14 @@ use crate::types::{
block::{
address::{AccountAddress, Address},
output::{
account::AccountId,
feature::{verify_allowed_features, Feature, FeatureFlags, Features},
unlock_condition::{
verify_allowed_unlock_conditions, UnlockCondition, UnlockConditionFlags, UnlockConditions,
},
verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, ChainId, FoundryId,
NativeToken, NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure,
StateTransitionError, StateTransitionVerifier, TokenId, TokenScheme,
verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, ChainId, NativeToken,
NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError,
StateTransitionVerifier, TokenId, TokenScheme,
},
protocol::ProtocolParameters,
semantic::{TransactionFailureReason, ValidationContext},
Expand All @@ -32,6 +33,54 @@ use crate::types::{
ValidationParams,
};

impl_id!(pub FoundryId, 38, "Defines the unique identifier of a foundry.");

#[cfg(feature = "serde")]
string_serde_impl!(FoundryId);

impl From<TokenId> for FoundryId {
fn from(token_id: TokenId) -> Self {
Self::new(*token_id)
}
}

impl FoundryId {
/// Builds a new [`FoundryId`] from its components.
pub fn build(account_address: &AccountAddress, serial_number: u32, token_scheme_kind: u8) -> Self {
let mut bytes = [0u8; Self::LENGTH];
let mut packer = SlicePacker::new(&mut bytes);

// PANIC: packing to an array of the correct length can't fail.
Address::Account(*account_address).pack(&mut packer).unwrap();
serial_number.pack(&mut packer).unwrap();
token_scheme_kind.pack(&mut packer).unwrap();

Self::new(bytes)
}

/// Returns the [`AccountAddress`] of the [`FoundryId`].
pub fn account_address(&self) -> AccountAddress {
// PANIC: the lengths are known.
AccountAddress::from(AccountId::new(self.0[1..AccountId::LENGTH + 1].try_into().unwrap()))
}

/// Returns the serial number of the [`FoundryId`].
pub fn serial_number(&self) -> u32 {
// PANIC: the lengths are known.
u32::from_le_bytes(
self.0[AccountId::LENGTH + 1..AccountId::LENGTH + 1 + core::mem::size_of::<u32>()]
.try_into()
.unwrap(),
)
}

/// Returns the [`TokenScheme`](crate::types::block::output::TokenScheme) kind of the [`FoundryId`].
pub fn token_scheme_kind(&self) -> u8 {
// PANIC: the length is known.
*self.0.last().unwrap()
}
}

/// Builder for a [`FoundryOutput`].
#[derive(Clone)]
#[must_use]
Expand Down
57 changes: 0 additions & 57 deletions sdk/src/types/block/output/foundry_id.rs

This file was deleted.

16 changes: 4 additions & 12 deletions sdk/src/types/block/output/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

mod account_id;
mod chain_id;
mod delegation;
mod foundry_id;
mod inputs_commitment;
mod metadata;
mod native_token;
mod nft_id;
mod output_id;
mod rent;
mod state_transition;
mod token_id;
mod token_scheme;

///
Expand Down Expand Up @@ -46,23 +42,19 @@ pub(crate) use self::{
unlock_condition::AddressUnlockCondition,
};
pub use self::{
account::{AccountOutput, AccountOutputBuilder, AccountTransition},
account_id::AccountId,
account::{AccountId, AccountOutput, AccountOutputBuilder, AccountTransition},
basic::{BasicOutput, BasicOutputBuilder},
chain_id::ChainId,
delegation::{DelegationId, DelegationOutput, DelegationOutputBuilder},
feature::{Feature, Features},
foundry::{FoundryOutput, FoundryOutputBuilder},
foundry_id::FoundryId,
foundry::{FoundryId, FoundryOutput, FoundryOutputBuilder},
inputs_commitment::InputsCommitment,
metadata::OutputMetadata,
native_token::{NativeToken, NativeTokens, NativeTokensBuilder},
nft::{NftOutput, NftOutputBuilder},
nft_id::NftId,
native_token::{NativeToken, NativeTokens, NativeTokensBuilder, TokenId},
nft::{NftId, NftOutput, NftOutputBuilder},
output_id::OutputId,
rent::{MinimumStorageDepositBasicOutput, Rent, RentStructure},
state_transition::{StateTransitionError, StateTransitionVerifier},
token_id::TokenId,
token_scheme::{SimpleTokenScheme, TokenScheme},
unlock_condition::{UnlockCondition, UnlockConditions},
};
Expand Down
13 changes: 12 additions & 1 deletion sdk/src/types/block/output/native_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ use iterator_sorted::is_unique_sorted;
use packable::{bounded::BoundedU8, prefix::BoxedSlicePrefix, Packable};
use primitive_types::U256;

use crate::types::block::{output::TokenId, Error};
use crate::types::block::{output::foundry::FoundryId, Error};

impl_id!(pub TokenId, 38, "Unique identifiers of native tokens. The TokenId of native tokens minted by a specific foundry is the same as the FoundryId.");

#[cfg(feature = "serde")]
string_serde_impl!(TokenId);

impl From<FoundryId> for TokenId {
fn from(foundry_id: FoundryId) -> Self {
Self::new(*foundry_id)
}
}

///
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Packable)]
Expand Down
26 changes: 25 additions & 1 deletion sdk/src/types/block/output/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::types::{
verify_allowed_unlock_conditions, UnlockCondition, UnlockConditionFlags, UnlockConditions,
},
verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, ChainId, NativeToken,
NativeTokens, NftId, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError,
NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError,
StateTransitionVerifier,
},
protocol::ProtocolParameters,
Expand All @@ -30,6 +30,30 @@ use crate::types::{
ValidationParams,
};

impl_id!(pub NftId, 32, "Unique identifier of an NFT, which is the BLAKE2b-256 hash of the Output ID that created it.");

#[cfg(feature = "serde")]
string_serde_impl!(NftId);

impl From<&OutputId> for NftId {
fn from(output_id: &OutputId) -> Self {
Self::from(output_id.hash())
}
}

impl NftId {
///
pub fn or_from_output_id(self, output_id: &OutputId) -> Self {
if self.is_null() { Self::from(output_id) } else { self }
}
}

impl From<NftId> for Address {
fn from(value: NftId) -> Self {
Self::Nft(NftAddress::new(value))
}
}

/// Builder for an [`NftOutput`].
#[derive(Clone)]
#[must_use]
Expand Down
31 changes: 0 additions & 31 deletions sdk/src/types/block/output/nft_id.rs

This file was deleted.

15 changes: 0 additions & 15 deletions sdk/src/types/block/output/token_id.rs

This file was deleted.

0 comments on commit fc390b5

Please sign in to comment.