Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

AA verify #332

Merged
merged 13 commits into from
Aug 16, 2021
Merged
2 changes: 0 additions & 2 deletions crates/abi/decoder/src/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{Cursor, Decoder};
/// Its main usage is by the `svm-sdk` crate for decoding the binary `calldata` into a Rust native values.
pub struct CallData {
cursor: Cursor,

decoder: Decoder,
}

Expand All @@ -17,7 +16,6 @@ impl CallData {
pub fn new(bytes: &[u8]) -> Self {
Self {
cursor: Cursor::new(bytes),

decoder: Decoder::new(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/codec/build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
set -e
cargo +nightly build --release --target wasm32-unknown-unknown
cargo +nightly build --release --target wasm32-unknown-unknown
73 changes: 42 additions & 31 deletions crates/codec/examples/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ function generateAddress(s) {
return repeatString(s, 20);
}

function encodeCallData(instance, object) {
function encodeInput(instance, object) {
const buf = wasmNewBuffer(instance, object);
const result = instanceCall(instance, "wasm_encode_calldata", buf);
const result = instanceCall(instance, "wasm_encode_inputdata", buf);

const encoded = loadWasmBufferDataAsJson(instance, result);

Expand All @@ -136,9 +136,9 @@ function encodeCallData(instance, object) {
return encoded;
}

function decodeCallData(instance, encodedData) {
function decodeInput(instance, encodedData) {
const buf = wasmNewBuffer(instance, encodedData);
const result = instanceCall(instance, "wasm_decode_calldata", buf);
const result = instanceCall(instance, "wasm_decode_inputdata", buf);
const json = loadWasmBufferDataAsJson(instance, result);

wasmBufferFree(instance, buf);
Expand All @@ -163,58 +163,58 @@ function binToString(array) {
return result;
}

describe("Encode Calldata", function () {
function testCallData(instance, abi, data) {
describe("Encode InputData", function () {
function testInputData(instance, abi, data) {
const calldata = {
abi: abi,
data: data,
};

let encoded = encodeCallData(instance, calldata);
let decoded = decodeCallData(instance, encoded);
let encoded = encodeInput(instance, calldata);
let decoded = decodeInput(instance, encoded);

assert.deepStrictEqual(decoded, calldata);
}

it("i8", function () {
return compileWasmCodec().then((instance) => {
testCallData(instance, ["i8"], [-10]);
testInputData(instance, ["i8"], [-10]);
});
});

it("u8", function () {
return compileWasmCodec().then((instance) => {
testCallData(instance, ["u8"], [10]);
testInputData(instance, ["u8"], [10]);
});
});

it("i16", function () {
return compileWasmCodec().then((instance) => {
testCallData(instance, ["i16"], [-10]);
testInputData(instance, ["i16"], [-10]);
});
});

it("u16", function () {
return compileWasmCodec().then((instance) => {
testCallData(instance, ["u16"], [10]);
testInputData(instance, ["u16"], [10]);
});
});

it("i32", function () {
return compileWasmCodec().then((instance) => {
testCallData(instance, ["i32"], [-10]);
testInputData(instance, ["i32"], [-10]);
});
});

it("u32", function () {
return compileWasmCodec().then((instance) => {
testCallData(instance, ["u32"], [10]);
testInputData(instance, ["u32"], [10]);
});
});

it("amount", function () {
return compileWasmCodec().then((instance) => {
testCallData(instance, ["amount"], [10]);
testInputData(instance, ["amount"], [10]);
});
});

Expand All @@ -226,8 +226,8 @@ describe("Encode Calldata", function () {
data: [addr],
};

let encoded = encodeCallData(instance, object);
let decoded = decodeCallData(instance, encoded);
let encoded = encodeInput(instance, object);
let decoded = decodeInput(instance, encoded);

assert.deepStrictEqual(decoded, {
abi: ["address"],
Expand All @@ -246,8 +246,8 @@ describe("Encode Calldata", function () {
data: [[addr1, addr2]],
};

let encoded = encodeCallData(instance, object);
let decoded = decodeCallData(instance, encoded);
let encoded = encodeInput(instance, object);
let decoded = decodeInput(instance, encoded);

assert.deepStrictEqual(decoded, {
abi: [["address"]],
Expand Down Expand Up @@ -312,10 +312,7 @@ describe("Deploy Template", function () {
const result = instanceCall(instance, "wasm_encode_deploy", buf);

const error = loadWasmBufferError(instance, result);
assert.strictEqual(
error,
'A non-optional field was missing (`name`).'
);
assert.strictEqual(error, "A non-optional field was missing (`name`).");

wasmBufferFree(instance, buf);
wasmBufferFree(instance, result);
Expand Down Expand Up @@ -369,7 +366,7 @@ describe("Spawn Account", function () {
data: [10, 20],
};

let calldata = encodeCallData(instance, object);
let calldata = encodeInput(instance, object);
const bytes = encodeSpawn(instance, template, name, calldata["data"]);
const json = decodeSpawn(instance, bytes);

Expand Down Expand Up @@ -398,7 +395,7 @@ describe("Spawn Account", function () {
const error = loadWasmBufferError(instance, result);
assert.strictEqual(
error,
'The value of a specific field is invalid (`template`).'
"The value of a specific field is invalid (`template`)."
);

wasmBufferFree(instance, buf);
Expand All @@ -408,11 +405,12 @@ describe("Spawn Account", function () {
});

describe("Call Account", function () {
function encodeCall(instance, target, calldata) {
function encodeCall(instance, target, verifydata, calldata) {
let tx = {
version: 0,
target: target,
func_name: "do_something",
verifydata: verifydata,
calldata: calldata,
};

Expand Down Expand Up @@ -446,19 +444,32 @@ describe("Call Account", function () {
return compileWasmCodec().then((instance) => {
const target = generateAddress("1020304050");

const object = {
let verifydata = encodeInput(instance, {
abi: ["bool", "i8"],
data: [true, 5],
});

let calldata = encodeInput(instance, {
abi: ["i32", "i64"],
data: [10, 20],
};
});

let calldata = encodeCallData(instance, object);
const bytes = encodeCall(instance, target, calldata["data"]);
const bytes = encodeCall(
instance,
target,
verifydata["data"],
calldata["data"]
);
const json = decodeCall(instance, bytes);

assert.deepStrictEqual(json, {
version: 0,
target: target,
func_name: "do_something",
verifydata: {
abi: ["bool", "i8"],
data: [true, 5],
},
calldata: {
abi: ["i32", "i64"],
data: [10, 20],
Expand All @@ -476,7 +487,7 @@ describe("Call Account", function () {
const error = loadWasmBufferError(instance, result);
assert.strictEqual(
error,
'The value of a specific field is invalid (`target`).'
"The value of a specific field is invalid (`target`)."
);

wasmBufferFree(instance, buf);
Expand Down
28 changes: 14 additions & 14 deletions crates/codec/src/api/builder/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct CallBuilder {
version: Option<u16>,
target: Option<Address>,
func_name: Option<String>,
// verifydata: Option<Vec<u8>>,
verifydata: Option<Vec<u8>>,
calldata: Option<Vec<u8>>,
}

Expand All @@ -26,14 +26,14 @@ pub struct CallBuilder {
/// let target = Address::of("@target").into();
///
/// let func_name = "do_work";
/// // let verifydata = vec![0x10, 0x20, 0x30];
/// let verifydata = vec![0x10, 0x20, 0x30];
/// let calldata = vec![0x10, 0x20, 0x30];
///
/// let bytes = CallBuilder::new()
/// .with_version(0)
/// .with_target(&target)
/// .with_func(func_name)
/// // .with_verifydata(&verifydata)
/// .with_verifydata(&verifydata)
/// .with_calldata(&calldata)
/// .build();
///
Expand All @@ -43,7 +43,7 @@ pub struct CallBuilder {
/// version: 0,
/// target,
/// func_name: func_name.to_string(),
/// // verifydata,
/// verifydata,
/// calldata,
/// };
///
Expand All @@ -58,7 +58,7 @@ impl CallBuilder {
version: None,
target: None,
func_name: None,
// verifydata: None,
verifydata: None,
calldata: None,
}
}
Expand All @@ -78,10 +78,10 @@ impl CallBuilder {
self
}

// pub fn with_verifydata(mut self, verifydata: &[u8]) -> Self {
// self.verifydata = Some(verifydata.to_vec());
// self
// }
pub fn with_verifydata(mut self, verifydata: &[u8]) -> Self {
self.verifydata = Some(verifydata.to_vec());
self
}

pub fn with_calldata(mut self, calldata: &[u8]) -> Self {
self.calldata = Some(calldata.to_vec());
Expand All @@ -93,10 +93,10 @@ impl CallBuilder {
let target = self.target.unwrap();
let func_name = self.func_name.unwrap();

// let verifydata = match self.verifydata {
// None => vec![],
// Some(verifydata) => verifydata.to_vec(),
// };
let verifydata = match self.verifydata {
None => vec![],
Some(verifydata) => verifydata.to_vec(),
};

let calldata = match self.calldata {
None => vec![],
Expand All @@ -107,7 +107,7 @@ impl CallBuilder {
version,
target,
func_name,
// verifydata,
verifydata,
calldata,
};

Expand Down
Loading