diff --git a/function/src/main/java/com/bloxbean/cardano/client/function/TxBuilderContext.java b/function/src/main/java/com/bloxbean/cardano/client/function/TxBuilderContext.java index f4d3b4cf..3fa54d02 100644 --- a/function/src/main/java/com/bloxbean/cardano/client/function/TxBuilderContext.java +++ b/function/src/main/java/com/bloxbean/cardano/client/function/TxBuilderContext.java @@ -267,9 +267,7 @@ public static TxBuilderContext init(UtxoSupplier utxoSupplier, ProtocolParamsSup * @throws com.bloxbean.cardano.client.function.exception.TxBuildException if exception during transaction build */ public Transaction build(TxBuilder txBuilder) { - Transaction transaction = new Transaction(); - transaction.setEra(getSerializationEra()); - txBuilder.apply(this, transaction); + Transaction transaction = buildTransaction(txBuilder); clearTempStates(); return transaction; } @@ -282,8 +280,10 @@ public Transaction build(TxBuilder txBuilder) { * @throws com.bloxbean.cardano.client.function.exception.TxBuildException if exception during transaction build */ public Transaction buildAndSign(TxBuilder txBuilder, TxSigner signer) { - Transaction transaction = build(txBuilder); - return signer.sign(transaction); + Transaction transaction = buildTransaction(txBuilder); + Transaction signedTransaction = signer.sign(this, transaction); + clearTempStates(); + return signedTransaction; } /** @@ -297,6 +297,13 @@ public void build(Transaction transaction, TxBuilder txBuilder) { clearTempStates(); } + private Transaction buildTransaction(TxBuilder txBuilder) { + Transaction transaction = new Transaction(); + transaction.setEra(getSerializationEra()); + txBuilder.apply(this, transaction); + return transaction; + } + private void clearTempStates() { clearMintMultiAssets(); clearUtxos(); diff --git a/quicktx/src/main/java/com/bloxbean/cardano/client/quicktx/QuickTxBuilder.java b/quicktx/src/main/java/com/bloxbean/cardano/client/quicktx/QuickTxBuilder.java index 42be7129..bb58ecb6 100644 --- a/quicktx/src/main/java/com/bloxbean/cardano/client/quicktx/QuickTxBuilder.java +++ b/quicktx/src/main/java/com/bloxbean/cardano/client/quicktx/QuickTxBuilder.java @@ -22,7 +22,7 @@ import com.bloxbean.cardano.client.transaction.spec.Transaction; import com.bloxbean.cardano.client.transaction.spec.TransactionInput; import com.bloxbean.cardano.client.util.JsonUtil; -import com.bloxbean.cardano.hdwallet.Wallet; +import com.bloxbean.cardano.client.util.Tuple; import com.bloxbean.cardano.hdwallet.supplier.WalletUtxoSupplier; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; @@ -116,6 +116,12 @@ public QuickTxBuilder(BackendService backendService) { } } + /** + * Create a QuickTxBuilder instance with specified BackendService and UtxoSupplier. + * + * @param backendService backend service to get protocol params and submit transactions + * @param utxoSupplier utxo supplier to get utxos + */ public QuickTxBuilder(BackendService backendService, UtxoSupplier utxoSupplier) { this(utxoSupplier, new DefaultProtocolParamsSupplier(backendService.getEpochService()), @@ -166,7 +172,6 @@ public class TxContext { private boolean ignoreScriptCostEvaluationError = true; private Era serializationEra; private boolean removeDuplicateScriptWitnesses = false; - private Wallet signerWallet; TxContext(AbstractTx... txs) { this.txList = txs; @@ -234,6 +239,25 @@ public TxContext additionalSignersCount(int additionalSigners) { * @return Transaction */ public Transaction build() { + Tuple tuple = _build(); + return tuple._1.build(tuple._2); + } + + /** + * Build and sign transaction + * + * @return Transaction + */ + public Transaction buildAndSign() { + Tuple tuple = _build(); + + if (signers != null) + return tuple._1.buildAndSign(tuple._2, signers); + else + throw new IllegalStateException("No signers found"); + } + + private Tuple _build() { TxBuilder txBuilder = (context, txn) -> { }; boolean containsScriptTx = false; @@ -383,7 +407,8 @@ public Transaction build() { tx.postBalanceTx(transaction); })); } - return txBuilderContext.build(txBuilder); + + return new Tuple<>(txBuilderContext, txBuilder); } private int getTotalSigners() { @@ -394,24 +419,6 @@ private int getTotalSigners() { return totalSigners; } - /** - * Build and sign transaction - * - * @return Transaction - */ - public Transaction buildAndSign() { - Transaction transaction = build(); - if (signers != null) - transaction = signers.sign(transaction); - if(signerWallet != null) { - if(!(utxoSupplier instanceof WalletUtxoSupplier)) - throw new TxBuildException("Provide a WalletUtxoSupplier when using a sender wallet"); - transaction = signerWallet.sign(transaction, (WalletUtxoSupplier) utxoSupplier); - } - - return transaction; - } - private TxBuilder buildCollateralOutput(String feePayer) { if (collateralInputs != null && !collateralInputs.isEmpty()) { List collateralUtxos = collateralInputs.stream() @@ -580,15 +587,6 @@ public TxContext withSigner(@NonNull TxSigner signer) { this.signers = this.signers.andThen(signer); return this; } - /** - * Sign transaction with the given wallet - * @param wallet - * @return TxContext - */ - public TxContext withSigner(Wallet wallet) { - this.signerWallet = wallet; - return this; - } /** * Add validity start slot to the transaction. This value is set in "validity start from" field of the transaction.