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 45c6ac1 commit 310273f
Show file tree
Hide file tree
Showing 2 changed files with 304 additions and 16 deletions.
77 changes: 61 additions & 16 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,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 @@ -2284,7 +2284,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 @@ -2528,14 +2528,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(`Register is already pending: ${name}, can't send yet.`);

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 @@ -2612,17 +2617,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 does not own name: ${name}.`);

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

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

if (credit.spent)
throw new Error(`Update is already pending: ${name}, can't send yet.`);

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

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

Expand Down Expand Up @@ -2753,14 +2763,20 @@ 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: ${name}.`);

if (credit.spent) {
throw new Error(`Renewal is already pending: ${name}, can't send yet.`);
}

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: ${name}.`);
Expand Down Expand Up @@ -2968,14 +2984,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: ${name}.`);

if (credit.spent)
throw new Error(`Transfer is already pending: ${name}, can't send yet.`);

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: ${name}.`);
Expand All @@ -2986,6 +3007,9 @@ class Wallet extends EventEmitter {
if (!ns.isClosed(height, network))
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 @@ -3230,14 +3254,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: ${name}.`);

if (credit.spent)
throw new Error(`Finalize is already pending: ${name}, can't send yet.`);

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: ${name}.`);
Expand Down Expand Up @@ -3452,14 +3481,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: ${name}.`);

if (credit.spent)
throw new Error(`Revoke is already pending: ${name}, can't send yet.`);

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

const coin = credit.coin;

// Is local?
if (coin.height < ns.height)
throw new Error(`Wallet does not own name: ${name}.`);
Expand Down Expand Up @@ -4632,6 +4666,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 310273f

Please sign in to comment.