Skip to content

Commit

Permalink
Merge 79b91bf into 0f2e73f
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed May 30, 2023
2 parents 0f2e73f + 79b91bf commit 27be59e
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 14 deletions.
108 changes: 95 additions & 13 deletions lib/wallet/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,12 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(tx, passphrase);

res.json(200, tx.getJSON(this.network));
const json = tx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, tx, req.wallet);

res.json(200, json);
});

// Sign TX
Expand Down Expand Up @@ -1081,7 +1086,12 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(mtx, passphrase);

return res.json(200, mtx.getJSON(this.network));
const json = mtx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, mtx, req.wallet);

return res.json(200, json);
});

// Create Bid
Expand Down Expand Up @@ -1110,7 +1120,12 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(mtx, passphrase);

return res.json(200, mtx.getJSON(this.network));
const json = mtx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, mtx, req.wallet);

return res.json(200, json);
});

// Create auction-related transactions in advance (bid and reveal for now)
Expand Down Expand Up @@ -1146,9 +1161,17 @@ class HTTP extends Server {
await req.wallet.sign(auctionTxs.reveal, passphrase);
}

const jsonBid = auctionTxs.bid.getJSON(this.network);
const jsonReveal = auctionTxs.reveal.getJSON(this.network);

if (options.paths) {
await this.addOutputPaths(jsonBid, auctionTxs.bid, req.wallet);
await this.addOutputPaths(jsonReveal, auctionTxs.reveal, req.wallet);
}

return res.json(200, {
bid: auctionTxs.bid.getJSON(this.network),
reveal: auctionTxs.reveal.getJSON(this.network)
bid: jsonBid,
reveal: jsonReveal
});
});

Expand Down Expand Up @@ -1178,7 +1201,12 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(mtx, passphrase);

return res.json(200, mtx.getJSON(this.network));
const json = mtx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, mtx, req.wallet);

return res.json(200, json);
});

// Create Redeem
Expand Down Expand Up @@ -1209,7 +1237,12 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(mtx, passphrase);

return res.json(200, mtx.getJSON(this.network));
const json = mtx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, mtx, req.wallet);

return res.json(200, json);
});

// Create Update
Expand Down Expand Up @@ -1243,7 +1276,12 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(mtx, passphrase);

return res.json(200, mtx.getJSON(this.network));
const json = mtx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, mtx, req.wallet);

return res.json(200, json);
});

// Create Renewal
Expand All @@ -1268,7 +1306,12 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(mtx, passphrase);

return res.json(200, mtx.getJSON(this.network));
const json = mtx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, mtx, req.wallet);

return res.json(200, json);
});

// Create Transfer
Expand Down Expand Up @@ -1296,7 +1339,12 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(mtx, passphrase);

return res.json(200, mtx.getJSON(this.network));
const json = mtx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, mtx, req.wallet);

return res.json(200, json);
});

// Create Cancel
Expand All @@ -1321,7 +1369,12 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(mtx, passphrase);

return res.json(200, mtx.getJSON(this.network));
const json = mtx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, mtx, req.wallet);

return res.json(200, json);
});

// Create Finalize
Expand All @@ -1346,7 +1399,12 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(mtx, passphrase);

return res.json(200, mtx.getJSON(this.network));
const json = mtx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, mtx, req.wallet);

return res.json(200, json);
});

// Create Revoke
Expand All @@ -1371,10 +1429,34 @@ class HTTP extends Server {
if (sign)
await req.wallet.sign(mtx, passphrase);

return res.json(200, mtx.getJSON(this.network));
const json = mtx.getJSON(this.network);

if (options.paths)
await this.addOutputPaths(json, mtx, req.wallet);

return res.json(200, json);
});
}

/**
* Add wallet path information to JSON outputs
* @private
*/

async addOutputPaths(json, tx, wallet) {
for (let i = 0; i < tx.outputs.length; i++) {
const {address} = tx.outputs[i];
const path = await wallet.getPath(address);

if (!path)
continue;

json.outputs[i].path = path.getJSON(this.network);
}

return json;
}

/**
* Initialize websockets.
* @private
Expand Down
46 changes: 45 additions & 1 deletion test/wallet-http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe('Wallet HTTP', function() {
assert.equal(tx.locktime, 0);
});

it('should create a transaction with HD paths', async () => {
it('should create self-send transaction with HD paths', async () => {
const tx = await wallet.createTX({
paths: true,
outputs: [{ address: cbAddress, value: 1e4 }]
Expand All @@ -201,6 +201,50 @@ describe('Wallet HTTP', function() {
assert.ok(typeof path.change === 'boolean');
assert.ok(typeof path.derivation === 'string');
}

// cbAddress is a self-send
// so all output paths including change should be known
for (let i = 0; i < tx.outputs.length; i++) {
const path = tx.outputs[i].path;

assert.ok(typeof path.name === 'string');
assert.ok(typeof path.account === 'number');
assert.ok(typeof path.change === 'boolean');
assert.ok(typeof path.derivation === 'string');
}
});

it('should create a transaction with HD paths', async () => {
const tx = await wallet.createTX({
paths: true,
outputs: [{
address: 'rs1qlf5se77y0xlg5940slyf00djvveskcsvj9sdrd',
value: 1e4
}]
});

assert.ok(tx);
assert.ok(tx.inputs);

for (let i = 0; i < tx.inputs.length; i++) {
const path = tx.inputs[i].path;

assert.ok(typeof path.name === 'string');
assert.ok(typeof path.account === 'number');
assert.ok(typeof path.change === 'boolean');
assert.ok(typeof path.derivation === 'string');
}
{
const path = tx.outputs[1].path; // change
assert.ok(typeof path.name === 'string');
assert.ok(typeof path.account === 'number');
assert.ok(typeof path.change === 'boolean');
assert.ok(typeof path.derivation === 'string');
}
{
const path = tx.outputs[0].path; // receiver
assert(!path);
}
});

it('should create a transaction with a locktime', async () => {
Expand Down

0 comments on commit 27be59e

Please sign in to comment.