Skip to content

Commit

Permalink
wallet : refactoring to stay clear from makeReveal & changing how fil…
Browse files Browse the repository at this point in the history
…l takes specific coins
  • Loading branch information
HDardenne committed Jan 11, 2021
1 parent 08a2f0d commit 1cda4a1
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 76 deletions.
1 change: 1 addition & 0 deletions lib/wallet/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ class HTTP extends Server {
return res.json(200, mtx.getJSON(this.network));
});

// Create auction-related transactions in advance (bid and reveal for now)
this.post('/wallet/:id/auction-txs', async (req, res) => {
const valid = Validator.fromRequest(req);
const name = valid.str('name');
Expand Down
3 changes: 1 addition & 2 deletions lib/wallet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ exports.plugin = require('./plugin');
exports.records = require('./records');
exports.RPC = require('./rpc');
exports.Node = require('./node');
const { TXDB } = require('./txdb');
exports.TXDB = TXDB;
exports.TXDB = require('./txdb');
exports.WalletDB = require('./walletdb');
exports.Wallet = require('./wallet');
exports.WalletKey = require('./walletkey');
5 changes: 1 addition & 4 deletions lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3966,7 +3966,4 @@ class BidReveal extends bio.Struct {
* Expose
*/

exports.TXDB = TXDB;
exports.BlindBid = BlindBid;

module.exports = exports;
module.exports = TXDB;
112 changes: 42 additions & 70 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const base58 = require('bcrypto/lib/encoding/base58');
const bio = require('bufio');
const blake2b = require('bcrypto/lib/blake2b');
const cleanse = require('bcrypto/lib/cleanse');
const { TXDB, BlindBid } = require('./txdb');
const Outpoint = require('../primitives/outpoint');
const TXDB = require('./txdb');
const Path = require('./path');
const common = require('./common');
const Address = require('../primitives/address');
Expand All @@ -39,6 +38,7 @@ const {types} = rules;
const {Mnemonic} = HD;
const {BufferSet} = require('buffer-map');
const Coin = require('../primitives/coin');
const Outpoint = require('../primitives/outpoint');

/*
* Constants
Expand Down Expand Up @@ -1183,7 +1183,7 @@ class Wallet extends EventEmitter {
* @see MTX#fill
*/

async fill(mtx, options, specificCoins) {
async fill(mtx, options) {
if (!options)
options = {};

Expand All @@ -1197,14 +1197,14 @@ class Wallet extends EventEmitter {
if (rate == null)
rate = await this.wdb.estimateFee(options.blocks);

let coins = specificCoins;
if (!coins) {
if (options.smart) {
coins = await this.getSmartCoins(options.account);
} else {
coins = await this.getCoins(options.account);
coins = this.txdb.filterLocked(coins);
}
let coins = options.coins || [];
if (options.smart) {
const smartCoins = await this.getSmartCoins(options.account);
coins = coins.concat(smartCoins);
} else {
let availableCoins = await this.getCoins(options.account);
availableCoins = this.txdb.filterLocked(availableCoins);
coins = coins.concat(availableCoins);
}

await mtx.fund(coins, {
Expand Down Expand Up @@ -1831,6 +1831,7 @@ class Wallet extends EventEmitter {
* @param {Number} value
* @param {Number} lockup
* @param {Object} options
* @returns {{ bid: MTX; reveal: MTX;}} {bid, reveal}
*/

async createAuctionTxs(name, value, lockup, options) {
Expand All @@ -1849,37 +1850,40 @@ class Wallet extends EventEmitter {
* @param {Number} value
* @param {Number} lockup
* @param {Object} options
* @returns {{ bid: MTX; reveal: MTX;}} {bid, reveal}
*/

async _createAuctionTxs(name, value, lockup, options) {
const bid = await this._createBid(name, value, lockup, options);

const bidOuputIndex = bid.outputs.findIndex(o => o.covenant.isBid());
const bidCoin = Coin.fromTX(bid, bidOuputIndex, this.wdb.height);

// Prepare the data needed to make the reveal in advance
const rawName = Buffer.from(name, 'ascii');
const nameHash = rules.hashName(rawName);
const bidBlind = new BlindBid();
bidBlind.name = name;
bidBlind.nameHash = nameHash;
bidBlind.prevout = Outpoint.fromTX(bid, 0);
bidBlind.value = bid;
bidBlind.lockup = lockup;
bidBlind.blind = bid.outputs[0].covenant.getHash(3);
bidBlind.own = true;
const ns = await this.getNameState(nameHash);
const coins = [];
const bidCoin = Coin.fromTX(bid, 0, this.wdb.height);
coins.push(bidCoin);

const acct = options ? options.account : null;
const force = options ? options.force || false : false;
const reveal = await this.makeReveal(
name,
acct,
force,
bidBlind,
bidCoin.address
);
const blind = bid.outputs[bidOuputIndex].covenant.getHash(3);
const bv = await this.getBlind(blind);
if (!bv)
throw new Error('Blind value not found.');
const { nonce } = bv;

await this.fill(reveal, options, coins);
const reveal = new MTX();
const output = new Output();
output.address = bidCoin.address;
output.value = value;
output.covenant.type = types.REVEAL;
output.covenant.pushHash(nameHash);
output.covenant.pushU32(ns.height);
output.covenant.pushHash(nonce);
reveal.addOutpoint(Outpoint.fromTX(bid, bidOuputIndex));
reveal.outputs.push(output);

await this.fill(reveal, {...options, coins: coins});
const finalReveal = await this.finalize(reveal, options);
return { bid, reveal: finalReveal };
}
Expand All @@ -1888,13 +1892,10 @@ class Wallet extends EventEmitter {
* Make a reveal MTX.
* @param {String} name
* @param {(Number|String)?} acct
* @param {Boolean} force
* @param {BlindBid} blindBid
* @param {Address} bidAddress
* @returns {MTX}
*/

async makeReveal(name, acct, force, blindBid, bidAddress) {
async makeReveal(name, acct) {
assert(typeof name === 'string');

if (acct != null) {
Expand All @@ -1905,12 +1906,6 @@ class Wallet extends EventEmitter {
if (!rules.verifyName(name))
throw new Error('Invalid name.');

if (blindBid) {
if (blindBid.name.toString('ascii') !== name) {
throw new Error('Bid is not for this name');
}
}

const rawName = Buffer.from(name, 'ascii');
const nameHash = rules.hashName(rawName);
const ns = await this.getNameState(nameHash);
Expand All @@ -1924,7 +1919,7 @@ class Wallet extends EventEmitter {

const state = ns.state(height, network);

if (!force && state < states.REVEAL)
if (state < states.REVEAL)
throw new Error('Cannot reveal yet.');

if (state > states.REVEAL)
Expand All @@ -1934,36 +1929,17 @@ class Wallet extends EventEmitter {

const mtx = new MTX();

if (blindBid) {
const bv = await this.getBlind(blindBid.blind);
if (!bv)
throw new Error('Blind value not found.');
const { value, nonce } = bv;

const output = new Output();
output.address = bidAddress;
output.value = value;
output.covenant.type = types.REVEAL;
output.covenant.pushHash(blindBid.nameHash);
output.covenant.pushU32(ns.height);
output.covenant.pushHash(nonce);
mtx.addOutpoint(blindBid.prevout);
mtx.outputs.push(output);
} else {
for (const { prevout, own } of bids) {
for (const {prevout, own} of bids) {
if (!own)
continue;

const { hash, index } = prevout;
const {hash, index} = prevout;
const coin = await this.getCoin(hash, index);

if (!coin)
continue;

if (
acct != null &&
!(await this.txdb.hasCoinByAccount(acct, hash, index))
)
if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
continue;

// Is local?
Expand All @@ -1976,7 +1952,7 @@ class Wallet extends EventEmitter {
if (!bv)
throw new Error('Blind value not found.');

const { value, nonce } = bv;
const {value, nonce} = bv;

const output = new Output();
output.address = coin.address;
Expand All @@ -1988,7 +1964,6 @@ class Wallet extends EventEmitter {

mtx.addOutpoint(prevout);
mtx.outputs.push(output);
}
}

if (mtx.outputs.length === 0)
Expand All @@ -2002,15 +1977,12 @@ class Wallet extends EventEmitter {
* MTX without a lock.
* @param {String} name
* @param {Object} options
* @param {BlindBid} blindBid
* @param {Address} bidAddress
* @returns {MTX}
*/

async _createReveal(name, options, bidBlind, bidAddress) {
async _createReveal(name, options) {
const acct = options ? options.account : null;
const force = options ? options.force || false : false;
const mtx = await this.makeReveal(name, acct, force, bidBlind, bidAddress);
const mtx = await this.makeReveal(name, acct);
await this.fill(mtx, options);
return this.finalize(mtx, options);
}
Expand Down
40 changes: 40 additions & 0 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2994,6 +2994,16 @@ describe('Wallet', function() {
await wallet.txdb.add(winningBid, block);
cTXCount++;

const winningBlindFromMtx = winningBid.outputs
.find(o => o.covenant.isBid())
.covenant.getHash(3);
let allBids = await wallet.getBids();
assert.strictEqual(allBids.length, 1);
const winningBlindFromWallet = allBids.find(
b => b.blind.toString('hex') === winningBlindFromMtx.toString('hex')
);
assert(winningBlindFromWallet);

bal = await wallet.getBalance();
assert.strictEqual(bal.tx, 3);
assert.strictEqual(bal.coin, 3);
Expand Down Expand Up @@ -3027,6 +3037,16 @@ describe('Wallet', function() {
await wallet.txdb.add(losingBid, block);
cTXCount++;

const losingBlindFromMtx = losingBid.outputs
.find(o => o.covenant.isBid())
.covenant.getHash(3);
allBids = await wallet.getBids();
assert.strictEqual(allBids.length, 2);
const losingBlindFromWallet = allBids.find(
b => b.blind.toString('hex') === losingBlindFromMtx.toString('hex')
);
assert(losingBlindFromWallet);

// Check
bal = await wallet.getBalance();
assert.strictEqual(bal.tx, 4);
Expand Down Expand Up @@ -3061,6 +3081,16 @@ describe('Wallet', function() {
await wallet.txdb.add(reveal, block);
cTXCount++;

const revealValueFromMtx = reveal.outputs
.find(o => o.covenant.isReveal())
.value;
let allReveals = await wallet.getReveals();
assert.strictEqual(allReveals.length, 1);
const revealFromWallet = allReveals.find(
b => b.value === revealValueFromMtx
);
assert(revealFromWallet);

// Check
bal = await wallet.getBalance();
assert.strictEqual(bal.tx, 5);
Expand All @@ -3082,6 +3112,16 @@ describe('Wallet', function() {
await wallet.txdb.add(reveal2, block2);
cTXCount++;

const reveal2ValueFromMtx = reveal.outputs
.find(o => o.covenant.isReveal())
.value;
allReveals = await wallet.getReveals();
assert.strictEqual(allReveals.length, 2);
const reveal2FromWallet = allReveals.find(
b => b.value === reveal2ValueFromMtx
);
assert(reveal2FromWallet);

// Check
bal = await wallet.getBalance();
assert.strictEqual(bal.tx, 6);
Expand Down

0 comments on commit 1cda4a1

Please sign in to comment.