Skip to content

Commit

Permalink
enable sign in amino wasm, wasmplus store message with access type (#77)
Browse files Browse the repository at this point in the history
* feat: enable wasm, wasmplus store amino with access type

* chore: delete unused things

* chore: change port

* feat: change codes after bump up wasm

* fix: error when omit empty

* chore: add changelog

* chore: bumpup lbmjs-types and change tests

* test: re generate template
  • Loading branch information
loin3 authored Mar 31, 2023
1 parent bdca8c3 commit b054a95
Show file tree
Hide file tree
Showing 47 changed files with 573 additions and 106 deletions.
10 changes: 5 additions & 5 deletions .pnp.cjs

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

This file was deleted.

Git LFS file not shown
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to
### Added
- [\#69](https://github.com/line/lbmjs/pull/69) apply the changes of `x/token` and `x/collection` protos
- [\#71](https://github.com/line/lbmjs/pull/71) @lbmjs/finschia: Add custom GetBlockWithTxs for lbm
- [\#77](https://github.com/line/lbmjs/pull/77) @lbmjs/finschia: Enable amino sign store msgs in `wasm` and `wasmplus`
- [\#80](https://github.com/line/lbmjs/pull/80) add MsgInstantiateContract2
- [\#81](https://github.com/line/lbmjs/pull/81) add git PR template

Expand Down
2 changes: 1 addition & 1 deletion packages/finschia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@cosmjs/tendermint-rpc": "^0.29.0",
"@cosmjs/utils": "^0.29.0",
"cosmjs-types": "^0.5.1",
"lbmjs-types": "^0.47.0-alpha1",
"lbmjs-types": "^1.0.0-rc2",
"long": "^4.0.0",
"pako": "^2.0.2",
"protobufjs": "~6.10.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ describe("Amino sign", () => {
authority: authorityAddress,
params: {
foundationTax: Decimal.fromUserInput("0.1", 18).atomics,
censoredMsgTypeUrls: ["/lbm.foundation.v1.MsgWithdrawFromTreasury"],
},
},
};
Expand Down Expand Up @@ -226,7 +225,6 @@ describe("Amino sign", () => {
authority: authorityAddress,
params: {
foundationTax: Decimal.fromUserInput("0.1", 18).atomics,
censoredMsgTypeUrls: ["/lbm.foundation.v1.MsgWithdrawFromTreasury"],
},
},
};
Expand Down Expand Up @@ -475,7 +473,6 @@ describe("AminoTypes", () => {
authority: faucet.address0,
params: {
foundationTax: "0",
censoredMsgTypeUrls: ["/lbm.foundation.v1.MsgWithdrawFromTreasury"],
},
};
const aminoTypes = new AminoTypes(createFoundationAminoConverters());
Expand All @@ -489,7 +486,6 @@ describe("AminoTypes", () => {
authority: faucet.address0,
params: {
foundation_tax: "0.000000000000000000",
censored_msg_type_urls: ["/lbm.foundation.v1.MsgWithdrawFromTreasury"],
},
},
};
Expand Down Expand Up @@ -833,7 +829,6 @@ describe("AminoTypes", () => {
authority: faucet.address0,
params: {
foundation_tax: "0",
censored_msg_type_urls: ["/lbm.foundation.v1.MsgWithdrawFromTreasury"],
},
},
};
Expand All @@ -843,7 +838,6 @@ describe("AminoTypes", () => {
authority: faucet.address0,
params: {
foundationTax: "0",
censoredMsgTypeUrls: ["/lbm.foundation.v1.MsgWithdrawFromTreasury"],
},
};
expect(msg).toEqual({
Expand Down
3 changes: 0 additions & 3 deletions packages/finschia/src/modules/foundation/aminomessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {

interface Params {
foundation_tax: string;
censored_msg_type_urls: string[];
}

interface DecisionPolicyWindows {
Expand Down Expand Up @@ -292,7 +291,6 @@ export function createFoundationAminoConvertersWithoutSubmitProposal(): AminoCon
authority: authority,
params: {
foundation_tax: protoDecimalToJson(params.foundationTax),
censored_msg_type_urls: params.censoredMsgTypeUrls,
},
};
},
Expand All @@ -302,7 +300,6 @@ export function createFoundationAminoConvertersWithoutSubmitProposal(): AminoCon
authority: authority,
params: {
foundationTax: jsonDecimalToProto(params.foundation_tax),
censoredMsgTypeUrls: params.censored_msg_type_urls,
},
};
},
Expand Down
112 changes: 112 additions & 0 deletions packages/finschia/src/modules/wasm/aminomessages.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { fromBase64 } from "@cosmjs/encoding";
import { AminoTypes } from "@cosmjs/stargate";
import { MsgStoreCode } from "lbmjs-types/cosmwasm/wasm/v1/tx";
import { AccessType } from "lbmjs-types/cosmwasm/wasm/v1/types";

import { AminoMsgStoreCode, createWasmAminoConverters } from "./aminomessages";

describe("AminoTypes", () => {
describe("toAmino", () => {
it("works for MsgStoreCode", () => {
const msg: MsgStoreCode = {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
instantiatePermission: undefined,
};
const aminoMsg = new AminoTypes(createWasmAminoConverters()).toAmino({
typeUrl: "/cosmwasm.wasm.v1.MsgStoreCode",
value: msg,
});
const expected: AminoMsgStoreCode = {
type: "wasm/MsgStoreCode",
value: {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasm_byte_code: "WUVMTE9XIFNVQk1BUklORQ==",
instantiate_permission: undefined,
},
};
expect(aminoMsg).toEqual(expected);
});

it("works for MsgStoreCode with access type", () => {
const msg: MsgStoreCode = {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
instantiatePermission: {
permission: AccessType.ACCESS_TYPE_ONLY_ADDRESS,
address: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
addresses: [],
},
};
const aminoMsg = new AminoTypes(createWasmAminoConverters()).toAmino({
typeUrl: "/cosmwasm.wasm.v1.MsgStoreCode",
value: msg,
});
const expected: AminoMsgStoreCode = {
type: "wasm/MsgStoreCode",
value: {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasm_byte_code: "WUVMTE9XIFNVQk1BUklORQ==",
instantiate_permission: {
permission: "OnlyAddress",
address: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
addresses: undefined,
},
},
};
expect(aminoMsg).toEqual(expected);
});
});

describe("fromAmino", () => {
it("works for MsgStoreCode", () => {
const aminoMsg: AminoMsgStoreCode = {
type: "wasm/MsgStoreCode",
value: {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasm_byte_code: "WUVMTE9XIFNVQk1BUklORQ==",
},
};
const msg = new AminoTypes(createWasmAminoConverters()).fromAmino(aminoMsg);
const expectedValue: MsgStoreCode = {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
instantiatePermission: undefined,
};
expect(msg).toEqual({
typeUrl: "/cosmwasm.wasm.v1.MsgStoreCode",
value: expectedValue,
});
});

it("works for MsgStoreCode with access type", () => {
const aminoMsg: AminoMsgStoreCode = {
type: "wasm/MsgStoreCode",
value: {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasm_byte_code: "WUVMTE9XIFNVQk1BUklORQ==",
instantiate_permission: {
permission: "OnlyAddress",
address: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
addresses: undefined,
},
},
};
const msg = new AminoTypes(createWasmAminoConverters()).fromAmino(aminoMsg);
const expectedValue: MsgStoreCode = {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
instantiatePermission: {
permission: AccessType.ACCESS_TYPE_ONLY_ADDRESS,
address: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
addresses: [],
},
};
expect(msg).toEqual({
typeUrl: "/cosmwasm.wasm.v1.MsgStoreCode",
value: expectedValue,
});
});
});
});
117 changes: 117 additions & 0 deletions packages/finschia/src/modules/wasm/aminomessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { createWasmAminoConverters as createAminoConverters } from "@cosmjs/cosmwasm-stargate";
import { fromBase64, toBase64 } from "@cosmjs/encoding";
import { AminoConverters } from "@cosmjs/stargate";
import { MsgStoreCode } from "lbmjs-types/cosmwasm/wasm/v1/tx";
import { AccessType } from "lbmjs-types/cosmwasm/wasm/v1/types";

export function accessTypeFromString(str: string): AccessType {
switch (str) {
case "Unspecified":
return AccessType.ACCESS_TYPE_UNSPECIFIED;
case "Nobody":
return AccessType.ACCESS_TYPE_NOBODY;
case "OnlyAddress":
return AccessType.ACCESS_TYPE_ONLY_ADDRESS;
case "Everybody":
return AccessType.ACCESS_TYPE_EVERYBODY;
case "AnyOfAddresses":
return AccessType.ACCESS_TYPE_ANY_OF_ADDRESSES;
default:
return AccessType.UNRECOGNIZED;
}
}

export function accessTypeToString(object: any): string {
switch (object) {
case AccessType.ACCESS_TYPE_UNSPECIFIED:
return "Unspecified";
case AccessType.ACCESS_TYPE_NOBODY:
return "Nobody";
case AccessType.ACCESS_TYPE_ONLY_ADDRESS:
return "OnlyAddress";
case AccessType.ACCESS_TYPE_EVERYBODY:
return "Everybody";
case AccessType.ACCESS_TYPE_ANY_OF_ADDRESSES:
return "AnyOfAddresses";
case AccessType.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
}
}

/**
* @see https://github.com/CosmWasm/wasmd/blob/v0.18.0-rc1/proto/cosmwasm/wasm/v1/types.proto#L36-L41
*/
export interface AccessConfig {
/**
* Permission should be one kind of string 'Nobody', 'OnlyAddress', 'Everybody', 'Unspecified'
* @see https://github.com/CosmWasm/wasmd/blob/v0.28.0/x/wasm/types/params.go#L54
*/
readonly permission: string;
/**
* Address
* Deprecated: replaced by addresses
*/
readonly address?: string;
readonly addresses?: string[];
}

/**
* The Amino JSON representation of [MsgStoreCode].
*
* [MsgStoreCode]: https://github.com/CosmWasm/wasmd/blob/v0.18.0-rc1/proto/cosmwasm/wasm/v1/tx.proto#L28-L39
*/
export interface AminoMsgStoreCode {
type: "wasm/MsgStoreCode";
value: {
/** Bech32 account address */
readonly sender: string;
/** Base64 encoded Wasm */
readonly wasm_byte_code: string;
readonly instantiate_permission?: AccessConfig;
};
}

export function createWasmAminoConverters(): AminoConverters {
return {
...createAminoConverters(),
"/cosmwasm.wasm.v1.MsgStoreCode": {
aminoType: "wasm/MsgStoreCode",
toAmino: ({
sender,
wasmByteCode,
instantiatePermission,
}: MsgStoreCode): AminoMsgStoreCode["value"] => ({
sender: sender,
wasm_byte_code: toBase64(wasmByteCode),
instantiate_permission: instantiatePermission
? {
permission: accessTypeToString(instantiatePermission.permission),
address: instantiatePermission.address || undefined,
addresses:
instantiatePermission.addresses.length !== 0 ? instantiatePermission.addresses : undefined,
}
: undefined,
}),
fromAmino: ({
sender,
wasm_byte_code,
instantiate_permission,
}: AminoMsgStoreCode["value"]): MsgStoreCode => ({
sender: sender,
wasmByteCode: fromBase64(wasm_byte_code),
instantiatePermission: instantiate_permission
? {
permission: accessTypeFromString(instantiate_permission.permission),
address: instantiate_permission.address ?? "",
addresses: instantiate_permission.addresses ?? [],
}
: undefined,
}),
},
};
}

/** @deprecated use `createWasmAminoConverters()` */
export const cosmWasmTypes: AminoConverters = createWasmAminoConverters();
10 changes: 9 additions & 1 deletion packages/finschia/src/modules/wasm/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EncodeObject, GeneratedType } from "@cosmjs/proto-signing";
import { Coin } from "lbmjs-types/cosmos/base/v1beta1/coin";
import {
MsgClearAdmin,
MsgExecuteContract,
Expand All @@ -8,7 +9,6 @@ import {
MsgStoreCode,
MsgUpdateAdmin,
} from "lbmjs-types/cosmwasm/wasm/v1/tx";
import { Coin } from "lbmjs-types/cosmos/base/v1beta1/coin";

export const wasmTypes: ReadonlyArray<[string, GeneratedType]> = [
["/cosmwasm.wasm.v1.MsgClearAdmin", MsgClearAdmin],
Expand All @@ -20,6 +20,14 @@ export const wasmTypes: ReadonlyArray<[string, GeneratedType]> = [
["/cosmwasm.wasm.v1.MsgUpdateAdmin", MsgUpdateAdmin],
];

export interface MsgStoreCodeEncodeObject extends EncodeObject {
readonly typeUrl: "/cosmwasm.wasm.v1.MsgStoreCode";
readonly value: Partial<MsgStoreCode>;
}

export function isMsgStoreCodeEncodeObject(object: EncodeObject): object is MsgStoreCodeEncodeObject {
return (object as MsgStoreCodeEncodeObject).typeUrl === "/cosmwasm.wasm.v1.MsgStoreCode";
}
export interface MsgInstantiateContract2EncodeObject extends EncodeObject {
readonly typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract2";
readonly value: Partial<MsgInstantiateContract2>;
Expand Down
Loading

0 comments on commit b054a95

Please sign in to comment.