Changes from review

This commit is contained in:
2022-04-21 09:44:24 +05:30
committed by Ashwin Phatak
parent 252665512d
commit 58227b68be
15 changed files with 200 additions and 1048 deletions
+2 -4
View File
@@ -4,7 +4,7 @@ import * as ecc from 'tiny-secp256k1';
import * as bip39 from 'bip39';
import { MessageTypes, signTypedData, SignTypedDataVersion } from '@metamask/eth-sig-util';
import { Ripemd160, Secp256k1 } from "@cosmjs/crypto";
import { toBech32, toHex } from '@cosmjs/encoding';
import { toBech32 } from '@cosmjs/encoding';
import { rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino";
const HDPATH = "m/44'/60'/0'/0";
@@ -22,14 +22,13 @@ interface TypedMessageDomain {
/**
* Registry account.
*/
// TODO(egor): This is a wrapper around the private key and doesn't have any account related stuff (e.g. account number/sequence). Maybe rename to Key?
export class Account {
_privateKey: Buffer
_publicKey?: Uint8Array
_cosmosAddress?: string
_formattedCosmosAddress?: string
/**
/**
* Generate bip39 mnemonic.
*/
static generateMnemonic() {
@@ -53,7 +52,6 @@ export class Account {
/**
* New Account.
* @param {buffer} privateKey
*/
constructor(privateKey: Buffer) {
assert(privateKey);
+3 -12
View File
@@ -1,9 +1,9 @@
import { Registry } from './index';
import { getConfig, wait } from './testing/helper';
import { getConfig } from './testing/helper';
const TX_WAIT_TIME = 5000; // in milliseconds.
const { mockServer, chibaClonk: { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } } = getConfig();
const { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } = getConfig();
jest.setTimeout(90 * 1000);
@@ -21,7 +21,6 @@ const bondTests = () => {
bondId1 = await registry.getNextBondId(accountAddress);
expect(bondId1).toBeDefined();
await registry.createBond({ denom: 'aphoton', amount: '1000000000' }, accountAddress, privateKey, fee);
await wait(TX_WAIT_TIME)
})
test('Get bond by ID.', async () => {
@@ -49,7 +48,6 @@ const bondTests = () => {
test('Refill bond.', async () => {
await registry.refillBond({ id: bondId1, denom: 'aphoton', amount: '500' }, accountAddress, privateKey, fee);
await wait(TX_WAIT_TIME);
const [bond] = await registry.getBondsByIds([bondId1]);
expect(bond).toBeDefined();
@@ -60,7 +58,6 @@ const bondTests = () => {
test('Withdraw bond.', async () => {
await registry.withdrawBond({ id: bondId1, denom: 'aphoton', amount: '500' }, accountAddress, privateKey, fee);
await wait(TX_WAIT_TIME);
const [bond] = await registry.getBondsByIds([bondId1]);
expect(bond).toBeDefined();
@@ -71,7 +68,6 @@ const bondTests = () => {
test('Cancel bond.', async () => {
await registry.cancelBond({ id: bondId1 }, accountAddress, privateKey, fee);
await wait(TX_WAIT_TIME);
const [bond] = await registry.getBondsByIds([bondId1]);
expect(bond.id).toBe("");
@@ -80,9 +76,4 @@ const bondTests = () => {
});
};
if (mockServer) {
// Required as jest complains if file has no tests.
test('skipping bond tests', () => {});
} else {
describe('Bonds', bondTests);
}
describe('Bonds', bondTests);
+4 -8
View File
@@ -1,7 +1,7 @@
import isUrl from 'is-url';
import { sha256 } from 'js-sha256';
import { generatePostBodyBroadcast } from '@tharsis/provider';
import { generatePostBodyBroadcast, BroadcastMode } from '@tharsis/provider';
import {
Chain,
Sender,
@@ -10,11 +10,11 @@ import {
MessageSendParams
} from '@tharsis/transactions'
import { createTxMsgCancelBond, createTxMsgCreateBond, createTxMsgRefillBond, createTxMsgWithdrawBond, MessageMsgCancelBond, MessageMsgCreateBond, MessageMsgRefillBond, MessageMsgWithdrawBond } from "./bond";
import { createTxMsgCancelBond, createTxMsgCreateBond, createTxMsgRefillBond, createTxMsgWithdrawBond, MessageMsgCancelBond, MessageMsgCreateBond, MessageMsgRefillBond, MessageMsgWithdrawBond } from "./messages/bond";
import { RegistryClient } from "./registry-client";
import { Account } from "./account";
import { createTransaction } from "./txbuilder";
import { createTxMsgReserveAuthority, MessageMsgReserveAuthority } from './nameservice';
import { createTxMsgReserveAuthority, MessageMsgReserveAuthority } from './messages/nameservice';
const DEFAULT_WRITE_ERROR = 'Unable to write to chiba-clonk.';
@@ -82,10 +82,6 @@ export class Registry {
/**
* Send coins.
* @param {object[]} amount
* @param {string} toAddress
* @param {string} privateKey
* @param {object} fee
*/
async sendCoins(params: MessageSendParams, senderAddress: string, privateKey: string, fee: Fee) {
let result;
@@ -296,7 +292,7 @@ export class Registry {
// Generate signed Tx.
const transaction = createTransaction(message, account, sender, this._chain);
const tx = generatePostBodyBroadcast(transaction)
const tx = generatePostBodyBroadcast(transaction, BroadcastMode.Block)
// Submit Tx to chain.
const { tx_response: response } = await this._client.submit(tx);
+7 -150
View File
@@ -1,7 +1,4 @@
import {
createEIP712,
generateFee,
generateMessage,
generateTypes,
} from '@tharsis/eip712'
import {
@@ -9,10 +6,10 @@ import {
Sender,
Fee,
} from '@tharsis/transactions'
import { createTransaction } from '@tharsis/proto'
import * as bondTx from './proto/vulcanize/bond/v1beta1/tx'
import * as coin from './proto/cosmos/base/v1beta1/coin'
import * as bondTx from '../proto/vulcanize/bond/v1beta1/tx'
import * as coin from '../proto/cosmos/base/v1beta1/coin'
import { createTx } from './util'
const MSG_CREATE_BOND_TYPES = {
MsgValue: [
@@ -84,13 +81,6 @@ export function createTxMsgCreateBond(
memo: string,
params: MessageMsgCreateBond,
) {
// EIP712
const feeObject = generateFee(
fee.amount,
fee.denom,
fee.gas,
sender.accountAddress,
)
const types = generateTypes(MSG_CREATE_BOND_TYPES)
const msg = createMsgCreateBond(
@@ -99,41 +89,13 @@ export function createTxMsgCreateBond(
params.denom
)
const messages = generateMessage(
sender.accountNumber.toString(),
sender.sequence.toString(),
chain.cosmosChainId,
memo,
feeObject,
msg,
)
const eipToSign = createEIP712(types, chain.chainId, messages)
// Cosmos
const msgCosmos = protoCreateMsgCreateBond(
sender.accountAddress,
params.amount,
params.denom
)
const tx = createTransaction(
msgCosmos,
memo,
fee.amount,
fee.denom,
parseInt(fee.gas, 10),
'ethsecp256',
sender.pubkey,
sender.sequence,
sender.accountNumber,
chain.cosmosChainId,
)
return {
signDirect: tx.signDirect,
legacyAmino: tx.legacyAmino,
eipToSign,
}
return createTx(chain, sender, fee, memo, types, msg, msgCosmos)
}
export function createTxMsgRefillBond(
@@ -143,13 +105,6 @@ export function createTxMsgRefillBond(
memo: string,
params: MessageMsgRefillBond,
) {
// EIP712
const feeObject = generateFee(
fee.amount,
fee.denom,
fee.gas,
sender.accountAddress,
)
const types = generateTypes(MSG_REFILL_BOND_TYPES)
const msg = createMsgRefillBond(
@@ -159,17 +114,6 @@ export function createTxMsgRefillBond(
params.denom
)
const messages = generateMessage(
sender.accountNumber.toString(),
sender.sequence.toString(),
chain.cosmosChainId,
memo,
feeObject,
msg,
)
const eipToSign = createEIP712(types, chain.chainId, messages)
// Cosmos
const msgCosmos = protoCreateMsgRefillBond(
params.id,
sender.accountAddress,
@@ -177,24 +121,7 @@ export function createTxMsgRefillBond(
params.denom
)
const tx = createTransaction(
msgCosmos,
memo,
fee.amount,
fee.denom,
parseInt(fee.gas, 10),
'ethsecp256',
sender.pubkey,
sender.sequence,
sender.accountNumber,
chain.cosmosChainId,
)
return {
signDirect: tx.signDirect,
legacyAmino: tx.legacyAmino,
eipToSign,
}
return createTx(chain, sender, fee, memo, types, msg, msgCosmos)
}
export function createTxMsgWithdrawBond(
@@ -204,13 +131,6 @@ export function createTxMsgWithdrawBond(
memo: string,
params: MessageMsgWithdrawBond,
) {
// EIP712
const feeObject = generateFee(
fee.amount,
fee.denom,
fee.gas,
sender.accountAddress,
)
const types = generateTypes(MSG_WITHDRAW_BOND_TYPES)
const msg = createMsgWithdrawBond(
@@ -220,17 +140,6 @@ export function createTxMsgWithdrawBond(
params.denom
)
const messages = generateMessage(
sender.accountNumber.toString(),
sender.sequence.toString(),
chain.cosmosChainId,
memo,
feeObject,
msg,
)
const eipToSign = createEIP712(types, chain.chainId, messages)
// Cosmos
const msgCosmos = protoCreateMsgWithdrawBond(
params.id,
sender.accountAddress,
@@ -238,24 +147,7 @@ export function createTxMsgWithdrawBond(
params.denom
)
const tx = createTransaction(
msgCosmos,
memo,
fee.amount,
fee.denom,
parseInt(fee.gas, 10),
'ethsecp256',
sender.pubkey,
sender.sequence,
sender.accountNumber,
chain.cosmosChainId,
)
return {
signDirect: tx.signDirect,
legacyAmino: tx.legacyAmino,
eipToSign,
}
return createTx(chain, sender, fee, memo, types, msg, msgCosmos)
}
export function createTxMsgCancelBond(
@@ -265,13 +157,6 @@ export function createTxMsgCancelBond(
memo: string,
params: MessageMsgCancelBond,
) {
// EIP712
const feeObject = generateFee(
fee.amount,
fee.denom,
fee.gas,
sender.accountAddress,
)
const types = generateTypes(MSG_CANCEL_BOND_TYPES)
const msg = createMsgCancelBond(
@@ -279,40 +164,12 @@ export function createTxMsgCancelBond(
sender.accountAddress
)
const messages = generateMessage(
sender.accountNumber.toString(),
sender.sequence.toString(),
chain.cosmosChainId,
memo,
feeObject,
msg,
)
const eipToSign = createEIP712(types, chain.chainId, messages)
// Cosmos
const msgCosmos = protoCreateMsgCancelBond(
params.id,
sender.accountAddress
)
const tx = createTransaction(
msgCosmos,
memo,
fee.amount,
fee.denom,
parseInt(fee.gas, 10),
'ethsecp256',
sender.pubkey,
sender.sequence,
sender.accountNumber,
chain.cosmosChainId,
)
return {
signDirect: tx.signDirect,
legacyAmino: tx.legacyAmino,
eipToSign,
}
return createTx(chain, sender, fee, memo, types, msg, msgCosmos)
}
function createMsgCreateBond(
@@ -1,7 +1,4 @@
import {
createEIP712,
generateFee,
generateMessage,
generateTypes,
} from '@tharsis/eip712'
import {
@@ -9,9 +6,9 @@ import {
Sender,
Fee,
} from '@tharsis/transactions'
import { createTransaction } from '@tharsis/proto'
import * as nameserviceTx from './proto/vulcanize/nameservice/v1beta1/tx'
import * as nameserviceTx from '../proto/vulcanize/nameservice/v1beta1/tx'
import { createTx } from './util'
const MSG_RESERVE_AUTHORITY_TYPES = {
MsgValue: [
@@ -33,13 +30,6 @@ export function createTxMsgReserveAuthority(
memo: string,
params: MessageMsgReserveAuthority,
) {
// EIP712
const feeObject = generateFee(
fee.amount,
fee.denom,
fee.gas,
sender.accountAddress,
)
const types = generateTypes(MSG_RESERVE_AUTHORITY_TYPES)
const msg = createMsgReserveAuthority(
@@ -48,41 +38,13 @@ export function createTxMsgReserveAuthority(
params.owner
)
const messages = generateMessage(
sender.accountNumber.toString(),
sender.sequence.toString(),
chain.cosmosChainId,
memo,
feeObject,
msg,
)
const eipToSign = createEIP712(types, chain.chainId, messages)
// Cosmos
const msgCosmos = protoCreateMsgReserveAuthority(
params.name,
sender.accountAddress,
params.owner
)
const tx = createTransaction(
msgCosmos,
memo,
fee.amount,
fee.denom,
parseInt(fee.gas, 10),
'ethsecp256',
sender.pubkey,
sender.sequence,
sender.accountNumber,
chain.cosmosChainId,
)
return {
signDirect: tx.signDirect,
legacyAmino: tx.legacyAmino,
eipToSign,
}
return createTx(chain, sender, fee, memo, types, msg, msgCosmos)
}
function createMsgReserveAuthority(
+79
View File
@@ -0,0 +1,79 @@
import { Message } from "google-protobuf";
import {
createEIP712,
generateFee,
generateMessage,
generateTypes,
} from '@tharsis/eip712'
import {
Chain,
Sender,
Fee,
} from '@tharsis/transactions'
import { createTransaction } from '@tharsis/proto'
interface Msg {
type: string
value: any
}
interface MsgCosmos {
message: Message
path: string
}
interface Types {
[key: string]: Array<{
name: string
type: string
}>
}
export const createTx = (
chain: Chain,
sender: Sender,
fee: Fee,
memo: string,
messageTypes: Types,
msg: Msg,
msgCosmos: MsgCosmos,
) => {
// EIP712
const feeObject = generateFee(
fee.amount,
fee.denom,
fee.gas,
sender.accountAddress,
)
const types = generateTypes(messageTypes)
const messages = generateMessage(
sender.accountNumber.toString(),
sender.sequence.toString(),
chain.cosmosChainId,
memo,
feeObject,
msg,
)
const eipToSign = createEIP712(types, chain.chainId, messages)
// Cosmos
const tx = createTransaction(
msgCosmos,
memo,
fee.amount,
fee.denom,
parseInt(fee.gas, 10),
'ethsecp256',
sender.pubkey,
sender.sequence,
sender.accountNumber,
chain.cosmosChainId,
)
return {
signDirect: tx.signDirect,
legacyAmino: tx.legacyAmino,
eipToSign,
}
}
+3 -9
View File
@@ -2,11 +2,11 @@ import assert from 'assert';
import { Account } from './account';
import { Registry } from './index';
import { getConfig, wait } from './testing/helper';
import { getConfig } from './testing/helper';
jest.setTimeout(120 * 1000);
const { mockServer, chibaClonk: { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } } = getConfig();
const { chainId, restEndpoint, gqlEndpoint, privateKey, accountAddress, fee } = getConfig();
const namingTests = () => {
let registry: Registry;
@@ -21,13 +21,11 @@ const namingTests = () => {
// Create bond.
bondId = await registry.getNextBondId(accountAddress);
await registry.createBond({ denom: 'aphoton', amount: '1000000000' }, accountAddress, privateKey, fee);
await wait(5000)
});
test('Reserve authority.', async () => {
authorityName = `dxos-${Date.now()}`;
await registry.reserveAuthority({ name: authorityName, owner: accountAddress }, accountAddress, privateKey, fee);
await wait(5000)
});
test('Lookup authority.', async () => {
@@ -54,7 +52,6 @@ const namingTests = () => {
test('Reserve sub-authority.', async () => {
const subAuthority = `echo.${authorityName}`;
await registry.reserveAuthority({ name: subAuthority, owner: accountAddress }, accountAddress, privateKey, fee);
await wait(5000)
const [record] = await registry.lookupAuthorities([subAuthority]);
expect(record).toBeDefined();
@@ -77,11 +74,8 @@ const namingTests = () => {
assert(otherAccount2.formattedCosmosAddress)
await registry.sendCoins({ denom: 'aphoton', amount: '10', destinationAddress: otherAccount2.formattedCosmosAddress }, accountAddress, privateKey, fee);
await wait(5000)
const subAuthority = `halo.${authorityName}`;
await registry.reserveAuthority({ name: subAuthority, owner: otherAccount1.formattedCosmosAddress }, accountAddress, privateKey, fee);
await wait(5000)
const [record] = await registry.lookupAuthorities([subAuthority]);
expect(record).toBeDefined();
@@ -92,7 +86,7 @@ const namingTests = () => {
});
};
if (mockServer || process.env.WIRE_AUCTIONS_ENABLED) {
if (process.env.AUCTIONS_ENABLED) {
// Required as jest complains if file has no tests.
test('skipping naming tests', () => {});
} else {
+8 -9
View File
@@ -1,14 +1,13 @@
const DEFAULT_PRIVATE_KEY = '39e06e1471f69a76491e60d1d22908789bf7801039a9ac2197ed432ad45d2daf';
const DEFAULT_ADDRESS = 'ethm1p9fqwtlypqptuqgndpce5g6wncj4py9z30wfkt'
import assert from 'assert';
export const wait = (time: number) => new Promise(resolve => setTimeout(resolve, time))
export const getConfig = () => {
assert(process.env.PRIVATE_KEY);
assert(process.env.ACCOUNT_ADDRESS);
export const getConfig = () => ({
mockServer: process.env.MOCK_SERVER || false,
chibaClonk: {
return {
chainId: process.env.CHIBA_CLONK_CHAIN_ID || 'ethermint_9000-1',
privateKey: DEFAULT_PRIVATE_KEY,
accountAddress: DEFAULT_ADDRESS,
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: {
@@ -17,4 +16,4 @@ export const getConfig = () => ({
gas: '200000',
}
}
});
};