Skip to content

Commit

Permalink
Merge PR #904 from 'nodech/preferred-inputs-bug'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Oct 7, 2024
2 parents 45c6ac1 + 7b5aab1 commit 1b331ee
Show file tree
Hide file tree
Showing 2 changed files with 303 additions and 14 deletions.
73 changes: 59 additions & 14 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(`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 @@ -2612,14 +2617,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(`Credit is already pending for: ${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);

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(`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: ${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(`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: ${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(`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: ${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(`Credit is already pending for: ${name}.`);

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 1b331ee

Please sign in to comment.