Skip to content

Commit

Permalink
Merge pull request #298 from crypto-org-chain/tx-revamp-dev
Browse files Browse the repository at this point in the history
calvinaco authored Aug 10, 2021

Unverified

The email in this signature doesn’t match the committer email.
2 parents 091d2bd + c82b19b commit 542f991
Showing 124 changed files with 30,470 additions and 3,577 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -2,3 +2,5 @@
lib/**/codec/generated/codecimpl.*
lib/dist
docs
*/**/*.md
*/**/*.json
154 changes: 152 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -151,8 +151,158 @@ const queryResult = await client.query().<module>.<operation>
// example client.query().bank.allBalances(<address>)
```

### 1.6. Transaction Decoding/Encoding support
Our SDK supports transaction decoding from hex-encoded strings.

## 2. Cosmos Protobuf Definitions
```typescript
import { TxDecoder } from './txDecoder';
const txDecoder = new TxDecoder();
const decodedTx = txDecoder.fromHex('0a9b010a8c010a1c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e64126c0a2b7463726f31667a63727a61336a3466323637376a667578756c6b6733337a36383532717371733868783530122b7463726f31667a63727a61336a3466323637376a667578756c6b6733337a363835327173717338687835301a100a08626173657463726f120431303030120a616d696e6f2074657374126b0a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a210223c9395d41013e6470c8d27da8b75850554faada3fe3e812660cbdf4534a85d712040a020801180112170a110a08626173657463726f1205313030303010a08d061a4031f4c489b98decb367972790747139c7706f54aafd9e5a3a5ada4f72c7b017646f1eb5cb1bdf518603d5d8991466a13c3f68844dcd9b168b5d4ca0cb5ea514bc');

//Prints decoded in Cosmos compatible JSON format
console.log(decodedTx.toCosmosJSON())

// Prints
// "{"tx":{"body":{"messages":[{"@type":"/cosmos.bank.v1beta1.MsgSend","amount":[{"denom":"basetcro","amount":"1000"}],"from_address":"tcro1fzcrza3j4f2677jfuxulkg33z6852qsqs8hx50","to_address":"tcro1fzcrza3j4f2677jfuxulkg33z6852qsqs8hx50"}],"memo":"amino test","timeout_height":"0","extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[{"public_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AiPJOV1BAT5kcMjSfai3WFBVT6raP+PoEmYMvfRTSoXX"},"mode_info":{"single":{"mode":"SIGN_MODE_DIRECT"}},"sequence":"1"}],"fee":{"amount":[{"denom":"basetcro","amount":"10000"}],"gas_limit":"100000","payer":"","granter":""}},"signatures":["MfTEibmN7LNnlyeQdHE5x3BvVKr9nlo6WtpPcsewF2RvHrXLG99RhgPV2JkUZqE8P2iETc2bFotdTKDLXqUUvA=="]}}"

```

### 1.7. Offline Signing
Our SDK supports offline signing for secure external transaction management.

#### Flow:
Machine 1 (Online):
1. Build a `RawTransactionV2` instance.
2. Export Cosmos compatible JSON by using `.toCosmosJSON()`.
3. Export Signer(s) list using `.exportSignerAccounts()`.

Machine 2 (Offline/Online):
1. Create a `SignableTransactionV2` instance from a stringified cosmos compatible JSON string.
2. You can import Signer(s) list using two methods:
1. call `importSignerAccounts()` on the instance above **OR**
2. (Advance usage) call `setSignerAccountNumberAtIndex()` to manually set AccountNumber at a specified index.
3. You can choose to export the signed hex encoded transaction and broadcast it manually

Eg:
```typescript
// import respective classes
// ....

/* Machine 1: */
const rawTx = new cro.v2.RawTransactionV2();
// .... Do rest operations here
const exportUnsignedCosmosJSON = rawTx.toCosmosJSON();
const exportSignerInfoToJSON = rawTx.exportSignerAccounts();

/* Machine 2: */
const signerAccountsOptional: SignerAccount[] = cro
.v2
.RawTransactionV2
.parseSignerAccounts(exportSignerInfoToJSON);
/* SignerAccount[] has the structure of
[{
publicKey: <Bytes>;
accountNumber: new Big(0);
signMode: SIGN_MODE.DIRECT;
}];
*/

const signableTx = new SignableTransactionV2({
rawTxJSON: exportUnsignedCosmosJSON,
network: <CroNetwork>,
signerAccounts: signerAccountsOptional,
});

/* `Import SignerAccounts` starts */

// METHOD 1: using importSignerAccounts()
signableTx.importSignerAccounts([
// SignerAccount 1
{
publicKey: Bytes.fromHexString('hexString');
accountNumber: new Big(0);
signMode: SIGN_MODE.DIRECT;
},
// SignerAccount 2
{
publicKey: Bytes.fromUint8Array(<Uint8>);
accountNumber: new Big(2);
signMode: SIGN_MODE.DIRECT;
}
]);

// METHOD 2 (For Advance Users): using setSignerAccountNumberAtIndex()
const signerInfoListINDEX: number = 1;
const newAccountNumber: Big = new Big(1);
signableTx.setSignerAccountNumberAtIndex(signerInfoListINDEX, newAccountNumber);

/* `Import SignerAccounts` ends */

// .... Do rest operations here on SignableTransaction

const signedTx = signableTx.toSigned();

console.log(signedTx.getHexEncoded());
// 0aa4010a8c010a1c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e64126c0a2b7463726f313635747a63726832796c3833673871657178756567326735677a6775353779336665336b6333122b7463726f313635747a63726832796c3833673871657178756567326735677a6775353779336665336b63331a100a08626173657463726f120431323130120f48656c6c6f2054657374204d656d6f1896ef14126a0a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a2103c3d281a28592adce81bee3094f00eae26932cbc682fba239b90f47dac9fe703612040a020801180d12160a100a08626173657463726f12043635303010c08b111a40fe9b30f29bb9a83df3685f5bf8b7e6c34bae9ee8ba93115af4136289354c5bf947698ef3a3c0a1f6092ba7a2069616c436f4bcf6f3ecef11b92ad4d319ec0347

// Note that the result of signedTx.getHexEncoded() can be directly broadcasted to the network as a raw tx

```


### 1.8. Create a message from Cosmos compatible JSON
All **Cosmos message** types supported on our SDK can be instantiated using the function `.fromCosmosMsgJSON()` on respective classes. You need to pass a valid Msg `JSON` string and a `network` instance.
Eg.
```typescript
const msgSendJson ='{ "@type": "/cosmos.bank.v1beta1.MsgSend", "amount": [{ "denom": "basetcro", "amount": "3478499933290496" }], "from_address": "tcro1x07kkkepfj2hl8etlcuqhej7jj6myqrp48y4hg", "to_address": "tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3" }';

const msgSend = cro.v2.bank.MsgSendV2.fromCosmosMsgJSON(msgSendJson, CroNetwork.Testnet);
// `msgSend` is a valid instance of `MsgSendV2` and can be used for Transaction building


const msgFundCommunityPoolJson = '{"@type":"/cosmos.distribution.v1beta1.MsgFundCommunityPool","amount":[{ "denom": "basetcro", "amount": "3478499933290496" }],"depositor":"tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3"}';

const msgFundCommPool = cro.v2.distribution.MsgFundCommunityPoolV2.fromCosmosMsgJSON(msgFundCommunityPoolJson, CroNetwork.Testnet);
// `msgFundCommPool`is a valid instance of `MsgFundCommunityPoolV2` and can be used for Transaction building

```

## 2. Introducing `V2` message types
Our SDK has introduced `V2` message types in order to support:
- Custom `denom`
- Multiple `amount` in several Cosmos Message types
- Multiple `fee` amount in `SignerInfo`

You can use the `v2` property on the `CroSDK` instance like in the example below:

```typescript
// imports here

const cro = CroSDK({ network: sdk.CroNetwork.Testnet });

// v2 methods below
const coin1 = new cro.Coin('88888888', Units.BASE);
const coin2 = new cro.Coin('99999999', Units.BASE);
const msgSendV2 = new cro.v2.bank.MsgSendV2({
fromAddress: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
toAddress: 'tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3',
amount: [coin1, coin2],
});
```

### 2.1 List of new `V2` methods

* New classes for external transaction management:
- `RawTransactionV2`
- `.toCosmosJSON()` : Get a Cosmos-sdk compatible JSON string
- `.exportSignerAccounts()` : Exports a human readable JSON of `SignerAccount`
- `appendFeeAmount(...)` : Add multiple fee amount to `SignerInfo` list
- `CoinV2` : Supports custom denom support
- `SignableTransactionV2` : Load your Cosmos Tx JSON for transaction management.

Please note new message types may be added under the `CroSDK` instance.

## 3. Cosmos Protobuf Definitions

### Generate Cosmos Protobuf Definitions in JavaScript

@@ -191,4 +341,4 @@ npm run docs:build
The resulting generated documentation will be created in the `docs/dist` directory

## 4. License
[Apache 2.0](./LICENSE)
[Apache 2.0](./LICENSE)
2 changes: 2 additions & 0 deletions lib/e2e/offline-signing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
chain-maind
home/
23 changes: 23 additions & 0 deletions lib/e2e/offline-signing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Offline Signing End-to-End Test

This test will compare the offline signing results of chain-maind and the JSlib.

## How to run

### 1. Go to correct directory
```bash
cd ./lib/e2e/offling-signing
```

### 2. Download latest `chain-maind` release to the folder

https://github.com/crypto-com/chain-main/releases

### 3. Run compare tool
```bash
# Go to jslib root directory
cd ../../../
npm run test:e2e:offline-signing
```

The test will take some time to complete.
Loading

0 comments on commit 542f991

Please sign in to comment.