corda / net.corda.core.transactions / TransactionBuilder

TransactionBuilder

open class TransactionBuilder

A TransactionBuilder is a transaction class that's mutable (unlike the others which are all immutable). It is intended to be passed around contracts that may edit it by adding new states/commands. Then once the states and commands are right, this class can be used as a holding bucket to gather signatures from multiple parties.

The builder can be customised for specific transaction types, e.g. where additional processing is needed before adding a state/command.

Parameters

notary - Notary used for the transaction. If null, this indicates the transaction DOES NOT have a notary. When this is set to a non-null value, an output state can be added by just passing in a ContractState – a TransactionState with this notary specified will be generated automatically.

Constructors

<init>

TransactionBuilder(notary: Party? = null, lockId: UUID = defaultLockId(), inputs: MutableList<StateRef> = arrayListOf(), attachments: MutableList<SecureHash> = arrayListOf(), outputs: MutableList<TransactionState<ContractState>> = arrayListOf(), commands: MutableList<Command<*>> = arrayListOf(), window: TimeWindow? = null, privacySalt: PrivacySalt = PrivacySalt())
TransactionBuilder(notary: Party)TransactionBuilder(notary: Party? = null, lockId: UUID = defaultLockId(), inputs: MutableList<StateRef> = arrayListOf(), attachments: MutableList<SecureHash> = arrayListOf(), outputs: MutableList<TransactionState<ContractState>> = arrayListOf(), commands: MutableList<Command<*>> = arrayListOf(), window: TimeWindow? = null, privacySalt: PrivacySalt = PrivacySalt(), references: MutableList<StateRef> = arrayListOf(), serviceHub: ServiceHub? = (Strand.currentStrand() as? FlowStateMachine<*>)?.serviceHub)

A TransactionBuilder is a transaction class that's mutable (unlike the others which are all immutable). It is intended to be passed around contracts that may edit it by adding new states/commands. Then once the states and commands are right, this class can be used as a holding bucket to gather signatures from multiple parties.

Properties

attachments

val attachments: MutableList<SecureHash>

commands

val commands: MutableList<Command<*>>

inputs

val inputs: MutableList<StateRef>

lockId

var lockId: UUID

notary

var notary: Party?

Notary used for the transaction. If null, this indicates the transaction DOES NOT have a notary. When this is set to a non-null value, an output state can be added by just passing in a ContractState – a TransactionState with this notary specified will be generated automatically.

outputs

val outputs: MutableList<TransactionState<ContractState>>

privacySalt

var privacySalt: PrivacySalt

references

val references: MutableList<StateRef>

serviceHub

val serviceHub: ServiceHub?

window

var window: TimeWindow?

Functions

addAttachment

fun addAttachment(attachmentId: SecureHash): TransactionBuilder

Adds an attachment with the specified hash to the TransactionBuilder.

addCommand

fun addCommand(arg: Command<*>): TransactionBuilder

Adds a Command to the transaction.

fun addCommand(data: CommandData, vararg keys: PublicKey): TransactionBuilder

Adds a Command to the transaction, specified by the encapsulated CommandData object and required list of signing PublicKeys.

fun addCommand(data: CommandData, keys: List<PublicKey>): TransactionBuilder

addInputState

open fun addInputState(stateAndRef: StateAndRef<*>): TransactionBuilder

Adds an input StateRef to the transaction.

addOutputState

fun addOutputState(state: TransactionState<*>): TransactionBuilder

Adds an output state to the transaction.

fun addOutputState(state: ContractState, contract: ContractClassName = requireNotNull(state.requiredContractClassName) { //TODO: add link to docsite page, when there is one. """ Unable to infer Contract class name because state class ${state::class.java.name} is not annotated with @BelongsToContract, and does not have an enclosing class which implements Contract. Either annotate ${state::class.java.name} with @BelongsToContract, or supply an explicit contract parameter to addOutputState(). """.trimIndent().replace('\n', ' ') }, notary: Party, encumbrance: Int? = null, constraint: AttachmentConstraint = AutomaticPlaceholderConstraint): TransactionBuilder

Adds an output state, with associated contract code (and constraints), and notary, to the transaction.

fun addOutputState(state: ContractState, contract: ContractClassName = requireNotNull(state.requiredContractClassName) { //TODO: add link to docsite page, when there is one. """ Unable to infer Contract class name because state class ${state::class.java.name} is not annotated with @BelongsToContract, and does not have an enclosing class which implements Contract. Either annotate ${state::class.java.name} with @BelongsToContract, or supply an explicit contract parameter to addOutputState(). """.trimIndent().replace('\n', ' ') }, constraint: AttachmentConstraint = AutomaticPlaceholderConstraint): TransactionBuilder

A default notary must be specified during builder construction to use this method

addReferenceState

open fun addReferenceState(referencedStateAndRef: ReferencedStateAndRef<*>): TransactionBuilder

Adds a reference input StateRef to the transaction.

attachments

fun attachments(): List<SecureHash>

Returns an immutable list of attachment hashes.

commands

fun commands(): List<Command<*>>

Returns an immutable list of Commands.

copy

fun copy(): TransactionBuilder

Creates a copy of the builder.

inputStates

fun inputStates(): List<StateRef>

Returns an immutable list of input StateRefs.

outputStates

fun outputStates(): List<TransactionState<*>>

Returns an immutable list of output TransactionStates.

referenceStates

fun referenceStates(): List<StateRef>

Returns an immutable list of reference input StateRefs.

setPrivacySalt

fun setPrivacySalt(privacySalt: PrivacySalt): TransactionBuilder

setTimeWindow

fun setTimeWindow(timeWindow: TimeWindow): TransactionBuilder

Sets the TimeWindow for this transaction, replacing the existing TimeWindow if there is one. To be valid, the transaction must then be signed by the notary service within this window of time. In this way, the notary acts as the Timestamp Authority.

fun setTimeWindow(time: Instant, timeTolerance: Duration): TransactionBuilder

The TimeWindow for the transaction can also be defined as time +/- timeTolerance. The tolerance should be chosen such that your code can finish building the transaction and sending it to the Timestamp Authority within that window of time, taking into account factors such as network latency. Transactions being built by a group of collaborating parties may therefore require a higher time tolerance than a transaction being built by a single node.

toLedgerTransaction

fun toLedgerTransaction(services: ServiceHub): LedgerTransaction

toSignedTransaction

fun toSignedTransaction(keyManagementService: KeyManagementService, publicKey: PublicKey, signatureMetadata: SignatureMetadata, services: ServicesForResolution): SignedTransaction

Sign the built transaction and return it. This is an internal function for use by the service hub, please use ServiceHub.signInitialTransaction instead.

toWireTransaction

fun toWireTransaction(services: ServicesForResolution): WireTransaction

Generates a WireTransaction from this builder, resolves any AutomaticPlaceholderConstraint, and selects the attachments to use for this transaction.

verify

fun verify(services: ServiceHub): Unit

withItems

fun withItems(vararg items: Any): TransactionBuilder

A more convenient way to add items to this transaction that calls the add* methods for you based on type

Extension Functions

contextLogger

fun Any.contextLogger(): Logger

When called from a companion object, returns the logger for the enclosing class.