Skip to content

Commit

Permalink
wip: override filestore
Browse files Browse the repository at this point in the history
  • Loading branch information
tiero committed Aug 24, 2024
1 parent 8e298b9 commit b13b2d4
Showing 1 changed file with 61 additions and 9 deletions.
70 changes: 61 additions & 9 deletions pkg/client-sdk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,53 @@

const fs = require('fs').promises;
const path = require('path');
const dns = require('dns');

// Set Google's DNS server
dns.setServers(['8.8.8.8']);

// Load the Go WASM runtime
require('./wasm_exec');

// Custom ConfigStore implementation using local filesystem
class LocalConfigStore {
constructor(baseDir) {
this.baseDir = baseDir;
this.filePath = path.join(baseDir, 'config.json');
}

async getData() {
try {
const data = await fs.readFile(this.filePath, 'utf8');
return JSON.parse(data);
} catch (error) {
if (error.code === 'ENOENT') {
return null; // File doesn't exist yet
}
throw error;
}
}

async addData(data) {
await fs.mkdir(this.baseDir, { recursive: true });
await fs.writeFile(this.filePath, JSON.stringify(data, null, 2));
}

async cleanData() {
try {
await fs.unlink(this.filePath);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}
}

getType() {
return 'file';
}

getDatadir() {
return this.baseDir;
}
}

async function loadWasm() {
const go = new Go();
const wasmBuffer = await fs.readFile(path.join(__dirname, 'build/ark-sdk.wasm'));
Expand All @@ -22,24 +61,37 @@ async function main() {
await loadWasm();
console.log("ARK SDK WASM module loaded successfully");

// Create a local config store
const configStore = new LocalConfigStore(path.join(__dirname, 'config'));

// Override the WASM module's ConfigStore methods
global.addData = (data) => configStore.addData(data);
global.getData = () => configStore.getData();
global.cleanData = () => configStore.cleanData();
global.getType = () => configStore.getType();
global.getDatadir = () => configStore.getDatadir();

// Set the network
const network = await global.getNetwork();
console.log("Network:", network);

// Call the Init function
try {
const result = await global.init(
"singlekey", // Wallet type
"grpc", // Client type
"https://asp-signet.arklabs.to", // ASP URL (replace with actual URL)
"abandon abandon abandon", // Seed (replace with actual seed)
"sercet", // Password (replace with actual password)
"signet" // Chain (either "bitcoin" or "liquid")
"rest", // Client type
"http://127.0.0.1:7070", // ASP URL
"abandon abandon abandon", // Seed (replace with actual seed)
"secret", // Password (replace with actual password)
"regtest" // Chain
);
console.log("Init function called successfully", result);
} catch (error) {
console.error("Error calling Init function:", error);
}

// Keep the Node.js process alive
setInterval(() => {}, 1000);
} catch (error) {
console.error("Error loading WASM module:", error);
}
Expand Down

0 comments on commit b13b2d4

Please sign in to comment.