From b8c9bdbcdf4281ce568f83043ea8826abc10c1d2 Mon Sep 17 00:00:00 2001 From: DaughterOfMars Date: Sun, 29 Oct 2023 14:34:52 -0400 Subject: [PATCH] Add transaction capabilities to bindings and fix some issues (#1509) * Add transaction capabilities to bindings and fix some issues * remove len from hex string --------- Co-authored-by: Thibault Martinez --- bindings/nodejs/lib/types/block/address.ts | 5 ++-- .../payload/signed_transaction/transaction.ts | 29 +++++++++++++++++++ bindings/python/iota_sdk/types/address.py | 8 ++--- bindings/python/iota_sdk/types/essence.py | 9 ++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/bindings/nodejs/lib/types/block/address.ts b/bindings/nodejs/lib/types/block/address.ts index c253dbc033..1e6420e265 100644 --- a/bindings/nodejs/lib/types/block/address.ts +++ b/bindings/nodejs/lib/types/block/address.ts @@ -183,7 +183,7 @@ class RestrictedAddress extends Address { /** * The allowed capabilities bitflags. */ - private allowedCapabilities: HexEncodedString = '0x00'; + private allowedCapabilities: HexEncodedString = '0x'; /** * @param address An address. */ @@ -196,14 +196,13 @@ class RestrictedAddress extends Address { if (allowedCapabilities.some((c) => c != 0)) { this.allowedCapabilities = '0x' + - allowedCapabilities.length.toString(16) + Buffer.from( allowedCapabilities.buffer, allowedCapabilities.byteOffset, allowedCapabilities.byteLength, ).toString('hex'); } else { - this.allowedCapabilities = '0x00'; + this.allowedCapabilities = '0x'; } } diff --git a/bindings/nodejs/lib/types/block/payload/signed_transaction/transaction.ts b/bindings/nodejs/lib/types/block/payload/signed_transaction/transaction.ts index ce7290f7e2..e6e217c52e 100644 --- a/bindings/nodejs/lib/types/block/payload/signed_transaction/transaction.ts +++ b/bindings/nodejs/lib/types/block/payload/signed_transaction/transaction.ts @@ -9,6 +9,7 @@ import { Output, OutputDiscriminator } from '../../output'; import { SlotIndex } from '../../slot'; import { Payload, PayloadType } from '../payload'; import { TaggedDataPayload } from '../tagged/tagged'; +import { HexEncodedString } from '../../../utils'; /** * PayloadDiscriminator for payloads inside of a Transaction. @@ -43,6 +44,8 @@ class Transaction { readonly allotments: ManaAllotment[]; + private capabilities: HexEncodedString = '0x'; + @Type(() => Payload, { discriminator: PayloadDiscriminator, }) @@ -77,6 +80,32 @@ class Transaction { this.payload = payload; this.outputs = outputs; } + + setCapabilities(capabilities: Uint8Array) { + if (capabilities.some((c) => c != 0)) { + this.capabilities = + '0x' + + Buffer.from( + capabilities.buffer, + capabilities.byteOffset, + capabilities.byteLength, + ).toString('hex'); + } else { + this.capabilities = '0x'; + } + } + + withCapabilities(capabilities: Uint8Array): Transaction { + this.setCapabilities(capabilities); + return this; + } + + /** Get the capability bitflags of the transaction. */ + getCapabilities(): Uint8Array { + return Uint8Array.from( + Buffer.from(this.capabilities.substring(2), 'hex'), + ); + } } export { Transaction }; diff --git a/bindings/python/iota_sdk/types/address.py b/bindings/python/iota_sdk/types/address.py index 50d7df544b..dc4294b271 100644 --- a/bindings/python/iota_sdk/types/address.py +++ b/bindings/python/iota_sdk/types/address.py @@ -100,16 +100,16 @@ class RestrictedAddress: allowed_capabilities: The allowed capabilities bitflags. """ address: Union[Ed25519Address, AccountAddress, NFTAddress] - allowed_capabilities: HexStr = field(default='0x00', init=False) + allowed_capabilities: HexStr = field(default='0x', init=False) type: int = field(default_factory=lambda: int( AddressType.RESTRICTED), init=False) - def with_allowed_capabilities(self, allowed_capabilities: bytes): + def with_allowed_capabilities(self, capabilities: bytes): """Sets the allowed capabilities from a byte array. Attributes: - allowed_capabilities: The allowed capabilities bitflags. + capabilities: The allowed capabilities bitflags. """ - self.allowed_capabilities = '0x00' + allowed_capabilities.hex() + self.allowed_capabilities = '0x' + capabilities.hex() @json diff --git a/bindings/python/iota_sdk/types/essence.py b/bindings/python/iota_sdk/types/essence.py index cd9ae1bdc6..32df2042e6 100644 --- a/bindings/python/iota_sdk/types/essence.py +++ b/bindings/python/iota_sdk/types/essence.py @@ -41,6 +41,7 @@ class RegularTransactionEssence: outputs: The outputs that are created by the Transaction Payload context_inputs: The inputs that provide additional contextual information for the execution of a transaction. allotments: The allotments of Mana which which will be added upon commitment of the slot. + capabilities: The capability bitflags of the transaction. payload: An optional tagged data payload """ network_id: str @@ -50,10 +51,18 @@ class RegularTransactionEssence: outputs: List[Output] context_inputs: Optional[List[ContextInput]] = None allotments: Optional[List[ManaAllotment]] = None + capabilities: HexStr = field(default='0x', init=False) payload: Optional[Payload] = None type: int = field( default_factory=lambda: EssenceType.RegularTransactionEssence, init=False) + def with_capabilities(self, capabilities: bytes): + """Sets the transaction capabilities from a byte array. + Attributes: + capabilities: The transaction capabilities bitflags. + """ + self.capabilities = '0x' + capabilities.hex() + TransactionEssence: TypeAlias = RegularTransactionEssence