Skip to content

Commit

Permalink
crypto-org-chain#284: Support MsgUpdateClient
Browse files Browse the repository at this point in the history
  • Loading branch information
cdc-Hitesh committed Jul 8, 2021
1 parent 4e432a3 commit b36cb14
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/src/transaction/common/constants/typeurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export const typeUrlToMsgClassMapping = (cro: any, typeUrl: string) => {
return cro.ibc.MsgTransfer;
case COSMOS_MSG_TYPEURL.ibc.MsgCreateClient:
return cro.ibc.MsgCreateClient;
case COSMOS_MSG_TYPEURL.ibc.MsgUpdateClient:
return cro.ibc.MsgUpdateClient;

// nft
case COSMOS_MSG_TYPEURL.nft.MsgIssueDenom:
Expand Down
57 changes: 50 additions & 7 deletions lib/src/transaction/msg/ibc/core/MsgUpdateClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { fuzzyDescribe } from '../../../../test/mocha-fuzzy/suite';
import { Msg } from '../../../../cosmos/v1beta1/types/msg';
import { Secp256k1KeyPair } from '../../../../keypair/secp256k1';
import { Bytes } from '../../../../utils/bytes/bytes';
import { CroSDK } from '../../../../core/cro';
import { CroSDK, CroNetwork } from '../../../../core/cro';
import { COSMOS_MSG_TYPEURL } from '../../../common/constants/typeurl';
import { google } from '../../../../cosmos/v1beta1/codec';

const cro = CroSDK({
network: {
Expand Down Expand Up @@ -73,10 +72,7 @@ describe('Testing MsgUpdateClient', function () {

const MsgUpdateClient = new cro.ibc.MsgUpdateClient({
signer: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
header: google.protobuf.Any.create({
type_url: '/some.valid.type.url',
value: new Uint8Array([1, 2, 35, 5]),
}),
header: undefined,
clientId: 'clientId',
});

Expand All @@ -94,7 +90,7 @@ describe('Testing MsgUpdateClient', function () {

const signedTxHex = signedTx.encode().toHexString();
expect(signedTxHex).to.be.eql(
'0a7e0a7c0a232f6962632e636f72652e636c69656e742e76312e4d7367557064617465436c69656e7412550a08636c69656e744964121c0a142f736f6d652e76616c69642e747970652e75726c1204010223051a2b7463726f313573667570643236737036716633376c6c3571367875663333306b37646639746e767271687412580a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a2103fd0d560b6c4aa1ca16721d039a192867c3457e19dad553edb98e7ba88b159c2712040a0208011802120410c09a0c1a40a3e29655c317f80832e34c978220cbd8d9f38a80353228b6d3b2db4a3d7eafe5717f449fa2ab5afacb0531ca63afb55b9088571248cf07876896595459487cd2',
'0a600a5e0a232f6962632e636f72652e636c69656e742e76312e4d7367557064617465436c69656e7412370a08636c69656e7449641a2b7463726f313573667570643236737036716633376c6c3571367875663333306b37646639746e767271687412580a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a2103fd0d560b6c4aa1ca16721d039a192867c3457e19dad553edb98e7ba88b159c2712040a0208011802120410c09a0c1a40c8529d07e0c51b9d14a2fc77475c6ffbefd4fed6305392f5979f489164e6102546f3e5c537fcbee75587e36eb0206326639c6807d0e2afd1d1c3c3c16e7ec5ec',
);
});

Expand All @@ -117,4 +113,51 @@ describe('Testing MsgUpdateClient', function () {

expect(() => MsgUpdateClient.toRawAminoMsg()).to.throw('IBC Module not supported under amino encoding scheme');
});
describe('fromCosmosJSON', function () {
it('should throw Error if the JSON is not a IBC MsgUpdateClient', function () {
const json =
'{ "@type": "/cosmos.bank.v1beta1.MsgCreateValidator", "amount": [{ "denom": "basetcro", "amount": "3478499933290496" }], "from_address": "tcro1x07kkkepfj2hl8etlcuqhej7jj6myqrp48y4hg", "to_address": "tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3" }';
expect(() => cro.ibc.MsgUpdateClient.fromCosmosMsgJSON(json, CroNetwork.Testnet)).to.throw(
'Expected /ibc.core.client.v1.MsgUpdateClient but got /cosmos.bank.v1beta1.MsgCreateValidator',
);
});
it('should throw on invalid `signer`', function () {
const json = `
{
"@type": "/ibc.core.client.v1.MsgUpdateClient",
"signer": "cosmos1u8prj0rj3ur7kr23dhjgyteuq55ntahfuzlf6g",
"client_id": "07-tendermint-33"
}
`;

expect(() => cro.ibc.MsgUpdateClient.fromCosmosMsgJSON(json, CroNetwork.Testnet)).to.throw(
'Provided `signer` does not match network selected',
);
});
it('should throw on invalid `clientId`', function () {
const json = `
{
"@type": "/ibc.core.client.v1.MsgUpdateClient",
"signer": "cosmos1u8prj0rj3ur7kr23dhjgyteuq55ntahfuzlf6g"
}
`;

expect(() => cro.ibc.MsgUpdateClient.fromCosmosMsgJSON(json, CroNetwork.Testnet)).to.throw(
'Expected property `clientId` to be of type `string` but received type `undefined` in object `options`',
);
});
it('should return the IBC MsgUpdateClient corresponding to the JSON', function () {
const json = `{
"@type": "/ibc.core.client.v1.MsgUpdateClient",
"signer": "tcro1agr5hwr6gxljf4kpg6fm7l7ehjxtyazg86nef8",
"client_id": "07-tendermint-33"
}
`;

const MsgUpdateClient = cro.ibc.MsgUpdateClient.fromCosmosMsgJSON(json, CroNetwork.Testnet);
expect(MsgUpdateClient.signer).to.eql('tcro1agr5hwr6gxljf4kpg6fm7l7ehjxtyazg86nef8');
expect(MsgUpdateClient.clientId).to.eql('07-tendermint-33');
expect(MsgUpdateClient.header).to.be.null;
});
});
});
119 changes: 119 additions & 0 deletions lib/src/transaction/msg/ibc/core/MsgUpdateClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable camelcase */
import ow from 'ow';
import { google } from '../../../../cosmos/v1beta1/codec/generated/codecimpl';
import { InitConfigurations } from '../../../../core/cro';
Expand All @@ -7,6 +8,7 @@ import { COSMOS_MSG_TYPEURL } from '../../../common/constants/typeurl';
import { validateAddress, AddressType } from '../../../../utils/address';
import { owMsgUpdateClientOptions } from '../../ow.types';
import * as legacyAmino from '../../../../cosmos/amino';
import { Network } from '../../../../network/network';

export const msgUpdateClientIBC = function (config: InitConfigurations) {
return class MsgUpdateClient implements CosmosMsg {
Expand Down Expand Up @@ -53,6 +55,30 @@ export const msgUpdateClientIBC = function (config: InitConfigurations) {
throw new Error('IBC Module not supported under amino encoding scheme');
}

/**
* Returns an instance of IBC.MsgUpdateClient
* @param {string} msgJsonStr
* @param {Network} network
* @returns {MsgUpdateClient}
*/
public static fromCosmosMsgJSON(msgJsonStr: string, _network: Network): MsgUpdateClient {
const parsedMsg = JSON.parse(msgJsonStr) as MsgUpdateClientJsonRaw;
if (parsedMsg['@type'] !== COSMOS_MSG_TYPEURL.ibc.MsgUpdateClient) {
throw new Error(`Expected ${COSMOS_MSG_TYPEURL.ibc.MsgUpdateClient} but got ${parsedMsg['@type']}`);
}

// TODO: The `header` value needs to be handled, currently keeping it as `null`
if (typeof parsedMsg.header === 'object' && Object.keys(parsedMsg.header).length > 0) {
throw new Error('IBC MsgUpdateClient does not support `header` decoding.');
}

return new MsgUpdateClient({
clientId: parsedMsg.client_id,
header: null,
signer: parsedMsg.signer,
});
}

validateAddresses() {
// TODO: Can `signer` be from non-CRO network
if (
Expand All @@ -73,3 +99,96 @@ export type MsgUpdateClientOptions = {
header?: google.protobuf.IAny | null;
signer: string;
};

export interface MsgUpdateClientJsonRaw {
'@type': string;
client_id: string;
header: any | MsgUpdateClientJsonRawHeader;
signer: string;
}

export interface MsgUpdateClientJsonRawHeader {
'@type': string;
signed_header: SignedHeader;
validator_set: TrustedValidators;
trusted_height: TrustedHeight;
trusted_validators: TrustedValidators;
}

export interface SignedHeader {
header: SignedHeaderHeader;
commit: Commit;
}

export interface Commit {
height: string;
round: number;
block_id: Blockid;
signatures: Signature[];
}

export interface Blockid {
hash: string;
part_set_header: PartSetHeader;
}

export interface PartSetHeader {
total: number;
hash: string;
}

export interface Signature {
block_id_flag: BlockidFlag;
validator_address: null | string;
timestamp: Date;
signature: null | string;
}

export enum BlockidFlag {
BlockIDFlagAbsent = 'BLOCK_ID_FLAG_ABSENT',
BlockIDFlagCommit = 'BLOCK_ID_FLAG_COMMIT',
}

export interface SignedHeaderHeader {
version: Version;
chain_id: string;
height: string;
time: Date;
last_block_id: Blockid;
last_commit_hash: string;
data_hash: string;
validators_hash: string;
next_validators_hash: string;
consensus_hash: string;
app_hash: string;
last_results_hash: string;
evidence_hash: string;
proposer_address: string;
}

export interface Version {
block: string;
app: string;
}

export interface TrustedHeight {
revision_number: string;
revision_height: string;
}

export interface TrustedValidators {
validators: Proposer[];
proposer: Proposer;
total_voting_power: string;
}

export interface Proposer {
address: string;
pub_key: PubKey;
voting_power: string;
proposer_priority: string;
}

export interface PubKey {
ed25519: string;
}

0 comments on commit b36cb14

Please sign in to comment.