signTransaction returns TransactionBuilder
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user