2022-04-05 09:20:12 +00:00
|
|
|
import assert from 'assert';
|
2022-04-08 05:29:03 +00:00
|
|
|
import path from 'path';
|
2022-04-05 09:20:12 +00:00
|
|
|
|
|
|
|
import { Account } from './account';
|
2022-04-05 05:31:10 +00:00
|
|
|
import { Registry } from './index';
|
2022-04-14 04:34:52 +00:00
|
|
|
import { ensureUpdatedConfig, getConfig } from './testing/helper';
|
2022-04-08 05:29:03 +00:00
|
|
|
|
2023-03-06 21:54:02 +00:00
|
|
|
const WATCHER_YML_PATH = path.join(__dirname, './testing/data/watcher.yml');
|
2022-04-05 05:31:10 +00:00
|
|
|
|
|
|
|
jest.setTimeout(120 * 1000);
|
|
|
|
|
2022-04-12 10:54:26 +00:00
|
|
|
const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
|
2022-04-05 05:31:10 +00:00
|
|
|
|
|
|
|
const namingTests = () => {
|
|
|
|
let registry: Registry;
|
2022-04-08 10:32:24 +00:00
|
|
|
|
2022-04-05 05:31:10 +00:00
|
|
|
let bondId: string;
|
2022-04-08 05:29:03 +00:00
|
|
|
let watcher: any;
|
|
|
|
let watcherId: string;
|
2022-04-08 10:32:24 +00:00
|
|
|
|
2022-04-05 05:31:10 +00:00
|
|
|
let authorityName: string;
|
2022-04-08 10:32:24 +00:00
|
|
|
let otherAuthorityName: string;
|
|
|
|
let otherPrivateKey: string;
|
|
|
|
|
2022-04-20 07:38:57 +00:00
|
|
|
let crn: string;
|
2022-04-05 05:31:10 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-12-09 08:55:13 +00:00
|
|
|
registry = new Registry(gqlEndpoint, restEndpoint, chainId);
|
2022-04-05 05:31:10 +00:00
|
|
|
|
|
|
|
// Create bond.
|
2022-04-12 10:54:26 +00:00
|
|
|
bondId = await registry.getNextBondId(privateKey);
|
2023-02-10 19:44:23 +00:00
|
|
|
await registry.createBond({ denom: 'aphoton', amount: '2000000000' }, privateKey, fee);
|
2022-04-08 05:29:03 +00:00
|
|
|
|
2022-04-19 10:24:41 +00:00
|
|
|
// Create watcher.
|
2022-04-13 12:07:24 +00:00
|
|
|
watcher = await ensureUpdatedConfig(WATCHER_YML_PATH);
|
2022-04-20 11:54:37 +00:00
|
|
|
const result = await registry.setRecord(
|
2022-04-08 05:29:03 +00:00
|
|
|
{
|
|
|
|
privateKey,
|
|
|
|
bondId,
|
|
|
|
record: watcher.record
|
|
|
|
},
|
|
|
|
privateKey,
|
|
|
|
fee
|
|
|
|
)
|
|
|
|
|
2022-04-20 11:54:37 +00:00
|
|
|
watcherId = result.data.id;
|
2022-04-05 05:31:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Reserve authority.', async () => {
|
2022-09-28 19:22:57 +00:00
|
|
|
authorityName = `laconic-${Date.now()}`;
|
2022-04-12 10:54:26 +00:00
|
|
|
await registry.reserveAuthority({ name: authorityName }, privateKey, fee);
|
2022-04-05 05:31:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Lookup authority.', async () => {
|
|
|
|
const [record] = await registry.lookupAuthorities([authorityName]);
|
|
|
|
|
|
|
|
expect(record).toBeDefined();
|
|
|
|
expect(record.ownerAddress).not.toBe('');
|
|
|
|
expect(record.ownerPublicKey).not.toBe('');
|
|
|
|
expect(Number(record.height)).toBeGreaterThan(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Lookup non existing authority', async () => {
|
|
|
|
const [record] = await registry.lookupAuthorities(['does-not-exist']);
|
|
|
|
|
|
|
|
expect(record.ownerAddress).toBe('');
|
|
|
|
expect(record.ownerPublicKey).toBe('');
|
|
|
|
expect(Number(record.height)).toBe(0);
|
|
|
|
});
|
2022-04-05 09:20:12 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
test('Reserve already reserved authority', async () => {
|
2022-04-12 10:54:26 +00:00
|
|
|
await expect(registry.reserveAuthority({ name: authorityName }, privateKey, fee)).rejects.toThrow('Name already reserved.');
|
2022-04-05 09:20:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Reserve sub-authority.', async () => {
|
|
|
|
const subAuthority = `echo.${authorityName}`;
|
2022-04-12 10:54:26 +00:00
|
|
|
await registry.reserveAuthority({ name: subAuthority }, privateKey, fee);
|
2022-04-05 09:20:12 +00:00
|
|
|
|
|
|
|
const [record] = await registry.lookupAuthorities([subAuthority]);
|
|
|
|
expect(record).toBeDefined();
|
|
|
|
expect(record.ownerAddress).not.toBe('');
|
|
|
|
expect(record.ownerPublicKey).not.toBe('');
|
|
|
|
expect(Number(record.height)).toBeGreaterThan(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Reserve sub-authority with different owner.', async () => {
|
|
|
|
// Create another account, send tx to set public key on the account.
|
|
|
|
const mnenonic1 = Account.generateMnemonic();
|
|
|
|
const otherAccount1 = await Account.generateFromMnemonic(mnenonic1);
|
2022-04-12 10:54:26 +00:00
|
|
|
await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccount1.formattedCosmosAddress }, privateKey, fee);
|
2022-04-05 09:20:12 +00:00
|
|
|
|
|
|
|
const mnenonic2 = Account.generateMnemonic();
|
|
|
|
const otherAccount2 = await Account.generateFromMnemonic(mnenonic2);
|
2022-04-12 10:54:26 +00:00
|
|
|
await registry.sendCoins({ denom: 'aphoton', amount: '10', destinationAddress: otherAccount2.formattedCosmosAddress }, otherAccount1.getPrivateKey(), fee);
|
2022-04-05 09:20:12 +00:00
|
|
|
|
|
|
|
const subAuthority = `halo.${authorityName}`;
|
2022-04-12 10:54:26 +00:00
|
|
|
await registry.reserveAuthority({ name: subAuthority, owner: otherAccount1.formattedCosmosAddress }, privateKey, fee);
|
2022-04-05 09:20:12 +00:00
|
|
|
|
|
|
|
const [record] = await registry.lookupAuthorities([subAuthority]);
|
|
|
|
expect(record).toBeDefined();
|
|
|
|
expect(record.ownerAddress).toBeDefined();
|
2022-04-25 09:42:46 +00:00
|
|
|
expect(record.ownerAddress).toBe(otherAccount1.getCosmosAddress());
|
2022-04-05 09:20:12 +00:00
|
|
|
expect(record.ownerPublicKey).toBeDefined();
|
|
|
|
expect(Number(record.height)).toBeGreaterThan(0);
|
|
|
|
});
|
2022-04-08 05:29:03 +00:00
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
test('Set name for unbonded authority', async () => {
|
2022-04-20 07:38:57 +00:00
|
|
|
crn = `crn://${authorityName}/app/test`;
|
2022-04-08 05:29:03 +00:00
|
|
|
assert(watcherId)
|
2022-04-22 10:15:47 +00:00
|
|
|
await expect(registry.setName({ crn, cid: watcherId }, privateKey, fee)).rejects.toThrow('Authority bond not found.');
|
2022-04-08 05:29:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Set authority bond', async () => {
|
2022-04-12 10:54:26 +00:00
|
|
|
await registry.setAuthorityBond({ name: authorityName, bondId }, privateKey, fee);
|
2022-04-08 05:29:03 +00:00
|
|
|
});
|
2022-04-08 10:32:24 +00:00
|
|
|
|
|
|
|
test('Set name', async () => {
|
2022-04-20 07:38:57 +00:00
|
|
|
crn = `crn://${authorityName}/app/test`;
|
2022-04-22 10:15:47 +00:00
|
|
|
await registry.setName({ crn, cid: watcherId }, privateKey, fee);
|
2022-04-08 10:32:24 +00:00
|
|
|
|
2022-04-20 07:38:57 +00:00
|
|
|
// Query records should return it (some CRN points to it).
|
2023-03-06 21:54:02 +00:00
|
|
|
const records = await registry.queryRecords({ type: 'WebsiteRegistrationRecord', version: watcher.record.version });
|
2022-04-08 10:32:24 +00:00
|
|
|
expect(records).toBeDefined();
|
2022-04-13 12:07:24 +00:00
|
|
|
expect(records).toHaveLength(1);
|
2022-04-08 10:32:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Lookup name', async () => {
|
2022-04-20 07:38:57 +00:00
|
|
|
const records = await registry.lookupNames([crn]);
|
2022-04-08 10:32:24 +00:00
|
|
|
expect(records).toBeDefined();
|
|
|
|
expect(records).toHaveLength(1);
|
|
|
|
|
|
|
|
const [{ latest, history }] = records;
|
|
|
|
expect(latest).toBeDefined();
|
|
|
|
expect(latest.id).toBeDefined();
|
|
|
|
expect(latest.id).toBe(watcherId);
|
|
|
|
expect(latest.height).toBeDefined();
|
|
|
|
expect(history).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Resolve name', async () => {
|
2022-04-20 07:38:57 +00:00
|
|
|
const records = await registry.resolveNames([crn]);
|
2022-04-08 10:32:24 +00:00
|
|
|
expect(records).toBeDefined();
|
|
|
|
expect(records).toHaveLength(1);
|
|
|
|
|
|
|
|
const [{ attributes }] = records;
|
2022-04-08 12:32:47 +00:00
|
|
|
expect(attributes).toEqual(watcher.record);
|
2022-04-08 10:32:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Lookup name with history', async () => {
|
2022-04-13 12:07:24 +00:00
|
|
|
const updatedWatcher = await ensureUpdatedConfig(WATCHER_YML_PATH);
|
2022-04-20 11:54:37 +00:00
|
|
|
const result = await registry.setRecord(
|
2022-04-08 10:32:24 +00:00
|
|
|
{
|
|
|
|
privateKey,
|
|
|
|
bondId,
|
|
|
|
record: updatedWatcher.record
|
|
|
|
},
|
|
|
|
privateKey,
|
|
|
|
fee
|
|
|
|
)
|
|
|
|
|
2022-04-20 11:54:37 +00:00
|
|
|
const updatedWatcherId = result.data.id;
|
2022-04-22 10:15:47 +00:00
|
|
|
await registry.setName({ crn, cid: updatedWatcherId }, privateKey, fee);
|
2022-04-08 10:32:24 +00:00
|
|
|
|
2022-04-20 07:38:57 +00:00
|
|
|
const records = await registry.lookupNames([crn], true);
|
2022-04-08 10:32:24 +00:00
|
|
|
expect(records).toHaveLength(1);
|
|
|
|
|
|
|
|
const [{ latest, history }] = records;
|
|
|
|
expect(latest).toBeDefined();
|
|
|
|
expect(latest.id).toBeDefined();
|
|
|
|
expect(latest.id).toBe(updatedWatcherId);
|
|
|
|
expect(latest.height).toBeDefined();
|
|
|
|
expect(history).toBeDefined();
|
|
|
|
expect(history).toHaveLength(1);
|
|
|
|
|
|
|
|
const [oldRecord] = history;
|
|
|
|
expect(oldRecord).toBeDefined();
|
|
|
|
expect(oldRecord.id).toBeDefined();
|
|
|
|
expect(oldRecord.id).toBe(watcherId);
|
|
|
|
expect(oldRecord.height).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2022-04-11 06:58:14 +00:00
|
|
|
test('Set name without reserving authority', async () => {
|
2022-04-20 07:38:57 +00:00
|
|
|
await expect(registry.setName({ crn: 'crn://not-reserved/app/test', cid: watcherId }, privateKey, fee))
|
2022-04-08 10:32:24 +00:00
|
|
|
.rejects.toThrow('Name authority not found.');
|
|
|
|
});
|
|
|
|
|
2022-04-12 10:54:26 +00:00
|
|
|
test('Set name for non-owned authority', async () => {
|
2022-04-08 10:32:24 +00:00
|
|
|
// Create another account.
|
|
|
|
const mnenonic = Account.generateMnemonic();
|
|
|
|
const otherAccount = await Account.generateFromMnemonic(mnenonic);
|
2022-04-12 10:54:26 +00:00
|
|
|
await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccount.formattedCosmosAddress }, privateKey, fee);
|
2022-04-08 10:32:24 +00:00
|
|
|
|
|
|
|
// Other account reserves an authority.
|
|
|
|
otherAuthorityName = `other-${Date.now()}`;
|
|
|
|
otherPrivateKey = otherAccount.privateKey.toString('hex');
|
2022-04-12 10:54:26 +00:00
|
|
|
await registry.reserveAuthority({ name: otherAuthorityName }, otherPrivateKey, fee);
|
2022-04-08 10:32:24 +00:00
|
|
|
|
|
|
|
// Try setting name under other authority.
|
2022-04-20 07:38:57 +00:00
|
|
|
await expect(registry.setName({ crn: `crn://${otherAuthorityName}/app/test`, cid: watcherId }, privateKey, fee)).rejects.toThrow('Access denied.');
|
2022-04-08 10:32:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Lookup non existing name', async () => {
|
2022-04-20 07:38:57 +00:00
|
|
|
const records = await registry.lookupNames(['crn://not-reserved/app/test']);
|
2022-04-08 10:32:24 +00:00
|
|
|
expect(records).toBeDefined();
|
|
|
|
expect(records).toHaveLength(1);
|
|
|
|
const [record] = records;
|
|
|
|
expect(record).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Resolve non existing name', async () => {
|
2022-04-20 07:38:57 +00:00
|
|
|
const records = await registry.resolveNames(['crn://not-reserved/app/test']);
|
2022-04-08 10:32:24 +00:00
|
|
|
expect(records).toBeDefined();
|
|
|
|
expect(records).toHaveLength(1);
|
|
|
|
const [record] = records;
|
|
|
|
expect(record).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Delete name', async () => {
|
2022-04-22 10:15:47 +00:00
|
|
|
await registry.deleteName({ crn }, privateKey, fee);
|
2022-04-08 10:32:24 +00:00
|
|
|
|
2022-04-20 07:38:57 +00:00
|
|
|
let records = await registry.lookupNames([crn], true);
|
2022-04-08 10:32:24 +00:00
|
|
|
expect(records).toBeDefined();
|
|
|
|
expect(records).toHaveLength(1);
|
|
|
|
|
|
|
|
const [{ latest }] = records;
|
|
|
|
expect(latest).toBeDefined();
|
|
|
|
expect(latest.id).toBeDefined();
|
|
|
|
expect(latest.id).toBe('');
|
|
|
|
expect(latest.height).toBeDefined();
|
|
|
|
|
2022-04-20 07:38:57 +00:00
|
|
|
// Query records should NOT return it (no CRN points to it).
|
2023-03-06 21:54:02 +00:00
|
|
|
records = await registry.queryRecords({ type: 'WebsiteRegistrationRecord', version: watcher.record.version });
|
2022-04-08 10:32:24 +00:00
|
|
|
expect(records).toBeDefined();
|
2022-04-13 12:07:24 +00:00
|
|
|
expect(records).toHaveLength(0);
|
2022-04-08 10:32:24 +00:00
|
|
|
|
|
|
|
// Query all records should return it (all: true).
|
2023-03-06 21:54:02 +00:00
|
|
|
records = await registry.queryRecords({ type: 'WebsiteRegistrationRecord', version: watcher.record.version }, true);
|
2022-04-08 10:32:24 +00:00
|
|
|
expect(records).toBeDefined();
|
2022-04-13 12:07:24 +00:00
|
|
|
expect(records).toHaveLength(1);
|
2022-04-08 10:32:24 +00:00
|
|
|
});
|
2022-04-08 12:32:47 +00:00
|
|
|
|
|
|
|
test('Delete already deleted name', async () => {
|
2022-04-22 10:15:47 +00:00
|
|
|
await registry.deleteName({ crn }, privateKey, fee);
|
2022-04-08 12:32:47 +00:00
|
|
|
|
2022-04-20 07:38:57 +00:00
|
|
|
const records = await registry.lookupNames([crn], true);
|
2022-04-08 12:32:47 +00:00
|
|
|
expect(records).toBeDefined();
|
|
|
|
expect(records).toHaveLength(1);
|
|
|
|
|
|
|
|
const [{ latest }] = records;
|
|
|
|
expect(latest).toBeDefined();
|
|
|
|
expect(latest.id).toBeDefined();
|
|
|
|
expect(latest.id).toBe('');
|
|
|
|
expect(latest.height).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2022-04-12 10:54:26 +00:00
|
|
|
test('Delete name for non-owned authority.', async () => {
|
2022-04-08 12:32:47 +00:00
|
|
|
const otherBondId = await registry.getNextBondId(otherPrivateKey);
|
2022-04-12 10:54:26 +00:00
|
|
|
await registry.createBond({ denom: 'aphoton', amount: '10000' }, otherPrivateKey, fee);
|
|
|
|
await registry.setAuthorityBond({ name: otherAuthorityName, bondId: otherBondId }, otherPrivateKey, fee);
|
2022-04-20 07:38:57 +00:00
|
|
|
await registry.setName({ crn: `crn://${otherAuthorityName}/app/test`, cid: watcherId }, otherPrivateKey, fee);
|
2022-04-08 12:32:47 +00:00
|
|
|
|
|
|
|
// Try deleting name under other authority.
|
2022-04-20 07:38:57 +00:00
|
|
|
await expect(registry.deleteName({ crn: `crn://${otherAuthorityName}/app/test` }, privateKey, fee)).rejects.toThrow('Access denied.');
|
2022-04-08 12:32:47 +00:00
|
|
|
});
|
2022-04-05 05:31:10 +00:00
|
|
|
};
|
|
|
|
|
2022-04-19 10:24:41 +00:00
|
|
|
if (process.env.TEST_AUCTIONS_ENABLED) {
|
2022-04-05 05:31:10 +00:00
|
|
|
// Required as jest complains if file has no tests.
|
|
|
|
test('skipping naming tests', () => {});
|
|
|
|
} else {
|
|
|
|
describe('Naming', namingTests);
|
|
|
|
}
|