Fix malformed REST response/parsing

This commit is contained in:
Ethan Frey 2020-02-11 10:03:20 +01:00
parent 5677976463
commit 13c708e787
5 changed files with 40 additions and 14 deletions

View File

@ -468,7 +468,7 @@ describe("RestClient", () => {
.catch(error => expect(error).toMatch(`No contract with address ${beneficiaryAddress}`));
});
fdescribe("contract state", () => {
describe("contract state", () => {
const client = new RestClient(httpUrl);
const noContract = makeRandomAddress();
const expectedKey = toAscii("config");
@ -496,9 +496,8 @@ describe("RestClient", () => {
const state = await client.getAllContractState(contractAddress!);
expect(state.length).toEqual(1);
const data = state[0];
expect(data.key.toLowerCase()).toEqual(toHex(expectedKey));
const value = JSON.parse(fromAscii(fromBase64(data.val)));
console.log(value);
expect(data.key).toEqual(expectedKey);
const value = JSON.parse(fromAscii(data.val));
expect(value.verifier).toBeDefined();
expect(value.beneficiary).toBeDefined();

View File

@ -1,7 +1,17 @@
import { Encoding } from "@iov/encoding";
import axios, { AxiosError, AxiosInstance } from "axios";
import { AminoTx, CodeInfo, ContractInfo, CosmosSdkAccount, isAminoStdTx, StdTx, WasmData } from "./types";
import {
AminoTx,
CodeInfo,
ContractInfo,
CosmosSdkAccount,
isAminoStdTx,
Model,
parseWasmData,
StdTx,
WasmData,
} from "./types";
const { fromBase64, fromUtf8, toHex, toUtf8 } = Encoding;
@ -311,15 +321,11 @@ export class RestClient {
// Returns all contract state.
// This is an empty array if no such contract, or contract has no data.
public async getAllContractState(address: string): Promise<readonly WasmData[]> {
public async getAllContractState(address: string): Promise<readonly Model[]> {
const path = `/wasm/contract/${address}/state`;
const responseData = (await this.get(path)) as WasmResponse<WasmData[]>;
console.log("all state");
console.log(responseData);
console.log("***");
const r = unwrapWasmResponse(responseData);
console.log(r);
return r || [];
return r ? r.map(parseWasmData) : [];
}
// Returns the data at the key if present (unknown decoded json),
@ -340,7 +346,6 @@ export class RestClient {
const responseData = (await this.get(path)) as WasmResponse<SmartQueryResponse>;
const result = unwrapWasmResponse(responseData);
// no extra parse here for now, see https://github.com/confio/cosmwasm/issues/144
console.log(result);
return fromBase64(result.smart);
}
}

View File

@ -1,3 +1,7 @@
import { Encoding } from "@iov/encoding";
const { fromBase64, fromHex } = Encoding;
// We will move all needed *interfaces* from amino-js here
// This means bcp can just import them from here (if needed at all)
export interface Tx {
@ -193,3 +197,16 @@ export interface WasmData {
// value is base64 encoded
readonly val: string;
}
// Model is a parsed WasmData object
export interface Model {
readonly key: Uint8Array;
readonly val: Uint8Array;
}
export function parseWasmData({ key, val }: WasmData): Model {
return {
key: fromHex(key),
val: fromBase64(val),
};
}

View File

@ -1,4 +1,4 @@
import { AminoTx, CodeInfo, ContractInfo, CosmosSdkAccount, StdTx, WasmData } from "./types";
import { AminoTx, CodeInfo, ContractInfo, CosmosSdkAccount, Model, StdTx } from "./types";
interface NodeInfo {
readonly network: string;
}
@ -102,7 +102,7 @@ export declare class RestClient {
listContractAddresses(): Promise<readonly string[]>;
listContractsByCodeId(id: number): Promise<readonly ContractInfo[]>;
getContractInfo(address: string): Promise<ContractInfo>;
getAllContractState(address: string): Promise<readonly WasmData[]>;
getAllContractState(address: string): Promise<readonly Model[]>;
queryContractRaw(address: string, key: Uint8Array): Promise<Uint8Array | null>;
queryContractSmart(address: string, query: object): Promise<Uint8Array>;
}

View File

@ -143,4 +143,9 @@ export interface WasmData {
readonly key: string;
readonly val: string;
}
export interface Model {
readonly key: Uint8Array;
readonly val: Uint8Array;
}
export declare function parseWasmData({ key, val }: WasmData): Model;
export {};