Skip to content

Commit

Permalink
crypto-org-chain#284: MsgSubmitBhevaiour supports offline signing
Browse files Browse the repository at this point in the history
  • Loading branch information
cdc-Hitesh committed Jul 12, 2021
1 parent 44c04c4 commit 1eea6b1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 11 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 @@ -100,6 +100,8 @@ export const typeUrlToMsgClassMapping = (cro: any, typeUrl: string) => {
return cro.ibc.MsgUpdateClient;
case COSMOS_MSG_TYPEURL.ibc.MsgUpgradeClient:
return cro.ibc.MsgUpgradeClient;
case COSMOS_MSG_TYPEURL.ibc.MsgSubmitMisbehaviour:
return cro.ibc.MsgSubmitMisbehaviour;

// nft
case COSMOS_MSG_TYPEURL.nft.MsgIssueDenom:
Expand Down
77 changes: 66 additions & 11 deletions lib/src/transaction/msg/ibc/core/MsgSubmitMisbehaviour.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 @@ -72,10 +71,7 @@ describe('Testing MsgSubmitMisbehaviour', function () {

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

Expand All @@ -93,17 +89,14 @@ describe('Testing MsgSubmitMisbehaviour', function () {

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

it('Should validate MsgSubmitMisbehaviour provided addresses with network config', function () {
const params1 = {
signer: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
misbehaviour: google.protobuf.Any.create({
type_url: '/some.valid.type.url',
value: new Uint8Array([1, 2, 35, 5]),
}),
misbehaviour: undefined,
clientId: 'clientId',
};

Expand All @@ -122,4 +115,66 @@ describe('Testing MsgSubmitMisbehaviour', function () {
'IBC Module not supported under amino encoding scheme',
);
});

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

expect(() => cro.ibc.MsgSubmitMisbehaviour.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.MsgSubmitMisbehaviour",
"signer": "cosmos1u8prj0rj3ur7kr23dhjgyteuq55ntahfuzlf6g"
}
`;

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

expect(() => cro.ibc.MsgSubmitMisbehaviour.fromCosmosMsgJSON(json, CroNetwork.Testnet)).to.throw(
'IBC MsgSubmitMisbehaviour does not support `misbehaviour` decoding.',
);
});
it('should return the IBC MsgSubmitMisbehaviour corresponding to the JSON', function () {
const json = `{
"@type": "/ibc.core.client.v1.MsgSubmitMisbehaviour",
"signer": "tcro1agr5hwr6gxljf4kpg6fm7l7ehjxtyazg86nef8",
"client_id": "07-tendermint-33"
}
`;

const MsgSubmitMisbehaviour = cro.ibc.MsgSubmitMisbehaviour.fromCosmosMsgJSON(json, CroNetwork.Testnet);
expect(MsgSubmitMisbehaviour.signer).to.eql('tcro1agr5hwr6gxljf4kpg6fm7l7ehjxtyazg86nef8');
expect(MsgSubmitMisbehaviour.clientId).to.eql('07-tendermint-33');
expect(MsgSubmitMisbehaviour.misbehaviour).to.be.null;
});
});
});
35 changes: 35 additions & 0 deletions lib/src/transaction/msg/ibc/core/MsgSubmitMisbehaviour.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 { owMsgSubmitMisbehaviourOptions } from '../../ow.types';
import * as legacyAmino from '../../../../cosmos/amino';
import { Network } from '../../../../network/network';

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

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

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

return new MsgSubmitMisbehaviour({
clientId: parsedMsg.client_id,
misbehaviour: null,
signer: parsedMsg.signer,
});
}

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

export interface MsgSubmitMisbehaviourJsonRaw {
'@type': string;
client_id: string;
misbehaviour?: any;
signer: string;
}

0 comments on commit 1eea6b1

Please sign in to comment.