Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into fix/upgradeDependency
  • Loading branch information
cdc-Hitesh committed Jun 3, 2022
2 parents 394a945 + 09ee7ce commit ac77333
Show file tree
Hide file tree
Showing 14 changed files with 2,116 additions and 92 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

All notable changes to this project will be documented in this file.

## v1.0.3
## v1.0.5
- [Dependency] Update dependency for `protobufjs` to `6.11.3`

## v1.0.4
- Added support for `MsgCreateVestingAccount` and `DelayedVestingAccount`

## v1.0.3
- Added support for transferring very large numbers. Using BigInt

## v1.0.2
- Added IBC MsgTransfer amino support
- Added utility `convertEVMAddressToBech32Address` for EVM<->Bech32 address conversion
Expand Down
6 changes: 6 additions & 0 deletions lib/src/core/cro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import { msgConsensusState } from '../transaction/msg/ibc/lightclients/Consensus
import { msgHeader } from '../transaction/msg/ibc/lightclients/Header';
import { MsgConnectionOpenConfirmIBC } from '../transaction/msg/ibc/core/connection/MsgConnectionOpenConfirm';
import { MsgConnectionOpenTryIBC } from '../transaction/msg/ibc/core/connection/MsgConnectionOpenTry';
import { msgCreateVestingAccount } from '../transaction/msg/account/MsgCreateVestingAccount';
import { delayedVestingAccount } from '../transaction/msg/account/DelayedVestingAccount';

export const CroSDK = function (configs: InitConfigurations) {
ow(configs, 'configs', owCroSDKInitParams);
Expand Down Expand Up @@ -90,6 +92,10 @@ export const CroSDK = function (configs: InitConfigurations) {
MsgTransferNFT: msgTransferNFT(configs),
MsgBurnNFT: msgBurnNFT(configs),
},
accounts: {
MsgCreateVestingAccount: msgCreateVestingAccount(configs),
DelayedVestingAccount: delayedVestingAccount(configs),
},
ibc: {
MsgTransfer: msgTransferIBC(configs),
MsgCreateClient: msgCreateClientIBC(configs),
Expand Down
718 changes: 718 additions & 0 deletions lib/src/cosmos/v1beta1/codec/generated/codecimpl.d.ts

Large diffs are not rendered by default.

592 changes: 592 additions & 0 deletions lib/src/cosmos/v1beta1/codec/generated/codecimpl.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/src/cosmos/v1beta1/scripts/predefine-proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ mkdir -p "$GENERATED_DIR"
"$COSMOS_PROTO_DIR/gov/v1beta1/gov.proto" \
"$COSMOS_PROTO_DIR/params/v1beta1/params.proto" \
"$COSMOS_PROTO_DIR/upgrade/v1beta1/upgrade.proto" \
"$COSMOS_PROTO_DIR/auth/v1beta1/auth.proto" \
"$COSMOS_PROTO_DIR/vesting/v1beta1/tx.proto" \
"$COSMOS_PROTO_DIR/vesting/v1beta1/vesting.proto" \
"$ICS23_PROTO_DIR/proofs.proto" \
"$IBC_PROTO_DIR/core/commitment/v1/commitment.proto" \
"$IBC_PROTO_DIR/applications/transfer/v1/tx.proto" \
Expand Down
2 changes: 2 additions & 0 deletions lib/src/cosmos/v1beta1/types/typeurls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export const typeUrlMappings: {
'/ibc.lightclients.tendermint.v1.ClientState': ibc.lightclients.tendermint.v1.ClientState,
'/ibc.lightclients.tendermint.v1.ConsensusState': ibc.lightclients.tendermint.v1.ConsensusState,
'/ibc.lightclients.tendermint.v1.Header': ibc.lightclients.tendermint.v1.Header,
'/cosmos.vesting.v1beta1.MsgCreateVestingAccount': cosmos.vesting.v1beta1.MsgCreateVestingAccount,
'/cosmos.vesting.v1beta1.DelayedVestingAccount': cosmos.vesting.v1beta1.DelayedVestingAccount,
};

export interface GeneratedType {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/transaction/common/constants/typeurl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export const COSMOS_MSG_TYPEURL = {
MsgConnectionOpenTry: '/ibc.core.connection.v1.MsgConnectionOpenTry',
},
},
account: {
MsgCreateVestingAccount: '/cosmos.vesting.v1beta1.MsgCreateVestingAccount',
DelayedVestingAccount: '/cosmos.vesting.v1beta1.DelayedVestingAccount',
},
};

export const typeUrlToMsgClassMapping = (cro: any, typeUrl: string) => {
Expand Down
160 changes: 160 additions & 0 deletions lib/src/transaction/msg/account/DelayedVestingAccount.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import 'mocha';
import { expect } from 'chai';
import Big from 'big.js';

import Long from 'long';
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 { Units } from '../../../coin/coin';
import { CroSDK } from '../../../core/cro';

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 DelayedVestingAccount', function () {
describe('fromCosmosJSON', function () {
it('should throw Error if the JSON is not a DelayedVestingAccount', function () {
const json =
'{ "@type": "/cosmos.bank.v1beta1.MsgCreateValidator", "amount": { "denom": "basetcro", "amount": "3478499933290496" }, "from_address": "tcro1x07kkkepfj2hl8etlcuqhej7jj6myqrp48y4hg", "to_address": "tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3" }';
expect(() => cro.accounts.DelayedVestingAccount.fromCosmosMsgJSON(json)).to.throw(
'Expected /cosmos.vesting.v1beta1.DelayedVestingAccount but got /cosmos.bank.v1beta1.MsgCreateValidator',
);
});
it('should return the DelayedVestingAccount corresponding to the JSON', function () {
const json = `{"@type":"/cosmos.vesting.v1beta1.DelayedVestingAccount","base_vesting_account":{"base_account":{"address":"cro18n9xzc576np866k2ze3x5fwp8wvelf52avtwdf","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"Argwi2l2NcgtYU2NSPknoeciqOBDWZgv96O7Q3iOK1ei"},"account_number":"67","sequence":"8"},"original_vesting":[{"denom":"basecro","amount":"606902735563"}],"delegated_free":[{"denom":"basecro","amount":"2647264437"}],"delegated_vesting":[{"denom":"basecro","amount":"606902735563"}],"end_time":"1648170000"}}`;
const DelayedVestingAccount = cro.accounts.DelayedVestingAccount.fromCosmosMsgJSON(json);

expect(DelayedVestingAccount.endTime.toString()).to.eql('1648170000');

expect(DelayedVestingAccount.delegatedVesting[0].toCosmosCoin().amount).to.eql('606902735563');
expect(DelayedVestingAccount.delegatedVesting[0].toCosmosCoin().denom).to.eql('basecro');

expect(DelayedVestingAccount.delegatedFree[0].toCosmosCoin().amount).to.eql('2647264437');
expect(DelayedVestingAccount.delegatedFree[0].toCosmosCoin().denom).to.eql('basecro');

expect(DelayedVestingAccount.originalVesting[0].toCosmosCoin().amount).to.eql('606902735563');
expect(DelayedVestingAccount.originalVesting[0].toCosmosCoin().denom).to.eql('basecro');
});
});

fuzzyDescribe('should throw Error when options is invalid', function (fuzzy) {
const anyValidOptions = {
fromAddress: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
toAddress: 'tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3',
amount: new cro.Coin('1000', Units.BASE),
};
const testRunner = fuzzy(fuzzy.ObjArg(anyValidOptions));

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

it('Test DelayedVestingAccount conversion', function () {
const DelayedVestingAccount = new cro.accounts.DelayedVestingAccount({
baseAccount: {
address: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
pubKey: null,
accountNumber: Long.fromString('10'),
sequence: Long.fromString('10'),
},
originalVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
delegatedFree: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
delegatedVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
endTime: Long.fromString('1648170000'),
});

const rawMsg: Msg = {
typeUrl: '/cosmos.vesting.v1beta1.DelayedVestingAccount',
value: {
baseVestingAccount: {
baseAccount: {
accountNumber: Long.fromString('10'),
address: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
sequence: Long.fromString('10'),
},
delegatedFree: [
{
amount: '111111',
denom: 'basetcro',
},
],
delegatedVesting: [
{
amount: '111111',
denom: 'basetcro',
},
],
endTime: Long.fromString('1648170000'),
originalVesting: [
{
amount: '111111',
denom: 'basetcro',
},
],
},
},
};

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

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

const DelayedVestingAccount = new cro.accounts.DelayedVestingAccount({
baseAccount: {
address: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
pubKey: null,
accountNumber: Long.fromString('10'),
sequence: Long.fromString('10'),
},
originalVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
delegatedFree: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
delegatedVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
endTime: Long.fromString('1648170000'),
});

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

const rawTx = new cro.RawTransaction();

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

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

const signedTxHex = signedTx.encode().toHexString();
expect(signedTxHex).to.be.eql(
'0aab010aa8010a2d2f636f736d6f732e76657374696e672e763162657461312e44656c6179656456657374696e674163636f756e7412770a750a310a2b7463726f313635747a63726832796c3833673871657178756567326735677a6775353779336665336b6333180a200a12120a08626173657463726f12063131313131311a120a08626173657463726f120631313131313122120a08626173657463726f12063131313131312890a8f4910612580a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a2103fd0d560b6c4aa1ca16721d039a192867c3457e19dad553edb98e7ba88b159c2712040a0208011802120410c09a0c1a409e4105e3d17d411f722f69afff864e6d22c2d4cc5a66b4b2ec77b6fb512401f67ba8615e61f16b6db243e33e376ce7252190d5ee202346f06b189523fb6c05d6',
);
});
});
165 changes: 165 additions & 0 deletions lib/src/transaction/msg/account/DelayedVestingAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/* eslint-disable camelcase */
import ow from 'ow';
import Long from 'long';
import { Msg } from '../../../cosmos/v1beta1/types/msg';
import { owDelayedVestingAccountOptions } from '../ow.types';
import { CosmosMsg } from '../cosmosMsg';
import { COSMOS_MSG_TYPEURL } from '../../common/constants/typeurl';
import * as legacyAmino from '../../../cosmos/amino';
import { ICoin } from '../../../coin/v2.coin/v2.coin';
import { IGoogleAny } from '../ibc/IGoogleAny';
import { CroSDK, InitConfigurations } from '../../../core/cro';

export const delayedVestingAccount = function (config: InitConfigurations) {
return class DelayedVestingAccount implements CosmosMsg {
/** BaseVestingAccount baseAccount. */
public baseAccount?: IBaseAccount | null;

/** BaseVestingAccount originalVesting. */
public originalVesting: ICoin[];

/** BaseVestingAccount delegatedFree. */
public delegatedFree: ICoin[];

/** BaseVestingAccount delegatedVesting. */
public delegatedVesting: ICoin[];

/** BaseVestingAccount endTime. */
public endTime: Long;

/**
* Constructor to create a new DelayedVestingAccount
* @param {DelayedVestingAccountOptions} options
* @returns {DelayedVestingAccount}
* @throws {Error} when options is invalid
*/
constructor(options: DelayedVestingAccountOptions) {
ow(options, 'options', owDelayedVestingAccountOptions);
this.baseAccount = options.baseAccount;
this.originalVesting = options.originalVesting;
this.delegatedFree = options.delegatedFree;
this.delegatedVesting = options.delegatedVesting;
this.endTime = options.endTime;
}

/**
* Returns an instance of DelayedVestingAccount
* @param {string} msgJsonStr
* @param {Network} network
* @returns {DelayedVestingAccount}
*/
public static fromCosmosMsgJSON(msgJsonStr: string): DelayedVestingAccount {
const parsedMsg = JSON.parse(msgJsonStr) as DelayedVestingAccountRaw;
const cro = CroSDK({ network: config.network });

if (parsedMsg['@type'] !== COSMOS_MSG_TYPEURL.account.DelayedVestingAccount) {
throw new Error(
`Expected ${COSMOS_MSG_TYPEURL.account.DelayedVestingAccount} but got ${parsedMsg['@type']}`,
);
}
if (
parsedMsg.base_vesting_account.delegated_vesting.length < 1 ||
parsedMsg.base_vesting_account.original_vesting.length < 1 ||
parsedMsg.base_vesting_account.delegated_free.length < 1
) {
throw new Error('Invalid Amount fields in the Msg.');
}

return new DelayedVestingAccount({
baseAccount: {
address: parsedMsg.base_vesting_account.base_account?.address!,
accountNumber: Long.fromString(parsedMsg.base_vesting_account.base_account?.account_number || '0'),
sequence: Long.fromString(parsedMsg.base_vesting_account.base_account?.sequence || '0'),
},
originalVesting: parsedMsg.base_vesting_account.original_vesting.map((coin) =>
cro.v2.CoinV2.fromCustomAmountDenom(coin.amount, coin.denom),
),
delegatedFree: parsedMsg.base_vesting_account.delegated_free.map((coin) =>
cro.v2.CoinV2.fromCustomAmountDenom(coin.amount, coin.denom),
),
delegatedVesting: parsedMsg.base_vesting_account.delegated_vesting.map((coin) =>
cro.v2.CoinV2.fromCustomAmountDenom(coin.amount, coin.denom),
),
endTime: Long.fromString(parsedMsg.base_vesting_account.end_time || '0'),
});
}

/**
* Returns the raw Msg representation of DelayedVestingAccount
* @returns {Msg}
*/
toRawMsg(): Msg {
return {
typeUrl: COSMOS_MSG_TYPEURL.account.DelayedVestingAccount,
value: {
baseVestingAccount: {
baseAccount: {
address: this.baseAccount?.address,
// pubKey: this.baseAccount?.pubKey?.getEncoded(),
accountNumber: this.baseAccount?.accountNumber,
sequence: this.baseAccount?.sequence,
},
originalVesting: this.originalVesting.map((amt) => amt.toCosmosCoin()),
delegatedFree: this.delegatedFree.map((amt) => amt.toCosmosCoin()),
delegatedVesting: this.delegatedVesting.map((amt) => amt.toCosmosCoin()),
endTime: this.endTime,
},
},
};
}

// eslint-disable-next-line class-methods-use-this
toRawAminoMsg(): legacyAmino.Msg {
throw new Error("DelayedVestingAccount isn't supported for Amino encoding.");
}
};
};

export type DelayedVestingAccountOptions = {
baseAccount?: IBaseAccount | null;
originalVesting: ICoin[];
delegatedFree: ICoin[];
delegatedVesting: ICoin[];
endTime: Long;
};

export interface IBaseAccount {
address: string;
pubKey?: IGoogleAny | null;
accountNumber: Long;
sequence: Long;
}

/**
* JSON Type definitions
* */

export interface DelayedVestingAccountRaw {
'@type': string;
base_vesting_account: BaseVestingAccountRaw;
}

export interface BaseVestingAccountRaw {
base_account?: BaseAccountRaw;
original_vesting: AmountRaw[];
delegated_free: AmountRaw[];
delegated_vesting: AmountRaw[];
end_time: string;
}

export interface BaseAccountRaw {
address: string;
pub_key: PubKey;
account_number: string;
sequence: string;
}

export interface AmountRaw {
denom: string;
amount: string;
}

export interface PubKey {
'@type': string;
key: string;
}
Loading

0 comments on commit ac77333

Please sign in to comment.