Skip to content

Commit 8641a62

Browse files
authored
Merge branch 'master' into feat/facade-send-transaction-methods
2 parents 484539f + 081fb51 commit 8641a62

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

__tests__/integration/hathorwallet_facade.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,61 @@ describe('sendManyOutputsTransaction', () => {
17361736
});
17371737
});
17381738

1739+
describe('authority utxo selection', () => {
1740+
afterEach(async () => {
1741+
await stopAllWallets();
1742+
await GenesisWalletHelper.clearListeners();
1743+
});
1744+
1745+
it('getMintAuthority', async () => {
1746+
// Setting up the custom token
1747+
const hWallet = await generateWalletHelper();
1748+
await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 1);
1749+
const { hash: tokenUid } = await createTokenHelper(hWallet, 'Token to test', 'ATST', 100);
1750+
1751+
// Mark mint authority as selected_as_input
1752+
const [mintInput] = await hWallet.getMintAuthority(tokenUid, { many: false });
1753+
await hWallet.markUtxoSelected(mintInput.txId, mintInput.index, true);
1754+
1755+
// getMintAuthority should return even if the utxo is already selected_as_input
1756+
await expect(hWallet.getMintAuthority(tokenUid, { many: false })).resolves.toStrictEqual([
1757+
mintInput,
1758+
]);
1759+
await expect(
1760+
hWallet.getMintAuthority(tokenUid, { many: false, only_available_utxos: false })
1761+
).resolves.toStrictEqual([mintInput]);
1762+
1763+
// getMintAuthority should not return selected_as_input utxos if only_available_utxos is true
1764+
await expect(
1765+
hWallet.getMintAuthority(tokenUid, { many: false, only_available_utxos: true })
1766+
).resolves.toStrictEqual([]);
1767+
});
1768+
1769+
it('getMeltAuthority', async () => {
1770+
// Setting up the custom token
1771+
const hWallet = await generateWalletHelper();
1772+
await GenesisWalletHelper.injectFunds(hWallet, await hWallet.getAddressAtIndex(0), 1);
1773+
const { hash: tokenUid } = await createTokenHelper(hWallet, 'Token to test', 'ATST', 100);
1774+
1775+
// Mark melt authority as selected_as_input
1776+
const [meltInput] = await hWallet.getMeltAuthority(tokenUid, { many: false });
1777+
await hWallet.markUtxoSelected(meltInput.txId, meltInput.index, true);
1778+
1779+
// getMeltAuthority should return even if the utxo is already selected_as_input
1780+
await expect(hWallet.getMeltAuthority(tokenUid, { many: false })).resolves.toStrictEqual([
1781+
meltInput,
1782+
]);
1783+
await expect(
1784+
hWallet.getMeltAuthority(tokenUid, { many: false, only_available_utxos: false })
1785+
).resolves.toStrictEqual([meltInput]);
1786+
1787+
// getMeltAuthority should not return selected_as_input utxos if only_available_utxos is true
1788+
await expect(
1789+
hWallet.getMeltAuthority(tokenUid, { many: false, only_available_utxos: true })
1790+
).resolves.toStrictEqual([]);
1791+
});
1792+
});
1793+
17391794
describe('createNewToken', () => {
17401795
afterEach(async () => {
17411796
await stopAllWallets();

src/new/wallet.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,8 +1753,9 @@ class HathorWallet extends EventEmitter {
17531753
* Get mint authorities
17541754
*
17551755
* @param {string} tokenUid UID of the token to select the authority utxo
1756-
* @param [options] Object with custom options.
1756+
* @param {Object} [options] Object with custom options.
17571757
* @param {boolean} [options.many=false] if should return many utxos or just one (default false)
1758+
* @param {boolean} [options.only_available_utxos=false] If we should filter for available utxos.
17581759
*
17591760
* @return {Promise<{
17601761
* txId: string,
@@ -1769,6 +1770,7 @@ class HathorWallet extends EventEmitter {
17691770
const newOptions = {
17701771
token: tokenUid,
17711772
authorities: 1, // mint authority
1773+
only_available_utxos: options.only_available_utxos ?? false,
17721774
};
17731775
if (!options.many) {
17741776
// limit number of utxos to select if many is false
@@ -1785,8 +1787,9 @@ class HathorWallet extends EventEmitter {
17851787
* Get melt authorities
17861788
*
17871789
* @param {string} tokenUid UID of the token to select the authority utxo
1788-
* @param [options] Object with custom options.
1790+
* @param {Object} [options] Object with custom options.
17891791
* @param {boolean} [options.many=false] if should return many utxos or just one (default false)
1792+
* @param {boolean} [options.only_available_utxos=false] If we should filter for available utxos.
17901793
*
17911794
* @return {Promise<{
17921795
* txId: string,
@@ -1801,6 +1804,7 @@ class HathorWallet extends EventEmitter {
18011804
const newOptions = {
18021805
token: tokenUid,
18031806
authorities: 2, // melt authority
1807+
only_available_utxos: options.only_available_utxos ?? false,
18041808
};
18051809
if (!options.many) {
18061810
// limit number of utxos to select if many is false
@@ -1873,7 +1877,10 @@ class HathorWallet extends EventEmitter {
18731877

18741878
const mintAddress = newOptions.address || (await this.getCurrentAddress()).address;
18751879

1876-
const mintInput = await this.getMintAuthority(tokenUid, { many: false });
1880+
const mintInput = await this.getMintAuthority(tokenUid, {
1881+
many: false,
1882+
only_available_utxos: true,
1883+
});
18771884

18781885
if (!mintInput || mintInput.length === 0) {
18791886
throw new Error("Don't have mint authority output available.");
@@ -1990,7 +1997,10 @@ class HathorWallet extends EventEmitter {
19901997
}
19911998
}
19921999

1993-
const meltInput = await this.getMeltAuthority(tokenUid, { many: false });
2000+
const meltInput = await this.getMeltAuthority(tokenUid, {
2001+
many: false,
2002+
only_available_utxos: true,
2003+
});
19942004

19952005
if (!meltInput || meltInput.length === 0) {
19962006
throw new Error("Don't have melt authority output available.");
@@ -2083,9 +2093,15 @@ class HathorWallet extends EventEmitter {
20832093
const { createAnother } = newOptions;
20842094
let delegateInput;
20852095
if (type === 'mint') {
2086-
delegateInput = await this.getMintAuthority(tokenUid, { many: false });
2096+
delegateInput = await this.getMintAuthority(tokenUid, {
2097+
many: false,
2098+
only_available_utxos: true,
2099+
});
20872100
} else if (type === 'melt') {
2088-
delegateInput = await this.getMeltAuthority(tokenUid, { many: false });
2101+
delegateInput = await this.getMeltAuthority(tokenUid, {
2102+
many: false,
2103+
only_available_utxos: true,
2104+
});
20892105
} else {
20902106
throw new Error('This should never happen.');
20912107
}
@@ -2182,9 +2198,15 @@ class HathorWallet extends EventEmitter {
21822198
}
21832199
let destroyInputs;
21842200
if (type === 'mint') {
2185-
destroyInputs = await this.getMintAuthority(tokenUid, { many: true });
2201+
destroyInputs = await this.getMintAuthority(tokenUid, {
2202+
many: true,
2203+
only_available_utxos: true,
2204+
});
21862205
} else if (type === 'melt') {
2187-
destroyInputs = await this.getMeltAuthority(tokenUid, { many: true });
2206+
destroyInputs = await this.getMeltAuthority(tokenUid, {
2207+
many: true,
2208+
only_available_utxos: true,
2209+
});
21882210
} else {
21892211
throw new Error('This should never happen.');
21902212
}

0 commit comments

Comments
 (0)