Skip to content

Commit

Permalink
crypto-org-chain#284: Support MsgUpgradeClient
Browse files Browse the repository at this point in the history
  • Loading branch information
cdc-Hitesh committed Jun 22, 2021
1 parent ae9445e commit c0d57b3
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/src/core/cro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { msgBurnNFT } from '../transaction/msg/nft/MsgBurnNFT';
import { msgTransferIBC } from '../transaction/msg/ibc/applications/MsgTransfer';
import { msgCreateClientIBC } from '../transaction/msg/ibc/core/MsgCreateClient';
import { msgUpdateClientIBC } from '../transaction/msg/ibc/core/MsgUpdateClient';
import { msgUpgradeClientIBC } from '../transaction/msg/ibc/core/MsgUpgradeClient';

export const CroSDK = function (configs: InitConfigurations) {
ow(configs, 'configs', owCroSDKInitParams);
Expand Down Expand Up @@ -81,6 +82,7 @@ export const CroSDK = function (configs: InitConfigurations) {
MsgTransfer: msgTransferIBC(configs),
MsgCreateClient: msgCreateClientIBC(configs),
MsgUpdateClient: msgUpdateClientIBC(configs),
MsgUpgradeClient: msgUpgradeClientIBC(configs),
},
Options: configs,
};
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 @@ -34,6 +34,7 @@ export const typeUrlMappings: {
'/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,
'/ibc.core.client.v1.MsgUpgradeClient': ibc.core.client.v1.MsgUpgradeClient,
};

export interface GeneratedType {
Expand Down
1 change: 1 addition & 0 deletions lib/src/transaction/common/constants/typeurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ export const COSMOS_MSG_TYPEURL = {
MsgTransfer: '/ibc.applications.transfer.v1.MsgTransfer',
MsgCreateClient: '/ibc.core.client.v1.MsgCreateClient',
MsgUpdateClient: '/ibc.core.client.v1.MsgUpdateClient',
MsgUpgradeClient: '/ibc.core.client.v1.MsgUpgradeClient',
},
};
141 changes: 141 additions & 0 deletions lib/src/transaction/msg/ibc/core/MsgUpgradeClient.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
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 } from '../../../../core/cro';
import { COSMOS_MSG_TYPEURL } from '../../../common/constants/typeurl';
import { google } from '../../../../cosmos/v1beta1/codec';

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 MsgUpgradeClient', 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.MsgUpgradeClient(options.value)).to.throw(
'Expected `options` to be of type `object`',
);
});
});

it('Test MsgUpgradeClient conversion', function () {
const MsgUpgradeClient = new cro.ibc.MsgUpgradeClient({
signer: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
clientId: 'clientId',
proofUpgradeConsensusState: new Uint8Array([1, 2]),
proofUpgradeClient: new Uint8Array([3, 4]),
});

const rawMsg: Msg = {
typeUrl: COSMOS_MSG_TYPEURL.ibc.MsgUpgradeClient,
value: {
signer: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
clientId: 'clientId',
clientState: undefined,
proofUpgradeConsensusState: new Uint8Array([1, 2]),
proofUpgradeClient: new Uint8Array([3, 4]),
},
};

expect(MsgUpgradeClient.toRawMsg()).to.deep.eq(rawMsg);
});

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

const MsgUpgradeClient = new cro.ibc.MsgUpgradeClient({
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]),
}),
clientId: 'clientId',
proofUpgradeClient: new Uint8Array(),
proofUpgradeConsensusState: new Uint8Array(),
});

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

const rawTx = new cro.RawTransaction();

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

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

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

it('Should validate MsgUpgradeClient provided addresses with network config', function () {
const params1 = {
signer: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
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]),
}),
clientId: 'clientId',
proofUpgradeClient: new Uint8Array(),
proofUpgradeConsensusState: new Uint8Array(),
};

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

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

expect(() => MsgUpgradeClient.toRawAminoMsg()).to.throw('IBC Module not supported under amino encoding scheme');
});
});
96 changes: 96 additions & 0 deletions lib/src/transaction/msg/ibc/core/MsgUpgradeClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import ow from 'ow';
import { google } from '../../../../cosmos/v1beta1/codec/generated/codecimpl';
import { InitConfigurations } from '../../../../core/cro';
import { CosmosMsg } from '../../cosmosMsg';
import { Msg } from '../../../../cosmos/v1beta1/types/msg';
import { COSMOS_MSG_TYPEURL } from '../../../common/constants/typeurl';
import { validateAddress, AddressType } from '../../../../utils/address';
import { owMsgUpgradeClientOptions } from '../../ow.types';
import * as legacyAmino from '../../../../cosmos/amino';

export const msgUpgradeClientIBC = function (config: InitConfigurations) {
return class MsgUpgradeClient implements CosmosMsg {
/** MsgUpgradeClient clientId. */
public clientId: string;

/** MsgUpgradeClient clientState. */
public clientState?: google.protobuf.IAny | null;

/** MsgUpgradeClient consensusState. */
public consensusState?: google.protobuf.IAny | null;

/** MsgUpgradeClient proofUpgradeClient. */
public proofUpgradeClient: Uint8Array;

/** MsgUpgradeClient proofUpgradeConsensusState. */
public proofUpgradeConsensusState: Uint8Array;

/** MsgUpgradeClient signer. */
public signer: string;

/**
* Constructor to create a new IBC.MsgUpgradeClient
* @param {MsgUpgradeClientOptions} options
* @returns {MsgUpgradeClient}
* @throws {Error} when options is invalid
*/
constructor(options: MsgUpgradeClientOptions) {
ow(options, 'options', owMsgUpgradeClientOptions);
this.clientId = options.clientId;
this.clientState = options.clientState;
this.proofUpgradeClient = options.proofUpgradeClient;
this.proofUpgradeConsensusState = options.proofUpgradeConsensusState;
this.signer = options.signer;
this.validateAddresses();
}

/**
* Returns the raw Msg representation of Ibc.MsgUpgradeClient
* @returns {Msg}
*/
toRawMsg(): Msg {
return {
typeUrl: COSMOS_MSG_TYPEURL.ibc.MsgUpgradeClient,
value: {
clientId: this.clientId,
clientState: this.clientState,
proofUpgradeClient: this.proofUpgradeClient,
proofUpgradeConsensusState: this.proofUpgradeConsensusState,
signer: this.signer,
},
};
}

// eslint-disable-next-line class-methods-use-this
toRawAminoMsg(): legacyAmino.Msg {
throw new Error('IBC Module not supported under amino encoding scheme');
}

validateAddresses() {
// TODO: Can `signer` be from non-CRO network
if (
!validateAddress({
address: this.signer,
network: config.network,
type: AddressType.USER,
})
) {
throw new TypeError('Provided `signer` does not match network selected');
}
}
};
};

export type MsgUpgradeClientOptions = {
clientId: string;

clientState?: google.protobuf.IAny | null;

consensusState?: google.protobuf.IAny | null;

proofUpgradeClient: Uint8Array;

proofUpgradeConsensusState: Uint8Array;

signer: string;
};
9 changes: 9 additions & 0 deletions lib/src/transaction/msg/ow.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,12 @@ export const owMsgUpdateClientOptions = owStrictObject().exactShape({
clientId: ow.string,
header: ow.optional.any(owGoogleProtoAnyOptional(), ow.optional.null),
});

export const owMsgUpgradeClientOptions = owStrictObject().exactShape({
clientId: ow.string,
clientState: ow.optional.any(owGoogleProtoAnyOptional(), ow.optional.null),
consensusState: ow.optional.any(owGoogleProtoAnyOptional(), ow.optional.null),
proofUpgradeClient: ow.uint8Array,
proofUpgradeConsensusState: ow.uint8Array,
signer: ow.string,
});

0 comments on commit c0d57b3

Please sign in to comment.