TransactionService

Use this guide to learn how to sign transactions with the TransactionService, and how to sign TransactionBuilders directly, without TransactionService.

Sign transactions using the TransactionService

TransactionService can be injected into flows and services. You can sign transactions using TransactionService. This includes signing both TransactionBuilders and SignedTransactions.

Sign a TransactionBuilder

To sign a TransactionBuilder:

  • Kotlin

    // Sign with the system's default identity key
    val signedTransaction: SignedTransaction = transactionService.sign(transactionBuilder)
    
    // Sign with a single [PublicKey]
    val signedTransaction: SignedTransaction = transactionService.sign(transactionBuilder, publicKey)
    
    // Sign with multiple [PublicKey]s
    val signedTransaction: SignedTransaction = transactionService.sign(transactionBuilder, listOf(publicKey, anotherPublicKey))
    
  • Java

    // Sign with the system's default identity key
    SignedTransaction signedTransaction = transactionService.sign(transactionBuilder)
    
    // Sign with a single [PublicKey]
    SignedTransaction signedTransaction = transactionService.sign(transactionBuilder, publicKey)
    
    // Sign with multiple [PublicKey]s
    SignedTransaction signedTransaction = transactionService.sign(transactionBuilder, List.of(publicKey, anotherPublicKey))
    

Sign a SignedTransaction

To sign a SignedTransaction:

  • Kotlin

    // Sign with the system's default identity key
    val signedTransactionWithAnotherSignature: SignedTransaction = transactionService.sign(signedTransaction)
    
    // Sign with a single [PublicKey]
    val signedTransactionWithAnotherSignature: SignedTransaction = transactionService.sign(signedTransaction, publicKey)
    
  • Java

    // Sign with the system's default identity key
    SignedTransaction signedTransactionWithAnotherSignature = transactionService.sign(signedTransaction)
    
    // Sign with a single [PublicKey]
    SignedTransaction signedTransactionWithAnotherSignature = transactionService.sign(signedTransaction, publicKey)
    

Create a signature

You can also create the signature without signing the input transaction itself by using TransactionService.createSignature.

There are overloads to create signatures for SignedTransactions and FilteredTransactions.

To create a signature for a SignedTransaction or FilteredTransaction:

  • Kotlin

    // Create a signature with the system's default identity key
    val signature: DigitalSignatureAndMeta = transactionService.createSignature(transaction)
    
    // Create a signature with a [PublicKey]
    val signature: DigitalSignatureAndMeta = transactionService.createSignature(transaction, publicKey)
    
  • Java

    // Create a signature with the system's default identity key
    DigitalSignatureAndMeta signature = transactionService.createSignature(transaction)
    
    // Create a signature with a [PublicKey]
    DigitalSignatureAndMeta signature = transactionService.createSignature(transaction, publicKey)
    

If the input transaction to createSignature was a SignedTransaction, then the returned DigitalSignatureAndMeta can be combined with it after operations using the signature are complete, returning a copy of the SignedTransaction with the signature. This is equivalent to using TransactionService.sign, but allows you to interact with the DigitalSignatureAndMeta as your application requires.

Sign a TransactionBuilder directly

You can sign a TransactionBuilder directly without accessing a TransactionService. This is a convenience function to make building transactions simpler. It is functionally equivalent to the TransactionService.sign method.

  • Kotlin

    // Sign with the system's default identity key
    val signedTransaction: SignedTransaction = transactionBuilder.sign()
    
    // Sign with a single [PublicKey]
    val signedTransaction: SignedTransaction = transactionBuilder.sign(publicKey)
    
    // Sign with multiple [PublicKey]s
    val signedTransaction: SignedTransaction = transactionBuilder.sign(listOf(publicKey, anotherPublicKey))
    
  • Java

    // Sign with the system's default identity key
    SignedTransaction signedTransaction = transactionBuilder.sign()
    
    // Sign with a single [PublicKey]
    SignedTransaction signedTransaction = transactionBuilder.sign(publicKey)
    
    // Sign with multiple [PublicKey]s
    SignedTransaction signedTransaction = transactionBuilder.sign(List.of(publicKey, anotherPublicKey))
    

Was this page helpful?

Thanks for your feedback!

Chat with us

Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.

Propose documentation improvements directly

Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.

We're sorry this page wasn't helpful. Let us know how we can make it better!

Chat with us

Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.

Create an issue

Create a new GitHub issue in this repository - submit technical feedback, draw attention to a potential documentation bug, or share ideas for improvement and general feedback.

Propose documentation improvements directly

Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.