Class: Transaction
Transactions describe a transfer of value, usually from the sender to the recipient. However, transactions can also have no value, when they are used to signal a change in the staking contract.
Transactions can be used to create contracts, such as vesting contracts and HTLCs.
Transactions require a valid signature proof over their serialized content. Furthermore, transactions are only valid for 2 hours after their validity-start block height.
Constructors
new Transaction()
new Transaction(
sender
,sender_type
,sender_data
,recipient
,recipient_type
,recipient_data
,value
,fee
,flags
,validity_start_height
,network_id
):Transaction
Creates a new unsigned transaction that transfers value
amount of luna (NIM's smallest unit) from the sender to the recipient, where both sender and recipient can be any account type, and custom extra data can be added to the transaction.
Basic transactions
If both the sender and recipient types are omitted or 0
and both data and flags are empty, a smaller basic transaction is created.
Extended transactions
If no flags are given, but sender type is not basic (0
) or data is set, an extended transaction is created.
Contract creation transactions
To create a new vesting or HTLC contract, set flags
to 0b1
and specify the contract type as the recipient_type
: 1
for vesting, 2
for HTLC. The data
bytes must have the correct format of contract creation data for the respective contract type.
Signaling transactions
To interact with the staking contract, signaling transaction are often used to not transfer any value, but to simply signal a state change instead, such as changing one's delegation from one validator to another. To create such a transaction, set flags
to 0b10
and populate the data
bytes accordingly.
The returned transaction is not yet signed. You can sign it e.g. with tx.sign(keyPair)
.
Throws when an account type is unknown, the numbers given for value and fee do not fit within a u64 or the networkId is unknown. Also throws when no data or recipient type is given for contract creation transactions, or no data is given for signaling transactions.
Parameters
sender
sender_type
number
sender_data
Uint8Array
recipient
recipient_type
number
recipient_data
Uint8Array
value
bigint
fee
bigint
flags
number
validity_start_height
number
network_id
number
Returns
Defined in
@nimiq/core/types/wasm/web.d.ts:2107
Properties
data
data:
Uint8Array
The transaction's data as a byte array.
Defined in
@nimiq/core/types/wasm/web.d.ts:2192
fee
readonly
fee:bigint
The transaction's fee in luna (NIM's smallest unit).
Defined in
@nimiq/core/types/wasm/web.d.ts:2196
feePerByte
readonly
feePerByte:number
The transaction's fee per byte in luna (NIM's smallest unit).
Defined in
@nimiq/core/types/wasm/web.d.ts:2200
flags
readonly
flags:TransactionFlag
The transaction's flags: 0b1
= contract creation, 0b10
= signaling.
Defined in
@nimiq/core/types/wasm/web.d.ts:2204
format
readonly
format:TransactionFormat
The transaction's TransactionFormat.
Defined in
@nimiq/core/types/wasm/web.d.ts:2208
networkId
readonly
networkId:number
The transaction's network ID.
Defined in
@nimiq/core/types/wasm/web.d.ts:2212
proof
proof:
Uint8Array
The transaction's signature proof as a byte array.
Defined in
@nimiq/core/types/wasm/web.d.ts:2216
recipient
readonly
recipient:Address
The transaction's recipient address.
Defined in
@nimiq/core/types/wasm/web.d.ts:2220
recipientType
readonly
recipientType:AccountType
The transaction's recipient AccountType.
Defined in
@nimiq/core/types/wasm/web.d.ts:2224
sender
readonly
sender:Address
The transaction's sender address.
Defined in
@nimiq/core/types/wasm/web.d.ts:2228
senderData
readonly
senderData:Uint8Array
The transaction's sender data as a byte array.
Defined in
@nimiq/core/types/wasm/web.d.ts:2232
senderType
readonly
senderType:AccountType
The transaction's sender AccountType.
Defined in
@nimiq/core/types/wasm/web.d.ts:2236
serializedSize
readonly
serializedSize:number
The transaction's byte size.
Defined in
@nimiq/core/types/wasm/web.d.ts:2240
validityStartHeight
readonly
validityStartHeight:number
The transaction's validity-start height. The transaction is valid for 2 hours after this block height.
Defined in
@nimiq/core/types/wasm/web.d.ts:2244
value
readonly
value:bigint
The transaction's value in luna (NIM's smallest unit).
Defined in
@nimiq/core/types/wasm/web.d.ts:2248
Methods
__getClassname()
__getClassname():
string
Returns
string
Defined in
@nimiq/core/types/wasm/web.d.ts:2065
free()
free():
void
Returns
void
Defined in
@nimiq/core/types/wasm/web.d.ts:2061
getContractCreationAddress()
getContractCreationAddress():
Address
Returns the address of the contract that is created with this transaction.
Returns
Defined in
@nimiq/core/types/wasm/web.d.ts:2145
hash()
hash():
string
Computes the transaction's hash, which is used as its unique identifier on the blockchain.
Returns
string
Defined in
@nimiq/core/types/wasm/web.d.ts:2124
isValidAt()
isValidAt(
block_height
):boolean
Tests if the transaction is valid at the specified block height.
Parameters
block_height
number
Returns
boolean
Defined in
@nimiq/core/types/wasm/web.d.ts:2140
serialize()
serialize():
Uint8Array
Serializes the transaction to a byte array.
Returns
Uint8Array
Defined in
@nimiq/core/types/wasm/web.d.ts:2155
serializeContent()
serializeContent():
Uint8Array
Serializes the transaction's content to be used for creating its signature.
Returns
Uint8Array
Defined in
@nimiq/core/types/wasm/web.d.ts:2150
sign()
sign(
key_pair
):void
Signs the transaction with the provided key pair. Automatically determines the format of the signature proof required for the transaction.
Limitations
- HTLC redemption is not supported and will throw.
- For transaction to the staking contract, both signatures are made with the same keypair, so it is not possible to interact with a staker that is different from the sender address or using a different cold or signing key for validator transactions.
Parameters
key_pair
Returns
void
Defined in
@nimiq/core/types/wasm/web.d.ts:2119
toHex()
toHex():
string
Serializes the transaction into a HEX string.
Returns
string
Defined in
@nimiq/core/types/wasm/web.d.ts:2160
toPlain()
toPlain():
PlainTransaction
Creates a JSON-compatible plain object representing the transaction.
Returns
Defined in
@nimiq/core/types/wasm/web.d.ts:2165
verify()
verify(
network_id
?):void
Verifies that a transaction has valid properties and a valid signature proof. Optionally checks if the transaction is valid on the provided network.
Throws with any transaction validity error. Returns without exception if the transaction is valid.
Throws when the given networkId is unknown.
Parameters
network_id?
number
Returns
void
Defined in
@nimiq/core/types/wasm/web.d.ts:2134
deserialize()
static
deserialize(bytes
):Transaction
Deserializes a transaction from a byte array.
Parameters
bytes
Uint8Array
Returns
Defined in
@nimiq/core/types/wasm/web.d.ts:2171
fromAny()
static
fromAny(tx
):Transaction
Parses a transaction from a Transaction instance, a plain object, a hex string representation, or a byte array.
Throws when a transaction cannot be parsed from the argument.
Parameters
tx
string
| Uint8Array
| PlainTransaction
| Transaction
Returns
Defined in
@nimiq/core/types/wasm/web.d.ts:2180
fromPlain()
static
fromPlain(plain
):Transaction
Parses a transaction from a plain object.
Throws when a transaction cannot be parsed from the argument.
Parameters
plain
Returns
Defined in
@nimiq/core/types/wasm/web.d.ts:2188