Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs: add Weighted and MultiAddress #1645

Merged
merged 8 commits into from
Nov 21, 2023
78 changes: 78 additions & 0 deletions bindings/nodejs/lib/types/block/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -78,6 +80,7 @@ abstract class Address {
throw new Error('Invalid JSON');
}
}

/**
* An Ed25519 Address.
*/
Expand Down Expand Up @@ -256,6 +259,78 @@ 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 },
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
{
value: ImplicitAccountCreationAddress,
name: AddressType.ImplicitAccountCreation as any,
},
{
value: RestrictedAddress,
name: AddressType.Restricted 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: [
Expand All @@ -267,6 +342,7 @@ const AddressDiscriminator = {
value: ImplicitAccountCreationAddress,
name: AddressType.ImplicitAccountCreation as any,
},
{ value: MultiAddress, name: AddressType.Multi as any },
{ value: RestrictedAddress, name: AddressType.Restricted as any },
],
};
Expand All @@ -281,5 +357,7 @@ export {
NftAddress,
AnchorAddress,
ImplicitAccountCreationAddress,
WeightedAddress,
MultiAddress,
RestrictedAddress,
};
Loading