Open coolaj86 opened 2 years ago
I did eventually get this figured out. Here's a quick copy/paste of the working code (I think):
"use strict";
require("dotenv").config({ path: ".env" });
let Storage = require("dom-storage");
let localStorage = new Storage("./db.json", { strict: true, ws: " " });
let JsonStorage = require("json-storage").JsonStorage;
let store = JsonStorage.create(localStorage, "dash", { stringify: true });
store.getItem = store.get;
store.setItem = store.set;
let PASSPHRASE = process.env.PASSPHRASE || "YOUR PASSPHRASE GOES HERE";
// testnet height is 640357 as of Dec 28, 2021
// Find current height at https://testnet-insight.dashevo.org/insight/
// Or https://explorer.mydashwallet.org/insight/
let SKIP_FROM = 1618595;
let Dash = require("dash");
let dash = new Dash.Client({
//network: "testnet", // change when you want to really spend money
//storage: { adapter: {} },
//adapter: {},
wallet: {
adapter: store,
//storage: { adapter: {} },
mnemonic: PASSPHRASE,
//offlineMode: true,
},
unsafeOptions: {
skipSynchronizationBeforeHeight: SKIP_FROM,
},
});
console.info(`Downloading blocks since #${SKIP_FROM}...`);
console.info(`(this may take several minutes)`);
console.info();
async function main() {
// TODO How to set storage adapter?
const account = await dash.getWalletAccount();
let { address } = account.getUnusedAddress();
console.info(`Address: ${address}`);
account.events.on("FETCHED/UNCONFIRMED_TRANSACTION", function (data) {
console.info("FETCHED/UNCONFIRMED_TRANSACTION");
// Which address?
console.dir(data);
});
account.events.on("FETCHED/CONFIRMED_TRANSACTION", function (data) {
console.info("FETCHED/CONFIRMED_TRANSACTION");
console.dir(data);
account.disconnect();
disconnect();
});
let balance = account.getConfirmedBalance();
console.info(`Balance: ${balance}`);
// wait for user to click URL and accept
await new Promise(function (resolve) {
console.info("Go to one of these faucets, and fill up yo' address:");
[
"https://testnet-faucet.dash.org/",
"http://faucet.testnet.networks.dash.org/",
"http://faucet.test.dash.crowdnode.io/",
].forEach(function (url) {
console.info(` ${url}`);
});
console.info();
console.info("Did you fill up yo' bucket? (and get a confirmation?)");
console.info("Hit the <any> key to continue...");
console.info();
process.stdin.once("data", resolve);
});
//process.stdin.pause();
balance = account.getConfirmedBalance();
console.info(`Balance: ${balance}`);
}
function disconnect() {
console.info("Disconnect");
dash.disconnect();
setTimeout(function () {
process.exit(0);
}, 2000);
}
main().catch(function (e) {
console.error(e);
});
Ran into this same issue as @coolaj86 except in browser.
Here's my take-away. It seems like you could drop in window.localStorage
or window.sessionStorage
, and you nearly can, you just need to JSON.stringify
and JSON.parse
.
Taking the InMem adapter and modifying it to the code below appears to work:
class InBrowserStorage {
constructor(storage = window.localStorage) {
this.isConfig = false;
this.storage = storage
}
config() {
this.isConfig = true;
}
setItem(key, item) {
this.storage.setItem(key, JSON.stringify(item));
return item;
}
getItem(key) {
return JSON.parse(this.storage.getItem(key));
}
}
module.exports = InBrowserStorage;
let dash = new Dash.Client({
wallet: {
// adapter: window.localStorage, // wont work
adapter: new InBrowserStorage(
// window.sessionStorage // if you wanna override the default localStorage
),
mnemonic: PASSPHRASE,
},
unsafeOptions: {
skipSynchronizationBeforeHeight: SKIP_FROM,
},
});
@thephez do we have it documented?
When calling
new Dash.Client({ ... }).getWalletAccount()
I get this warning message:How do I find and initialize a storage adapter? I can't find a match for
adapter
at all on any of the documentation sites:I'm not familiar at all with bitcoin core or any of the cryptocurrency forks. I'm assuming that setting a storage adapter must be something everybody knows how to do... except me.
For reference, I'm expecting to be able to use the native
fs
module in node, or something similar to that. (I would imagine SQL would be far too slow for large chains.)