Skip to content

Commit

Permalink
Merge PR #816 from 'nodech/wallet-create-tx'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed May 29, 2023
2 parents e122b12 + 4392dcc commit 61c1e05
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
80 changes: 43 additions & 37 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3613,27 +3613,19 @@ class Wallet extends EventEmitter {
return size;
}

/**
* Build a transaction, fill it with outputs and inputs,
* sort the members according to BIP69 (set options.sort=false
* to avoid sorting), set locktime, and template it.
* @param {Object} options - See {@link Wallet#fund options}.
* @param {Object[]} options.outputs - See {@link MTX#addOutput}.
* @param {Object[]} options.outputs - See {@link MTX#addOutput}.
* @param {MTX?} mtx
* @returns {Promise} - Returns {@link MTX}.
*/
/**
* Make a transaction with normal outputs.
* @param {Object[]} outputs - See {@link MTX#addOutput}
* @param {MTX} [mtx=null] - MTX to modify instead of new one.
* @returns {MTX} - MTX with populated outputs.
*/

async createTX(options, force, mtx) {
const outputs = options.outputs;
let finish = false;
if (!mtx) {
finish = true;
mtx = new MTX();
}
makeTX(outputs, mtx) {
assert(Array.isArray(outputs), 'output must be an array.');
assert(outputs.length > 0, 'At least one output is required.');

assert(Array.isArray(outputs), 'Outputs must be an array.');
assert(outputs.length > 0, 'At least one output required.');
if (!mtx)
mtx = new MTX();

// Add the outputs
for (const obj of outputs) {
Expand All @@ -3654,20 +3646,38 @@ class Wallet extends EventEmitter {
mtx.outputs.push(output);
}

// If a MTX was passed in to this function as an argument,
// we assume the caller is constructing a bigger TX and
// may still have more ins/out to add to it.
// That caller will have to call fund() and finalize()
// on their own when they are ready.
if (!finish)
return mtx;
return mtx;
}

// Fill the inputs with unspents
await this.fund(mtx, options, force);
/**
* Build a transaction, fill and finalize without a lock.
* @param {Object} options - See {@link Wallet#fund options}.
* @param {Object[]} options.outputs - See {@link MTX#addOutput}.
* @returns {Promise<MTX>} - MTX with populated inputs and outputs.
*/

async _createTX(options) {
const mtx = this.makeTX(options.outputs);
await this.fill(mtx, options);
return this.finalize(mtx, options);
}

/**
* Build a transaction, fill and finalize with a lock.
* @param {Object} options - See {@link Wallet#fund options}.
* @param {Object[]} options.outputs - See {@link MTX#addOutput}.
* @returns {Promise} - Returns {@link MTX}.
*/

async createTX(options) {
const unlock = await this.fundLock.lock();
try {
return await this._createTX(options);
} finally {
unlock();
}
}

/**
* Make a batch transaction with multiple actions.
* @param {Array} actions
Expand Down Expand Up @@ -3722,14 +3732,10 @@ class Wallet extends EventEmitter {
switch (type) {
case 'NONE':
assert(action.length === 2);
await this.createTX(
{outputs: [{
address: action[0],
value: action[1]
}]},
force,
mtx
);
this.makeTX([{
address: action[0],
value: action[1]
}], mtx);
break;
case 'OPEN':
assert(action.length === 1, 'Bad arguments for OPEN.');
Expand Down Expand Up @@ -3998,7 +4004,7 @@ class Wallet extends EventEmitter {
*/

async _send(options, passphrase) {
const mtx = await this.createTX(options, true);
const mtx = await this._createTX(options);
return this.sendMTX(mtx, passphrase);
}

Expand Down
2 changes: 1 addition & 1 deletion test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ describe('Wallet', function() {
}

assert(err);
assert.equal(err.message, 'At least one output required.');
assert.equal(err.message, 'At least one output is required.');
});

it('should cleanup', async () => {
Expand Down

0 comments on commit 61c1e05

Please sign in to comment.