Skip to content

Commit

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

// nft
case COSMOS_MSG_TYPEURL.nft.MsgIssueDenom:
return cro.nft.MsgIssueDenom;
Expand Down
36 changes: 34 additions & 2 deletions lib/src/transaction/msg/ibc/core/MsgCreateClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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';

Expand Down Expand Up @@ -95,7 +95,7 @@ describe('Testing MsgCreateClient', function () {

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

Expand All @@ -116,4 +116,36 @@ describe('Testing MsgCreateClient', function () {

expect(() => MsgCreateClient.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 MsgCreateClient', function () {
const json =
'{ "@type": "/cosmos.bank.v1beta1.MsgCreateValidator", "amount": [{ "denom": "basetcro", "amount": "3478499933290496" }], "from_address": "tcro1x07kkkepfj2hl8etlcuqhej7jj6myqrp48y4hg", "to_address": "tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3" }';
expect(() => cro.ibc.MsgCreateClient.fromCosmosMsgJSON(json, CroNetwork.Testnet)).to.throw(
'Expected /ibc.core.client.v1.MsgCreateClient but got /cosmos.bank.v1beta1.MsgCreateValidator',
);
});
it('should throw on invalid `sender`', function () {
const json = `
{
"@type": "/ibc.core.client.v1.MsgCreateClient",
"signer": "cosmos1u8prj0rj3ur7kr23dhjgyteuq55ntahfuzlf6g"
}
`;

expect(() => cro.ibc.MsgCreateClient.fromCosmosMsgJSON(json, CroNetwork.Testnet)).to.throw(
'Provided `signer` does not match network selected',
);
});
it('should return the IBC MsgCreateClient corresponding to the JSON', function () {
const json = `{
"@type": "/ibc.core.client.v1.MsgCreateClient",
"signer": "tcro1agr5hwr6gxljf4kpg6fm7l7ehjxtyazg86nef8"
}
`;

const MsgCreateClient = cro.ibc.MsgCreateClient.fromCosmosMsgJSON(json, CroNetwork.Testnet);
expect(MsgCreateClient.signer).to.eql('tcro1agr5hwr6gxljf4kpg6fm7l7ehjxtyazg86nef8');
});
});
});
33 changes: 33 additions & 0 deletions lib/src/transaction/msg/ibc/core/MsgCreateClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { COSMOS_MSG_TYPEURL } from '../../../common/constants/typeurl';
import { validateAddress, AddressType } from '../../../../utils/address';
import { owMsgCreateClientOptions } from '../../ow.types';
import * as legacyAmino from '../../../../cosmos/amino';
import { Network } from '../../../../network/network';

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

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

return new MsgCreateClient({
clientState: google.protobuf.Any.create({
type_url: parsedMsg.clientState?.['@type'],
value: parsedMsg.clientState?.value,
}),
consensusState: google.protobuf.Any.create({
type_url: parsedMsg.consensusState?.['@type'],
value: parsedMsg.consensusState?.value,
}),
signer: parsedMsg.signer,
});
}

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

interface IBCMsgCreateClientRaw {
'@type': string;
signer: string;
clientState?: { '@type': string; value: any };
consensusState?: { '@type': string; value: any };
}

0 comments on commit 3fc0f4c

Please sign in to comment.