Skip to content

Commit ac77333

Browse files
committed
Merge branch 'dev' of https://github.com/crypto-org-chain/chain-jslib into fix/upgradeDependency
2 parents 394a945 + 09ee7ce commit ac77333

File tree

14 files changed

+2116
-92
lines changed

14 files changed

+2116
-92
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

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

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

8+
## v1.0.4
9+
- Added support for `MsgCreateVestingAccount` and `DelayedVestingAccount`
10+
11+
## v1.0.3
12+
- Added support for transferring very large numbers. Using BigInt
13+
814
## v1.0.2
915
- Added IBC MsgTransfer amino support
1016
- Added utility `convertEVMAddressToBech32Address` for EVM<->Bech32 address conversion

lib/src/core/cro.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import { msgConsensusState } from '../transaction/msg/ibc/lightclients/Consensus
4646
import { msgHeader } from '../transaction/msg/ibc/lightclients/Header';
4747
import { MsgConnectionOpenConfirmIBC } from '../transaction/msg/ibc/core/connection/MsgConnectionOpenConfirm';
4848
import { MsgConnectionOpenTryIBC } from '../transaction/msg/ibc/core/connection/MsgConnectionOpenTry';
49+
import { msgCreateVestingAccount } from '../transaction/msg/account/MsgCreateVestingAccount';
50+
import { delayedVestingAccount } from '../transaction/msg/account/DelayedVestingAccount';
4951

5052
export const CroSDK = function (configs: InitConfigurations) {
5153
ow(configs, 'configs', owCroSDKInitParams);
@@ -90,6 +92,10 @@ export const CroSDK = function (configs: InitConfigurations) {
9092
MsgTransferNFT: msgTransferNFT(configs),
9193
MsgBurnNFT: msgBurnNFT(configs),
9294
},
95+
accounts: {
96+
MsgCreateVestingAccount: msgCreateVestingAccount(configs),
97+
DelayedVestingAccount: delayedVestingAccount(configs),
98+
},
9399
ibc: {
94100
MsgTransfer: msgTransferIBC(configs),
95101
MsgCreateClient: msgCreateClientIBC(configs),

lib/src/cosmos/v1beta1/codec/generated/codecimpl.d.ts

Lines changed: 718 additions & 0 deletions
Large diffs are not rendered by default.

lib/src/cosmos/v1beta1/codec/generated/codecimpl.js

Lines changed: 592 additions & 0 deletions
Large diffs are not rendered by default.

lib/src/cosmos/v1beta1/scripts/predefine-proto.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ mkdir -p "$GENERATED_DIR"
4545
"$COSMOS_PROTO_DIR/gov/v1beta1/gov.proto" \
4646
"$COSMOS_PROTO_DIR/params/v1beta1/params.proto" \
4747
"$COSMOS_PROTO_DIR/upgrade/v1beta1/upgrade.proto" \
48+
"$COSMOS_PROTO_DIR/auth/v1beta1/auth.proto" \
49+
"$COSMOS_PROTO_DIR/vesting/v1beta1/tx.proto" \
50+
"$COSMOS_PROTO_DIR/vesting/v1beta1/vesting.proto" \
4851
"$ICS23_PROTO_DIR/proofs.proto" \
4952
"$IBC_PROTO_DIR/core/commitment/v1/commitment.proto" \
5053
"$IBC_PROTO_DIR/applications/transfer/v1/tx.proto" \

lib/src/cosmos/v1beta1/types/typeurls.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export const typeUrlMappings: {
6060
'/ibc.lightclients.tendermint.v1.ClientState': ibc.lightclients.tendermint.v1.ClientState,
6161
'/ibc.lightclients.tendermint.v1.ConsensusState': ibc.lightclients.tendermint.v1.ConsensusState,
6262
'/ibc.lightclients.tendermint.v1.Header': ibc.lightclients.tendermint.v1.Header,
63+
'/cosmos.vesting.v1beta1.MsgCreateVestingAccount': cosmos.vesting.v1beta1.MsgCreateVestingAccount,
64+
'/cosmos.vesting.v1beta1.DelayedVestingAccount': cosmos.vesting.v1beta1.DelayedVestingAccount,
6365
};
6466

6567
export interface GeneratedType {

lib/src/transaction/common/constants/typeurl.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export const COSMOS_MSG_TYPEURL = {
5050
MsgConnectionOpenTry: '/ibc.core.connection.v1.MsgConnectionOpenTry',
5151
},
5252
},
53+
account: {
54+
MsgCreateVestingAccount: '/cosmos.vesting.v1beta1.MsgCreateVestingAccount',
55+
DelayedVestingAccount: '/cosmos.vesting.v1beta1.DelayedVestingAccount',
56+
},
5357
};
5458

5559
export const typeUrlToMsgClassMapping = (cro: any, typeUrl: string) => {
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import Big from 'big.js';
4+
5+
import Long from 'long';
6+
import { fuzzyDescribe } from '../../../test/mocha-fuzzy/suite';
7+
import { Msg } from '../../../cosmos/v1beta1/types/msg';
8+
import { Secp256k1KeyPair } from '../../../keypair/secp256k1';
9+
import { Bytes } from '../../../utils/bytes/bytes';
10+
import { Units } from '../../../coin/coin';
11+
import { CroSDK } from '../../../core/cro';
12+
13+
const cro = CroSDK({
14+
network: {
15+
defaultNodeUrl: '',
16+
chainId: 'testnet-croeseid-1',
17+
addressPrefix: 'tcro',
18+
validatorAddressPrefix: 'tcrocncl',
19+
validatorPubKeyPrefix: 'tcrocnclconspub',
20+
coin: {
21+
baseDenom: 'basetcro',
22+
croDenom: 'tcro',
23+
},
24+
bip44Path: {
25+
coinType: 1,
26+
account: 0,
27+
},
28+
rpcUrl: '',
29+
},
30+
});
31+
32+
describe('Testing DelayedVestingAccount', function () {
33+
describe('fromCosmosJSON', function () {
34+
it('should throw Error if the JSON is not a DelayedVestingAccount', function () {
35+
const json =
36+
'{ "@type": "/cosmos.bank.v1beta1.MsgCreateValidator", "amount": { "denom": "basetcro", "amount": "3478499933290496" }, "from_address": "tcro1x07kkkepfj2hl8etlcuqhej7jj6myqrp48y4hg", "to_address": "tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3" }';
37+
expect(() => cro.accounts.DelayedVestingAccount.fromCosmosMsgJSON(json)).to.throw(
38+
'Expected /cosmos.vesting.v1beta1.DelayedVestingAccount but got /cosmos.bank.v1beta1.MsgCreateValidator',
39+
);
40+
});
41+
it('should return the DelayedVestingAccount corresponding to the JSON', function () {
42+
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"}}`;
43+
const DelayedVestingAccount = cro.accounts.DelayedVestingAccount.fromCosmosMsgJSON(json);
44+
45+
expect(DelayedVestingAccount.endTime.toString()).to.eql('1648170000');
46+
47+
expect(DelayedVestingAccount.delegatedVesting[0].toCosmosCoin().amount).to.eql('606902735563');
48+
expect(DelayedVestingAccount.delegatedVesting[0].toCosmosCoin().denom).to.eql('basecro');
49+
50+
expect(DelayedVestingAccount.delegatedFree[0].toCosmosCoin().amount).to.eql('2647264437');
51+
expect(DelayedVestingAccount.delegatedFree[0].toCosmosCoin().denom).to.eql('basecro');
52+
53+
expect(DelayedVestingAccount.originalVesting[0].toCosmosCoin().amount).to.eql('606902735563');
54+
expect(DelayedVestingAccount.originalVesting[0].toCosmosCoin().denom).to.eql('basecro');
55+
});
56+
});
57+
58+
fuzzyDescribe('should throw Error when options is invalid', function (fuzzy) {
59+
const anyValidOptions = {
60+
fromAddress: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
61+
toAddress: 'tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3',
62+
amount: new cro.Coin('1000', Units.BASE),
63+
};
64+
const testRunner = fuzzy(fuzzy.ObjArg(anyValidOptions));
65+
66+
testRunner(function (options) {
67+
if (options.valid) {
68+
return;
69+
}
70+
expect(() => new cro.accounts.DelayedVestingAccount(options.value)).to.throw(
71+
'Expected `options` to be of type `object`',
72+
);
73+
});
74+
});
75+
76+
it('Test DelayedVestingAccount conversion', function () {
77+
const DelayedVestingAccount = new cro.accounts.DelayedVestingAccount({
78+
baseAccount: {
79+
address: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
80+
pubKey: null,
81+
accountNumber: Long.fromString('10'),
82+
sequence: Long.fromString('10'),
83+
},
84+
originalVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
85+
delegatedFree: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
86+
delegatedVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
87+
endTime: Long.fromString('1648170000'),
88+
});
89+
90+
const rawMsg: Msg = {
91+
typeUrl: '/cosmos.vesting.v1beta1.DelayedVestingAccount',
92+
value: {
93+
baseVestingAccount: {
94+
baseAccount: {
95+
accountNumber: Long.fromString('10'),
96+
address: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
97+
sequence: Long.fromString('10'),
98+
},
99+
delegatedFree: [
100+
{
101+
amount: '111111',
102+
denom: 'basetcro',
103+
},
104+
],
105+
delegatedVesting: [
106+
{
107+
amount: '111111',
108+
denom: 'basetcro',
109+
},
110+
],
111+
endTime: Long.fromString('1648170000'),
112+
originalVesting: [
113+
{
114+
amount: '111111',
115+
denom: 'basetcro',
116+
},
117+
],
118+
},
119+
},
120+
};
121+
122+
expect(DelayedVestingAccount.toRawMsg()).to.eqls(rawMsg);
123+
});
124+
125+
it('Test appendTxBody DelayedVestingAccount Tx signing', function () {
126+
const anyKeyPair = Secp256k1KeyPair.fromPrivKey(
127+
Bytes.fromHexString('66633d18513bec30dd11a209f1ceb1787aa9e2069d5d47e590174dc9665102b3'),
128+
);
129+
130+
const DelayedVestingAccount = new cro.accounts.DelayedVestingAccount({
131+
baseAccount: {
132+
address: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
133+
pubKey: null,
134+
accountNumber: Long.fromString('10'),
135+
sequence: Long.fromString('10'),
136+
},
137+
originalVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
138+
delegatedFree: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
139+
delegatedVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')],
140+
endTime: Long.fromString('1648170000'),
141+
});
142+
143+
const anySigner = {
144+
publicKey: anyKeyPair.getPubKey(),
145+
accountNumber: new Big(0),
146+
accountSequence: new Big(2),
147+
};
148+
149+
const rawTx = new cro.RawTransaction();
150+
151+
const signableTx = rawTx.appendMessage(DelayedVestingAccount).addSigner(anySigner).toSignable();
152+
153+
const signedTx = signableTx.setSignature(0, anyKeyPair.sign(signableTx.toSignDocumentHash(0))).toSigned();
154+
155+
const signedTxHex = signedTx.encode().toHexString();
156+
expect(signedTxHex).to.be.eql(
157+
'0aab010aa8010a2d2f636f736d6f732e76657374696e672e763162657461312e44656c6179656456657374696e674163636f756e7412770a750a310a2b7463726f313635747a63726832796c3833673871657178756567326735677a6775353779336665336b6333180a200a12120a08626173657463726f12063131313131311a120a08626173657463726f120631313131313122120a08626173657463726f12063131313131312890a8f4910612580a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a2103fd0d560b6c4aa1ca16721d039a192867c3457e19dad553edb98e7ba88b159c2712040a0208011802120410c09a0c1a409e4105e3d17d411f722f69afff864e6d22c2d4cc5a66b4b2ec77b6fb512401f67ba8615e61f16b6db243e33e376ce7252190d5ee202346f06b189523fb6c05d6',
158+
);
159+
});
160+
});
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/* eslint-disable camelcase */
2+
import ow from 'ow';
3+
import Long from 'long';
4+
import { Msg } from '../../../cosmos/v1beta1/types/msg';
5+
import { owDelayedVestingAccountOptions } from '../ow.types';
6+
import { CosmosMsg } from '../cosmosMsg';
7+
import { COSMOS_MSG_TYPEURL } from '../../common/constants/typeurl';
8+
import * as legacyAmino from '../../../cosmos/amino';
9+
import { ICoin } from '../../../coin/v2.coin/v2.coin';
10+
import { IGoogleAny } from '../ibc/IGoogleAny';
11+
import { CroSDK, InitConfigurations } from '../../../core/cro';
12+
13+
export const delayedVestingAccount = function (config: InitConfigurations) {
14+
return class DelayedVestingAccount implements CosmosMsg {
15+
/** BaseVestingAccount baseAccount. */
16+
public baseAccount?: IBaseAccount | null;
17+
18+
/** BaseVestingAccount originalVesting. */
19+
public originalVesting: ICoin[];
20+
21+
/** BaseVestingAccount delegatedFree. */
22+
public delegatedFree: ICoin[];
23+
24+
/** BaseVestingAccount delegatedVesting. */
25+
public delegatedVesting: ICoin[];
26+
27+
/** BaseVestingAccount endTime. */
28+
public endTime: Long;
29+
30+
/**
31+
* Constructor to create a new DelayedVestingAccount
32+
* @param {DelayedVestingAccountOptions} options
33+
* @returns {DelayedVestingAccount}
34+
* @throws {Error} when options is invalid
35+
*/
36+
constructor(options: DelayedVestingAccountOptions) {
37+
ow(options, 'options', owDelayedVestingAccountOptions);
38+
this.baseAccount = options.baseAccount;
39+
this.originalVesting = options.originalVesting;
40+
this.delegatedFree = options.delegatedFree;
41+
this.delegatedVesting = options.delegatedVesting;
42+
this.endTime = options.endTime;
43+
}
44+
45+
/**
46+
* Returns an instance of DelayedVestingAccount
47+
* @param {string} msgJsonStr
48+
* @param {Network} network
49+
* @returns {DelayedVestingAccount}
50+
*/
51+
public static fromCosmosMsgJSON(msgJsonStr: string): DelayedVestingAccount {
52+
const parsedMsg = JSON.parse(msgJsonStr) as DelayedVestingAccountRaw;
53+
const cro = CroSDK({ network: config.network });
54+
55+
if (parsedMsg['@type'] !== COSMOS_MSG_TYPEURL.account.DelayedVestingAccount) {
56+
throw new Error(
57+
`Expected ${COSMOS_MSG_TYPEURL.account.DelayedVestingAccount} but got ${parsedMsg['@type']}`,
58+
);
59+
}
60+
if (
61+
parsedMsg.base_vesting_account.delegated_vesting.length < 1 ||
62+
parsedMsg.base_vesting_account.original_vesting.length < 1 ||
63+
parsedMsg.base_vesting_account.delegated_free.length < 1
64+
) {
65+
throw new Error('Invalid Amount fields in the Msg.');
66+
}
67+
68+
return new DelayedVestingAccount({
69+
baseAccount: {
70+
address: parsedMsg.base_vesting_account.base_account?.address!,
71+
accountNumber: Long.fromString(parsedMsg.base_vesting_account.base_account?.account_number || '0'),
72+
sequence: Long.fromString(parsedMsg.base_vesting_account.base_account?.sequence || '0'),
73+
},
74+
originalVesting: parsedMsg.base_vesting_account.original_vesting.map((coin) =>
75+
cro.v2.CoinV2.fromCustomAmountDenom(coin.amount, coin.denom),
76+
),
77+
delegatedFree: parsedMsg.base_vesting_account.delegated_free.map((coin) =>
78+
cro.v2.CoinV2.fromCustomAmountDenom(coin.amount, coin.denom),
79+
),
80+
delegatedVesting: parsedMsg.base_vesting_account.delegated_vesting.map((coin) =>
81+
cro.v2.CoinV2.fromCustomAmountDenom(coin.amount, coin.denom),
82+
),
83+
endTime: Long.fromString(parsedMsg.base_vesting_account.end_time || '0'),
84+
});
85+
}
86+
87+
/**
88+
* Returns the raw Msg representation of DelayedVestingAccount
89+
* @returns {Msg}
90+
*/
91+
toRawMsg(): Msg {
92+
return {
93+
typeUrl: COSMOS_MSG_TYPEURL.account.DelayedVestingAccount,
94+
value: {
95+
baseVestingAccount: {
96+
baseAccount: {
97+
address: this.baseAccount?.address,
98+
// pubKey: this.baseAccount?.pubKey?.getEncoded(),
99+
accountNumber: this.baseAccount?.accountNumber,
100+
sequence: this.baseAccount?.sequence,
101+
},
102+
originalVesting: this.originalVesting.map((amt) => amt.toCosmosCoin()),
103+
delegatedFree: this.delegatedFree.map((amt) => amt.toCosmosCoin()),
104+
delegatedVesting: this.delegatedVesting.map((amt) => amt.toCosmosCoin()),
105+
endTime: this.endTime,
106+
},
107+
},
108+
};
109+
}
110+
111+
// eslint-disable-next-line class-methods-use-this
112+
toRawAminoMsg(): legacyAmino.Msg {
113+
throw new Error("DelayedVestingAccount isn't supported for Amino encoding.");
114+
}
115+
};
116+
};
117+
118+
export type DelayedVestingAccountOptions = {
119+
baseAccount?: IBaseAccount | null;
120+
originalVesting: ICoin[];
121+
delegatedFree: ICoin[];
122+
delegatedVesting: ICoin[];
123+
endTime: Long;
124+
};
125+
126+
export interface IBaseAccount {
127+
address: string;
128+
pubKey?: IGoogleAny | null;
129+
accountNumber: Long;
130+
sequence: Long;
131+
}
132+
133+
/**
134+
* JSON Type definitions
135+
* */
136+
137+
export interface DelayedVestingAccountRaw {
138+
'@type': string;
139+
base_vesting_account: BaseVestingAccountRaw;
140+
}
141+
142+
export interface BaseVestingAccountRaw {
143+
base_account?: BaseAccountRaw;
144+
original_vesting: AmountRaw[];
145+
delegated_free: AmountRaw[];
146+
delegated_vesting: AmountRaw[];
147+
end_time: string;
148+
}
149+
150+
export interface BaseAccountRaw {
151+
address: string;
152+
pub_key: PubKey;
153+
account_number: string;
154+
sequence: string;
155+
}
156+
157+
export interface AmountRaw {
158+
denom: string;
159+
amount: string;
160+
}
161+
162+
export interface PubKey {
163+
'@type': string;
164+
key: string;
165+
}

0 commit comments

Comments
 (0)