Skip to content

Commit

Permalink
feat: enable wasm, wasmplus store amino with access type
Browse files Browse the repository at this point in the history
  • Loading branch information
loin3 committed Feb 22, 2023
1 parent 1339f16 commit bd19a19
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 28 deletions.
108 changes: 108 additions & 0 deletions packages/finschia/src/modules/wasm/aminomessages.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { fromBase64 } from "@cosmjs/encoding";
import { AminoTypes } from "@cosmjs/stargate";
import { MsgStoreCode } from "cosmjs-types/cosmwasm/wasm/v1/tx";
import { AccessType } from "cosmjs-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",
},
};
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",
},
},
};
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",
},
},
};
const msg = new AminoTypes(createWasmAminoConverters()).fromAmino(aminoMsg);
const expectedValue: MsgStoreCode = {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
instantiatePermission: {
permission: AccessType.ACCESS_TYPE_ONLY_ADDRESS,
address: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
},
};
expect(msg).toEqual({
typeUrl: "/cosmwasm.wasm.v1.MsgStoreCode",
value: expectedValue,
});
});
});
});
105 changes: 105 additions & 0 deletions packages/finschia/src/modules/wasm/aminomessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* 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 "cosmjs-types/cosmwasm/wasm/v1/tx";
import { AccessType } from "cosmjs-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;
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.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;
readonly address?: 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,
}
: 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 ?? "",
}
: undefined,
}),
},
};
}

/** @deprecated use `createWasmAminoConverters()` */
export const cosmWasmTypes: AminoConverters = createWasmAminoConverters();
141 changes: 141 additions & 0 deletions packages/finschia/src/modules/wasmplus/aminomessages.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { fromBase64, toUtf8 } from "@cosmjs/encoding";
import { AminoTypes, coins } from "@cosmjs/stargate";
import { AccessType } from "cosmjs-types/cosmwasm/wasm/v1/types";
import { MsgStoreCodeAndInstantiateContract } from "lbmjs-types/lbm/wasm/v1/tx";

import { AminoMsgStoreCodeAndInstantiateContract, createWasmplusAminoConverters } from "./aminomessages";

describe("AminoTypes", () => {
describe("toAmino", () => {
it("works for MsgStoreCodeAndInstantiate", () => {
const msg: MsgStoreCodeAndInstantiateContract = {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
instantiatePermission: undefined,
admin: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
label: "sticky",
msg: toUtf8(`{"foo":"bar"}`),
funds: coins(1234, "cony"),
};
const aminoMsg = new AminoTypes(createWasmplusAminoConverters()).toAmino({
typeUrl: "/lbm.wasm.v1.MsgStoreCodeAndInstantiateContract",
value: msg,
});
const expected: AminoMsgStoreCodeAndInstantiateContract = {
type: "wasm/MsgStoreCodeAndInstantiateContract",
value: {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasm_byte_code: "WUVMTE9XIFNVQk1BUklORQ==",
instantiate_permission: undefined,
admin: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
label: "sticky",
msg: { foo: "bar" },
funds: coins(1234, "cony"),
},
};
expect(aminoMsg).toEqual(expected);
});

it("works for MsgStoreCodeAndInstantiate with access type", () => {
const msg: MsgStoreCodeAndInstantiateContract = {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
instantiatePermission: {
permission: AccessType.ACCESS_TYPE_ONLY_ADDRESS,
address: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
},
admin: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
label: "sticky",
msg: toUtf8(`{"foo":"bar"}`),
funds: coins(1234, "cony"),
};
const aminoMsg = new AminoTypes(createWasmplusAminoConverters()).toAmino({
typeUrl: "/lbm.wasm.v1.MsgStoreCodeAndInstantiateContract",
value: msg,
});
const expected: AminoMsgStoreCodeAndInstantiateContract = {
type: "wasm/MsgStoreCodeAndInstantiateContract",
value: {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasm_byte_code: "WUVMTE9XIFNVQk1BUklORQ==",
instantiate_permission: {
permission: "OnlyAddress",
address: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
},
admin: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
label: "sticky",
msg: { foo: "bar" },
funds: coins(1234, "cony"),
},
};
expect(aminoMsg).toEqual(expected);
});
});

describe("fromAmino", () => {
it("works for MsgStoreCodeAndInstantiateContract", () => {
const aminoMsg: AminoMsgStoreCodeAndInstantiateContract = {
type: "wasm/MsgStoreCodeAndInstantiateContract",
value: {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasm_byte_code: "WUVMTE9XIFNVQk1BUklORQ==",
instantiate_permission: undefined,
admin: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
label: "sticky",
msg: { foo: "bar" },
funds: coins(1234, "cony"),
},
};
const msg = new AminoTypes(createWasmplusAminoConverters()).fromAmino(aminoMsg);
const expectedValue: MsgStoreCodeAndInstantiateContract = {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
instantiatePermission: undefined,
admin: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
label: "sticky",
msg: toUtf8(`{"foo":"bar"}`),
funds: coins(1234, "cony"),
};
expect(msg).toEqual({
typeUrl: "/lbm.wasm.v1.MsgStoreCodeAndInstantiateContract",
value: expectedValue,
});
});

it("works for MsgStoreCodeAndInstantiateContract with access type", () => {
const aminoMsg: AminoMsgStoreCodeAndInstantiateContract = {
type: "wasm/MsgStoreCodeAndInstantiateContract",
value: {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasm_byte_code: "WUVMTE9XIFNVQk1BUklORQ==",
instantiate_permission: {
permission: "OnlyAddress",
address: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
},
admin: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
label: "sticky",
msg: { foo: "bar" },
funds: coins(1234, "cony"),
},
};
const msg = new AminoTypes(createWasmplusAminoConverters()).fromAmino(aminoMsg);
const expectedValue: MsgStoreCodeAndInstantiateContract = {
sender: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
wasmByteCode: fromBase64("WUVMTE9XIFNVQk1BUklORQ=="),
instantiatePermission: {
permission: AccessType.ACCESS_TYPE_ONLY_ADDRESS,
address: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
},
admin: "link1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
label: "sticky",
msg: toUtf8(`{"foo":"bar"}`),
funds: coins(1234, "cony"),
};
expect(msg).toEqual({
typeUrl: "/lbm.wasm.v1.MsgStoreCodeAndInstantiateContract",
value: expectedValue,
});
});
});
});
Loading

0 comments on commit bd19a19

Please sign in to comment.