Skip to content

Commit

Permalink
Merge pull request #286 from cdc-Hitesh/#284/ibc-MsgUpdateClient
Browse files Browse the repository at this point in the history
Problem: Support IBC `MsgUpdateClient`
  • Loading branch information
crypto-eddy authored Jul 12, 2021
2 parents d51008d + d7832d3 commit da8cc2f
Show file tree
Hide file tree
Showing 8 changed files with 449 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/src/core/cro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { msgFundCommunityPoolV2 } from '../transaction/msg/v2/distribution/v2.Ms
import { msgDepositV2 } from '../transaction/msg/v2/gov/v2.MsgDeposit';
import { communityPoolSpendProposalV2 } from '../transaction/msg/v2/gov/proposal/v2.CommunityPoolSpendProposal';
import { msgSubmitProposalV2 } from '../transaction/msg/v2/gov/v2.MsgSubmitProposal';
import { msgUpdateClientIBC } from '../transaction/msg/ibc/core/MsgUpdateClient';

export const CroSDK = function (configs: InitConfigurations) {
ow(configs, 'configs', owCroSDKInitParams);
Expand Down Expand Up @@ -83,6 +84,7 @@ export const CroSDK = function (configs: InitConfigurations) {
ibc: {
MsgTransfer: msgTransferIBC(configs),
MsgCreateClient: msgCreateClientIBC(configs),
MsgUpdateClient: msgUpdateClientIBC(configs),
},
v2: {
bank: {
Expand Down
1 change: 1 addition & 0 deletions lib/src/cosmos/v1beta1/types/typeurls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const typeUrlMappings: {
'/chainmain.nft.v1.MsgBurnNFT': chainmain.nft.v1.MsgBurnNFT,
'/ibc.applications.transfer.v1.MsgTransfer': ibc.applications.transfer.v1.MsgTransfer,
'/ibc.core.client.v1.MsgCreateClient': ibc.core.client.v1.MsgCreateClient,
'/ibc.core.client.v1.MsgUpdateClient': ibc.core.client.v1.MsgUpdateClient,
};

export interface GeneratedType {
Expand Down
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 @@ -37,6 +37,7 @@ export const COSMOS_MSG_TYPEURL = {
ibc: {
MsgTransfer: '/ibc.applications.transfer.v1.MsgTransfer',
MsgCreateClient: '/ibc.core.client.v1.MsgCreateClient',
MsgUpdateClient: '/ibc.core.client.v1.MsgUpdateClient',
},
};

Expand Down Expand Up @@ -93,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
13 changes: 3 additions & 10 deletions lib/src/transaction/msg/ibc/core/MsgCreateClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Secp256k1KeyPair } from '../../../../keypair/secp256k1';
import { Bytes } from '../../../../utils/bytes/bytes';
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 @@ -71,14 +70,8 @@ describe('Testing MsgCreateClient', function () {

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

const anySigner = {
Expand All @@ -95,7 +88,7 @@ describe('Testing MsgCreateClient', function () {

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

Expand Down
89 changes: 77 additions & 12 deletions lib/src/transaction/msg/ibc/core/MsgCreateClient.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 Down Expand Up @@ -61,20 +62,24 @@ export const msgCreateClientIBC = function (config: InitConfigurations) {
* @returns {MsgCreateClient}
*/
public static fromCosmosMsgJSON(msgJsonStr: string, _network: Network): MsgCreateClient {
const parsedMsg = JSON.parse(msgJsonStr) as IBCMsgCreateClientRaw;
const parsedMsg = JSON.parse(msgJsonStr) as MsgCreateClientJsonRaw;
if (parsedMsg['@type'] !== COSMOS_MSG_TYPEURL.ibc.MsgCreateClient) {
throw new Error(`Expected ${COSMOS_MSG_TYPEURL.ibc.MsgCreateClient} but got ${parsedMsg['@type']}`);
}

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

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

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,
}),
clientState: null,
consensusState: null,
signer: parsedMsg.signer,
});
}
Expand All @@ -100,9 +105,69 @@ export type MsgCreateClientOptions = {
signer: string;
};

interface IBCMsgCreateClientRaw {
export interface MsgCreateClientJsonRaw {
'@type': string;
client_state?: ClientState;
consensus_state?: ConsensusState;
signer: string;
clientState?: { '@type': string; value: any };
consensusState?: { '@type': string; value: any };
}

export interface ClientState {
'@type': string;
chain_id: string;
trust_level: TrustLevel;
trusting_period: string;
unbonding_period: string;
max_clock_drift: string;
frozen_height: Height;
latest_height: Height;
proof_specs: ProofSpec[];
upgrade_path: string[];
allow_update_after_expiry: boolean;
allow_update_after_misbehaviour: boolean;
}

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

export interface ProofSpec {
leaf_spec: LeafSpec;
inner_spec: InnerSpec;
max_depth: number;
min_depth: number;
}

export interface InnerSpec {
child_order: number[];
child_size: number;
min_prefix_length: number;
max_prefix_length: number;
empty_child: null;
hash: string;
}

export interface LeafSpec {
hash: string;
prehash_key: string;
prehash_value: string;
length: string;
prefix: string;
}

export interface TrustLevel {
numerator: string;
denominator: string;
}

export interface ConsensusState {
'@type': string;
timestamp: Date;
root: Root;
next_validators_hash: string;
}

export interface Root {
hash: string;
}
163 changes: 163 additions & 0 deletions lib/src/transaction/msg/ibc/core/MsgUpdateClient.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import 'mocha';
import { expect } from 'chai';
import Big from 'big.js';

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, CroNetwork } from '../../../../core/cro';
import { COSMOS_MSG_TYPEURL } from '../../../common/constants/typeurl';

const cro = CroSDK({
network: {
defaultNodeUrl: '',
chainId: 'testnet-croeseid-1',
addressPrefix: 'tcro',
validatorAddressPrefix: 'tcrocncl',
validatorPubKeyPrefix: 'tcrocnclconspub',
coin: {
baseDenom: 'basetcro',
croDenom: 'tcro',
},
bip44Path: {
coinType: 1,
account: 0,
},
rpcUrl: '',
},
});

describe('Testing MsgUpdateClient', function () {
fuzzyDescribe('should throw Error when options is invalid', function (fuzzy) {
const anyValidOptions = {
signer: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
};

const testRunner = fuzzy(fuzzy.ObjArg(anyValidOptions));

testRunner(function (options) {
if (options.valid) {
return;
}
expect(() => new cro.ibc.MsgUpdateClient(options.value)).to.throw(
'Expected `options` to be of type `object`',
);
});
});

it('Test MsgUpdateClient conversion', function () {
const MsgUpdateClient = new cro.ibc.MsgUpdateClient({
signer: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
clientId: 'clientId',
header: null,
});

const rawMsg: Msg = {
typeUrl: COSMOS_MSG_TYPEURL.ibc.MsgUpdateClient,
value: {
signer: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
clientId: 'clientId',
header: null,
},
};

expect(MsgUpdateClient.toRawMsg()).to.eqls(rawMsg);
});

it('Test appendTxBody MsgUpdateClient Tx signing', function () {
const anyKeyPair = Secp256k1KeyPair.fromPrivKey(
Bytes.fromHexString('66633d18513bec30dd11a209f1ceb1787aa9e2069d5d47e590174dc9665102b3'),
);

const MsgUpdateClient = new cro.ibc.MsgUpdateClient({
signer: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
header: undefined,
clientId: 'clientId',
});

const anySigner = {
publicKey: anyKeyPair.getPubKey(),
accountNumber: new Big(0),
accountSequence: new Big(2),
};

const rawTx = new cro.RawTransaction();

const signableTx = rawTx.appendMessage(MsgUpdateClient).addSigner(anySigner).toSignable();

const signedTx = signableTx.setSignature(0, anyKeyPair.sign(signableTx.toSignDocumentHash(0))).toSigned();

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

it('Should validate MsgUpdateClient provided addresses with network config', function () {
const params1 = {
signer: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
clientId: 'clientId',
};

expect(() => new cro.ibc.MsgUpdateClient(params1)).to.throw(
'Provided `signer` does not match network selected',
);
});

it('Should throw on getting toRawAminoMsg()', function () {
const MsgUpdateClient = new cro.ibc.MsgUpdateClient({
signer: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
clientId: 'clientId',
});

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;
});
});
});
Loading

0 comments on commit da8cc2f

Please sign in to comment.