Skip to content

Commit

Permalink
Add transaction capabilities to bindings and fix some issues (#1509)
Browse files Browse the repository at this point in the history
* Add transaction capabilities to bindings and fix some issues

* remove len from hex string

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
DaughterOfMars and thibault-martinez authored Oct 29, 2023
1 parent 46d979b commit b8c9bdb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
5 changes: 2 additions & 3 deletions bindings/nodejs/lib/types/block/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class RestrictedAddress extends Address {
/**
* The allowed capabilities bitflags.
*/
private allowedCapabilities: HexEncodedString = '0x00';
private allowedCapabilities: HexEncodedString = '0x';
/**
* @param address An address.
*/
Expand All @@ -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';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -43,6 +44,8 @@ class Transaction {

readonly allotments: ManaAllotment[];

private capabilities: HexEncodedString = '0x';

@Type(() => Payload, {
discriminator: PayloadDiscriminator,
})
Expand Down Expand Up @@ -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 };
8 changes: 4 additions & 4 deletions bindings/python/iota_sdk/types/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions bindings/python/iota_sdk/types/essence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit b8c9bdb

Please sign in to comment.