Skip to content

Commit

Permalink
wallet-http: put send transaction endpoints behind fund locks.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Aug 28, 2023
1 parent 61ae19c commit a38b102
Show file tree
Hide file tree
Showing 2 changed files with 563 additions and 46 deletions.
136 changes: 90 additions & 46 deletions lib/wallet/http.js
Expand Up @@ -473,6 +473,8 @@ class HTTP extends Server {
const passphrase = valid.str('passphrase');
const sign = valid.bool('sign', true);

// TODO: Add create TX with locks for used Coins and/or
// adds to the pending list.
const options = TransactionOptions.fromValidator(valid);
const tx = await req.wallet.createTX(options);

Expand Down Expand Up @@ -1068,23 +1070,26 @@ class HTTP extends Server {
this.post('/wallet/:id/open', async (req, res) => {
const valid = Validator.fromRequest(req);
const name = valid.str('name');
const passphrase = valid.str('passphrase');
const broadcast = valid.bool('broadcast', true);
const sign = valid.bool('sign', true);

enforce(name, 'Name is required.');
enforce(broadcast ? sign : true, 'Must sign when broadcasting.');

const options = TransactionOptions.fromValidator(valid);
const mtx = await req.wallet.createOpen(name, options);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
// TODO: Add abort signal to close when request closes.
const tx = await req.wallet.sendOpen(name, options);
return res.json(200, tx.getJSON(this.network));
}

// TODO: Add create TX with locks for used Coins and/or
// adds to the pending list.
const mtx = await req.wallet.createOpen(name, options);

if (sign)
await req.wallet.sign(mtx, passphrase);
await req.wallet.sign(mtx, options.passphrase);

const json = mtx.getJSON(this.network);

Expand All @@ -1100,7 +1105,6 @@ class HTTP extends Server {
const name = valid.str('name');
const bid = valid.u64('bid');
const lockup = valid.u64('lockup');
const passphrase = valid.str('passphrase');
const broadcast = valid.bool('broadcast', true);
const sign = valid.bool('sign', true);

Expand All @@ -1110,15 +1114,19 @@ class HTTP extends Server {
enforce(broadcast ? sign : true, 'Must sign when broadcasting.');

const options = TransactionOptions.fromValidator(valid);
const mtx = await req.wallet.createBid(name, bid, lockup, options);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
// TODO: Add abort signal to close when request closes.
const tx = await req.wallet.sendBid(name, bid, lockup, options);
return res.json(200, tx.getJSON(this.network));
}

// TODO: Add create TX with locks for used Coins and/or
// adds to the pending list.
const mtx = await req.wallet.createBid(name, bid, lockup, options);

if (sign)
await req.wallet.sign(mtx, passphrase);
await req.wallet.sign(mtx, options.passphrase);

const json = mtx.getJSON(this.network);

Expand Down Expand Up @@ -1180,29 +1188,39 @@ class HTTP extends Server {
this.post('/wallet/:id/reveal', async (req, res) => {
const valid = Validator.fromRequest(req);
const name = valid.str('name');
const passphrase = valid.str('passphrase');
const broadcast = valid.bool('broadcast', true);
const sign = valid.bool('sign', true);

enforce(broadcast ? sign : true, 'Must sign when broadcasting.');

const options = TransactionOptions.fromValidator(valid);

if (broadcast) {
let tx;

if (name) {
// TODO: Add abort signal to close when request closes.
tx = await req.wallet.sendReveal(name, options);
} else {
// TODO: Add abort signal to close when request closes.
tx = await req.wallet.sendRevealAll(options);
}

return res.json(200, tx.getJSON(this.network));
}

let mtx;

// TODO: Add create TX with locks for used Coins and/or
// adds to the pending list.
if (!name) {
mtx = await req.wallet.createRevealAll(options);
} else {
mtx = await req.wallet.createReveal(name, options);
}

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
return res.json(200, tx.getJSON(this.network));
}

if (sign)
await req.wallet.sign(mtx, passphrase);
await req.wallet.sign(mtx, options.passphrase);

const json = mtx.getJSON(this.network);

Expand All @@ -1216,29 +1234,39 @@ class HTTP extends Server {
this.post('/wallet/:id/redeem', async (req, res) => {
const valid = Validator.fromRequest(req);
const name = valid.str('name');
const passphrase = valid.str('passphrase');
const broadcast = valid.bool('broadcast', true);
const sign = valid.bool('sign', true);

enforce(broadcast ? sign : true, 'Must sign when broadcasting.');

const options = TransactionOptions.fromValidator(valid);

if (broadcast) {
let tx;

if (name) {
// TODO: Add abort signal to close when request closes.
tx = await req.wallet.sendRedeem(name, options);
} else {
// TODO: Add abort signal to close when request closes.
tx = await req.wallet.sendRedeemAll(options);
}

return res.json(200, tx.getJSON(this.network));
}

let mtx;

// TODO: Add create TX with locks for used Coins and/or
// adds to the pending list.
if (!name) {
mtx = await req.wallet.createRedeemAll(options);
} else {
mtx = await req.wallet.createRedeem(name, options);
}

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
return res.json(200, tx.getJSON(this.network));
}

if (sign)
await req.wallet.sign(mtx, passphrase);
await req.wallet.sign(mtx, options.passphrase);

const json = mtx.getJSON(this.network);

Expand All @@ -1253,7 +1281,6 @@ class HTTP extends Server {
const valid = Validator.fromRequest(req);
const name = valid.str('name');
const data = valid.obj('data');
const passphrase = valid.str('passphrase');
const broadcast = valid.bool('broadcast', true);
const sign = valid.bool('sign', true);

Expand All @@ -1269,15 +1296,19 @@ class HTTP extends Server {
}

const options = TransactionOptions.fromValidator(valid);
const mtx = await req.wallet.createUpdate(name, resource, options);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
// TODO: Add abort signal to close when request closes.
const tx = await req.wallet.sendUpdate(name, resource, options);
return res.json(200, tx.getJSON(this.network));
}

// TODO: Add create TX with locks for used Coins and/or
// adds to the pending list.
const mtx = await req.wallet.createUpdate(name, resource, options);

if (sign)
await req.wallet.sign(mtx, passphrase);
await req.wallet.sign(mtx, options.passphrase);

const json = mtx.getJSON(this.network);

Expand All @@ -1291,23 +1322,24 @@ class HTTP extends Server {
this.post('/wallet/:id/renewal', async (req, res) => {
const valid = Validator.fromRequest(req);
const name = valid.str('name');
const passphrase = valid.str('passphrase');
const broadcast = valid.bool('broadcast', true);
const sign = valid.bool('sign', true);

enforce(broadcast ? sign : true, 'Must sign when broadcasting.');
enforce(name, 'Must pass name.');

const options = TransactionOptions.fromValidator(valid);
const mtx = await req.wallet.createRenewal(name, options);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
// TODO: Add abort signal to close when request closes.
const tx = await req.wallet.sendRenewal(name, options);
return res.json(200, tx.getJSON(this.network));
}

const mtx = await req.wallet.createRenewal(name, options);

if (sign)
await req.wallet.sign(mtx, passphrase);
await req.wallet.sign(mtx, options.passphrase);

const json = mtx.getJSON(this.network);

Expand All @@ -1322,7 +1354,6 @@ class HTTP extends Server {
const valid = Validator.fromRequest(req);
const name = valid.str('name');
const address = valid.str('address');
const passphrase = valid.str('passphrase');
const broadcast = valid.bool('broadcast', true);
const sign = valid.bool('sign', true);

Expand All @@ -1332,15 +1363,19 @@ class HTTP extends Server {

const addr = Address.fromString(address, this.network);
const options = TransactionOptions.fromValidator(valid);
const mtx = await req.wallet.createTransfer(name, addr, options);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
// TODO: Add abort signal to close when request closes.
const tx = await req.wallet.sendTransfer(name, addr, options);
return res.json(200, tx.getJSON(this.network));
}

// TODO: Add create TX with locks for used Coins and/or
// adds to the pending list.
const mtx = await req.wallet.createTransfer(name, addr, options);

if (sign)
await req.wallet.sign(mtx, passphrase);
await req.wallet.sign(mtx, options.passphrase);

const json = mtx.getJSON(this.network);

Expand All @@ -1354,23 +1389,26 @@ class HTTP extends Server {
this.post('/wallet/:id/cancel', async (req, res) => {
const valid = Validator.fromRequest(req);
const name = valid.str('name');
const passphrase = valid.str('passphrase');
const broadcast = valid.bool('broadcast', true);
const sign = valid.bool('sign', true);

enforce(broadcast ? sign : true, 'Must sign when broadcasting.');
enforce(name, 'Must pass name.');

const options = TransactionOptions.fromValidator(valid);
const mtx = await req.wallet.createCancel(name, options);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
// TODO: Add abort signal to close when request closes.
const tx = await req.wallet.sendCancel(name, options);
return res.json(200, tx.getJSON(this.network));
}

// TODO: Add create TX with locks for used Coins and/or
// adds to the pending list.
const mtx = await req.wallet.createCancel(name, options);

if (sign)
await req.wallet.sign(mtx, passphrase);
await req.wallet.sign(mtx, options.passphrase);

const json = mtx.getJSON(this.network);

Expand All @@ -1384,23 +1422,24 @@ class HTTP extends Server {
this.post('/wallet/:id/finalize', async (req, res) => {
const valid = Validator.fromRequest(req);
const name = valid.str('name');
const passphrase = valid.str('passphrase');
const broadcast = valid.bool('broadcast', true);
const sign = valid.bool('sign', true);

enforce(broadcast ? sign : true, 'Must sign when broadcasting.');
enforce(name, 'Must pass name.');

const options = TransactionOptions.fromValidator(valid);
const mtx = await req.wallet.createFinalize(name, options);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
// TODO: Add abort signal to close when request closes.
const tx = await req.wallet.sendFinalize(name, options);
return res.json(200, tx.getJSON(this.network));
}

const mtx = await req.wallet.createFinalize(name, options);

if (sign)
await req.wallet.sign(mtx, passphrase);
await req.wallet.sign(mtx, options.passphrase);

const json = mtx.getJSON(this.network);

Expand All @@ -1414,23 +1453,26 @@ class HTTP extends Server {
this.post('/wallet/:id/revoke', async (req, res) => {
const valid = Validator.fromRequest(req);
const name = valid.str('name');
const passphrase = valid.str('passphrase');
const broadcast = valid.bool('broadcast', true);
const sign = valid.bool('sign', true);

enforce(broadcast ? sign : true, 'Must sign when broadcasting.');
enforce(name, 'Must pass name.');

const options = TransactionOptions.fromValidator(valid);
const mtx = await req.wallet.createRevoke(name, options);

if (broadcast) {
const tx = await req.wallet.sendMTX(mtx, passphrase);
// TODO: Add abort signal to close when request closes.
const tx = await req.wallet.sendRevoke(name, options);
return res.json(200, tx.getJSON(this.network));
}

// TODO: Add create TX with locks for used Coins and/or
// adds to the pending list.
const mtx = await req.wallet.createRevoke(name, options);

if (sign)
await req.wallet.sign(mtx, passphrase);
await req.wallet.sign(mtx, options.passphrase);

const json = mtx.getJSON(this.network);

Expand Down Expand Up @@ -1814,6 +1856,8 @@ class TransactionOptions {
this.subtractIndex = valid.i32('subtractIndex');
this.depth = valid.u32(['confirmations', 'depth']);
this.paths = valid.bool('paths');
this.passphrase = valid.str('passphrase');
this.hardFee = valid.u64('hardFee'),
this.outputs = [];

if (valid.has('outputs')) {
Expand Down

0 comments on commit a38b102

Please sign in to comment.