Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into feature/add-IntegrationTests
  • Loading branch information
cdc-Hitesh committed Sep 15, 2021
2 parents 8ba4a05 + 1375efe commit dab0991
Show file tree
Hide file tree
Showing 14 changed files with 5,798 additions and 755 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

## v1.0.1
- Added CRO `Croeseid-4` Testnet network

## v1.0.0
- Support for Offline transaction signing
- Added `TxDecoder` for decoding transaction from their raw `hex` representation
Expand Down
18 changes: 9 additions & 9 deletions lib/e2e/tx-decoder/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/e2e/tx-decoder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"axios": "0.21.1",
"axios": "0.21.2",
"big.js": "6.1.1",
"chai": "4.3.4",
"deep-object-diff": "1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion lib/src/client/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fuzzyDescribe } from '../test/mocha-fuzzy/suite';

import { CroNetwork, CroSDK } from '../core/cro';

const cro = CroSDK({ network: CroNetwork.Testnet });
const cro = CroSDK({ network: CroNetwork.Mainnet });
const statusQueryResponse = {
jsonrpc: '2.0',
id: -1,
Expand Down
17 changes: 17 additions & 0 deletions lib/src/core/cro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ export class CroNetwork {
},
rpcUrl: 'https://testnet-croeseid-3.crypto.org:26657',
};

public static TestnetCroeseid4: Network = {
defaultNodeUrl: 'https://testnet-croeseid-4.crypto.org',
chainId: 'testnet-croeseid-4',
addressPrefix: 'tcro',
validatorAddressPrefix: 'tcrocncl',
validatorPubKeyPrefix: 'tcrocnclconspub',
coin: {
baseDenom: 'basetcro',
croDenom: 'tcro',
},
bip44Path: {
coinType: 1,
account: 0,
},
rpcUrl: 'https://testnet-croeseid-4.crypto.org:26657',
};
}

export type InitConfigurations = {
Expand Down
26 changes: 26 additions & 0 deletions lib/src/cosmos/amino/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,29 @@ export interface MsgTransferNFT extends Msg {
export function isMsgTransferNFT(msg: Msg): msg is MsgTransferNFT {
return (msg as MsgTransferNFT).type === 'chainmain/nft/MsgTransferNFT';
}

export interface AminoTimeoutIbcHeight {
readonly revision_number: string;
readonly revision_height: string;
}

export interface IbcMsgTransferAmino extends Msg {
readonly type: 'cosmos-sdk/MsgTransfer';
readonly value: {
readonly source_port: string;
readonly source_channel: string;
readonly token?: Coin;
/** Bech32 account address */
readonly sender: string;
/** Bech32 account address */
readonly receiver: string;
readonly timeout_height?: AminoTimeoutIbcHeight;
// Timeout timestamp (in nanoseconds) relative to the current block timestamp.
// The timeout is disabled when set to 0.
readonly timeout_timestamp: string;
};
}

export function isAminoMsgTransfer(msg: Msg): msg is IbcMsgTransferAmino {
return msg.type === 'cosmos-sdk/MsgTransfer';
}
81 changes: 52 additions & 29 deletions lib/src/transaction/msg/ibc/applications/MsgTransfer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,11 @@ 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 { isAminoMsgTransfer } from '../../../../cosmos/amino/msg';

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: '',
},
});
const cro = CroSDK({ network: CroNetwork.TestnetCroeseid3 });

const tokenAmount = cro.Coin.fromBaseUnit('1234');
const timeoutHeight = {
Expand All @@ -45,7 +29,7 @@ describe('Testing MsgTransfer', function () {
sender: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
receiver: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
timeoutHeight,
timeoutTimestamp: Long.fromString('1620640362229420996'),
timeoutTimestampInNanoSeconds: Long.fromString('1620640362229420996'),
};

const testRunner = fuzzy(fuzzy.ObjArg(anyValidOptions));
Expand All @@ -66,7 +50,7 @@ describe('Testing MsgTransfer', function () {
sender: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
receiver: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
timeoutHeight,
timeoutTimestamp: Long.fromString('1620640362229420996'),
timeoutTimestampInNanoSeconds: Long.fromString('1620640362229420996'),
});

const rawMsg: Msg = {
Expand Down Expand Up @@ -97,7 +81,7 @@ describe('Testing MsgTransfer', function () {
sender: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
receiver: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
timeoutHeight,
timeoutTimestamp: Long.fromString('1620640362229420996'),
timeoutTimestampInNanoSeconds: Long.fromString('1620640362229420996'),
});

const anySigner = {
Expand All @@ -114,7 +98,7 @@ describe('Testing MsgTransfer', function () {

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

Expand All @@ -126,7 +110,7 @@ describe('Testing MsgTransfer', function () {
sender: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
receiver: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
timeoutHeight,
timeoutTimestamp: Long.fromString('1620640362229420996'),
timeoutTimestampInNanoSeconds: Long.fromString('1620640362229420996'),
};

expect(() => new cro.ibc.MsgTransfer(params1)).to.throw('Provided `sender` does not match network selected');
Expand All @@ -140,26 +124,65 @@ describe('Testing MsgTransfer', function () {
sender: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
receiver: '0x7e00664398A54AE12648CAe2785c36d00dd51672',
timeoutHeight,
timeoutTimestamp: Long.fromString('1620640362229420996'),
timeoutTimestampInNanoSeconds: Long.fromString('1620640362229420996'),
};

expect(() => new cro.ibc.MsgTransfer(params1)).to.throw(
'Provided `receiver` is not a valid Bech-32 encoded address',
);
});

it('Should throw on getting toRawAminoMsg()', function () {
it('Should return correct amino message on toRawAminoMsg()', function () {
const MsgTransfer = new cro.ibc.MsgTransfer({
sourcePort: 'transfer',
sourceChannel: 'channel-33',
token: tokenAmount,
sender: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
receiver: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
timeoutHeight,
timeoutTimestamp: Long.fromString('1620640362229420996'),
timeoutTimestampInNanoSeconds: Long.fromString('1620640362229420996'),
});
expect(isAminoMsgTransfer(MsgTransfer.toRawAminoMsg()).valueOf()).to.be.true;
expect(MsgTransfer.toRawAminoMsg()).to.deep.eq({
type: 'cosmos-sdk/MsgTransfer',
value: {
receiver: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
sender: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
source_channel: 'channel-33',
source_port: 'transfer',
timeout_height: {
revision_height: '1708515',
revision_number: '0',
},
timeout_timestamp: '1620640362229420996',
token: {
amount: '1234',
denom: 'basetcro',
},
},
});

expect(() => MsgTransfer.toRawAminoMsg()).to.throw('IBC Module not supported under amino encoding scheme');
// Case when timeoutHeight and token is undefined
const msgTransferIBC_UNdefined = new cro.ibc.MsgTransfer({
sourcePort: 'transfer',
sourceChannel: 'channel-33',
sender: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
receiver: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
timeoutTimestampInNanoSeconds: Long.fromString('1620640362229420996'),
});

expect(msgTransferIBC_UNdefined.toRawAminoMsg()).to.deep.eq({
type: 'cosmos-sdk/MsgTransfer',
value: {
receiver: 'cosmos1vw4ucaeagtduv5ep4sa95e3aqzqpsk5meda08c',
sender: 'tcro15sfupd26sp6qf37ll5q6xuf330k7df9tnvrqht',
source_channel: 'channel-33',
source_port: 'transfer',
timeout_height: undefined,
timeout_timestamp: '1620640362229420996',
token: undefined,
},
});
});

describe('fromCosmosJSON', function () {
Expand Down Expand Up @@ -318,7 +341,7 @@ describe('Testing MsgTransfer', function () {
expect(MsgTransfer.sourcePort).to.eql('transfer');
expect(MsgTransfer.token?.toCosmosCoin().amount).to.eql('1234');
expect(MsgTransfer.token?.toCosmosCoin().denom).to.eql('basetcro');
expect(MsgTransfer.timeoutTimestamp.toString()).to.eql('1624612912351977705');
expect(MsgTransfer.timeoutTimestampInNanoSeconds.toString()).to.eql('1624612912351977705');
expect(MsgTransfer.timeoutHeight).to.not.be.undefined;
expect(MsgTransfer.timeoutHeight?.revisionHeight!.toString()).to.eql('2390451');
expect(MsgTransfer.timeoutHeight?.revisionNumber!.toString()).to.eql('0');
Expand Down
34 changes: 26 additions & 8 deletions lib/src/transaction/msg/ibc/applications/MsgTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const msgTransferIBC = function (config: InitConfigurations) {
/** MsgTransfer timeoutHeight. */
public timeoutHeight?: IHeight | null;

/** MsgTransfer timeoutTimestamp. */
public timeoutTimestamp: Long;
/** MsgTransfer timeoutTimestamp in nano seconds. */
public timeoutTimestampInNanoSeconds: Long;

/**
* Constructor to create a new IBC.MsgTransfer
Expand All @@ -47,7 +47,7 @@ export const msgTransferIBC = function (config: InitConfigurations) {
this.sender = options.sender;
this.receiver = options.receiver;
this.timeoutHeight = options.timeoutHeight;
this.timeoutTimestamp = options.timeoutTimestamp;
this.timeoutTimestampInNanoSeconds = options.timeoutTimestampInNanoSeconds;

this.validateAddresses();
}
Expand All @@ -66,14 +66,32 @@ export const msgTransferIBC = function (config: InitConfigurations) {
sender: this.sender,
receiver: this.receiver,
timeoutHeight: this.timeoutHeight,
timeoutTimestamp: this.timeoutTimestamp,
} as MsgTransferOptions,
timeoutTimestamp: this.timeoutTimestampInNanoSeconds,
},
};
}

// eslint-disable-next-line class-methods-use-this
toRawAminoMsg(): legacyAmino.Msg {
throw new Error('IBC Module not supported under amino encoding scheme');
const tokenCoin = this.token ? this.token.toCosmosCoin() : undefined;
const timeoutHeight = this.timeoutHeight
? {
revision_number: this.timeoutHeight?.revisionNumber.toString(),
revision_height: this.timeoutHeight?.revisionHeight.toString(),
}
: undefined;
return {
type: 'cosmos-sdk/MsgTransfer',
value: {
source_port: this.sourcePort,
source_channel: this.sourceChannel,
token: tokenCoin,
sender: this.sender,
receiver: this.receiver,
timeout_height: timeoutHeight,
timeout_timestamp: this.timeoutTimestampInNanoSeconds.toString(),
},
} as legacyAmino.IbcMsgTransferAmino;
}

/**
Expand Down Expand Up @@ -108,7 +126,7 @@ export const msgTransferIBC = function (config: InitConfigurations) {
token: cro.v2.CoinV2.fromCustomAmountDenom(parsedMsg.token.amount, parsedMsg.token.denom),
sender: parsedMsg.sender,
receiver: parsedMsg.receiver,
timeoutTimestamp: Long.fromString(parsedMsg.timeout_timestamp),
timeoutTimestampInNanoSeconds: Long.fromString(parsedMsg.timeout_timestamp),
timeoutHeight,
});
}
Expand Down Expand Up @@ -140,7 +158,7 @@ export type MsgTransferOptions = {
sender: string;
receiver: string;
timeoutHeight?: IHeight | null;
timeoutTimestamp: Long;
timeoutTimestampInNanoSeconds: Long;
};

export type IHeight = {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/transaction/msg/ow.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export const owMsgTransferIBCOptions = owStrictObject().exactShape({
sender: ow.string,
receiver: ow.string,
timeoutHeight: owIBCHeightOptional(),
timeoutTimestamp: owLong(),
timeoutTimestampInNanoSeconds: owLong(),
});

export const owMsgUpgradeClientOptions = owStrictObject().exactShape({
Expand Down
17 changes: 16 additions & 1 deletion lib/src/utils/address.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'mocha';
import { expect } from 'chai';
import { AddressType, AddressValidator, validateAddress } from './address';
import { AddressType, AddressValidator, validateAddress, getBech32AddressFromEVMAddress } from './address';
import { CroNetwork } from '../core/cro';

describe('Validate address against network and checksums', function () {
Expand Down Expand Up @@ -98,5 +98,20 @@ describe('Validate address against network and checksums', function () {
const addressValidator = new AddressValidator(addressProps);
expect(addressValidator.isValid()).to.be.eq(true);
});

it('`getBech32AddressFromEVMAddress` should return converted address', function () {
expect(getBech32AddressFromEVMAddress('0xD47286f025F947482a2C374Fb70e9D4c94d809CF', 'eth')).to.be.eq(
'eth163egdup9l9r5s23vxa8mwr5afj2dszw04malry',
);
expect(getBech32AddressFromEVMAddress('D47286f025F947482a2C374Fb70e9D4c94d809CF', 'eth')).to.be.eq(
'eth163egdup9l9r5s23vxa8mwr5afj2dszw04malry',
);
});

it('`getBech32AddressFromEVMAddress` should throw on invalid address', function () {
expect(() => getBech32AddressFromEVMAddress('eth163egdup9l9r5s23vxa8mwr5afj2dszw04malry', 'eth')).to.throw(
'Please provide a valid EVM compatible address.',
);
});
});
});
Loading

0 comments on commit dab0991

Please sign in to comment.