Skip to content

Commit

Permalink
Adapt unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Nov 1, 2023
1 parent 5332e89 commit 2e18691
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ enum UnlockType {
*/
Account = 2,
/**
* An NFT unlock.
* An Anchor unlock.
*/
Nft = 3,
Anchor = 3,
/**
* An Anchor unlock.
* An NFT unlock.
*/
Anchor = 4,
Nft = 4,
}

/**
Expand Down Expand Up @@ -104,9 +104,9 @@ class AccountUnlock extends Unlock {
}

/**
* An unlock which must reference a previous unlock which unlocks the NFT that the input is locked to.
* An unlock which must reference a previous unlock which unlocks the anchor that the input is locked to.
*/
class NftUnlock extends Unlock {
class AnchorUnlock extends Unlock {
/**
* The reference.
*/
Expand All @@ -116,15 +116,15 @@ class NftUnlock extends Unlock {
* @param reference An index referencing a previous unlock.
*/
constructor(reference: number) {
super(UnlockType.Nft);
super(UnlockType.Anchor);
this.reference = reference;
}
}

/**
* An unlock which must reference a previous unlock which unlocks the anchor that the input is locked to.
* An unlock which must reference a previous unlock which unlocks the NFT that the input is locked to.
*/
class AnchorUnlock extends Unlock {
class NftUnlock extends Unlock {
/**
* The reference.
*/
Expand All @@ -134,7 +134,7 @@ class AnchorUnlock extends Unlock {
* @param reference An index referencing a previous unlock.
*/
constructor(reference: number) {
super(UnlockType.Anchor);
super(UnlockType.Nft);
this.reference = reference;
}
}
Expand All @@ -154,14 +154,14 @@ const UnlockDiscriminator = {
value: AccountUnlock,
name: UnlockType.Account as any,
},
{
value: NftUnlock,
name: UnlockType.Nft as any,
},
{
value: AnchorUnlock,
name: UnlockType.Anchor as any,
},
{
value: NftUnlock,
name: UnlockType.Nft as any,
},
],
};

Expand All @@ -171,7 +171,7 @@ export {
SignatureUnlock,
ReferenceUnlock,
AccountUnlock,
NftUnlock,
AnchorUnlock,
NftUnlock,
UnlockDiscriminator,
};
36 changes: 20 additions & 16 deletions bindings/python/iota_sdk/types/unlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ class UnlockType(IntEnum):
Signature (0): An unlock holding a signature unlocking one or more inputs.
Reference (1): An unlock which must reference a previous unlock which unlocks also the input at the same index as this Reference Unlock.
Account (2): An unlock which must reference a previous unlock which unlocks the account that the input is locked to.
Nft (3): An unlock which must reference a previous unlock which unlocks the NFT that the input is locked to.
Anchor (4): An unlock which must reference a previous unlock which unlocks the anchor that the input is locked to.
Anchor (3): An unlock which must reference a previous unlock which unlocks the anchor that the input is locked to.
Nft (4): An unlock which must reference a previous unlock which unlocks the NFT that the input is locked to.
"""
Signature = 0
Reference = 1
Account = 2
Nft = 3
Anchor = 4
Anchor = 3
Nft = 4


@json
Expand Down Expand Up @@ -70,15 +71,6 @@ class AccountUnlock:
init=False)


@json
@dataclass
class NftUnlock:
"""An unlock which must reference a previous unlock which unlocks the NFT that the input is locked to.
"""
reference: int
type: int = field(default_factory=lambda: int(UnlockType.Nft), init=False)


@json
@dataclass
class AnchorUnlock:
Expand All @@ -91,8 +83,20 @@ class AnchorUnlock:
init=False)


@json
@dataclass
class NftUnlock:
"""An unlock which must reference a previous unlock which unlocks the NFT that the input is locked to.
"""
reference: int
type: int = field(default_factory=lambda: int(UnlockType.Nft), init=False)


Unlock: TypeAlias = Union[SignatureUnlock,
ReferenceUnlock, AccountUnlock, NftUnlock, AnchorUnlock]
ReferenceUnlock,
AccountUnlock,
AnchorUnlock,
NftUnlock]


def deserialize_unlock(d: Dict[str, Any]) -> Unlock:
Expand All @@ -109,10 +113,10 @@ def deserialize_unlock(d: Dict[str, Any]) -> Unlock:
return ReferenceUnlock.from_dict(d)
if unlock_type == UnlockType.Account:
return AccountUnlock.from_dict(d)
if unlock_type == UnlockType.Nft:
return NftUnlock.from_dict(d)
if unlock_type == UnlockType.Anchor:
return AnchorUnlock.from_dict(d)
if unlock_type == UnlockType.Nft:
return NftUnlock.from_dict(d)
raise Exception(f'invalid unlock type: {unlock_type}')


Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/unlock/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl TryFrom<u16> for AnchorUnlock {

impl AnchorUnlock {
/// The [`Unlock`](crate::types::block::unlock::Unlock) kind of an [`AnchorUnlock`].
pub const KIND: u8 = 4;
pub const KIND: u8 = 3;

/// Creates a new [`AnchorUnlock`].
#[inline(always)]
Expand Down
20 changes: 10 additions & 10 deletions sdk/src/types/block/unlock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ pub enum Unlock {
/// An account unlock.
#[packable(tag = AccountUnlock::KIND)]
Account(AccountUnlock),
/// An NFT unlock.
#[packable(tag = NftUnlock::KIND)]
Nft(NftUnlock),
/// An Anchor unlock.
#[packable(tag = AnchorUnlock::KIND)]
Anchor(AnchorUnlock),
/// An NFT unlock.
#[packable(tag = NftUnlock::KIND)]
Nft(NftUnlock),
}

impl From<SignatureUnlock> for Unlock {
Expand All @@ -70,8 +70,8 @@ impl core::fmt::Debug for Unlock {
Self::Signature(unlock) => unlock.fmt(f),
Self::Reference(unlock) => unlock.fmt(f),
Self::Account(unlock) => unlock.fmt(f),
Self::Nft(unlock) => unlock.fmt(f),
Self::Anchor(unlock) => unlock.fmt(f),
Self::Nft(unlock) => unlock.fmt(f),
}
}
}
Expand All @@ -83,8 +83,8 @@ impl Unlock {
Self::Signature(_) => SignatureUnlock::KIND,
Self::Reference(_) => ReferenceUnlock::KIND,
Self::Account(_) => AccountUnlock::KIND,
Self::Nft(_) => NftUnlock::KIND,
Self::Anchor(_) => AnchorUnlock::KIND,
Self::Nft(_) => NftUnlock::KIND,
}
}

Expand Down Expand Up @@ -144,16 +144,16 @@ fn verify_unlocks<const VERIFY: bool>(unlocks: &[Unlock], _: &()) -> Result<(),
return Err(Error::InvalidUnlockAccount(index));
}
}
Unlock::Nft(nft) => {
if index == 0 || nft.index() >= index {
return Err(Error::InvalidUnlockNft(index));
}
}
Unlock::Anchor(anchor) => {
if index == 0 || anchor.index() >= index {
return Err(Error::InvalidUnlockAnchor(index));
}
}
Unlock::Nft(nft) => {
if index == 0 || nft.index() >= index {
return Err(Error::InvalidUnlockNft(index));
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/unlock/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl TryFrom<u16> for NftUnlock {

impl NftUnlock {
/// The [`Unlock`](crate::types::block::unlock::Unlock) kind of a [`NftUnlock`].
pub const KIND: u8 = 3;
pub const KIND: u8 = 4;

/// Creates a new [`NftUnlock`].
#[inline(always)]
Expand Down
5 changes: 3 additions & 2 deletions sdk/tests/types/unlock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod signature;

use iota_sdk::types::block::{
rand::signature::rand_signature,
unlock::{AccountUnlock, NftUnlock, ReferenceUnlock, SignatureUnlock, Unlock, Unlocks},
unlock::{AccountUnlock, AnchorUnlock, NftUnlock, ReferenceUnlock, SignatureUnlock, Unlock, Unlocks},
Error,
};
use packable::bounded::TryIntoBoundedU16Error;
Expand All @@ -19,7 +19,8 @@ fn kind() {
assert_eq!(Unlock::from(SignatureUnlock::from(rand_signature())).kind(), 0);
assert_eq!(Unlock::from(ReferenceUnlock::new(0).unwrap()).kind(), 1);
assert_eq!(Unlock::from(AccountUnlock::new(0).unwrap()).kind(), 2);
assert_eq!(Unlock::from(NftUnlock::new(0).unwrap()).kind(), 3);
assert_eq!(Unlock::from(AnchorUnlock::new(0).unwrap()).kind(), 3);
assert_eq!(Unlock::from(NftUnlock::new(0).unwrap()).kind(), 4);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion sdk/tests/types/unlock/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use pretty_assertions::assert_eq;

#[test]
fn kind() {
assert_eq!(NftUnlock::KIND, 3);
assert_eq!(NftUnlock::KIND, 4);
}

#[test]
Expand Down

0 comments on commit 2e18691

Please sign in to comment.