Skip to content

Commit

Permalink
Fix permit test
Browse files Browse the repository at this point in the history
  • Loading branch information
iKapitonau committed Jun 13, 2024
1 parent 3c26dc0 commit 28632db
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
22 changes: 14 additions & 8 deletions src/secret_network_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1312,10 +1312,14 @@ export class SecretNetworkClient {
}

const txMsgData = TxMsgData.decode(fromHex(txResp.data!));
const data = new Array<Uint8Array>(txMsgData.data.length);
const data = new Array<Uint8Array>(txMsgData.msg_responses.length);

for (let msgIndex = 0; msgIndex < txMsgData.data.length; msgIndex++) {
data[msgIndex] = txMsgData.data[msgIndex].data;
for (
let msgIndex = 0;
msgIndex < txMsgData.msg_responses.length;
msgIndex++
) {
data[msgIndex] = txMsgData.msg_responses[msgIndex].value;

const nonce = nonces[msgIndex];
if (nonce && nonce.length === 32) {
Expand All @@ -1326,7 +1330,7 @@ export class SecretNetworkClient {

if (type_url === "/secret.compute.v1beta1.MsgInstantiateContract") {
const decoded = MsgInstantiateContractResponse.decode(
txMsgData.data[msgIndex].data,
txMsgData.msg_responses[msgIndex].value,
);
const decrypted = fromBase64(
fromUtf8(await this.encryptionUtils.decrypt(decoded.data, nonce)),
Expand All @@ -1339,7 +1343,7 @@ export class SecretNetworkClient {
type_url === "/secret.compute.v1beta1.MsgExecuteContract"
) {
const decoded = MsgExecuteContractResponse.decode(
txMsgData.data[msgIndex].data,
txMsgData.msg_responses[msgIndex].value,
);
const decrypted = fromBase64(
fromUtf8(await this.encryptionUtils.decrypt(decoded.data, nonce)),
Expand All @@ -1351,7 +1355,7 @@ export class SecretNetworkClient {
type_url === "/secret.compute.v1beta1.MsgMigrateContract"
) {
const decoded = MsgMigrateContractResponse.decode(
txMsgData.data[msgIndex].data,
txMsgData.msg_responses[msgIndex].value,
);
const decrypted = fromBase64(
fromUtf8(await this.encryptionUtils.decrypt(decoded.data, nonce)),
Expand Down Expand Up @@ -1455,7 +1459,7 @@ export class SecretNetworkClient {

let tx_response: TxResponsePb | undefined;

/*
/*
if (mode === BroadcastMode.Block) {
waitForCommit = true;
Expand Down Expand Up @@ -1587,7 +1591,7 @@ export class SecretNetworkClient {
return await this.decodeTxResponse(tx_response!, ibcTxOptions);
}
} else */if (mode === BroadcastMode.Sync) {
} else */ if (mode === BroadcastMode.Sync) {
const { BroadcastMode } = await import(
"./grpc_gateway/cosmos/tx/v1beta1/service.pb"
);
Expand Down Expand Up @@ -1629,10 +1633,12 @@ export class SecretNetworkClient {
);
}

/*
if (!waitForCommit) {
//@ts-ignore
return { transactionHash: txhash };
}
*/

// sleep first because there's no point in checking right after broadcasting
await sleep(checkIntervalMs / 2);
Expand Down
11 changes: 6 additions & 5 deletions src/tx/compute.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { toBase64 } from "@cosmjs/encoding";
import { toBase64, Bech32 } from "@cosmjs/encoding";
import { MsgParams } from ".";
import { EncryptionUtils } from "..";
import { addressToBytes, is_gzip } from "../utils";
import { AminoMsg, Coin, Msg, ProtoMsg } from "./types";

export interface MsgInstantiateContractParams extends MsgParams {
/** The actor that signed the messages */
sender: Uint8Array;
sender: string;
/** The id of the contract's WASM code */
code_id: number | string;
/** A unique label across all contracts */
Expand All @@ -30,7 +30,6 @@ export interface MsgInstantiateContractParams extends MsgParams {
code_hash?: string;
/** Admin is an optional address that can execute migrations */
admin?: string;
sender_address: string;
}

export function getMissingCodeHashWarning(method: string): string {
Expand All @@ -40,6 +39,7 @@ export function getMissingCodeHashWarning(method: string): string {
/** Instantiate a contract from code id */
export class MsgInstantiateContract implements Msg {
public sender: Uint8Array;
public sender_address: string;
public codeId: string;
public label: string;
public initMsg: object;
Expand All @@ -58,7 +58,8 @@ export class MsgInstantiateContract implements Msg {
code_hash,
admin,
}: MsgInstantiateContractParams) {
this.sender = sender;
this.sender = Bech32.decode(sender).data;
this.sender_address = String(sender);
this.codeId = String(code_id);
this.label = label;
this.initMsg = init_msg;
Expand Down Expand Up @@ -90,7 +91,7 @@ export class MsgInstantiateContract implements Msg {

const msgContent = {
sender: this.sender,
sender_address: new TextDecoder().decode(this.sender),
sender_address: this.sender_address,
code_id: this.codeId,
label: this.label,
init_msg: this.initMsgEncrypted,
Expand Down
21 changes: 10 additions & 11 deletions test/permit.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import fs from "fs";
import { Permit, TxResultCode } from "../src";
import { accounts, getValueFromRawLog } from "./utils";
import { accounts, getValueFromEvents } from "./utils";

beforeAll(() => {
jest.spyOn(console, "warn").mockImplementation(() => {});
});

describe("permit", () => {
test("sign", async () => {
const { secretjs } = accounts[0];
const { secretjsProto } = accounts[0];

const txStore = await secretjs.tx.compute.storeCode(
const txStore = await secretjsProto.tx.compute.storeCode(
{
sender: accounts[0].address,
wasm_byte_code: fs.readFileSync(
Expand All @@ -29,13 +29,13 @@ describe("permit", () => {
}
expect(txStore.code).toBe(TxResultCode.Success);

const code_id = getValueFromRawLog(txStore.rawLog, "message.code_id");
const code_id = getValueFromEvents(txStore.events, "message.code_id");

const { code_hash } = await secretjs.query.compute.codeHashByCodeId({
const { code_hash } = await secretjsProto.query.compute.codeHashByCodeId({
code_id,
});

const txInit = await secretjs.tx.compute.instantiateContract(
const txInit = await secretjsProto.tx.compute.instantiateContract(
{
sender: accounts[0].address,
code_id,
Expand Down Expand Up @@ -63,11 +63,11 @@ describe("permit", () => {
gasLimit: 5_000_000,
},
);
const contractAddress = getValueFromRawLog(
txInit.rawLog,
const contractAddress = getValueFromEvents(
txInit.events,
"message.contract_address",
);
const permit = await secretjs.utils.accessControl.permit.sign(
const permit = await secretjsProto.utils.accessControl.permit.sign(
accounts[0].address,
"secret-2",
"test",
Expand All @@ -76,7 +76,7 @@ describe("permit", () => {
false,
);

const query = await secretjs.query.snip20.getBalance({
const query = await secretjsProto.query.snip20.getBalance({
contract: { address: contractAddress, code_hash: code_hash! },
address: accounts[0].address,
auth: { permit },
Expand Down Expand Up @@ -219,7 +219,6 @@ describe("permit", () => {

test("validatePermit", async () => {
const { secretjs } = accounts[0];
const secretjs2 = accounts[1].secretjs;

let permit = await secretjs.utils.accessControl.permit.sign(
accounts[0].address,
Expand Down
22 changes: 20 additions & 2 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ export function getValueFromRawLog(
return "";
}

export function getValueFromEvents(
events: any[] | undefined,
key: string,
): string {
if (!events) {
return "";
}

for (const e of events) {
for (const a of e.attributes) {
if (`${e.type}.${a.key}` === key) {
return String(a.value);
}
}
}

return "";
}

export async function storeContract(
wasmPath: string,
account: Account,
Expand Down Expand Up @@ -199,8 +218,7 @@ export async function initContract(

const txInit = await secretjs.tx.compute.instantiateContract(
{
sender: new TextEncoder().encode(account.address),
sender_address: account.address,
sender: account.address,
code_id,
code_hash,
init_msg,
Expand Down

0 comments on commit 28632db

Please sign in to comment.