Files
nym/wasm/credentials/internal-dev/worker.js
T
2024-04-09 16:46:49 +01:00

83 lines
2.9 KiB
JavaScript

// Copyright 2020-2023 Nym Technologies SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const RUST_WASM_URL = "nym_credential_client_wasm_bg.wasm"
importScripts('nym_credential_client_wasm.js');
console.log('Initializing worker');
// wasm_bindgen creates a global variable (with the exports attached) that is in scope after `importScripts`
const {
acquireCredential,
} = wasm_bindgen;
async function testGetCredential() {
// const preferredGateway = "6pXQcG1Jt9hxBzMgTbQL5Y58z6mu4KXVRbA1idmibwsw";
// const nymApi = 'https://qa-nym-api.qa.nymte.ch/api';
//
// // A) construct with hardcoded topology
// const topology = dummyTopology()
//
// // optional arguments: id, gateway
// // mandatory (one of) arguments: topology, nymApi
// const nodeTester = await new NymNodeTester({ id: "foomp", topology: topology });
//
// // B) first get topology directly from nym-api
// // const topology = await currentNetworkTopology(nymApi)
// // const nodeTester = await new NymNodeTester({topology});
//
// // C) use nym-api in the constructor (note: it does no filtering for 'good' nodes on other layers)
// // const validator = 'https://qa-nym-api.qa.nymte.ch/api';
// // const nodeTester = await new NymNodeTester({nymApi});
//
self.onmessage = async event => {
if (event.data && event.data.kind) {
switch (event.data.kind) {
case 'GetCredential': {
const { amount, mnemonic } = event.data.args;
// TODO: this should just use cosmjs' coin
let coin = `${amount}unym`
console.log(`getting credential for ${coin}`);
let credential = await acquireCredential(mnemonic, coin, { useSandbox: true })
self.postMessage({
kind : 'ReceivedCredential',
args: {
credential
}
})
}
}
}
};
}
async function main() {
console.log(">>>>>>>>>>>>>>>>>>>>> JS WORKER MAIN START");
// load rust WASM package
await wasm_bindgen(RUST_WASM_URL);
console.log('Loaded RUST WASM');
// run test on simplified and dedicated tester:
await testGetCredential();
//
console.log(">>>>>>>>>>>>>>>>>>>>> JS WORKER MAIN END")
}
// Let's get started!
main();