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

Add transaction capabilities to bindings and fix some issues #1509

Merged
merged 5 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
Attributes:
capabilities: The transaction capabilities bitflags.
"""
self.capabilities = '0x' + capabilities.hex()


TransactionEssence: TypeAlias = RegularTransactionEssence