From 3175a903a6b5d427503a4dea099c10d55f020645 Mon Sep 17 00:00:00 2001 From: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> Date: Tue, 21 Nov 2023 09:40:54 +0100 Subject: [PATCH] Nodejs: add Weighted and MultiAddress (#1645) * nodejs add Weighted and MultiAddress * Only allowed addresses in discriminator --------- Co-authored-by: Thibault Martinez --- bindings/nodejs/lib/types/block/address.ts | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/bindings/nodejs/lib/types/block/address.ts b/bindings/nodejs/lib/types/block/address.ts index b5b7c4889a..082c065632 100644 --- a/bindings/nodejs/lib/types/block/address.ts +++ b/bindings/nodejs/lib/types/block/address.ts @@ -24,6 +24,8 @@ enum AddressType { Anchor = 24, /** An implicit account creation address. */ ImplicitAccountCreation = 32, + /** A Multi address. */ + Multi = 40, /** An address with restricted capabilities. */ Restricted = 48, } @@ -78,6 +80,7 @@ abstract class Address { throw new Error('Invalid JSON'); } } + /** * An Ed25519 Address. */ @@ -256,6 +259,70 @@ class RestrictedAddress extends Address { } } +/** + * A weighted address. + */ +class WeightedAddress { + /** + * The unlocked address. + */ + @Type(() => Address, { + discriminator: { + property: 'type', + subTypes: [ + { value: Ed25519Address, name: AddressType.Ed25519 as any }, + { value: AccountAddress, name: AddressType.Account as any }, + { value: NftAddress, name: AddressType.Nft as any }, + { value: AnchorAddress, name: AddressType.Anchor as any }, + ], + }, + }) + readonly address: Address; + /** + * The weight of the unlocked address. + */ + readonly weight: number; + + /** + * @param address The unlocked address. + * @param weight The weight of the unlocked address. + */ + constructor(address: Address, weight: number) { + this.address = address; + this.weight = weight; + } +} + +/** + * An address that consists of addresses with weights and a threshold value. + * The Multi Address can be unlocked if the cumulative weight of all unlocked addresses is equal to or exceeds the + * threshold. + */ +class MultiAddress extends Address { + /** + * The weighted unlocked addresses. + */ + readonly addresses: WeightedAddress[]; + /** + * The threshold that needs to be reached by the unlocked addresses in order to unlock the multi address. + */ + readonly threshold: number; + + /** + * @param addresses The weighted unlocked addresses. + * @param threshold The threshold that needs to be reached by the unlocked addresses in order to unlock the multi address. + */ + constructor(addresses: WeightedAddress[], threshold: number) { + super(AddressType.Multi); + this.addresses = addresses; + this.threshold = threshold; + } + + toString(): string { + return JSON.stringify(this); + } +} + const AddressDiscriminator = { property: 'type', subTypes: [ @@ -267,6 +334,7 @@ const AddressDiscriminator = { value: ImplicitAccountCreationAddress, name: AddressType.ImplicitAccountCreation as any, }, + { value: MultiAddress, name: AddressType.Multi as any }, { value: RestrictedAddress, name: AddressType.Restricted as any }, ], }; @@ -281,5 +349,7 @@ export { NftAddress, AnchorAddress, ImplicitAccountCreationAddress, + WeightedAddress, + MultiAddress, RestrictedAddress, };