From 0bc619cad2e14ba3e35f95c8d8cff06ecd8ac474 Mon Sep 17 00:00:00 2001 From: nabarun Date: Tue, 12 Apr 2022 16:24:26 +0530 Subject: [PATCH] Send messages from secondary accounts --- .env.example | 1 - README.md | 31 ++++++--- src/account.ts | 25 ++++--- src/auction.test.ts | 20 ++---- src/bond.test.ts | 12 ++-- src/index.ts | 129 ++++++++++++++++++++---------------- src/messages/nameservice.ts | 3 +- src/naming.test.ts | 61 +++++++---------- src/testing/helper.ts | 2 - 9 files changed, 149 insertions(+), 135 deletions(-) diff --git a/.env.example b/.env.example index 27d69ae..71f0276 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1 @@ -ACCOUNT_ADDRESS='ethm17mfhntjckgrapl5yru2sfq5hmvn7qe6zhc06px' PRIVATE_KEY='75f719e613d05efab06a3f1dde5250b497723b13d4afa4f8ed80145764e40cf7' diff --git a/README.md b/README.md index 92d7737..10320d8 100644 --- a/README.md +++ b/README.md @@ -16,14 +16,7 @@ Follow these steps to run the tests: - Run the chain using `./init.sh`. -- Get the account details using: - ```bash - chibaclonkd keys list - ``` - -- Use the address of key `mykey` and assign it to `ACCOUNT_ADDRESS` in the `.env` file. - -- To export the private key run: +- Export the private key using: ```bash chibaclonkd keys export mykey --unarmored-hex --unsafe @@ -31,12 +24,32 @@ Follow these steps to run the tests: - Copy the private key and assign it to variable `PRIVATE_KEY` in the `.env` file. -- Run the test in chiba-clonk-client repo: +- Run the tests in chiba-clonk-client repo: ```bash yarn test ``` +- Run the tests with auctions enabled + + - In chiba-clonk repo run: + + ```bash + AUCTION_ENABLED=true ./init.sh + ``` + + - Export the private key and change it in `.env` file again using: + + ```bash + chibaclonkd keys export mykey --unarmored-hex --unsafe + ``` + + - Run tests: + + ```bash + yarn test:auctions + ``` + ## Development [README](./DEVELOPMENT.md) diff --git a/src/account.ts b/src/account.ts index bbea926..8b40cb5 100644 --- a/src/account.ts +++ b/src/account.ts @@ -9,6 +9,7 @@ import { MessageTypes, signTypedData, SignTypedDataVersion } from '@metamask/eth import { Ripemd160, Secp256k1 } from "@cosmjs/crypto"; import { fromHex, toHex } from '@cosmjs/encoding'; import { ethToEthermint } from "@tharsis/address-converter" +import { encodeSecp256k1Pubkey } from '@cosmjs/amino'; import { Payload, Signature } from './types'; import { sha256 } from 'js-sha256'; @@ -31,11 +32,12 @@ interface TypedMessageDomain { */ export class Account { _privateKey: Buffer - _publicKey?: Uint8Array - _formattedCosmosAddress?: string - _registryPublicKey?: string - _registryAddress?: string - _ethAddress?: string + _publicKey!: Uint8Array + _encodedPubkey!: string + _formattedCosmosAddress!: string + _registryPublicKey!: string + _registryAddress!: string + _ethAddress!: string /** * Generate bip39 mnemonic. @@ -66,12 +68,17 @@ export class Account { assert(privateKey); this._privateKey = privateKey; + this.init() } get privateKey() { return this._privateKey; } + get encodedPubkey() { + return this._encodedPubkey; + } + get formattedCosmosAddress() { return this._formattedCosmosAddress; } @@ -84,12 +91,10 @@ export class Account { return this._registryAddress; } - async init () { + init () { // Generate public key. - const keypair = await Secp256k1.makeKeypair(this._privateKey); - - const compressed = Secp256k1.compressPubkey(keypair.pubkey); - this._publicKey = compressed + this._publicKey = secp256k1.publicKeyCreate(this._privateKey) + this._encodedPubkey = encodeSecp256k1Pubkey(this._publicKey).value // 2. Generate eth address. this._ethAddress = utils.computeAddress(this._publicKey) diff --git a/src/auction.test.ts b/src/auction.test.ts index c859c45..5b1ed15 100644 --- a/src/auction.test.ts +++ b/src/auction.test.ts @@ -1,11 +1,9 @@ -import assert from 'assert'; - import { Registry, Account, createBid } from './index'; import { getConfig } from './testing/helper'; jest.setTimeout(30 * 60 * 1000); -const { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } = getConfig(); +const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); const auctionTests = (numBidders = 3) => { let registry: Registry; @@ -25,19 +23,15 @@ const auctionTests = (numBidders = 3) => { for (let i = 0; i < numBidders; i++) { const mnenonic = Account.generateMnemonic(); const account = await Account.generateFromMnemonic(mnenonic); - await account.init(); const bidderAddress = account.formattedCosmosAddress; - assert(bidderAddress) - await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: bidderAddress }, accountAddress, privateKey, fee); + await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: bidderAddress }, privateKey, fee); accounts.push({ address: bidderAddress, privateKey: account.privateKey.toString('hex') }); } - - accounts[0] = { address: accountAddress, privateKey }; }); test('Reserve authority.', async () => { authorityName = `dxos-${Date.now()}`; - await registry.reserveAuthority({ name: authorityName, owner: accounts[0].address }, accounts[0].address, accounts[0].privateKey, fee); + await registry.reserveAuthority({ name: authorityName }, accounts[0].privateKey, fee); }); test('Authority should be under auction.', async () => { @@ -55,7 +49,7 @@ const auctionTests = (numBidders = 3) => { test('Commit bids.', async () => { for (let i = 0; i < numBidders; i++) { accounts[i].bid = await createBid(chainId, auctionId, accounts[i].address, `${10000000 + (i * 500)}aphoton`); - await registry.commitBid({ auctionId, commitHash: accounts[i].bid.commitHash }, accounts[i].address, accounts[i].privateKey, fee); + await registry.commitBid({ auctionId, commitHash: accounts[i].bid.commitHash }, accounts[i].privateKey, fee); } }); @@ -80,7 +74,7 @@ const auctionTests = (numBidders = 3) => { for (let i = 0; i < numBidders; i++) { // eslint-disable-next-line no-await-in-loop - await registry.revealBid({ auctionId, reveal: accounts[i].bid.revealString }, accounts[i].address, accounts[i].privateKey, fee); + await registry.revealBid({ auctionId, reveal: accounts[i].bid.revealString }, accounts[i].privateKey, fee); } }); @@ -132,6 +126,6 @@ if (!process.env.AUCTIONS_ENABLED) { yarn test:auctions */ describe('Auction (1 bidder)', withNumBidders(1)); - xdescribe('Auction (2 bidders)', withNumBidders(2)); - xdescribe('Auction (4 bidders)', withNumBidders(4)); + describe('Auction (2 bidders)', withNumBidders(2)); + describe('Auction (4 bidders)', withNumBidders(4)); } diff --git a/src/bond.test.ts b/src/bond.test.ts index 943bbdb..1c4e700 100644 --- a/src/bond.test.ts +++ b/src/bond.test.ts @@ -1,7 +1,7 @@ import { Registry } from './index'; import { getConfig } from './testing/helper'; -const { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } = getConfig(); +const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); jest.setTimeout(90 * 1000); @@ -16,9 +16,9 @@ const bondTests = () => { }); test('Create bond.', async () => { - bondId1 = await registry.getNextBondId(accountAddress); + bondId1 = await registry.getNextBondId(privateKey); expect(bondId1).toBeDefined(); - await registry.createBond({ denom: 'aphoton', amount: '1000000000' }, accountAddress, privateKey, fee); + await registry.createBond({ denom: 'aphoton', amount: '1000000000' }, privateKey, fee); }) test('Get bond by ID.', async () => { @@ -45,7 +45,7 @@ const bondTests = () => { }); test('Refill bond.', async () => { - await registry.refillBond({ id: bondId1, denom: 'aphoton', amount: '500' }, accountAddress, privateKey, fee); + await registry.refillBond({ id: bondId1, denom: 'aphoton', amount: '500' }, privateKey, fee); const [bond] = await registry.getBondsByIds([bondId1]); expect(bond).toBeDefined(); @@ -55,7 +55,7 @@ const bondTests = () => { }); test('Withdraw bond.', async () => { - await registry.withdrawBond({ id: bondId1, denom: 'aphoton', amount: '500' }, accountAddress, privateKey, fee); + await registry.withdrawBond({ id: bondId1, denom: 'aphoton', amount: '500' }, privateKey, fee); const [bond] = await registry.getBondsByIds([bondId1]); expect(bond).toBeDefined(); @@ -65,7 +65,7 @@ const bondTests = () => { }); test('Cancel bond.', async () => { - await registry.cancelBond({ id: bondId1 }, accountAddress, privateKey, fee); + await registry.cancelBond({ id: bondId1 }, privateKey, fee); const [bond] = await registry.getBondsByIds([bondId1]); expect(bond.id).toBe(""); diff --git a/src/index.ts b/src/index.ts index 885fda1..064dfbc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -149,12 +149,11 @@ export class Registry { */ async setRecord( params: { privateKey: string, record: any, bondId: string }, - senderAddress: string, transactionPrivateKey: string, fee: Fee ) { let result; - result = await this._submitRecordTx(params, senderAddress, transactionPrivateKey, fee); + result = await this._submitRecordTx(params, transactionPrivateKey, fee); return parseTxResponse(result); } @@ -162,15 +161,16 @@ export class Registry { /** * Send coins. */ - async sendCoins(params: MessageSendParams, senderAddress: string, privateKey: string, fee: Fee) { + async sendCoins(params: MessageSendParams, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createMessageSend(this._chain, sender, fee, '', params) @@ -182,10 +182,10 @@ export class Registry { /** * Computes the next bondId for the given account private key. */ - async getNextBondId(address: string) { + async getNextBondId(privateKey: string) { let result; - const { account } = await this.getAccount(address); - const accountObj = account.base_account; + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountObj } } = await this.getAccount(account.formattedCosmosAddress); const nextSeq = parseInt(accountObj.sequence, 10) + 1; result = sha256(`${accountObj.address}:${accountObj.account_number}:${nextSeq}`); @@ -210,15 +210,16 @@ export class Registry { /** * Create bond. */ - async createBond(params: MessageMsgCreateBond, senderAddress: string, privateKey: string, fee: Fee) { + async createBond(params: MessageMsgCreateBond, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createTxMsgCreateBond(this._chain, sender, fee, '', params) @@ -230,15 +231,16 @@ export class Registry { /** * Refill bond. */ - async refillBond(params: MessageMsgRefillBond, senderAddress: string, privateKey: string, fee: Fee) { + async refillBond(params: MessageMsgRefillBond, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createTxMsgRefillBond(this._chain, sender, fee, '', params) @@ -250,15 +252,16 @@ export class Registry { /** * Withdraw (from) bond. */ - async withdrawBond(params: MessageMsgWithdrawBond, senderAddress: string, privateKey: string, fee: Fee) { + async withdrawBond(params: MessageMsgWithdrawBond, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createTxMsgWithdrawBond(this._chain, sender, fee, '', params) @@ -270,15 +273,16 @@ export class Registry { /** * Cancel bond. */ - async cancelBond(params: MessageMsgCancelBond, senderAddress: string, privateKey: string, fee: Fee) { + async cancelBond(params: MessageMsgCancelBond, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createTxMsgCancelBond(this._chain, sender, fee, '', params) @@ -290,18 +294,25 @@ export class Registry { /** * Reserve authority. */ - async reserveAuthority(params: MessageMsgReserveAuthority, senderAddress: string, privateKey: string, fee: Fee) { + async reserveAuthority(params: { name: string, owner?: string }, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } - const msg = createTxMsgReserveAuthority(this._chain, sender, fee, '', params) + const msgParams = { + name: params.name, + // TODO: Pass empty string as owner. + owner: params.owner || sender.accountAddress + } + + const msg = createTxMsgReserveAuthority(this._chain, sender, fee, '', msgParams) result = await this._submitTx(msg, privateKey, sender); return parseTxResponse(result); @@ -314,15 +325,16 @@ export class Registry { * @param {string} privateKey * @param {object} fee */ - async setAuthorityBond(params: MessageMsgSetAuthorityBond, senderAddress: string, privateKey: string, fee: Fee) { + async setAuthorityBond(params: MessageMsgSetAuthorityBond, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createTxMsgSetAuthorityBond(this._chain, sender, fee, '', params) @@ -334,15 +346,16 @@ export class Registry { /** * Commit auction bid. */ - async commitBid(params: MessageMsgCommitBid, senderAddress: string, privateKey: string, fee: Fee) { + async commitBid(params: MessageMsgCommitBid, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createTxMsgCommitBid(this._chain, sender, fee, '', params) @@ -354,15 +367,16 @@ export class Registry { /** * Reveal auction bid. */ - async revealBid(params: MessageMsgRevealBid, senderAddress: string, privateKey: string, fee: Fee) { + async revealBid(params: MessageMsgRevealBid, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createTxMsgRevealBid(this._chain, sender, fee, '', params) @@ -392,15 +406,16 @@ export class Registry { * @param {string} privateKey * @param {object} fee */ - async setName(params: MessageMsgSetName, senderAddress: string, privateKey: string, fee: Fee) { + async setName(params: MessageMsgSetName, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createTxMsgSetName(this._chain, sender, fee, '', params) @@ -419,15 +434,16 @@ export class Registry { /** * Delete name (WRN) mapping. */ - async deleteName(params: MessageMsgDeleteName, senderAddress: string, privateKey: string, fee: Fee) { + async deleteName(params: MessageMsgDeleteName, privateKey: string, fee: Fee) { let result; - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createTxMsgDeleteName(this._chain, sender, fee, '', params) @@ -443,7 +459,6 @@ export class Registry { */ async _submitRecordTx( { privateKey, record, bondId }: { privateKey: string, record: any, bondId: string }, - senderAddress: string, txPrivateKey: string, fee: Fee ) { @@ -457,16 +472,15 @@ export class Registry { // Sign record. const recordSignerAccount = new Account(Buffer.from(privateKey, 'hex')); - await recordSignerAccount.init(); const registryRecord = new Record(record); const payload = new Payload(registryRecord); await recordSignerAccount.signPayload(payload); // Send record payload Tx. - return this._submitRecordPayloadTx({ payload, bondId }, senderAddress, txPrivateKey, fee); + return this._submitRecordPayloadTx({ payload, bondId }, txPrivateKey, fee); } - async _submitRecordPayloadTx(params: MessageMsgSetRecord, senderAddress: string, privateKey: string, fee: Fee) { + async _submitRecordPayloadTx(params: MessageMsgSetRecord, privateKey: string, fee: Fee) { if (!isKeyValid(privateKey)) { throw new Error('Registry privateKey should be a hex string.'); } @@ -475,13 +489,14 @@ export class Registry { throw new Error(`Invalid bondId: ${params.bondId}.`); } - const { account: { base_account: accountInfo } } = await this.getAccount(senderAddress); + const account = new Account(Buffer.from(privateKey, 'hex')); + const { account: { base_account: accountInfo } } = await this.getAccount(account.formattedCosmosAddress); const sender = { - accountAddress: accountInfo.address, + accountAddress: account.formattedCosmosAddress, sequence: accountInfo.sequence, accountNumber: accountInfo.account_number, - pubkey: accountInfo.pub_key.key, + pubkey: account.encodedPubkey, } const msg = createTxMsgSetRecord(this._chain, sender, fee, '', params) diff --git a/src/messages/nameservice.ts b/src/messages/nameservice.ts index 24b8381..84baf44 100644 --- a/src/messages/nameservice.ts +++ b/src/messages/nameservice.ts @@ -70,7 +70,8 @@ const MSG_DELETE_NAME_TYPES = { export const NAMESERVICE_ERRORS = [ 'Name already reserved.', 'Authority bond not found.', - 'Name authority not found.' + 'Name authority not found.', + 'Access denied.', ] export interface MessageMsgReserveAuthority { diff --git a/src/naming.test.ts b/src/naming.test.ts index ea75cf8..696c669 100644 --- a/src/naming.test.ts +++ b/src/naming.test.ts @@ -12,7 +12,7 @@ const WATCHER_YML_PATH = path.join(__dirname, './testing/data/watcher.yml'); jest.setTimeout(120 * 1000); -const { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } = getConfig(); +const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig(); const namingTests = () => { let registry: Registry; @@ -24,7 +24,6 @@ const namingTests = () => { let authorityName: string; let otherAuthorityName: string; let otherPrivateKey: string; - let otherAccountAddress: string; let wrn: string; @@ -32,8 +31,8 @@ const namingTests = () => { registry = new Registry(restEndpoint, gqlEndpoint, chainId); // Create bond. - bondId = await registry.getNextBondId(accountAddress); - await registry.createBond({ denom: 'aphoton', amount: '1000000000' }, accountAddress, privateKey, fee); + bondId = await registry.getNextBondId(privateKey); + await registry.createBond({ denom: 'aphoton', amount: '1000000000' }, privateKey, fee); // Create bot. // TODO: Use ensureUpdatedConfig from helper. @@ -44,7 +43,6 @@ const namingTests = () => { bondId, record: watcher.record }, - accountAddress, privateKey, fee ) @@ -56,7 +54,7 @@ const namingTests = () => { test('Reserve authority.', async () => { authorityName = `dxos-${Date.now()}`; - await registry.reserveAuthority({ name: authorityName, owner: accountAddress }, accountAddress, privateKey, fee); + await registry.reserveAuthority({ name: authorityName }, privateKey, fee); }); test('Lookup authority.', async () => { @@ -77,12 +75,12 @@ const namingTests = () => { }); test('Reserve already reserved authority', async () => { - await expect(registry.reserveAuthority({ name: authorityName, owner: accountAddress }, accountAddress, privateKey, fee)).rejects.toThrow('Name already reserved.'); + await expect(registry.reserveAuthority({ name: authorityName }, privateKey, fee)).rejects.toThrow('Name already reserved.'); }); test('Reserve sub-authority.', async () => { const subAuthority = `echo.${authorityName}`; - await registry.reserveAuthority({ name: subAuthority, owner: accountAddress }, accountAddress, privateKey, fee); + await registry.reserveAuthority({ name: subAuthority }, privateKey, fee); const [record] = await registry.lookupAuthorities([subAuthority]); expect(record).toBeDefined(); @@ -95,18 +93,14 @@ const namingTests = () => { // Create another account, send tx to set public key on the account. const mnenonic1 = Account.generateMnemonic(); const otherAccount1 = await Account.generateFromMnemonic(mnenonic1); - await otherAccount1.init() - assert(otherAccount1.formattedCosmosAddress) - await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccount1.formattedCosmosAddress }, accountAddress, privateKey, fee); + await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccount1.formattedCosmosAddress }, privateKey, fee); const mnenonic2 = Account.generateMnemonic(); const otherAccount2 = await Account.generateFromMnemonic(mnenonic2); - await otherAccount2.init() - assert(otherAccount2.formattedCosmosAddress) - await registry.sendCoins({ denom: 'aphoton', amount: '10', destinationAddress: otherAccount2.formattedCosmosAddress }, accountAddress, privateKey, fee); + await registry.sendCoins({ denom: 'aphoton', amount: '10', destinationAddress: otherAccount2.formattedCosmosAddress }, otherAccount1.getPrivateKey(), fee); const subAuthority = `halo.${authorityName}`; - await registry.reserveAuthority({ name: subAuthority, owner: otherAccount1.formattedCosmosAddress }, accountAddress, privateKey, fee); + await registry.reserveAuthority({ name: subAuthority, owner: otherAccount1.formattedCosmosAddress }, privateKey, fee); const [record] = await registry.lookupAuthorities([subAuthority]); expect(record).toBeDefined(); @@ -119,16 +113,16 @@ const namingTests = () => { test('Set name for unbonded authority', async () => { wrn = `wrn://${authorityName}/app/test`; assert(watcherId) - await expect(registry.setName({ wrn, cid: watcherId }, accountAddress, privateKey, fee)).rejects.toThrow('Authority bond not found.'); + await expect(registry.setName({ wrn, cid: watcherId }, privateKey, fee)).rejects.toThrow('Authority bond not found.'); }); test('Set authority bond', async () => { - await registry.setAuthorityBond({ name: authorityName, bondId }, accountAddress, privateKey, fee); + await registry.setAuthorityBond({ name: authorityName, bondId }, privateKey, fee); }); test('Set name', async () => { wrn = `wrn://${authorityName}/app/test`; - await registry.setName({ wrn, cid: watcherId }, accountAddress, privateKey, fee); + await registry.setName({ wrn, cid: watcherId }, privateKey, fee); // Query records should return it (some WRN points to it). const records = await registry.queryRecords({ type: 'watcher', version: watcher.record.version }); @@ -169,7 +163,6 @@ const namingTests = () => { bondId, record: updatedWatcher.record }, - accountAddress, privateKey, fee ) @@ -177,7 +170,7 @@ const namingTests = () => { // TODO: Get id from setRecord response. // const updatedWatcherId = result.data; const updatedWatcherId = WATCHER_2_ID; - await registry.setName({ wrn, cid: updatedWatcherId }, accountAddress, privateKey, fee); + await registry.setName({ wrn, cid: updatedWatcherId }, privateKey, fee); const records = await registry.lookupNames([wrn], true); expect(records).toHaveLength(1); @@ -198,27 +191,23 @@ const namingTests = () => { }); test('Set name without reserving authority', async () => { - await expect(registry.setName({ wrn: 'wrn://not-reserved/app/test', cid: watcherId }, accountAddress, privateKey, fee)) + await expect(registry.setName({ wrn: 'wrn://not-reserved/app/test', cid: watcherId }, privateKey, fee)) .rejects.toThrow('Name authority not found.'); }); - xtest('Set name for non-owned authority', async () => { + test('Set name for non-owned authority', async () => { // Create another account. const mnenonic = Account.generateMnemonic(); const otherAccount = await Account.generateFromMnemonic(mnenonic); - await otherAccount.init() - // TODO: Get correct account address from private key. - assert(otherAccount.formattedCosmosAddress); - otherAccountAddress = otherAccount.formattedCosmosAddress - await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccountAddress }, accountAddress, privateKey, fee); + await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccount.formattedCosmosAddress }, privateKey, fee); // Other account reserves an authority. otherAuthorityName = `other-${Date.now()}`; otherPrivateKey = otherAccount.privateKey.toString('hex'); - await registry.reserveAuthority({ name: otherAuthorityName, owner: otherAccountAddress }, otherAccountAddress, otherPrivateKey, fee); + await registry.reserveAuthority({ name: otherAuthorityName }, otherPrivateKey, fee); // Try setting name under other authority. - await expect(registry.setName({ wrn: `wrn://${otherAuthorityName}/app/test`, cid: watcherId }, accountAddress, privateKey, fee)).rejects.toThrow('Access denied.'); + await expect(registry.setName({ wrn: `wrn://${otherAuthorityName}/app/test`, cid: watcherId }, privateKey, fee)).rejects.toThrow('Access denied.'); }); test('Lookup non existing name', async () => { @@ -238,7 +227,7 @@ const namingTests = () => { }); test('Delete name', async () => { - await registry.deleteName({ wrn }, accountAddress, privateKey, fee); + await registry.deleteName({ wrn }, privateKey, fee); let records = await registry.lookupNames([wrn], true); expect(records).toBeDefined(); @@ -263,7 +252,7 @@ const namingTests = () => { }); test('Delete already deleted name', async () => { - await registry.deleteName({ wrn }, accountAddress, privateKey, fee); + await registry.deleteName({ wrn }, privateKey, fee); const records = await registry.lookupNames([wrn], true); expect(records).toBeDefined(); @@ -277,14 +266,14 @@ const namingTests = () => { expect(latest.height).toBeDefined(); }); - xtest('Delete name for non-owned authority.', async () => { + test('Delete name for non-owned authority.', async () => { const otherBondId = await registry.getNextBondId(otherPrivateKey); - await registry.createBond({ denom: 'aphoton', amount: '10000' }, otherAccountAddress, otherPrivateKey, fee); - await registry.setAuthorityBond({ name: otherAuthorityName, bondId: otherBondId }, otherAccountAddress, otherPrivateKey, fee); - await registry.setName({ wrn: `wrn://${otherAuthorityName}/app/test`, cid: watcherId }, otherAccountAddress, otherPrivateKey, fee); + await registry.createBond({ denom: 'aphoton', amount: '10000' }, otherPrivateKey, fee); + await registry.setAuthorityBond({ name: otherAuthorityName, bondId: otherBondId }, otherPrivateKey, fee); + await registry.setName({ wrn: `wrn://${otherAuthorityName}/app/test`, cid: watcherId }, otherPrivateKey, fee); // Try deleting name under other authority. - await expect(registry.deleteName({ wrn: `wrn://${otherAuthorityName}/app/test` }, accountAddress, privateKey, fee)).rejects.toThrow('Access denied.'); + await expect(registry.deleteName({ wrn: `wrn://${otherAuthorityName}/app/test` }, privateKey, fee)).rejects.toThrow('Access denied.'); }); }; diff --git a/src/testing/helper.ts b/src/testing/helper.ts index 8303c6d..4fe2ffb 100644 --- a/src/testing/helper.ts +++ b/src/testing/helper.ts @@ -19,12 +19,10 @@ export const getBaseConfig = async (path: string) => { export const getConfig = () => { assert(process.env.PRIVATE_KEY); - assert(process.env.ACCOUNT_ADDRESS); return { chainId: process.env.CHIBA_CLONK_CHAIN_ID || 'chibaclonk_9000-1', privateKey: process.env.PRIVATE_KEY, - accountAddress: process.env.ACCOUNT_ADDRESS, restEndpoint: process.env.CHIBA_CLONK_REST_ENDPOINT || 'http://localhost:1317', gqlEndpoint: process.env.CHIBA_CLONK_GQL_ENDPOINT || 'http://localhost:9473/api', fee: {