Skip to content

Commit

Permalink
Test happy path for MsgMigrateContract
Browse files Browse the repository at this point in the history
- Fix encryption & decryption for MsgMigrateContract
- Update some docs
- Update MsgDecoder
  • Loading branch information
assafmo committed Jun 13, 2023
1 parent 853e416 commit 68418d4
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 63 deletions.
54 changes: 47 additions & 7 deletions src/secret_network_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ import { Any } from "./protobuf/google/protobuf/any";
import {
MsgExecuteContractResponse,
MsgInstantiateContractResponse,
MsgMigrateContractResponse,
} from "./protobuf/secret/compute/v1beta1/msg";
import { AuthQuerier } from "./query/auth";
import { AuthzQuerier } from "./query/authz";
Expand Down Expand Up @@ -187,6 +188,10 @@ import { UpgradeQuerier } from "./query/upgrade";
import {
AminoMsg,
Msg,
MsgClearAdmin,
MsgClearAdminParams,
MsgMigrateContract,
MsgMigrateContractParams,
MsgParams,
MsgPayPacketFee,
MsgPayPacketFeeAsync,
Expand All @@ -199,6 +204,7 @@ import {
MsgRegistry,
MsgSetAutoRestake,
MsgSetAutoRestakeParams,
MsgUpdateAdmin,
ProtoMsg,
} from "./tx";
import {
Expand Down Expand Up @@ -691,12 +697,18 @@ export type TxSender = {
send: SingleMsgTx<MsgSendParams>;
};
compute: {
/** Execute a function on a contract */
executeContract: SingleMsgTx<MsgExecuteContractParams<object>>;
/** Upload a compiled contract */
storeCode: SingleMsgTx<MsgStoreCodeParams>;
/** Instantiate a contract from code id */
instantiateContract: SingleMsgTx<MsgInstantiateContractParams>;
/** Upload a compiled contract to Secret Network */
storeCode: SingleMsgTx<MsgStoreCodeParams>;
/** Execute a function on a contract */
executeContract: SingleMsgTx<MsgExecuteContractParams<object>>;
/** Runs a code upgrade/downgrade for a contract */
migrateContract: SingleMsgTx<MsgMigrateContractParams<object>>;
/** Update the admin of a contract */
updateAdmin: SingleMsgTx<MsgUpdateAdminParams>;
/** Clear the admin of a contract */
clearAdmin: SingleMsgTx<MsgClearAdminParams>;
};
emergency_button: {
toggleIbcSwitch: SingleMsgTx<MsgToggleIbcSwitchParams>;
Expand Down Expand Up @@ -931,9 +943,12 @@ export class SecretNetworkClient {
send: doMsg(MsgSend),
},
compute: {
executeContract: doMsg(MsgExecuteContract),
instantiateContract: doMsg(MsgInstantiateContract),
storeCode: doMsg(MsgStoreCode),
instantiateContract: doMsg(MsgInstantiateContract),
executeContract: doMsg(MsgExecuteContract),
migrateContract: doMsg(MsgMigrateContract),
updateAdmin: doMsg(MsgUpdateAdmin),
clearAdmin: doMsg(MsgClearAdmin),
},
emergency_button: {
toggleIbcSwitch: doMsg(MsgToggleIbcSwitch),
Expand Down Expand Up @@ -1151,7 +1166,8 @@ export class SecretNetworkClient {
if (msg["@type"] === "/secret.compute.v1beta1.MsgInstantiateContract") {
contractInputMsgFieldName = "init_msg";
} else if (
msg["@type"] === "/secret.compute.v1beta1.MsgExecuteContract"
msg["@type"] === "/secret.compute.v1beta1.MsgExecuteContract" ||
msg["@type"] === "/secret.compute.v1beta1.MsgMigrateContract"
) {
contractInputMsgFieldName = "msg";
}
Expand Down Expand Up @@ -1298,6 +1314,18 @@ export class SecretNetworkClient {
data[msgIndex] = MsgExecuteContractResponse.encode({
data: decrypted,
}).finish();
} else if (
type_url === "/secret.compute.v1beta1.MsgMigrateContract"
) {
const decoded = MsgMigrateContractResponse.decode(
txMsgData.data[msgIndex].data,
);
const decrypted = fromBase64(
fromUtf8(await this.encryptionUtils.decrypt(decoded.data, nonce)),
);
data[msgIndex] = MsgMigrateContractResponse.encode({
data: decrypted,
}).finish();
}
} catch (decryptionError) {
// Not encrypted or can't decrypt because not original sender
Expand Down Expand Up @@ -1509,6 +1537,10 @@ export class SecretNetworkClient {
} else if (msg.type_url === "/secret.compute.v1beta1.MsgStoreCode") {
msg.value.sender = bytesToAddress(msg.value.sender);
msg.value.wasm_byte_code = toBase64(msg.value.wasm_byte_code);
} else if (
msg.type_url === "/secret.compute.v1beta1.MsgMigrateContract"
) {
msg.value.msg = toBase64(msg.value.msg);
}

tx.body!.messages![i] = msg;
Expand Down Expand Up @@ -1843,6 +1875,14 @@ export class SecretNetworkClient {
})
).code_hash!;
}
} else if (msg instanceof MsgMigrateContract) {
if (!msg.codeHash) {
msg.codeHash = (
await this.query.compute.codeHashByCodeId({
code_id: msg.codeId,
})
).code_hash!;
}
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/tx/compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface MsgInstantiateContractParams extends MsgParams {
* character hex string.
* This is used to make sure only the contract that's being invoked can decrypt the query data.
*
* codeHash is an optional parameter but using it will result in way faster execution time.
* codeHash is an optional parameter but using it will result in way faster execution time (otherwise secret.js will make an query to get the code_hash from the chain).
*
* Valid examples:
* - "af74387e276be8874f07bec3a87023ee49b0e7ebe08178c49d0a49c3c98ed60e"
Expand Down Expand Up @@ -147,7 +147,7 @@ export interface MsgExecuteContractParams<T> extends MsgParams {
* character hex string.
* This is used to make sure only the contract that's being invoked can decrypt the query data.
*
* codeHash is an optional parameter but using it will result in way faster execution time.
* codeHash is an optional parameter but using it will result in way faster execution time (otherwise secret.js will make an query to get the code_hash from the chain).
*
* Valid examples:
* - "af74387e276be8874f07bec3a87023ee49b0e7ebe08178c49d0a49c3c98ed60e"
Expand Down Expand Up @@ -324,21 +324,21 @@ export interface MsgMigrateContractParams<T> extends MsgParams {
/** The contract's address */
contract_address: string;
/** The new code id */
new_code_id: number | string;
code_id: number | string;
/** The input message */
msg: T;
/** The SHA256 hash value of the contract's *new* WASM bytecode, represented as case-insensitive 64 character hex string.
* This is used to make sure only the contract that's being invoked can decrypt the query data.
*
* codeHash is an optional parameter but using it will result in way faster execution time.
* codeHash is an optional parameter but using it will result in way faster execution time (otherwise secret.js will make an query to get the code_hash from the chain).
*
* Valid examples:
* - "af74387e276be8874f07bec3a87023ee49b0e7ebe08178c49d0a49c3c98ed60e"
* - "0xaf74387e276be8874f07bec3a87023ee49b0e7ebe08178c49d0a49c3c98ed60e"
* - "AF74387E276BE8874F07BEC3A87023EE49B0E7EBE08178C49D0A49C3C98ED60E"
* - "0xAF74387E276BE8874F07BEC3A87023EE49B0E7EBE08178C49D0A49C3C98ED60E"
*/
new_code_hash?: string;
code_hash?: string;
}

/** Execute a function on a contract */
Expand All @@ -355,8 +355,8 @@ export class MsgMigrateContract<T extends object> implements Msg {
sender,
contract_address: contractAddress,
msg,
new_code_id: codeId,
new_code_hash: codeHash,
code_id: codeId,
code_hash: codeHash,
}: MsgMigrateContractParams<T>) {
this.sender = sender;
this.contractAddress = contractAddress;
Expand Down
18 changes: 12 additions & 6 deletions src/tx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ import {
MsgUndelegate,
} from "../protobuf/cosmos/staking/v1beta1/tx";
import { MsgCreateVestingAccount } from "../protobuf/cosmos/vesting/v1beta1/tx";
import { MsgTransfer } from "../protobuf/ibc/applications/transfer/v1/tx";
import {
MsgPayPacketFee,
MsgPayPacketFeeAsync,
MsgRegisterPayee,
MsgRegisterCounterpartyPayee,
MsgRegisterPayee,
} from "../protobuf/ibc/applications/fee/v1/tx";
import { MsgTransfer } from "../protobuf/ibc/applications/transfer/v1/tx";
import {
MsgAcknowledgement,
MsgChannelCloseConfirm,
Expand All @@ -64,31 +64,34 @@ import {
MsgConnectionOpenTry,
} from "../protobuf/ibc/core/connection/v1/tx";
import {
MsgClearAdmin,
MsgExecuteContract,
MsgInstantiateContract,
MsgMigrateContract,
MsgStoreCode,
MsgUpdateAdmin,
} from "../protobuf/secret/compute/v1beta1/msg";
import { MsgToggleIbcSwitch } from "../protobuf/secret/emergencybutton/v1beta1/tx";
import { RaAuthenticate } from "../protobuf/secret/registration/v1beta1/msg";
import {MsgToggleIbcSwitch} from "../protobuf/secret/emergencybutton/v1beta1/tx";

export * from "./authz";
export * from "./bank";
export * from "./compute";
export * from "./crisis";
export * from "./distribution";
export * from "./emergency_button";
export * from "./evidence";
export * from "./feegrant";
export * from "./gov";
export * from "./ibc_channel";
export * from "./ibc_client";
export * from "./ibc_connection";
export * from "./ibc_transfer";
export * from "./ibc_fee";
export * from "./ibc_transfer";
export * from "./slashing";
export * from "./staking";
export * from "./vesting";
export * from "./types";
export * from "./emergency_button";
export * from "./vesting";

export type MsgDecoder = {
decode(input: Uint8Array): any;
Expand Down Expand Up @@ -157,6 +160,9 @@ export const MsgRegistry = new Map<string, MsgDecoder>([
["/secret.compute.v1beta1.MsgStoreCode", MsgStoreCode],
["/secret.compute.v1beta1.MsgInstantiateContract", MsgInstantiateContract],
["/secret.compute.v1beta1.MsgExecuteContract", MsgExecuteContract],
["/secret.compute.v1beta1.MsgMigrateContract", MsgMigrateContract],
["/secret.compute.v1beta1.MsgUpdateAdmin", MsgUpdateAdmin],
["/secret.compute.v1beta1.MsgClearAdmin", MsgClearAdmin],
["/secret.registration.v1beta1.RaAuthenticate", RaAuthenticate],
["/cosmos.vesting.v1beta1.MsgCreateVestingAccount", MsgCreateVestingAccount],
["/secret.emergencybutton.v1beta1.MsgToggleIbcSwitch", MsgToggleIbcSwitch],
Expand Down
4 changes: 2 additions & 2 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.9"
services:
localsecret-1:
image: "ghcr.io/scrtlabs/localsecret:v1.10.0-beta.0"
image: "ghcr.io/scrtlabs/localsecret:v0.0.0"
ports:
- "26657:26657" # RPC
- "26656:26656" # P2P
Expand All @@ -13,7 +13,7 @@ services:
FAST_BLOCKS: "true"
LOG_LEVEL: "TRACE"
localsecret-2:
image: "ghcr.io/scrtlabs/localsecret:v1.10.0-beta.0"
image: "ghcr.io/scrtlabs/localsecret:v0.0.0"
ports:
- "36657:26657" # RPC
- "36656:26656" # P2P
Expand Down
8 changes: 8 additions & 0 deletions test/ibc-hooks-contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,11 @@ pub fn execute(_deps: DepsMut, env: Env, info: MessageInfo, msg: Msg) -> StdResu
}
}
}

#[entry_point]
pub fn migrate(_deps: DepsMut, env: Env, msg: Msg) -> StdResult<Response> {
Ok(Response::default().add_attributes(vec![
("migrate.env", format!("{:?}", env)),
("migrate.msg", format!("{:?}", msg)),
]))
}
Binary file modified test/ibc-hooks.wasm.gz
Binary file not shown.
Loading

0 comments on commit 68418d4

Please sign in to comment.