Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow invoking with Uint8Array #947

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions packages/js/client/src/__tests__/core/plugin-subinvoke.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Client, PluginModule } from "@polywrap/core-js";
import { PolywrapClient } from "../..";

describe("plugin-subinvoke", () => {
const mockPlugin = () => {
class MockPlugin extends PluginModule<Record<string, unknown>> {
public async call(
args: { input: Uint8Array },
client: Client
): Promise<ArrayBuffer> {
const res = await client.invoke({
uri: "ens/is-even.eth",
method: "isEven",
dOrgJelli marked this conversation as resolved.
Show resolved Hide resolved
args: args.input,
});
return res.data as ArrayBuffer;
}
}

return {
factory: () => new MockPlugin({}),
manifest: {
schema: ``,
implements: [],
},
};
};

const isEvenPlugin = () => {
class IsEvenPlugin extends PluginModule<Record<string, unknown>> {
public isEven(args: {num: number}, client: Client): boolean {
return (args.num & 1) ? false : true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (args.num & 1) ? false : true;
return !(args.num & 1);

}
}

return {
factory: () => new IsEvenPlugin({}),
manifest: {
schema: ``,
implements: [],
},
};
};

test("call", async () => {
const client = new PolywrapClient({
plugins: [
{
uri: "ens/demo.eth",
plugin: mockPlugin(),
},
{
uri: "ens/is-even.eth",
plugin: isEvenPlugin(),
}
],
});

// msgpackEncode({num: 10000}) -> [129, 163, 110, 117, 109, 205, 39, 16]
const input = Uint8Array.from([129, 163, 110, 117, 109, 205, 39, 16])

const result = await client.invoke({
uri: "ens/demo.eth",
method: "call",
args: {
input,
},
});

expect(result.error).toBeFalsy();
expect(result.data).toBeTruthy();
});
});
4 changes: 1 addition & 3 deletions packages/js/client/src/plugin/PluginWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ export class PluginWrapper extends Wrapper {
await this._sanitizeAndLoadEnv(client, module);

let jsArgs: Record<string, unknown>;

// If the args are a msgpack buffer, deserialize it
if (args instanceof ArrayBuffer) {
if (args instanceof ArrayBuffer || ArrayBuffer.isView(args)) {
const result = msgpackDecode(args);

Tracer.addEvent("msgpack-decoded", result);
Expand Down
5 changes: 4 additions & 1 deletion packages/js/client/src/wasm/WasmWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ export class WasmWrapper extends Wrapper {
invokeResult: {} as InvokeResult,
method,
sanitizeEnv: {},
args: args instanceof ArrayBuffer ? args : msgpackEncode(args),
args:
args instanceof ArrayBuffer || args instanceof Uint8Array
Niraj-Kamdar marked this conversation as resolved.
Show resolved Hide resolved
? args
: msgpackEncode(args),
};

const abort = (message: string) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/js/core/src/types/Invoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface InvokeOptions<
* Arguments for the method, structured as a map,
* removing the chance of incorrectly ordering arguments.
*/
args?: Record<string, unknown> | ArrayBuffer;
args?: Record<string, unknown> | ArrayBufferView | ArrayBuffer;

/**
* If set to true, the invoke function will not decode the msgpack results
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fileSystemPlugin } from "../index";
import { fileSystemPlugin } from "..";
import { PolywrapClient, PolywrapClientConfig } from "@polywrap/client-js";
import { FileSystem_Module, FileSystem_EncodingEnum } from "../wrap";
import fs from "fs";
Expand Down
2 changes: 1 addition & 1 deletion packages/js/plugins/file-system/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { PluginFactory } from "@polywrap/core-js";
type NoConfig = Record<string, never>;

export class FileSystemPlugin extends Module<NoConfig> {
async readFile(args: Args_readFile, _client: Client): Promise<ArrayBuffer> {
async readFile(args: Args_readFile, _client: Client): Promise<Uint8Array> {
return fs.promises.readFile(args.path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type Int = number;
export type Int8 = number;
export type Int16 = number;
export type Int32 = number;
export type Bytes = ArrayBuffer;
export type Bytes = Uint8Array;
export type BigInt = string;
export type BigNumber = string;
export type Json = string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Int = number;
export type Int8 = number;
export type Int16 = number;
export type Int32 = number;
export type Bytes = ArrayBuffer;
export type Bytes = Uint8Array;
export type BigInt = string;
export type BigNumber = string;
export type Json = string;
Expand Down
3 changes: 2 additions & 1 deletion packages/templates/wasm/assemblyscript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
"test:env:up": "npx polywrap infra up --modules=eth-ens-ipfs",
"test:env:down": "npx polywrap infra down --modules=eth-ens-ipfs",
"deploy": "npx polywrap deploy",
"test": "yarn test:e2e && yarn test:workflow",
"test:e2e": "yarn build && yarn test:e2e:codegen && jest --passWithNoTests --runInBand --verbose",
"test:e2e:codegen": "npx polywrap app codegen -m ./src/__tests__/types/polywrap.app.yaml -c ./src/__tests__/types/wrap",
"test:workflow": "yarn build && yarn test:env:up && sleep 5 && npx polywrap run ./workflows/e2e.yaml && yarn test:env:down"
"test:workflow": "yarn build && yarn test:env:up && sleep 5 && npx polywrap run ./workflows/e2e.yaml -c ./workflows/config.ts && yarn test:env:down"
},
"devDependencies": {
"@polywrap/ethereum-plugin-js": "0.0.1-prealpha.90",
Expand Down
13 changes: 0 additions & 13 deletions packages/templates/wasm/assemblyscript/workflows/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,6 @@ function getPlugins(
}),
},
],
interfaces: [
{
interface: coreInterfaceUris.uriResolver.uri,
implementations: [
"wrap://ens/ipfs-resolver.polywrap.eth",
"wrap://ens/ens-resolver.polywrap.eth",
],
},
{
interface: coreInterfaceUris.logger.uri,
implementations: ["wrap://ens/js-logger.polywrap.eth"],
},
],
};
}

Expand Down
4 changes: 0 additions & 4 deletions packages/templates/wasm/assemblyscript/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ jobs:
args:
address: "$cases.0.data"
value: 100
connection:
networkNameOrChainId: testnet
- uri: fs/build
method: getData
args:
address: "$cases.0.data"
connection:
networkNameOrChainId: testnet
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type Int = number;
export type Int8 = number;
export type Int16 = number;
export type Int32 = number;
export type Bytes = ArrayBuffer;
export type Bytes = Uint8Array;
export type BigInt = string;
export type BigNumber = string;
export type Json = string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Int = number;
export type Int8 = number;
export type Int16 = number;
export type Int32 = number;
export type Bytes = ArrayBuffer;
export type Bytes = Uint8Array;
export type BigInt = string;
export type BigNumber = string;
export type Json = string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Int = number;
export type Int8 = number;
export type Int16 = number;
export type Int32 = number;
export type Bytes = ArrayBuffer;
export type Bytes = Uint8Array;
export type BigInt = string;
export type BigNumber = string;
export type Json = string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Int = number;
export type Int8 = number;
export type Int16 = number;
export type Int32 = number;
export type Bytes = ArrayBuffer;
export type Bytes = Uint8Array;
export type BigInt = string;
export type BigNumber = string;
export type Json = string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Int = number;
export type Int8 = number;
export type Int16 = number;
export type Int32 = number;
export type Bytes = ArrayBuffer;
export type Bytes = Uint8Array;
export type BigInt = string;
export type BigNumber = string;
export type Json = string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Int = number;
export type Int8 = number;
export type Int16 = number;
export type Int32 = number;
export type Bytes = ArrayBuffer;
export type Bytes = Uint8Array;
export type BigInt = string;
export type BigNumber = string;
export type Json = string;
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5611,6 +5611,11 @@ caniuse-lite@^1.0.0, caniuse-lite@^1.0.30000981, caniuse-lite@^1.0.30001035, can
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001359.tgz#a1c1cbe1c2da9e689638813618b4219acbd4925e"
integrity sha512-Xln/BAsPzEuiVLgJ2/45IaqD9jShtk3Y33anKb4+yLwQzws3+v6odKfpgES/cDEaZMLzSChpIGdbOYtH9MyuHw==

caniuse-lite@^1.0.30001358:
version "1.0.30001358"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001358.tgz#473d35dabf5e448b463095cab7924e96ccfb8c00"
integrity sha512-hvp8PSRymk85R20bsDra7ZTCpSVGN/PAz9pSAjPSjKC+rNmnUk5vCRgJwiTT/O4feQ/yu/drvZYpKxxhbFuChw==

capture-exit@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/capture-exit/-/capture-exit-2.0.0.tgz#fb953bfaebeb781f62898239dabb426d08a509a4"
Expand Down Expand Up @@ -7291,6 +7296,11 @@ electron-to-chromium@^1.3.378, electron-to-chromium@^1.4.164:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.172.tgz#87335795a3dc19e7b6dd5af291038477d81dc6b1"
integrity sha512-yDoFfTJnqBAB6hSiPvzmsBJSrjOXJtHSJoqJdI/zSIh7DYupYnIOHt/bbPw/WE31BJjNTybDdNAs21gCMnTh0Q==

electron-to-chromium@^1.4.164:
version "1.4.167"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.167.tgz#72424aebc85df12c5331d37b1bcfd1ae01322c55"
integrity sha512-lPHuHXBwpkr4RcfaZBKm6TKOWG/1N9mVggUpP4fY3l1JIUU2x4fkM8928smYdZ5lF+6KCTAxo1aK9JmqT+X71Q==

elliptic@6.5.4, elliptic@^6.5.3:
version "6.5.4"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb"
Expand Down