Skip to content

Commit

Permalink
wallet: Have specific error when linked coin is pending.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Oct 4, 2024
1 parent 031871f commit 807cdde
Show file tree
Hide file tree
Showing 2 changed files with 367 additions and 24 deletions.
107 changes: 85 additions & 22 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,7 @@ class Wallet extends EventEmitter {
continue;

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

if (!coin)
continue;
Expand Down Expand Up @@ -2761,7 +2761,7 @@ class Wallet extends EventEmitter {
if (prevout.equals(ns.owner))
continue;

const coin = await this.getCoin(hash, index);
const coin = await this.getUnspentCoin(hash, index);

if (!coin)
continue;
Expand Down Expand Up @@ -3204,14 +3204,19 @@ class Wallet extends EventEmitter {
throw new Error(`Auction not found: "${name}".`);

const { hash, index } = ns.owner;
const coin = await this.getCoin(hash, index);
const credit = await this.getCredit(hash, index);

if (!coin)
if (!credit)
throw new Error(`Wallet did not win the auction: "${name}".`);

if (credit.spent)
throw new Error(`Credit is already pending for: "${name}".`);

if (ns.isExpired(height, network))
throw new Error(`Name has expired: "${name}"!`);

const coin = credit.coin;

// Is local?
if (coin.height < ns.height)
throw new Error(`Wallet did not win the auction: "${name}".`);
Expand Down Expand Up @@ -3262,10 +3267,11 @@ class Wallet extends EventEmitter {
* @private
* @param {String} name
* @param {String?} resourceHex
* @param {Number} [acct]
* @returns {MTX}
*/

async _makeRawRegister(name, resourceHex) {
async _makeRawRegister(name, resourceHex, acct) {
assert(typeof name === 'string');
assert(!resourceHex || typeof resourceHex === 'string');

Expand All @@ -3282,14 +3288,22 @@ class Wallet extends EventEmitter {
throw new Error(`Auction not found: "${name}".`);

const { hash, index } = ns.owner;
const coin = await this.getCoin(hash, index);
const credit = await this.getCredit(hash, index);

if (!coin)
if (!credit)
throw new Error(`Wallet did not win the auction: "${name}".`);

if (credit.spent)
throw new Error(`Credit is already pending for: "${name}".`);

if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
throw new Error(`Account does not own: "${name}".`);

if (ns.isExpired(height, network))
throw new Error(`Name has expired: "${name}"!`);

const coin = credit.coin;

// Is local?
if (coin.height < ns.height)
throw new Error(`Wallet did not win the auction: "${name}".`);
Expand Down Expand Up @@ -3365,14 +3379,19 @@ class Wallet extends EventEmitter {
throw new Error(`Auction not found: "${name}".`);

const { hash, index } = ns.owner;
const coin = await this.getCoin(hash, index);
const credit = await this.getCredit(hash, index);

if (!coin)
if (!credit)
throw new Error(`Wallet does not own: "${name}".`);

if (credit.spent)
throw new Error(`Credit is already pending for: "${name}".`);

if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
throw new Error(`Account does not own: "${name}".`);

const coin = credit.coin;

if (coin.covenant.isReveal() || coin.covenant.isClaim())
return this._makeRegister(name, resource);

Expand Down Expand Up @@ -3419,10 +3438,11 @@ class Wallet extends EventEmitter {
* Make a raw update MTX.
* @param {String} name
* @param {String} resourceHex
* @param {Number} [acct]
* @returns {MTX}
*/

async makeRawUpdate(name, resourceHex) {
async makeRawUpdate(name, resourceHex, acct) {
assert(typeof name === 'string');
assert(typeof resourceHex === 'string');

Expand All @@ -3439,13 +3459,21 @@ class Wallet extends EventEmitter {
throw new Error(`Auction not found: "${name}".`);

const { hash, index } = ns.owner;
const coin = await this.getCoin(hash, index);
const credit = await this.getCredit(hash, index);

if (!coin)
if (!credit)
throw new Error(`Wallet does not own: "${name}".`);

if (credit.spent)
throw new Error(`Credit is already pending for: "${name}".`);

if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
throw new Error(`Account does not own: "${name}".`);

const coin = credit.coin;

if (coin.covenant.isReveal() || coin.covenant.isClaim())
return this._makeRawRegister(name, resourceHex);
return this._makeRawRegister(name, resourceHex, acct);

if (ns.isExpired(height, network))
throw new Error(`Name has expired: "${name}"!`);
Expand Down Expand Up @@ -3512,7 +3540,8 @@ class Wallet extends EventEmitter {
*/

async _createRawUpdate(name, resourceHex, options) {
const mtx = await this.makeRawUpdate(name, resourceHex);
const acct = options ? options.account : null;
const mtx = await this.makeRawUpdate(name, resourceHex, acct);
await this.fill(mtx, options);
return this.finalize(mtx, options);
}
Expand Down Expand Up @@ -3651,14 +3680,19 @@ class Wallet extends EventEmitter {
throw new Error(`Auction not found: "${name}".`);

const { hash, index } = ns.owner;
const coin = await this.getCoin(hash, index);
const credit = await this.getCredit(hash, index);

if (!coin)
if (!credit)
throw new Error(`Wallet does not own: "${name}".`);

if (credit.spent)
throw new Error(`Credit is already pending for: "${name}".`);

if (ns.isExpired(height, network))
throw new Error(`Name has expired: "${name}"!`);

const coin = credit.coin;

// Is local?
if (coin.height < ns.height)
throw new Error(`Wallet does not own: "${name}".`);
Expand Down Expand Up @@ -3895,14 +3929,19 @@ class Wallet extends EventEmitter {
throw new Error(`Auction not found: "${name}".`);

const { hash, index } = ns.owner;
const coin = await this.getCoin(hash, index);
const credit = await this.getCredit(hash, index);

if (!coin)
if (!credit)
throw new Error(`Wallet does not own: "${name}".`);

if (credit.spent)
throw new Error(`Credit is already pending for: "${name}".`);

if (ns.isExpired(height, network))
throw new Error(`Name has expired: "${name}"!`);

const coin = credit.coin;

// Is local?
if (coin.height < ns.height)
throw new Error(`Wallet does not own: "${name}".`);
Expand All @@ -3915,6 +3954,9 @@ class Wallet extends EventEmitter {
if (state !== states.CLOSED)
throw new Error(`Auction is not yet closed: "${name}".`);

if (coin.covenant.isTransfer())
throw new Error(`Name is already being transferred: "${name}".`);

if (!coin.covenant.isRegister()
&& !coin.covenant.isUpdate()
&& !coin.covenant.isRenew()
Expand Down Expand Up @@ -4278,14 +4320,19 @@ class Wallet extends EventEmitter {
throw new Error(`Auction not found: "${name}".`);

const { hash, index } = ns.owner;
const coin = await this.getCoin(hash, index);
const credit = await this.getCredit(hash, index);

if (!coin)
if (!credit)
throw new Error(`Wallet does not own: "${name}".`);

if (credit.spent)
throw new Error(`Credit is already pending for: "${name}".`);

if (ns.isExpired(height, network))
throw new Error(`Name has expired: "${name}"!`);

const coin = credit.coin;

// Is local?
if (coin.height < ns.height)
throw new Error(`Wallet does not own: "${name}".`);
Expand Down Expand Up @@ -4530,14 +4577,19 @@ class Wallet extends EventEmitter {
throw new Error(`Auction not found: "${name}".`);

const { hash, index } = ns.owner;
const coin = await this.getCoin(hash, index);
const credit = await this.getCredit(hash, index);

if (!coin)
if (!credit)
throw new Error(`Wallet does not own: "${name}".`);

if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
throw new Error(`Account does not own: "${name}".`);

if (credit.spent)
throw new Error(`Credit is already pending for: "${name}".`);

const coin = credit.coin;

// Is local?
if (coin.height < ns.height)
throw new Error(`Wallet does not own: "${name}".`);
Expand Down Expand Up @@ -5433,6 +5485,17 @@ class Wallet extends EventEmitter {
return credit.coin;
}

/**
* Get credit from the wallet.
* @param {Hash} hash
* @param {Number} index
* @returns {Promise<Credit>}
*/

getCredit(hash, index) {
return this.txdb.getCredit(hash, index);
}

/**
* Get a transaction from the wallet.
* @param {Hash} hash
Expand Down
Loading

0 comments on commit 807cdde

Please sign in to comment.