Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunnini committed Aug 3, 2023
2 parents 1f392b9 + 809af9f commit f38e008
Show file tree
Hide file tree
Showing 39 changed files with 537 additions and 274 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.12.18",
"version": "0.12.19-rc.2",
"useWorkspaces": true,
"npmClient": "yarn",
"command": {
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keplr-wallet/analytics",
"version": "0.12.18",
"version": "0.12.19-rc.2",
"main": "build/index.js",
"author": "chainapsis",
"license": "Apache-2.0",
Expand Down
24 changes: 12 additions & 12 deletions packages/background/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keplr-wallet/background",
"version": "0.12.18",
"version": "0.12.19-rc.2",
"main": "build/index.js",
"author": "chainapsis",
"license": "Apache-2.0",
Expand Down Expand Up @@ -28,17 +28,17 @@
"@ethersproject/hash": "^5.7.0",
"@ethersproject/transactions": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"@keplr-wallet/chain-validator": "0.12.18",
"@keplr-wallet/common": "0.12.18",
"@keplr-wallet/cosmos": "0.12.18",
"@keplr-wallet/crypto": "0.12.18",
"@keplr-wallet/ledger-cosmos": "0.12.18",
"@keplr-wallet/popup": "0.12.18",
"@keplr-wallet/proto-types": "0.12.18",
"@keplr-wallet/router": "0.12.18",
"@keplr-wallet/simple-fetch": "0.12.18",
"@keplr-wallet/types": "0.12.18",
"@keplr-wallet/unit": "0.12.18",
"@keplr-wallet/chain-validator": "0.12.19-rc.2",
"@keplr-wallet/common": "0.12.19-rc.2",
"@keplr-wallet/cosmos": "0.12.19-rc.2",
"@keplr-wallet/crypto": "0.12.19-rc.2",
"@keplr-wallet/ledger-cosmos": "0.12.19-rc.2",
"@keplr-wallet/popup": "0.12.19-rc.2",
"@keplr-wallet/proto-types": "0.12.19-rc.2",
"@keplr-wallet/router": "0.12.19-rc.2",
"@keplr-wallet/simple-fetch": "0.12.19-rc.2",
"@keplr-wallet/types": "0.12.19-rc.2",
"@keplr-wallet/unit": "0.12.19-rc.2",
"@keystonehq/base-eth-keyring": "^0.6.4",
"@keystonehq/bc-ur-registry": "^0.5.1",
"@keystonehq/bc-ur-registry-cosmos": "^0.1.1",
Expand Down
8 changes: 4 additions & 4 deletions packages/chain-validator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keplr-wallet/chain-validator",
"version": "0.12.18",
"version": "0.12.19-rc.2",
"main": "build/index.js",
"author": "chainapsis",
"license": "Apache-2.0",
Expand All @@ -16,9 +16,9 @@
"lint-fix": "eslint --fix \"src/**/*\" && prettier --write \"src/**/*\""
},
"dependencies": {
"@keplr-wallet/cosmos": "0.12.18",
"@keplr-wallet/simple-fetch": "0.12.18",
"@keplr-wallet/types": "0.12.18",
"@keplr-wallet/cosmos": "0.12.19-rc.2",
"@keplr-wallet/simple-fetch": "0.12.19-rc.2",
"@keplr-wallet/types": "0.12.19-rc.2",
"joi": "^17.5.0",
"utility-types": "^3.10.0"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keplr-wallet/common",
"version": "0.12.18",
"version": "0.12.19-rc.2",
"main": "build/index.js",
"author": "chainapsis",
"license": "Apache-2.0",
Expand All @@ -16,8 +16,8 @@
"lint-fix": "eslint --fix \"src/**/*\" && prettier --write \"src/**/*\""
},
"dependencies": {
"@keplr-wallet/crypto": "0.12.18",
"@keplr-wallet/types": "0.12.18",
"@keplr-wallet/crypto": "0.12.19-rc.2",
"@keplr-wallet/types": "0.12.19-rc.2",
"buffer": "^6.0.3",
"delay": "^4.4.0",
"mobx": "^6.1.7"
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmjs-test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cosmjs-test",
"version": "0.12.18",
"version": "0.12.19-rc.2",
"author": "chainapsis",
"license": "Apache-2.0",
"private": true,
Expand All @@ -12,7 +12,7 @@
},
"devDependencies": {
"@cosmjs/stargate": "^0.29.3",
"@keplr-wallet/provider-mock": "0.12.18",
"@keplr-wallet/provider-mock": "0.12.19-rc.2",
"secretjs": "^1.6.0"
}
}
14 changes: 7 additions & 7 deletions packages/cosmos/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keplr-wallet/cosmos",
"version": "0.12.18",
"version": "0.12.19-rc.2",
"main": "build/index.js",
"author": "chainapsis",
"license": "Apache-2.0",
Expand All @@ -17,12 +17,12 @@
},
"dependencies": {
"@ethersproject/address": "^5.6.0",
"@keplr-wallet/common": "0.12.18",
"@keplr-wallet/crypto": "0.12.18",
"@keplr-wallet/proto-types": "0.12.18",
"@keplr-wallet/simple-fetch": "0.12.18",
"@keplr-wallet/types": "0.12.18",
"@keplr-wallet/unit": "0.12.18",
"@keplr-wallet/common": "0.12.19-rc.2",
"@keplr-wallet/crypto": "0.12.19-rc.2",
"@keplr-wallet/proto-types": "0.12.19-rc.2",
"@keplr-wallet/simple-fetch": "0.12.19-rc.2",
"@keplr-wallet/types": "0.12.19-rc.2",
"@keplr-wallet/unit": "0.12.19-rc.2",
"bech32": "^1.1.4",
"buffer": "^6.0.3",
"long": "^4.0.0",
Expand Down
149 changes: 140 additions & 9 deletions packages/cosmos/src/stargate/codec/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import * as $protobuf from "protobufjs";
import { Any } from "@keplr-wallet/proto-types/google/protobuf/any";
import { MsgSend } from "@keplr-wallet/proto-types/cosmos/bank/v1beta1/tx";
import {
MsgMultiSend,
MsgSend,
} from "@keplr-wallet/proto-types/cosmos/bank/v1beta1/tx";
import {
MsgDelegate,
MsgUndelegate,
MsgBeginRedelegate,
} from "@keplr-wallet/proto-types/cosmos/staking/v1beta1/tx";
import {
MsgExec,
MsgGrant,
MsgRevoke,
} from "@keplr-wallet/proto-types/cosmos/authz/v1beta1/tx";
import { MsgVote } from "@keplr-wallet/proto-types/cosmos/gov/v1beta1/tx";
import { MsgWithdrawDelegatorReward } from "@keplr-wallet/proto-types/cosmos/distribution/v1beta1/tx";
import {
MsgSetWithdrawAddress,
MsgWithdrawDelegatorReward,
} from "@keplr-wallet/proto-types/cosmos/distribution/v1beta1/tx";
import {
MsgExecuteContract,
MsgInstantiateContract,
} from "@keplr-wallet/proto-types/cosmwasm/wasm/v1/tx";
import { MsgTransfer } from "@keplr-wallet/proto-types/ibc/applications/transfer/v1/tx";
import { UnknownMessage } from "./unknown";
import { GenericAuthorization } from "@keplr-wallet/proto-types/cosmos/authz/v1beta1/authz";
import { StakeAuthorization } from "@keplr-wallet/proto-types/cosmos/staking/v1beta1/authz";
import { SendAuthorization } from "@keplr-wallet/proto-types/cosmos/bank/v1beta1/authz";
import { Buffer } from "buffer/";

export * from "./unknown";

export type AnyWithUnpacked =
| Any
| (Any & { unpacked: unknown; factory: ProtoFactory });
export type AnyWithUnpacked = Any | (Any & { unpacked: unknown });

interface ProtoFactory {
encode: (message: any, writer?: $protobuf.Writer) => $protobuf.Writer;
Expand All @@ -35,34 +44,137 @@ interface ProtoFactory {
export class ProtoCodec {
protected typeUrlMap: Map<string, ProtoFactory> = new Map();

unpackAnyFactory(typeUrl: string): ProtoFactory | undefined {
if (!this.typeUrlMap.has(typeUrl)) {
return undefined;
}

return this.typeUrlMap.get(typeUrl);
}

/**
* Unpack the any to the registered message.
* NOTE: If there is no matched message, it will not throw an error but return the `UnknownMessage` class.
* @param any
*/
unpackAny(any: Any): AnyWithUnpacked {
if (!this.typeUrlMap.has(any.typeUrl)) {
const factory = this.unpackAnyFactory(any.typeUrl);
if (!factory) {
return new UnknownMessage(any.typeUrl, any.value);
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const factory = this.typeUrlMap.get(any.typeUrl)!;
const unpacked = factory.decode(any.value);

return {
...any,
unpacked,
factory,
};
}

unpackedAnyToJSONRecursive(unpacked: AnyWithUnpacked): unknown {
if (unpacked instanceof UnknownMessage) {
return unpacked.toJSON();
}

const factory = this.unpackAnyFactory(unpacked.typeUrl);
if (factory && "unpacked" in unpacked && unpacked.unpacked) {
const isJSONEncodedAny = (
any: any
): any is {
typeUrl: string;
value: string;
} => {
const r =
typeof any === "object" &&
!(any instanceof UnknownMessage) &&
"typeUrl" in any &&
any.typeUrl &&
typeof any.typeUrl === "string" &&
"value" in any &&
any.value &&
typeof any.value === "string";

if (r) {
try {
Buffer.from(any.value, "base64");
} catch {
return false;
}
}

return r;
};

const unpackJSONEncodedAnyInner = (jsonEncodedAny: {
typeUrl: string;
value: string;
}): {
typeUrl: string;
value: unknown;
} => {
const factory = this.unpackAnyFactory(jsonEncodedAny.typeUrl);

const bz = Buffer.from(jsonEncodedAny.value, "base64");

if (!factory) {
return new UnknownMessage(jsonEncodedAny.typeUrl, bz).toJSON();
}

const unpacked = factory.decode(bz);
return {
typeUrl: jsonEncodedAny.typeUrl,
value: factory.toJSON(unpacked),
};
};

const unpackedJSONEncodedAnyRecursive = (obj: object): object => {
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
const value = obj[i];
if (isJSONEncodedAny(value)) {
obj[i] = unpackJSONEncodedAnyInner(value);
} else if (typeof value === "object") {
obj[i] = unpackedJSONEncodedAnyRecursive(value);
}
}
} else {
for (const key in obj) {
const value = (obj as any)[key];
if (isJSONEncodedAny(value)) {
(obj as any)[key] = unpackJSONEncodedAnyInner(value);
} else if (typeof value === "object") {
(obj as any)[key] = unpackedJSONEncodedAnyRecursive(value);
}
}
}
return obj;
};

// This is mutated by logic.
let mutObj = factory.toJSON(unpacked.unpacked);
if (mutObj && typeof mutObj === "object") {
mutObj = unpackedJSONEncodedAnyRecursive(mutObj);
return {
typeUrl: unpacked.typeUrl,
value: mutObj,
};
}
}

return new UnknownMessage(unpacked.typeUrl, unpacked.value).toJSON();
}

registerAny(typeUrl: string, message: ProtoFactory): void {
this.typeUrlMap.set(typeUrl, message);
}
}

export const defaultProtoCodec = new ProtoCodec();
defaultProtoCodec.registerAny("/cosmos.bank.v1beta1.MsgSend", MsgSend);
defaultProtoCodec.registerAny(
"/cosmos.bank.v1beta1.MsgMultiSend",
MsgMultiSend
);
defaultProtoCodec.registerAny(
"/cosmos.staking.v1beta1.MsgDelegate",
MsgDelegate
Expand All @@ -87,10 +199,29 @@ defaultProtoCodec.registerAny(
"/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
MsgWithdrawDelegatorReward
);
defaultProtoCodec.registerAny(
"/cosmos.distribution.v1beta1.MsgSetWithdrawAddress",
MsgSetWithdrawAddress
);
defaultProtoCodec.registerAny(
"/ibc.applications.transfer.v1.MsgTransfer",
MsgTransfer
);
defaultProtoCodec.registerAny("/cosmos.gov.v1beta1.MsgVote", MsgVote);
defaultProtoCodec.registerAny("/cosmos.authz.v1beta1.MsgGrant", MsgGrant);
// ----- Authz grants -----
defaultProtoCodec.registerAny(
"/cosmos.authz.v1beta1.GenericAuthorization",
GenericAuthorization
);
defaultProtoCodec.registerAny(
"/cosmos.staking.v1beta1.StakeAuthorization",
StakeAuthorization
);
defaultProtoCodec.registerAny(
"/cosmos.bank.v1beta1.SendAuthorization",
SendAuthorization
);
// ----- Authz grants -----
defaultProtoCodec.registerAny("/cosmos.authz.v1beta1.MsgRevoke", MsgRevoke);
defaultProtoCodec.registerAny("/cosmos.authz.v1beta1.MsgExec", MsgExec);
2 changes: 1 addition & 1 deletion packages/cosmos/src/stargate/codec/unknown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class UnknownMessage implements Any {

toJSON() {
return {
type_url: this._typeUrl,
typeUrl: this._typeUrl,
value: Buffer.from(this._value).toString("base64"),
};
}
Expand Down
17 changes: 2 additions & 15 deletions packages/cosmos/src/stargate/decoder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import {
TxBody,
AuthInfo,
} from "@keplr-wallet/proto-types/cosmos/tx/v1beta1/tx";
import {
AnyWithUnpacked,
defaultProtoCodec,
ProtoCodec,
UnknownMessage,
} from "../codec";
import { AnyWithUnpacked, defaultProtoCodec, ProtoCodec } from "../codec";

export class ProtoSignDocDecoder {
public static decode(bytes: Uint8Array): ProtoSignDocDecoder {
Expand Down Expand Up @@ -66,15 +61,7 @@ export class ProtoSignDocDecoder {
...(TxBody.toJSON(this.txBody) as any),
...{
messages: this.txMsgs.map((msg) => {
if (msg) {
if (msg instanceof UnknownMessage) {
return msg.toJSON();
}
if ("factory" in msg) {
return msg.factory.toJSON(msg.unpacked);
}
}
return msg;
return this.protoCodec.unpackedAnyToJSONRecursive(msg);
}),
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keplr-wallet/crypto",
"version": "0.12.18",
"version": "0.12.19-rc.2",
"main": "build/index.js",
"author": "chainapsis",
"license": "Apache-2.0",
Expand Down
Loading

0 comments on commit f38e008

Please sign in to comment.