Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ wasm-sh:
# Compile dev builds of all WASM modules.
wasm:
time just wasm-img
time just wasm-simf
time just wasm-namada
# Compile dev builds of SimplicityHL module.
wasm-simf:
time docker run -v .:/app:rw --workdir=/app -it hackbg/fadroma:dev sh -c \
"cd lib/platform/simf && just wasm"
# Compile dev builds of Namada module.
wasm-namada:
time docker run -v .:/app:rw --workdir=/app -it hackbg/fadroma:dev sh -c \
"cd lib/platform/namada && just wasm"
# Report line counts.
Expand Down
22 changes: 2 additions & 20 deletions lib/platform/btc.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
#!/usr/bin/env -S deno run --allow-env --allow-read --allow-run --allow-write=/tmp/fadroma --allow-import=cdn.skypack.dev:443,deno.land:443 --allow-net=127.0.0.1
import { Fn, Test } from '../index.ts';
import { Test } from '../index.ts';
import { Btc } from './btc.ts';
const { the, is, has } = Test;
export const testBtcNode = the('Node', Btc,
(btc: Btc) => btc.kill());
export const testBtcNode = the('Node', Btc, (btc: Btc) => btc.kill());
export const testBtcOps = the('Ops',
the('Send', 'OP_CHECKSIG'),
the('Subscribe', 'TX', 'Block'),
the('Query', 'Block', 'Transaction', 'Address'));
export default Test.suite(import.meta, 'Btc',
testBtcNode, testBtcOps);

function cleanup (_, ctx: Test.Testing & { localnet?: { kill?: Fn } }) {
if (ctx.localnet?.kill) ctx.localnet.kill()
}

function calledWithMock (fn) {
return fn(mockContext());
}

function mockContext () {
const mock = [];
return {
mock,
exec (...args) { mock.push(args); return {} },
spawn (...args) { mock.push(args); return {} },
}
}
18 changes: 14 additions & 4 deletions lib/platform/btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,28 @@ export namespace Btc {
}

export interface Rest {
tx: Fn,
chaininfo: Fn,
getutxos: Fn,
}

/** Bitcoin node's optional REST API. */
export const Rest = function btcRest (url: string): Rest {
return {
async chaininfo () {
return JSON.parse(await callUrl(`${url}/rest/chaininfo.json`))
async chaininfo (format = "json") {
let data = await callUrl(`${url}/rest/chaininfo.${format}`);
if (format === 'json') data = JSON.parse(data);
return data;
},
async getutxos (...args: string[]) {
return JSON.parse(await callUrl(`${url}/rest/getutxos/${args.join('/')}.json`))
async tx (hash, format = "json") {
let data = await callUrl(`${url}/rest/tx/${hash}.${format}`);
if (format === 'json') data = JSON.parse(data);
return data;
},
async getutxos (format = 'json', ...args: string[]) {
let data = await callUrl(`${url}/rest/getutxos/${args.join('/')}.${format}`);
if (format === 'json') data = JSON.parse(data);
return data;
},
}
}
Expand Down
10 changes: 5 additions & 5 deletions lib/platform/namada/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions lib/platform/simf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,31 @@ export default Test.suite(import.meta, 'Simf',
));

function Withdraw (source, cb?) {
return Name('Withdraw', async ({ rpc, rest }, { log }) => {
return Name('Withdraw', async ({ rpc, rest, txId }, { log }) => {
const destination = await rpc.getnewaddress();
const tx = await rest.tx(txId);
const program = await Simf(source).compile();
log(program);
const spend = program.spend({
witness: '',
destination: '',
txId: '',
txBytes: '',
witness: '',
destination,
txId,
txBytes: tx.hex,
});
log(spend);
})
}

function Deploy (p2tr, cb) {
return Name(`Deploy ${p2tr}`, async (ctx: { rpc, rest }) => {
return Name(`Deploy ${p2tr}`, async (ctx: { rpc, rest }, { log }) => {
const address = await ctx.rpc.getnewaddress();
equal((await ctx.rpc.validateaddress(address)).isvalid, true);
const txid = await ctx.rpc.sendtoaddress(p2tr, 1000);
const txId = await ctx.rpc.sendtoaddress(p2tr, 1000);
log({ address, txId });
await ctx.rpc.generatetoaddress(1, address);
const { chainHeight, utxos } = await ctx.rest.getutxos(`${txid}-0/${txid}-1`);
const { chainHeight, utxos } = await ctx.rest.getutxos('json', `${txId}-0/${txId}-1`);
await cb({ chainHeight, utxos });
return ctx;
return Object.assign(ctx, { address, txId, chainHeight, utxos });
})
}

Expand Down
94 changes: 74 additions & 20 deletions lib/platform/simf/pkg/fadroma_simf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as __wbg_star0 from 'env';

let wasm;

function addToExternrefTable0(obj) {
Expand All @@ -6,6 +8,16 @@ function addToExternrefTable0(obj) {
return idx;
}

function _assertBoolean(n) {
if (typeof(n) !== 'boolean') {
throw new Error(`expected a boolean argument, found ${typeof(n)}`);
}
}

function _assertNum(n) {
if (typeof(n) !== 'number') throw new Error(`expected a number argument, found ${typeof(n)}`);
}

function debugString(val) {
// primitive types
const type = typeof val;
Expand Down Expand Up @@ -110,7 +122,24 @@ function isLikeNone(x) {
return x === undefined || x === null;
}

function logError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
let error = (function () {
try {
return e instanceof Error ? `${e.message}\n\nStack:\n${e.stack}` : e.toString();
} catch(_) {
return "<failed to stringify thrown value>";
}
}());
console.error("wasm-bindgen: imported JS function that was not marked as `catch` threw an error:", error);
throw e;
}
}

function passStringToWasm0(arg, malloc, realloc) {
if (typeof(arg) !== 'string') throw new Error(`expected a string argument, found ${typeof(arg)}`);
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
Expand Down Expand Up @@ -138,7 +167,7 @@ function passStringToWasm0(arg, malloc, realloc) {
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = cachedTextEncoder.encodeInto(arg, view);

if (ret.read !== arg.length) throw new Error('failed to pass whole string');
offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}
Expand Down Expand Up @@ -187,9 +216,12 @@ const ProgramFinalization = (typeof FinalizationRegistry === 'undefined')
: new FinalizationRegistry(ptr => wasm.__wbg_program_free(ptr >>> 0, 1));

/**
* Represents a valid and compiled SimplicityHL program.
* A valid compiled SimplicityHL program.
*/
export class Program {
constructor() {
throw new Error('cannot invoke `new` directly');
}
static __wrap(ptr) {
ptr = ptr >>> 0;
const obj = Object.create(Program.prototype);
Expand Down Expand Up @@ -219,6 +251,8 @@ export class Program {
* @returns {object}
*/
spend(options) {
if (this.__wbg_ptr == 0) throw new Error('Attempt to use a moved value');
_assertNum(this.__wbg_ptr);
const ret = wasm.program_spend(this.__wbg_ptr, options);
if (ret[2]) {
throw takeFromExternrefTable0(ret[1]);
Expand All @@ -229,6 +263,8 @@ export class Program {
* @returns {object}
*/
toJSON() {
if (this.__wbg_ptr == 0) throw new Error('Attempt to use a moved value');
_assertNum(this.__wbg_ptr);
const ret = wasm.program_toJSON(this.__wbg_ptr);
return ret;
}
Expand All @@ -241,6 +277,8 @@ export class Program {
let deferred1_0;
let deferred1_1;
try {
if (this.__wbg_ptr == 0) throw new Error('Attempt to use a moved value');
_assertNum(this.__wbg_ptr);
const ret = wasm.program_toString(this.__wbg_ptr);
deferred1_0 = ret[0];
deferred1_1 = ret[1];
Expand Down Expand Up @@ -315,10 +353,10 @@ async function __wbg_load(module, imports) {
function __wbg_get_imports() {
const imports = {};
imports.wbg = {};
imports.wbg.__wbg_Error_52673b7de5a0ca89 = function(arg0, arg1) {
imports.wbg.__wbg_Error_52673b7de5a0ca89 = function() { return logError(function (arg0, arg1) {
const ret = Error(getStringFromWasm0(arg0, arg1));
return ret;
};
}, arguments) };
imports.wbg.__wbg___wbindgen_debug_string_adfb662ae34724b6 = function(arg0, arg1) {
const ret = debugString(arg1);
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
Expand All @@ -328,15 +366,18 @@ function __wbg_get_imports() {
};
imports.wbg.__wbg___wbindgen_is_falsy_7b9692021c137978 = function(arg0) {
const ret = !arg0;
_assertBoolean(ret);
return ret;
};
imports.wbg.__wbg___wbindgen_is_object_ce774f3490692386 = function(arg0) {
const val = arg0;
const ret = typeof(val) === 'object' && val !== null;
_assertBoolean(ret);
return ret;
};
imports.wbg.__wbg___wbindgen_is_string_704ef9c8fc131030 = function(arg0) {
const ret = typeof(arg0) === 'string';
_assertBoolean(ret);
return ret;
};
imports.wbg.__wbg___wbindgen_string_get_a2a31e16edf96e42 = function(arg0, arg1) {
Expand All @@ -350,7 +391,7 @@ function __wbg_get_imports() {
imports.wbg.__wbg___wbindgen_throw_dd24417ed36fc46e = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function(arg0, arg1) {
imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function() { return logError(function (arg0, arg1) {
let deferred0_0;
let deferred0_1;
try {
Expand All @@ -360,56 +401,68 @@ function __wbg_get_imports() {
} finally {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
};
}, arguments) };
imports.wbg.__wbg_get_af9dab7e9603ea93 = function() { return handleError(function (arg0, arg1) {
const ret = Reflect.get(arg0, arg1);
return ret;
}, arguments) };
imports.wbg.__wbg_instanceof_Uint8Array_da54ccc9d3e09434 = function(arg0) {
imports.wbg.__wbg_instanceof_Uint8Array_da54ccc9d3e09434 = function() { return logError(function (arg0) {
let result;
try {
result = arg0 instanceof Uint8Array;
} catch (_) {
result = false;
}
const ret = result;
_assertBoolean(ret);
return ret;
};
imports.wbg.__wbg_length_22ac23eaec9d8053 = function(arg0) {
}, arguments) };
imports.wbg.__wbg_length_22ac23eaec9d8053 = function() { return logError(function (arg0) {
const ret = arg0.length;
_assertNum(ret);
return ret;
};
imports.wbg.__wbg_new_1ba21ce319a06297 = function() {
}, arguments) };
imports.wbg.__wbg_new_1ba21ce319a06297 = function() { return logError(function () {
const ret = new Object();
return ret;
};
imports.wbg.__wbg_new_8a6f238a6ece86ea = function() {
}, arguments) };
imports.wbg.__wbg_new_25f239778d6112b9 = function() { return logError(function () {
const ret = new Array();
return ret;
}, arguments) };
imports.wbg.__wbg_new_8a6f238a6ece86ea = function() { return logError(function () {
const ret = new Error();
return ret;
};
imports.wbg.__wbg_prototypesetcall_dfe9b766cdc1f1fd = function(arg0, arg1, arg2) {
}, arguments) };
imports.wbg.__wbg_prototypesetcall_dfe9b766cdc1f1fd = function() { return logError(function (arg0, arg1, arg2) {
Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
};
}, arguments) };
imports.wbg.__wbg_set_781438a03c0c3c81 = function() { return handleError(function (arg0, arg1, arg2) {
const ret = Reflect.set(arg0, arg1, arg2);
_assertBoolean(ret);
return ret;
}, arguments) };
imports.wbg.__wbg_stack_0ed75d68575b0f3c = function(arg0, arg1) {
imports.wbg.__wbg_stack_0ed75d68575b0f3c = function() { return logError(function (arg0, arg1) {
const ret = arg1.stack;
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len1 = WASM_VECTOR_LEN;
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
};
}, arguments) };
imports.wbg.__wbg_stringify_655a6390e1f5eb6b = function() { return handleError(function (arg0) {
const ret = JSON.stringify(arg0);
return ret;
}, arguments) };
imports.wbg.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) {
imports.wbg.__wbindgen_cast_2241b6af4c4b2941 = function() { return logError(function (arg0, arg1) {
// Cast intrinsic for `Ref(String) -> Externref`.
const ret = getStringFromWasm0(arg0, arg1);
return ret;
};
}, arguments) };
imports.wbg.__wbindgen_cast_d6cd19b81560fd6e = function() { return logError(function (arg0) {
// Cast intrinsic for `F64 -> Externref`.
const ret = arg0;
return ret;
}, arguments) };
imports.wbg.__wbindgen_init_externref_table = function() {
const table = wasm.__wbindgen_externrefs;
const offset = table.grow(4);
Expand All @@ -419,6 +472,7 @@ function __wbg_get_imports() {
table.set(offset + 2, true);
table.set(offset + 3, false);
};
imports['env'] = __wbg_star0;

return imports;
}
Expand Down
Binary file added lib/platform/simf/pkg/fadroma_simf_bg.wasm.old
Binary file not shown.
Loading