From 7a1310f4b42d9b5ecb0f1a48fe5c4d99ca0beee4 Mon Sep 17 00:00:00 2001 From: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> Date: Mon, 20 Nov 2023 10:42:30 +0100 Subject: [PATCH 1/2] nodejs: add multi and empty unlock classes (#1627) * nodejs: add multi and empty unlock classes * Add @Type to unlocks * Fix Cannot access 'UnlockDiscriminator' before initialization --------- Co-authored-by: Thibault Martinez --- .../payload/signed_transaction/unlock.ts | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/bindings/nodejs/lib/types/block/payload/signed_transaction/unlock.ts b/bindings/nodejs/lib/types/block/payload/signed_transaction/unlock.ts index de2107e7d2..b9e73199db 100644 --- a/bindings/nodejs/lib/types/block/payload/signed_transaction/unlock.ts +++ b/bindings/nodejs/lib/types/block/payload/signed_transaction/unlock.ts @@ -28,6 +28,14 @@ enum UnlockType { * An NFT unlock. */ Nft = 4, + /** + * A multi unlock. + */ + Multi = 5, + /** + * An empty unlock. + */ + Empty = 6, } /** @@ -139,6 +147,64 @@ class NftUnlock extends Unlock { } } +/** + * Used to maintain correct index relationship between addresses and signatures when unlocking a MultiUnlock where not all addresses are unlocked. + */ +class EmptyUnlock extends Unlock { + constructor() { + super(UnlockType.Empty); + } +} + +/** + * Unlocks a MultiAddress with a list of other unlocks. + */ +class MultiUnlock extends Unlock { + /** + * The inner unlocks. + */ + @Type(() => Unlock, { + discriminator: { + property: 'type', + subTypes: [ + { + value: SignatureUnlock, + name: UnlockType.Signature as any, + }, + { + value: ReferenceUnlock, + name: UnlockType.Reference as any, + }, + { + value: AccountUnlock, + name: UnlockType.Account as any, + }, + { + value: AnchorUnlock, + name: UnlockType.Anchor as any, + }, + { + value: NftUnlock, + name: UnlockType.Nft as any, + }, + { + value: EmptyUnlock, + name: UnlockType.Empty as any, + }, + ], + }, + }) + readonly unlocks: Unlock[]; + + /** + * @param unlocks The inner unlocks. + */ + constructor(unlocks: Unlock[]) { + super(UnlockType.Multi); + this.unlocks = unlocks; + } +} + const UnlockDiscriminator = { property: 'type', subTypes: [ @@ -162,6 +228,14 @@ const UnlockDiscriminator = { value: NftUnlock, name: UnlockType.Nft as any, }, + { + value: MultiUnlock, + name: UnlockType.Multi as any, + }, + { + value: EmptyUnlock, + name: UnlockType.Empty as any, + }, ], }; @@ -173,5 +247,7 @@ export { AccountUnlock, AnchorUnlock, NftUnlock, + MultiUnlock, + EmptyUnlock, UnlockDiscriminator, }; From e595d73504a302ebf7fae90215dadb45a7614725 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Mon, 20 Nov 2023 10:43:38 +0100 Subject: [PATCH 2/2] TaggedDataPayload cleanup (#1644) --- .../types/block/payload/candidacy_announcement.rs | 2 +- .../types/block/payload/signed_transaction/mod.rs | 2 +- .../payload/{tagged_data/mod.rs => tagged_data.rs} | 13 +++++-------- 3 files changed, 7 insertions(+), 10 deletions(-) rename sdk/src/types/block/payload/{tagged_data/mod.rs => tagged_data.rs} (87%) diff --git a/sdk/src/types/block/payload/candidacy_announcement.rs b/sdk/src/types/block/payload/candidacy_announcement.rs index b21916515b..87313d9bd2 100644 --- a/sdk/src/types/block/payload/candidacy_announcement.rs +++ b/sdk/src/types/block/payload/candidacy_announcement.rs @@ -8,6 +8,6 @@ use packable::Packable; pub struct CandidacyAnnouncementPayload; impl CandidacyAnnouncementPayload { - /// The payload kind of a [`CandidacyAnnouncementPayload`]. + /// The [`Payload`](crate::types::block::payload::Payload) kind of a [`CandidacyAnnouncementPayload`]. pub const KIND: u8 = 2; } diff --git a/sdk/src/types/block/payload/signed_transaction/mod.rs b/sdk/src/types/block/payload/signed_transaction/mod.rs index 5721d70afd..a7cdc0a510 100644 --- a/sdk/src/types/block/payload/signed_transaction/mod.rs +++ b/sdk/src/types/block/payload/signed_transaction/mod.rs @@ -23,7 +23,7 @@ pub struct SignedTransactionPayload { } impl SignedTransactionPayload { - /// The payload kind of a [`SignedTransactionPayload`]. + /// The [`Payload`](crate::types::block::payload::Payload) kind of a [`SignedTransactionPayload`]. pub const KIND: u8 = 1; /// Creates a new [`SignedTransactionPayload`]. diff --git a/sdk/src/types/block/payload/tagged_data/mod.rs b/sdk/src/types/block/payload/tagged_data.rs similarity index 87% rename from sdk/src/types/block/payload/tagged_data/mod.rs rename to sdk/src/types/block/payload/tagged_data.rs index 4eccdae458..71a93a9a26 100644 --- a/sdk/src/types/block/payload/tagged_data/mod.rs +++ b/sdk/src/types/block/payload/tagged_data.rs @@ -12,7 +12,7 @@ use packable::{ Packable, }; -use crate::types::block::{Block, Error}; +use crate::types::block::Error; pub(crate) type TagLength = BoundedU8<{ *TaggedDataPayload::TAG_LENGTH_RANGE.start() }, { *TaggedDataPayload::TAG_LENGTH_RANGE.end() }>; @@ -30,15 +30,12 @@ pub struct TaggedDataPayload { } impl TaggedDataPayload { - /// The payload kind of a [`TaggedDataPayload`]. + /// The [`Payload`](crate::types::block::payload::Payload) kind of a [`TaggedDataPayload`]. pub const KIND: u8 = 0; - /// Valid lengths for the tag. + /// Valid length range for the tag. pub const TAG_LENGTH_RANGE: RangeInclusive = 0..=64; - /// Valid lengths for the data. - // Less than max block length, because of the other fields in the block and payload kind, tagged payload field - // lengths. - // TODO https://github.com/iotaledger/iota-sdk/issues/1226 - pub const DATA_LENGTH_RANGE: RangeInclusive = 0..=(Block::LENGTH_MAX - Block::LENGTH_MIN - 9) as u32; + /// Valid length range for the data. + pub const DATA_LENGTH_RANGE: RangeInclusive = 0..=8192; /// Creates a new [`TaggedDataPayload`]. pub fn new(tag: impl Into>, data: impl Into>) -> Result {