Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: json-sort #71

Merged
merged 7 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('bank', () => {
// get account info
const account = await rest.auth
.account(sdk, fromAddress)
.then((res) => res.data.account && cosmosclient.codec.unpackCosmosAny(res.data.account))
.then((res) => res.data.account && cosmosclient.codec.protoJSONToInstance(res.data.account))
.catch((_) => undefined);

if (!(account instanceof proto.cosmos.auth.v1beta1.BaseAccount)) {
Expand All @@ -64,12 +64,12 @@ describe('bank', () => {
});

const txBody = new proto.cosmos.tx.v1beta1.TxBody({
messages: [cosmosclient.codec.packAny(msgSend)],
messages: [cosmosclient.codec.instanceToProtoAny(msgSend)],
});
const authInfo = new proto.cosmos.tx.v1beta1.AuthInfo({
signer_infos: [
{
public_key: cosmosclient.codec.packAny(pubKey),
public_key: cosmosclient.codec.instanceToProtoAny(pubKey),
mode_info: {
single: {
mode: proto.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
Expand Down
4 changes: 2 additions & 2 deletions 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 package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@cosmos-client/core",
"description": "REST API client for Cosmos SDK blockchain",
"version": "0.45.4",
"version": "0.45.5",
"author": "CauchyE, Inc.",
"bugs": {
"url": "https://github.com/cosmos-client/cosmos-client-ts/issues"
Expand Down
6 changes: 3 additions & 3 deletions src/rest/bank/bank.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('bank', () => {
// get account info
const account = await rest.auth
.account(sdk, fromAddress)
.then((res) => res.data.account && cosmosclient.codec.unpackCosmosAny(res.data.account))
.then((res) => res.data.account && cosmosclient.codec.protoJSONToInstance(res.data.account as { '@type': string }))
.catch(() => undefined);

if (!(account instanceof proto.cosmos.auth.v1beta1.BaseAccount)) {
Expand All @@ -36,12 +36,12 @@ describe('bank', () => {
});

const txBody = new proto.cosmos.tx.v1beta1.TxBody({
messages: [cosmosclient.codec.packAny(msgSend)],
messages: [cosmosclient.codec.instanceToProtoAny(msgSend)],
});
const authInfo = new proto.cosmos.tx.v1beta1.AuthInfo({
signer_infos: [
{
public_key: cosmosclient.codec.packAny(pubKey),
public_key: cosmosclient.codec.instanceToProtoAny(pubKey),
mode_info: {
single: {
mode: proto.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
Expand Down
18 changes: 18 additions & 0 deletions src/rest/tx/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
import { cosmos } from '../../proto';
import { codec } from '../../types';

export * as tx from './module';

codec.register('/cosmos.tx.v1beta1.AuthInfo', cosmos.tx.v1beta1.AuthInfo);
codec.register('/cosmos.tx.v1beta1.Fee', cosmos.tx.v1beta1.Fee);
codec.register('/cosmos.tx.v1beta1.ModeInfo', cosmos.tx.v1beta1.ModeInfo);
codec.register('/cosmos.tx.v1beta1.SignDoc', cosmos.tx.v1beta1.SignDoc);
codec.register('/cosmos.tx.v1beta1.SignerInfo', cosmos.tx.v1beta1.SignerInfo);
codec.register('/cosmos.tx.v1beta1.Tx', cosmos.tx.v1beta1.Tx);
codec.register('/cosmos.tx.v1beta1.TxBody', cosmos.tx.v1beta1.TxBody);
codec.register('/cosmos.tx.v1beta1.TxRaw', cosmos.tx.v1beta1.TxRaw);
codec.registerConvertJSON(cosmos.tx.v1beta1.ModeInfo, (value) => {
if (value?.single) {
value.single.mode = cosmos.tx.signing.v1beta1.SignMode[value.single.mode];
}
return value;
});
37 changes: 30 additions & 7 deletions src/types/codec/codec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { goTimeStringToJsDate, jsDateToGoTimeString, jsDateToProtobufTimestamp,
import Long from 'long';

describe('codec', () => {
it('cosmosJSONStringify', async () => {
it('txBuilder', async () => {
expect.hasAssertions();

const sdk = new cosmosclient.CosmosSDK('http://localhost:1317', 'testchain');
Expand All @@ -28,12 +28,12 @@ describe('codec', () => {
});

const txBody = new proto.cosmos.tx.v1beta1.TxBody({
messages: [cosmosclient.codec.packAny(msgSend)],
messages: [cosmosclient.codec.instanceToProtoAny(msgSend)],
});
const authInfo = new proto.cosmos.tx.v1beta1.AuthInfo({
signer_infos: [
{
public_key: cosmosclient.codec.packAny(pubKey),
public_key: cosmosclient.codec.instanceToProtoAny(pubKey),
mode_info: {
single: {
mode: proto.cosmos.tx.signing.v1beta1.SignMode.SIGN_MODE_DIRECT,
Expand All @@ -53,11 +53,34 @@ describe('codec', () => {
txBuilder.addSignature(privKey.sign(signDocBytes));

// broadcast
const json = txBuilder.cosmosJSONStringify(2);
const json = txBuilder.protoJSONStringify(2);
console.log(json);

expect(true).toBeTruthy();
});

it('protoJSONStringify', () => {
expect.hasAssertions();

const account = {
'@type': '/cosmos.auth.v1beta1.BaseAccount',
address: 'jpyx10lcj22kzftvnduchatmnsnhfljeky5ghd398wt',
account_number: '0',
pub_key: { '@type': '/cosmos.crypto.secp256k1.PubKey', key: 'AvSf2U/B23UZKvLTD0E4xqJ33Nn0Z552nXCkkwEfleiu' },
sequence: '13',
};

const unpacked = cosmosclient.codec.protoJSONToInstance(account);
if (!(unpacked instanceof proto.cosmos.auth.v1beta1.BaseAccount)) {
throw Error('');
}
const json = cosmosclient.codec.instanceToProtoJSON(unpacked);
console.log(json);

expect(true).toBeTruthy();
});

it('unpackAny', () => {
it('protoJSONToInstance', () => {
expect.hasAssertions();
const res = {
data: {
Expand Down Expand Up @@ -111,14 +134,14 @@ describe('codec', () => {
},
};

const unpacked = cosmosclient.codec.unpackCosmosAny(res.data.account);
const unpacked = cosmosclient.codec.protoJSONToInstance(res.data.account);
if (!(unpacked instanceof proto.cosmos.auth.v1beta1.BaseAccount)) {
throw Error('');
}

console.log(unpacked);

const key = cosmosclient.codec.unpackAny(unpacked.pub_key);
const key = cosmosclient.codec.protoAnyToInstance(unpacked.pub_key);
console.log(key);

expect(true).toBeTruthy();
Expand Down
9 changes: 4 additions & 5 deletions src/types/codec/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as $protobuf from 'protobufjs';
import { protoMessage } from './module';

export const codecMaps = {
inv: new Map<Function, string>(),
encode: {} as { [type: string]: (value: any) => $protobuf.Writer },
decode: {} as { [type: string]: (value: Uint8Array) => unknown },
fromObject: {} as { [type: string]: (value: any) => unknown },
constructor: {} as { [type: string]: protoMessage },
inv: new Map<protoMessage, string>(),
convertJSON: new Map<protoMessage, (value: any) => any>(),
};
Loading