diff --git a/packages/network/tests/signer/signers/vechain-private-key-signer/vechain-private-key-signer.unit.test.ts b/packages/network/tests/signer/signers/vechain-private-key-signer/vechain-private-key-signer.unit.test.ts index 9154e9cd1..7e74a46d2 100644 --- a/packages/network/tests/signer/signers/vechain-private-key-signer/vechain-private-key-signer.unit.test.ts +++ b/packages/network/tests/signer/signers/vechain-private-key-signer/vechain-private-key-signer.unit.test.ts @@ -159,6 +159,20 @@ describe('Vechain base signer tests', () => { }); describe('resolveName(vnsName)', () => { + test('Should return null if provider is not set', async () => { + const signer = new VechainPrivateKeySigner( + Buffer.from( + '7f9290cc44c5fd2b95fe21d6ad6fe5fa9c177e1cd6f3b4c96a97b13e09eaa158', + 'hex' + ), + null + ); + + const name = 'test-sdk.vet'; + const result = await signer.resolveName(name); + expect(result).toEqual(null); + }); + test('Should use vnsUtils.resolveName() to resolve an address by name', async () => { const signer = new VechainPrivateKeySigner( Buffer.from( diff --git a/packages/network/tests/thor-client/transactions/fixture.ts b/packages/network/tests/thor-client/transactions/fixture.ts index 62b2637a9..4c7340a3e 100644 --- a/packages/network/tests/thor-client/transactions/fixture.ts +++ b/packages/network/tests/thor-client/transactions/fixture.ts @@ -355,6 +355,88 @@ const buildTransactionBodyClausesTestCases = [ reserved: { features: 1 } } } + }, + { + description: + 'Should not modify "to" part of clauses when no name is used', + clauses: [ + { + to: '0x0000000000000000000000000000456E65726779', + value: '0', + data: '0x' + }, + { + to: null, + value: '0', + data: '0x' + }, + { + to: 'vtho.test-sdk.vet', + value: '0', + data: '0x' + } + ], + options: { + gasPriceCoef: 255, + expiration: 1000, + isDelegated: true, + dependsOn: + '0x9140e36f05000508465fd55d70947b99a78c84b3afa5e068b955e366b560935f' // Any valid tx id + }, + expected: { + solo: { + chainTag: 246, + clauses: [ + { + data: '0x', + to: '0x0000000000000000000000000000456E65726779', + value: '0' + }, + { + to: null, + data: '0x', + value: '0' + }, + { + data: '0x', + to: '0x0000000000000000000000000000456E65726779', + value: '0' + } + ], + dependsOn: + '0x9140e36f05000508465fd55d70947b99a78c84b3afa5e068b955e366b560935f', + expiration: 1000, + gas: 100046, + gasPriceCoef: 255, + reserved: { features: 1 } + }, + testnet: { + chainTag: 39, + clauses: [ + { + data: '0x', + to: '0x0000000000000000000000000000456E65726779', + value: '0' + }, + { + to: null, + data: '0x', + value: '0' + }, + { + data: '0x', + to: '0x0000000000000000000000000000456E65726779', + value: '0' + } + ], + dependsOn: + '0x9140e36f05000508465fd55d70947b99a78c84b3afa5e068b955e366b560935f', + expiration: 1000, + gas: 100046, + gasPriceCoef: 255, + reserved: { features: 1 } + } + } } ]; diff --git a/packages/network/tests/utils/vns/lookups.mock.solo.test.ts b/packages/network/tests/utils/vns/lookups.mock.solo.test.ts new file mode 100644 index 000000000..fb2969a4e --- /dev/null +++ b/packages/network/tests/utils/vns/lookups.mock.solo.test.ts @@ -0,0 +1,175 @@ +import { + afterEach, + beforeEach, + describe, + expect, + jest, + test +} from '@jest/globals'; +import { ThorClient, vnsUtils } from '../../../src'; +import { testnetUrl } from '../../fixture'; + +/** + * vnsUtils vet.domains tests + * + * @group unit/utils/vnsUtils + */ +describe('vnsUtils', () => { + /** + * ThorClient instances + */ + let thorClient: ThorClient; + + /** + * Init thor client and provider before each test + */ + beforeEach(() => { + thorClient = ThorClient.fromUrl(testnetUrl); + }); + + /** + * Destroy thor client after each test + */ + afterEach(() => { + thorClient.destroy(); + }); + + describe('resolveNames(string[])', () => { + test('Should use the correct resolveUtils based on the passed thor client', async () => { + const names = ['test-sdk-1.vet', 'test-sdk-2.vet']; + const executeCall = jest.fn(async () => { + return await Promise.reject(new Error('error')); + }); + jest.spyOn( + thorClient.contracts, + 'executeCall' + ).mockImplementationOnce(executeCall); + + await expect( + vnsUtils.resolveNames(thorClient, names) + ).rejects.toThrow(); + expect(executeCall).toHaveBeenCalledWith( + '0xc403b8EA53F707d7d4de095f0A20bC491Cf2bc94', + 'function getAddresses(string[] names) returns (address[] addresses)', + [names] + ); + }); + + test('Should return null if genesisBlock can not be loaded', async () => { + // Mock the getGenesisBlock method to return null + jest.spyOn( + thorClient.blocks, + 'getGenesisBlock' + ).mockResolvedValueOnce(null); + + const addresses = await vnsUtils.resolveNames(thorClient, [ + 'test-sdk.vet' + ]); + expect(addresses).toEqual([null]); + }); + + test('Should return null if genesis has no matching configuration unknown', async () => { + jest.spyOn( + thorClient.blocks, + 'getGenesisBlock' + ).mockResolvedValueOnce({ + number: 0, + id: '0x00000000c05a20fbca2bf6ae3affba6af4a74b800b585bf7a4988aba7aea0000', // different genesis id + size: 170, + parentID: + '0xffffffff00000000000000000000000000000000000000000000000000000000', + timestamp: 1526400000, + gasLimit: 10000000, + beneficiary: '0x0000000000000000000000000000000000000000', + gasUsed: 0, + totalScore: 0, + txsRoot: + '0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0', + txsFeatures: 0, + stateRoot: + '0x93de0ffb1f33bc0af053abc2a87c4af44594f5dcb1cb879dd823686a15d68550', + receiptsRoot: + '0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0', + signer: '0x0000000000000000000000000000000000000000', + isTrunk: true, + transactions: [] + }); + + const addresses = await vnsUtils.resolveNames(thorClient, [ + 'test-sdk.vet' + ]); + expect(addresses).toEqual([null]); + }); + }); + + describe('lookupAddresses(string[])', () => { + test('Should use the correct resolveUtils based on the passed thor client', async () => { + const addresses = [ + '0x0000000000000000000000000000456E65726779', + '0x625fCe8dd8E2C05e82e77847F3da06AF6e55A7AF' + ]; + const executeCall = jest.fn(async () => { + return await Promise.reject(new Error('error')); + }); + jest.spyOn( + thorClient.contracts, + 'executeCall' + ).mockImplementationOnce(executeCall); + + await expect( + vnsUtils.lookupAddresses(thorClient, addresses) + ).rejects.toThrow(); + expect(executeCall).toHaveBeenCalledWith( + '0xc403b8EA53F707d7d4de095f0A20bC491Cf2bc94', + 'function getNames(address[] addresses) returns (string[] names)', + [addresses] + ); + }); + + test('Should return null if genesisBlock can not be loaded', async () => { + // Mock the method to return null + jest.spyOn( + thorClient.blocks, + 'getGenesisBlock' + ).mockResolvedValueOnce(null); + + const names = await vnsUtils.lookupAddresses(thorClient, [ + '0x0000000000000000000000000000000000000000' + ]); + expect(names).toEqual([null]); + }); + + test('Should return null if genesis has no matching configuration unknown', async () => { + jest.spyOn( + thorClient.blocks, + 'getGenesisBlock' + ).mockResolvedValueOnce({ + number: 0, + id: '0x00000000c05a20fbca2bf6ae3affba6af4a74b800b585bf7a4988aba7aea0000', // different genesis id + size: 170, + parentID: + '0xffffffff00000000000000000000000000000000000000000000000000000000', + timestamp: 1526400000, + gasLimit: 10000000, + beneficiary: '0x0000000000000000000000000000000000000000', + gasUsed: 0, + totalScore: 0, + txsRoot: + '0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0', + txsFeatures: 0, + stateRoot: + '0x93de0ffb1f33bc0af053abc2a87c4af44594f5dcb1cb879dd823686a15d68550', + receiptsRoot: + '0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0', + signer: '0x0000000000000000000000000000000000000000', + isTrunk: true, + transactions: [] + }); + + const names = await vnsUtils.lookupAddresses(thorClient, [ + '0x0000000000000000000000000000000000000000' + ]); + expect(names).toEqual([null]); + }); + }); +}); diff --git a/packages/network/tests/utils/vns/lookups.unit.test.ts b/packages/network/tests/utils/vns/lookups.unit.test.ts index 462f3ae92..d6f44a2d2 100644 --- a/packages/network/tests/utils/vns/lookups.unit.test.ts +++ b/packages/network/tests/utils/vns/lookups.unit.test.ts @@ -1,8 +1,3 @@ -/** - * Vechain provider tests - * - * @group integration/providers/vechain-provider - */ import { afterEach, beforeEach, @@ -13,12 +8,11 @@ import { } from '@jest/globals'; import { ThorClient, vnsUtils } from '../../../src'; import { testnetUrl } from '../../fixture'; -// import { addressUtils, TESTNET_NETWORK } from '../../../../core'; /** - * vet.domains Name Service Utilities + * vnsUtils vet.domains tests * - * @group unit/utils/vns + * @group unit/utils/vnsUtils */ describe('vnsUtils', () => { /**