signTransaction returns TransactionBuilder

This commit is contained in:
Jim Hamill
2025-06-18 06:53:30 +00:00
parent 6a3e6ee090
commit 4cf729f244
5 changed files with 57 additions and 19 deletions
+2 -1
View File
@@ -1,3 +1,4 @@
import { type TransactionBuilder } from './transaction-builder.js';
import {
WalletUTXOs,
type InputTemplateBCH,
@@ -207,7 +208,7 @@ export abstract class BaseWallet<
*/
abstract signTransaction(
template: Partial<TransactionTemplate>,
): Promise<Uint8Array>;
): Promise<TransactionBuilder>;
/**
* Select UTXOs from this wallet to meet the amounts required and broadcast it to the Blockchain.
@@ -13,6 +13,7 @@ import {
encodeTransactionOutput,
generateTransaction,
getDustThreshold,
isDustOutput,
} from '@bitauth/libauth';
// TODO: This could probably still use a refactor, but isn't terrible anymore.
@@ -303,6 +304,15 @@ export class TransactionBuilder {
throw new Error(`Failed to generate transaction: ${errors}`);
}
// Check for dust outputs.
const hasDustOutputs = generatedTransaction.transaction.outputs.some(
(output) => isDustOutput(output),
);
if (hasDustOutputs) {
throw new Error('Output amounts must be above the dust threshold');
}
return generatedTransaction.transaction;
}
+7 -9
View File
@@ -293,7 +293,7 @@ export class WalletHD extends BaseWallet {
async signTransaction(
txTemplate: Partial<TransactionTemplate>,
): Promise<Uint8Array> {
): Promise<TransactionBuilder> {
// Populate the final transaction template by merging with defaults.
// NOTE: This is just a convenience to make "send" easier to use.
const txTemplateFinal: TransactionTemplate = {
@@ -317,21 +317,19 @@ export class WalletHD extends BaseWallet {
const unspents = await this.getUnspentDirectives();
// Build the transaction.
const tx = TransactionBuilder.build(
return TransactionBuilder.build(
txTemplateFinal,
unspents.toArray(),
changeLockingBytecode,
);
// Generate the transaction.
const generatedTxBytes = tx.toBytes();
return generatedTxBytes;
}
async sendTransaction(txTemplate: Partial<TransactionTemplate>) {
// Sign the transaction template.
const generatedTxBytes = await this.signTransaction(txTemplate);
// Generate the transaction.
const generatedTx = await this.signTransaction(txTemplate);
// Convert the tx to bytes.
const generatedTxBytes = generatedTx.toBytes();
// Broadcast the transaction to the blockchain.
await this.blockchain.broadcastTransaction(generatedTxBytes);
+5 -7
View File
@@ -360,21 +360,19 @@ export class WalletP2PKH extends BaseWallet {
const unspents = await this.getUnspentDirectives();
// Build the transaction.
const tx = TransactionBuilder.build(
return TransactionBuilder.build(
txTemplateFinal,
unspents.toArray(),
changeLockingBytecode,
);
// Generate the transaction.
const generatedTxBytes = tx.toBytes();
return generatedTxBytes;
}
async sendTransaction(txTemplate: Partial<TransactionTemplate>) {
// Generate the transaction.
const generatedTxBytes = await this.signTransaction(txTemplate);
const generatedTx = await this.signTransaction(txTemplate);
// Convert the tx to bytes.
const generatedTxBytes = generatedTx.toBytes();
// Broadcast the transaction to the blockchain.
await this.blockchain.broadcastTransaction(generatedTxBytes);