Send messages from secondary accounts

This commit is contained in:
nabarun 2022-04-12 16:24:26 +05:30 committed by Ashwin Phatak
parent 838edaa9a6
commit 0bc619cad2
9 changed files with 149 additions and 135 deletions

View File

@ -1,2 +1 @@
ACCOUNT_ADDRESS='ethm17mfhntjckgrapl5yru2sfq5hmvn7qe6zhc06px'
PRIVATE_KEY='75f719e613d05efab06a3f1dde5250b497723b13d4afa4f8ed80145764e40cf7' PRIVATE_KEY='75f719e613d05efab06a3f1dde5250b497723b13d4afa4f8ed80145764e40cf7'

View File

@ -16,14 +16,7 @@ Follow these steps to run the tests:
- Run the chain using `./init.sh`. - Run the chain using `./init.sh`.
- Get the account details using: - Export the private key 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:
```bash ```bash
chibaclonkd keys export mykey --unarmored-hex --unsafe 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. - 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 ```bash
yarn test 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 ## Development
[README](./DEVELOPMENT.md) [README](./DEVELOPMENT.md)

View File

@ -9,6 +9,7 @@ import { MessageTypes, signTypedData, SignTypedDataVersion } from '@metamask/eth
import { Ripemd160, Secp256k1 } from "@cosmjs/crypto"; import { Ripemd160, Secp256k1 } from "@cosmjs/crypto";
import { fromHex, toHex } from '@cosmjs/encoding'; import { fromHex, toHex } from '@cosmjs/encoding';
import { ethToEthermint } from "@tharsis/address-converter" import { ethToEthermint } from "@tharsis/address-converter"
import { encodeSecp256k1Pubkey } from '@cosmjs/amino';
import { Payload, Signature } from './types'; import { Payload, Signature } from './types';
import { sha256 } from 'js-sha256'; import { sha256 } from 'js-sha256';
@ -31,11 +32,12 @@ interface TypedMessageDomain {
*/ */
export class Account { export class Account {
_privateKey: Buffer _privateKey: Buffer
_publicKey?: Uint8Array _publicKey!: Uint8Array
_formattedCosmosAddress?: string _encodedPubkey!: string
_registryPublicKey?: string _formattedCosmosAddress!: string
_registryAddress?: string _registryPublicKey!: string
_ethAddress?: string _registryAddress!: string
_ethAddress!: string
/** /**
* Generate bip39 mnemonic. * Generate bip39 mnemonic.
@ -66,12 +68,17 @@ export class Account {
assert(privateKey); assert(privateKey);
this._privateKey = privateKey; this._privateKey = privateKey;
this.init()
} }
get privateKey() { get privateKey() {
return this._privateKey; return this._privateKey;
} }
get encodedPubkey() {
return this._encodedPubkey;
}
get formattedCosmosAddress() { get formattedCosmosAddress() {
return this._formattedCosmosAddress; return this._formattedCosmosAddress;
} }
@ -84,12 +91,10 @@ export class Account {
return this._registryAddress; return this._registryAddress;
} }
async init () { init () {
// Generate public key. // Generate public key.
const keypair = await Secp256k1.makeKeypair(this._privateKey); this._publicKey = secp256k1.publicKeyCreate(this._privateKey)
this._encodedPubkey = encodeSecp256k1Pubkey(this._publicKey).value
const compressed = Secp256k1.compressPubkey(keypair.pubkey);
this._publicKey = compressed
// 2. Generate eth address. // 2. Generate eth address.
this._ethAddress = utils.computeAddress(this._publicKey) this._ethAddress = utils.computeAddress(this._publicKey)

View File

@ -1,11 +1,9 @@
import assert from 'assert';
import { Registry, Account, createBid } from './index'; import { Registry, Account, createBid } from './index';
import { getConfig } from './testing/helper'; import { getConfig } from './testing/helper';
jest.setTimeout(30 * 60 * 1000); jest.setTimeout(30 * 60 * 1000);
const { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } = getConfig(); const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
const auctionTests = (numBidders = 3) => { const auctionTests = (numBidders = 3) => {
let registry: Registry; let registry: Registry;
@ -25,19 +23,15 @@ const auctionTests = (numBidders = 3) => {
for (let i = 0; i < numBidders; i++) { for (let i = 0; i < numBidders; i++) {
const mnenonic = Account.generateMnemonic(); const mnenonic = Account.generateMnemonic();
const account = await Account.generateFromMnemonic(mnenonic); const account = await Account.generateFromMnemonic(mnenonic);
await account.init();
const bidderAddress = account.formattedCosmosAddress; const bidderAddress = account.formattedCosmosAddress;
assert(bidderAddress) await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: bidderAddress }, privateKey, fee);
await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: bidderAddress }, accountAddress, privateKey, fee);
accounts.push({ address: bidderAddress, privateKey: account.privateKey.toString('hex') }); accounts.push({ address: bidderAddress, privateKey: account.privateKey.toString('hex') });
} }
accounts[0] = { address: accountAddress, privateKey };
}); });
test('Reserve authority.', async () => { test('Reserve authority.', async () => {
authorityName = `dxos-${Date.now()}`; 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 () => { test('Authority should be under auction.', async () => {
@ -55,7 +49,7 @@ const auctionTests = (numBidders = 3) => {
test('Commit bids.', async () => { test('Commit bids.', async () => {
for (let i = 0; i < numBidders; i++) { for (let i = 0; i < numBidders; i++) {
accounts[i].bid = await createBid(chainId, auctionId, accounts[i].address, `${10000000 + (i * 500)}aphoton`); 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++) { for (let i = 0; i < numBidders; i++) {
// eslint-disable-next-line no-await-in-loop // 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 yarn test:auctions
*/ */
describe('Auction (1 bidder)', withNumBidders(1)); describe('Auction (1 bidder)', withNumBidders(1));
xdescribe('Auction (2 bidders)', withNumBidders(2)); describe('Auction (2 bidders)', withNumBidders(2));
xdescribe('Auction (4 bidders)', withNumBidders(4)); describe('Auction (4 bidders)', withNumBidders(4));
} }

View File

@ -1,7 +1,7 @@
import { Registry } from './index'; import { Registry } from './index';
import { getConfig } from './testing/helper'; import { getConfig } from './testing/helper';
const { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } = getConfig(); const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
jest.setTimeout(90 * 1000); jest.setTimeout(90 * 1000);
@ -16,9 +16,9 @@ const bondTests = () => {
}); });
test('Create bond.', async () => { test('Create bond.', async () => {
bondId1 = await registry.getNextBondId(accountAddress); bondId1 = await registry.getNextBondId(privateKey);
expect(bondId1).toBeDefined(); 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 () => { test('Get bond by ID.', async () => {
@ -45,7 +45,7 @@ const bondTests = () => {
}); });
test('Refill bond.', async () => { 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]); const [bond] = await registry.getBondsByIds([bondId1]);
expect(bond).toBeDefined(); expect(bond).toBeDefined();
@ -55,7 +55,7 @@ const bondTests = () => {
}); });
test('Withdraw bond.', async () => { 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]); const [bond] = await registry.getBondsByIds([bondId1]);
expect(bond).toBeDefined(); expect(bond).toBeDefined();
@ -65,7 +65,7 @@ const bondTests = () => {
}); });
test('Cancel bond.', async () => { 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]); const [bond] = await registry.getBondsByIds([bondId1]);
expect(bond.id).toBe(""); expect(bond.id).toBe("");

View File

@ -149,12 +149,11 @@ export class Registry {
*/ */
async setRecord( async setRecord(
params: { privateKey: string, record: any, bondId: string }, params: { privateKey: string, record: any, bondId: string },
senderAddress: string,
transactionPrivateKey: string, transactionPrivateKey: string,
fee: Fee fee: Fee
) { ) {
let result; let result;
result = await this._submitRecordTx(params, senderAddress, transactionPrivateKey, fee); result = await this._submitRecordTx(params, transactionPrivateKey, fee);
return parseTxResponse(result); return parseTxResponse(result);
} }
@ -162,15 +161,16 @@ export class Registry {
/** /**
* Send coins. * Send coins.
*/ */
async sendCoins(params: MessageSendParams, senderAddress: string, privateKey: string, fee: Fee) { async sendCoins(params: MessageSendParams, privateKey: string, fee: Fee) {
let result; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createMessageSend(this._chain, sender, fee, '', params) 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. * Computes the next bondId for the given account private key.
*/ */
async getNextBondId(address: string) { async getNextBondId(privateKey: string) {
let result; let result;
const { account } = await this.getAccount(address); const account = new Account(Buffer.from(privateKey, 'hex'));
const accountObj = account.base_account; const { account: { base_account: accountObj } } = await this.getAccount(account.formattedCosmosAddress);
const nextSeq = parseInt(accountObj.sequence, 10) + 1; const nextSeq = parseInt(accountObj.sequence, 10) + 1;
result = sha256(`${accountObj.address}:${accountObj.account_number}:${nextSeq}`); result = sha256(`${accountObj.address}:${accountObj.account_number}:${nextSeq}`);
@ -210,15 +210,16 @@ export class Registry {
/** /**
* Create bond. * Create bond.
*/ */
async createBond(params: MessageMsgCreateBond, senderAddress: string, privateKey: string, fee: Fee) { async createBond(params: MessageMsgCreateBond, privateKey: string, fee: Fee) {
let result; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createTxMsgCreateBond(this._chain, sender, fee, '', params) const msg = createTxMsgCreateBond(this._chain, sender, fee, '', params)
@ -230,15 +231,16 @@ export class Registry {
/** /**
* Refill bond. * Refill bond.
*/ */
async refillBond(params: MessageMsgRefillBond, senderAddress: string, privateKey: string, fee: Fee) { async refillBond(params: MessageMsgRefillBond, privateKey: string, fee: Fee) {
let result; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createTxMsgRefillBond(this._chain, sender, fee, '', params) const msg = createTxMsgRefillBond(this._chain, sender, fee, '', params)
@ -250,15 +252,16 @@ export class Registry {
/** /**
* Withdraw (from) bond. * Withdraw (from) bond.
*/ */
async withdrawBond(params: MessageMsgWithdrawBond, senderAddress: string, privateKey: string, fee: Fee) { async withdrawBond(params: MessageMsgWithdrawBond, privateKey: string, fee: Fee) {
let result; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createTxMsgWithdrawBond(this._chain, sender, fee, '', params) const msg = createTxMsgWithdrawBond(this._chain, sender, fee, '', params)
@ -270,15 +273,16 @@ export class Registry {
/** /**
* Cancel bond. * Cancel bond.
*/ */
async cancelBond(params: MessageMsgCancelBond, senderAddress: string, privateKey: string, fee: Fee) { async cancelBond(params: MessageMsgCancelBond, privateKey: string, fee: Fee) {
let result; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createTxMsgCancelBond(this._chain, sender, fee, '', params) const msg = createTxMsgCancelBond(this._chain, sender, fee, '', params)
@ -290,18 +294,25 @@ export class Registry {
/** /**
* Reserve authority. * 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; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, 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); result = await this._submitTx(msg, privateKey, sender);
return parseTxResponse(result); return parseTxResponse(result);
@ -314,15 +325,16 @@ export class Registry {
* @param {string} privateKey * @param {string} privateKey
* @param {object} fee * @param {object} fee
*/ */
async setAuthorityBond(params: MessageMsgSetAuthorityBond, senderAddress: string, privateKey: string, fee: Fee) { async setAuthorityBond(params: MessageMsgSetAuthorityBond, privateKey: string, fee: Fee) {
let result; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createTxMsgSetAuthorityBond(this._chain, sender, fee, '', params) const msg = createTxMsgSetAuthorityBond(this._chain, sender, fee, '', params)
@ -334,15 +346,16 @@ export class Registry {
/** /**
* Commit auction bid. * Commit auction bid.
*/ */
async commitBid(params: MessageMsgCommitBid, senderAddress: string, privateKey: string, fee: Fee) { async commitBid(params: MessageMsgCommitBid, privateKey: string, fee: Fee) {
let result; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createTxMsgCommitBid(this._chain, sender, fee, '', params) const msg = createTxMsgCommitBid(this._chain, sender, fee, '', params)
@ -354,15 +367,16 @@ export class Registry {
/** /**
* Reveal auction bid. * Reveal auction bid.
*/ */
async revealBid(params: MessageMsgRevealBid, senderAddress: string, privateKey: string, fee: Fee) { async revealBid(params: MessageMsgRevealBid, privateKey: string, fee: Fee) {
let result; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createTxMsgRevealBid(this._chain, sender, fee, '', params) const msg = createTxMsgRevealBid(this._chain, sender, fee, '', params)
@ -392,15 +406,16 @@ export class Registry {
* @param {string} privateKey * @param {string} privateKey
* @param {object} fee * @param {object} fee
*/ */
async setName(params: MessageMsgSetName, senderAddress: string, privateKey: string, fee: Fee) { async setName(params: MessageMsgSetName, privateKey: string, fee: Fee) {
let result; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createTxMsgSetName(this._chain, sender, fee, '', params) const msg = createTxMsgSetName(this._chain, sender, fee, '', params)
@ -419,15 +434,16 @@ export class Registry {
/** /**
* Delete name (WRN) mapping. * Delete name (WRN) mapping.
*/ */
async deleteName(params: MessageMsgDeleteName, senderAddress: string, privateKey: string, fee: Fee) { async deleteName(params: MessageMsgDeleteName, privateKey: string, fee: Fee) {
let result; 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createTxMsgDeleteName(this._chain, sender, fee, '', params) const msg = createTxMsgDeleteName(this._chain, sender, fee, '', params)
@ -443,7 +459,6 @@ export class Registry {
*/ */
async _submitRecordTx( async _submitRecordTx(
{ privateKey, record, bondId }: { privateKey: string, record: any, bondId: string }, { privateKey, record, bondId }: { privateKey: string, record: any, bondId: string },
senderAddress: string,
txPrivateKey: string, txPrivateKey: string,
fee: Fee fee: Fee
) { ) {
@ -457,16 +472,15 @@ export class Registry {
// Sign record. // Sign record.
const recordSignerAccount = new Account(Buffer.from(privateKey, 'hex')); const recordSignerAccount = new Account(Buffer.from(privateKey, 'hex'));
await recordSignerAccount.init();
const registryRecord = new Record(record); const registryRecord = new Record(record);
const payload = new Payload(registryRecord); const payload = new Payload(registryRecord);
await recordSignerAccount.signPayload(payload); await recordSignerAccount.signPayload(payload);
// Send record payload Tx. // 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)) { if (!isKeyValid(privateKey)) {
throw new Error('Registry privateKey should be a hex string.'); throw new Error('Registry privateKey should be a hex string.');
} }
@ -475,13 +489,14 @@ export class Registry {
throw new Error(`Invalid bondId: ${params.bondId}.`); 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 = { const sender = {
accountAddress: accountInfo.address, accountAddress: account.formattedCosmosAddress,
sequence: accountInfo.sequence, sequence: accountInfo.sequence,
accountNumber: accountInfo.account_number, accountNumber: accountInfo.account_number,
pubkey: accountInfo.pub_key.key, pubkey: account.encodedPubkey,
} }
const msg = createTxMsgSetRecord(this._chain, sender, fee, '', params) const msg = createTxMsgSetRecord(this._chain, sender, fee, '', params)

View File

@ -70,7 +70,8 @@ const MSG_DELETE_NAME_TYPES = {
export const NAMESERVICE_ERRORS = [ export const NAMESERVICE_ERRORS = [
'Name already reserved.', 'Name already reserved.',
'Authority bond not found.', 'Authority bond not found.',
'Name authority not found.' 'Name authority not found.',
'Access denied.',
] ]
export interface MessageMsgReserveAuthority { export interface MessageMsgReserveAuthority {

View File

@ -12,7 +12,7 @@ const WATCHER_YML_PATH = path.join(__dirname, './testing/data/watcher.yml');
jest.setTimeout(120 * 1000); jest.setTimeout(120 * 1000);
const { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } = getConfig(); const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
const namingTests = () => { const namingTests = () => {
let registry: Registry; let registry: Registry;
@ -24,7 +24,6 @@ const namingTests = () => {
let authorityName: string; let authorityName: string;
let otherAuthorityName: string; let otherAuthorityName: string;
let otherPrivateKey: string; let otherPrivateKey: string;
let otherAccountAddress: string;
let wrn: string; let wrn: string;
@ -32,8 +31,8 @@ const namingTests = () => {
registry = new Registry(restEndpoint, gqlEndpoint, chainId); registry = new Registry(restEndpoint, gqlEndpoint, chainId);
// Create bond. // Create bond.
bondId = await registry.getNextBondId(accountAddress); bondId = await registry.getNextBondId(privateKey);
await registry.createBond({ denom: 'aphoton', amount: '1000000000' }, accountAddress, privateKey, fee); await registry.createBond({ denom: 'aphoton', amount: '1000000000' }, privateKey, fee);
// Create bot. // Create bot.
// TODO: Use ensureUpdatedConfig from helper. // TODO: Use ensureUpdatedConfig from helper.
@ -44,7 +43,6 @@ const namingTests = () => {
bondId, bondId,
record: watcher.record record: watcher.record
}, },
accountAddress,
privateKey, privateKey,
fee fee
) )
@ -56,7 +54,7 @@ const namingTests = () => {
test('Reserve authority.', async () => { test('Reserve authority.', async () => {
authorityName = `dxos-${Date.now()}`; 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 () => { test('Lookup authority.', async () => {
@ -77,12 +75,12 @@ const namingTests = () => {
}); });
test('Reserve already reserved authority', async () => { 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 () => { test('Reserve sub-authority.', async () => {
const subAuthority = `echo.${authorityName}`; 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]); const [record] = await registry.lookupAuthorities([subAuthority]);
expect(record).toBeDefined(); expect(record).toBeDefined();
@ -95,18 +93,14 @@ const namingTests = () => {
// Create another account, send tx to set public key on the account. // Create another account, send tx to set public key on the account.
const mnenonic1 = Account.generateMnemonic(); const mnenonic1 = Account.generateMnemonic();
const otherAccount1 = await Account.generateFromMnemonic(mnenonic1); const otherAccount1 = await Account.generateFromMnemonic(mnenonic1);
await otherAccount1.init() await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccount1.formattedCosmosAddress }, privateKey, fee);
assert(otherAccount1.formattedCosmosAddress)
await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccount1.formattedCosmosAddress }, accountAddress, privateKey, fee);
const mnenonic2 = Account.generateMnemonic(); const mnenonic2 = Account.generateMnemonic();
const otherAccount2 = await Account.generateFromMnemonic(mnenonic2); const otherAccount2 = await Account.generateFromMnemonic(mnenonic2);
await otherAccount2.init() await registry.sendCoins({ denom: 'aphoton', amount: '10', destinationAddress: otherAccount2.formattedCosmosAddress }, otherAccount1.getPrivateKey(), fee);
assert(otherAccount2.formattedCosmosAddress)
await registry.sendCoins({ denom: 'aphoton', amount: '10', destinationAddress: otherAccount2.formattedCosmosAddress }, accountAddress, privateKey, fee);
const subAuthority = `halo.${authorityName}`; 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]); const [record] = await registry.lookupAuthorities([subAuthority]);
expect(record).toBeDefined(); expect(record).toBeDefined();
@ -119,16 +113,16 @@ const namingTests = () => {
test('Set name for unbonded authority', async () => { test('Set name for unbonded authority', async () => {
wrn = `wrn://${authorityName}/app/test`; wrn = `wrn://${authorityName}/app/test`;
assert(watcherId) 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 () => { 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 () => { test('Set name', async () => {
wrn = `wrn://${authorityName}/app/test`; 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). // Query records should return it (some WRN points to it).
const records = await registry.queryRecords({ type: 'watcher', version: watcher.record.version }); const records = await registry.queryRecords({ type: 'watcher', version: watcher.record.version });
@ -169,7 +163,6 @@ const namingTests = () => {
bondId, bondId,
record: updatedWatcher.record record: updatedWatcher.record
}, },
accountAddress,
privateKey, privateKey,
fee fee
) )
@ -177,7 +170,7 @@ const namingTests = () => {
// TODO: Get id from setRecord response. // TODO: Get id from setRecord response.
// const updatedWatcherId = result.data; // const updatedWatcherId = result.data;
const updatedWatcherId = WATCHER_2_ID; 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); const records = await registry.lookupNames([wrn], true);
expect(records).toHaveLength(1); expect(records).toHaveLength(1);
@ -198,27 +191,23 @@ const namingTests = () => {
}); });
test('Set name without reserving authority', async () => { 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.'); .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. // Create another account.
const mnenonic = Account.generateMnemonic(); const mnenonic = Account.generateMnemonic();
const otherAccount = await Account.generateFromMnemonic(mnenonic); const otherAccount = await Account.generateFromMnemonic(mnenonic);
await otherAccount.init() await registry.sendCoins({ denom: 'aphoton', amount: '1000000000', destinationAddress: otherAccount.formattedCosmosAddress }, privateKey, fee);
// 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);
// Other account reserves an authority. // Other account reserves an authority.
otherAuthorityName = `other-${Date.now()}`; otherAuthorityName = `other-${Date.now()}`;
otherPrivateKey = otherAccount.privateKey.toString('hex'); 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. // 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 () => { test('Lookup non existing name', async () => {
@ -238,7 +227,7 @@ const namingTests = () => {
}); });
test('Delete name', async () => { test('Delete name', async () => {
await registry.deleteName({ wrn }, accountAddress, privateKey, fee); await registry.deleteName({ wrn }, privateKey, fee);
let records = await registry.lookupNames([wrn], true); let records = await registry.lookupNames([wrn], true);
expect(records).toBeDefined(); expect(records).toBeDefined();
@ -263,7 +252,7 @@ const namingTests = () => {
}); });
test('Delete already deleted name', async () => { 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); const records = await registry.lookupNames([wrn], true);
expect(records).toBeDefined(); expect(records).toBeDefined();
@ -277,14 +266,14 @@ const namingTests = () => {
expect(latest.height).toBeDefined(); 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); const otherBondId = await registry.getNextBondId(otherPrivateKey);
await registry.createBond({ denom: 'aphoton', amount: '10000' }, otherAccountAddress, otherPrivateKey, fee); await registry.createBond({ denom: 'aphoton', amount: '10000' }, otherPrivateKey, fee);
await registry.setAuthorityBond({ name: otherAuthorityName, bondId: otherBondId }, otherAccountAddress, otherPrivateKey, fee); await registry.setAuthorityBond({ name: otherAuthorityName, bondId: otherBondId }, otherPrivateKey, fee);
await registry.setName({ wrn: `wrn://${otherAuthorityName}/app/test`, cid: watcherId }, otherAccountAddress, otherPrivateKey, fee); await registry.setName({ wrn: `wrn://${otherAuthorityName}/app/test`, cid: watcherId }, otherPrivateKey, fee);
// Try deleting name under other authority. // 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.');
}); });
}; };

View File

@ -19,12 +19,10 @@ export const getBaseConfig = async (path: string) => {
export const getConfig = () => { export const getConfig = () => {
assert(process.env.PRIVATE_KEY); assert(process.env.PRIVATE_KEY);
assert(process.env.ACCOUNT_ADDRESS);
return { return {
chainId: process.env.CHIBA_CLONK_CHAIN_ID || 'chibaclonk_9000-1', chainId: process.env.CHIBA_CLONK_CHAIN_ID || 'chibaclonk_9000-1',
privateKey: process.env.PRIVATE_KEY, privateKey: process.env.PRIVATE_KEY,
accountAddress: process.env.ACCOUNT_ADDRESS,
restEndpoint: process.env.CHIBA_CLONK_REST_ENDPOINT || 'http://localhost:1317', restEndpoint: process.env.CHIBA_CLONK_REST_ENDPOINT || 'http://localhost:1317',
gqlEndpoint: process.env.CHIBA_CLONK_GQL_ENDPOINT || 'http://localhost:9473/api', gqlEndpoint: process.env.CHIBA_CLONK_GQL_ENDPOINT || 'http://localhost:9473/api',
fee: { fee: {