diff --git a/src/utxo.js b/src/utxo.js index 1a1e97e..96d0da5 100644 --- a/src/utxo.js +++ b/src/utxo.js @@ -37,6 +37,11 @@ class UTXO { * - groupTokens: [] - NFT Group tokens, used to create NFT tokens. * - groupMintBatons: [] - Minting baton to create more NFT Group tokens. * + * Note: You can pass in an optional second Boolean argument. The default + * `false` will use the normal waterfall validation method. Set to `true`, + * SLP UTXOs will be validated with the whitelist filtered SLPDB. This will + * result is many more UTXOs in the `nullUtxos` array. + * * @apiExample Example usage: * (async () => { * try { @@ -176,7 +181,7 @@ class UTXO { * * */ - async get (address) { + async get (address, useWhitelist = false) { try { // Convert address to an array if it is a string. if (typeof address === 'string') address = [address] @@ -192,7 +197,12 @@ class UTXO { // console.log(`utxoData: ${JSON.stringify(utxoData, null, 2)}`) // Hydate the utxos with token information. - const hydratedUtxos = await this.slp.Utils.hydrateUtxos(utxoData.utxos) + let hydratedUtxos + if (useWhitelist) { + hydratedUtxos = await this.slp.Utils.hydrateUtxosWL(utxoData.utxos) + } else { + hydratedUtxos = await this.slp.Utils.hydrateUtxos(utxoData.utxos) + } // console.log(`hydratedUtxos: ${JSON.stringify(hydratedUtxos, null, 2)}`) const retAry = [] // Return array diff --git a/test/integration/chains/bchn/utxo-integration.js b/test/integration/chains/bchn/utxo-integration.js index b05305d..771d67a 100644 --- a/test/integration/chains/bchn/utxo-integration.js +++ b/test/integration/chains/bchn/utxo-integration.js @@ -69,6 +69,26 @@ describe('#UTXO', () => { assert.isArray(result[0].slpUtxos.nft.groupTokens) assert.isArray(result[0].slpUtxos.nft.tokens) }) + + it('should use the whitelist when flag is set', async () => { + const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9' + const useWhitelist = true + + const result = await bchjs.Utxo.get(addr, useWhitelist) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], 'address') + assert.property(result[0], 'bchUtxos') + assert.property(result[0], 'nullUtxos') + assert.property(result[0], 'slpUtxos') + assert.isArray(result[0].bchUtxos) + assert.isArray(result[0].nullUtxos) + + // Most token UTXOs should end up in the nullUtxos array. + assert.isAbove(result[0].bchUtxos.length, 0) + assert.isAbove(result[0].nullUtxos.length, 1) + }) }) }) diff --git a/test/unit/utxo-unit.js b/test/unit/utxo-unit.js index 589fbe9..a331f38 100644 --- a/test/unit/utxo-unit.js +++ b/test/unit/utxo-unit.js @@ -122,5 +122,27 @@ describe('#utxo', () => { assert.isArray(result[0].slpUtxos.nft.groupTokens) assert.isArray(result[0].slpUtxos.nft.tokens) }) + + it('should use the whitelist when flag is set', async () => { + // Mock dependencies. + sandbox.stub(bchjs.Utxo.electrumx, 'utxo').resolves(mockData.mockUtxoData) + sandbox + .stub(bchjs.Utxo.slp.Utils, 'hydrateUtxosWL') + .resolves(mockData.mockHydratedUtxos) + + const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9' + const useWhitelist = true + + const result = await bchjs.Utxo.get(addr, useWhitelist) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.isArray(result) + assert.property(result[0], 'address') + assert.property(result[0], 'bchUtxos') + assert.property(result[0], 'nullUtxos') + assert.property(result[0], 'slpUtxos') + assert.isArray(result[0].bchUtxos) + assert.isArray(result[0].nullUtxos) + }) }) })