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] 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, };