67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
const { NodeClient, WalletClient } = require('hsd/lib/client');
|
|
const Network = require('hsd/lib/protocol/network.js');
|
|
const readline = require("readline");
|
|
|
|
class Context {
|
|
constructor(
|
|
networkName,
|
|
walletId,
|
|
apiKey,
|
|
passphraseGetter = noopPassphraseGetter,
|
|
host = '127.0.0.1',
|
|
nodeApiKey,
|
|
) {
|
|
this.networkName = networkName;
|
|
this.network = Network.get(networkName);
|
|
this.walletId = walletId;
|
|
this.nodeClient = new NodeClient({
|
|
port: this.network.rpcPort,
|
|
host,
|
|
apiKey: nodeApiKey || apiKey,
|
|
});
|
|
this.walletClient = new WalletClient({
|
|
port: this.network.walletPort,
|
|
host,
|
|
apiKey: apiKey,
|
|
});
|
|
this.wallet = this.walletClient.wallet(walletId);
|
|
this.passphraseGetter = passphraseGetter;
|
|
}
|
|
|
|
getPassphrase = () => {
|
|
return this.passphraseGetter();
|
|
};
|
|
|
|
execNode = (method, ...args) => {
|
|
return this.nodeClient.execute(method, args);
|
|
};
|
|
|
|
execWallet = async (method, ...args) => {
|
|
await this.walletClient.execute('selectwallet', [this.walletId]);
|
|
return this.walletClient.execute(method, args);
|
|
};
|
|
|
|
unlockWallet = async () => {
|
|
const pass = await this.getPassphrase();
|
|
if (pass === null) {
|
|
return;
|
|
}
|
|
await this.wallet.unlock(pass, 60);
|
|
};
|
|
|
|
getMTP = async () => {
|
|
const info = await this.execNode('getblockchaininfo');
|
|
return info.mediantime;
|
|
};
|
|
|
|
getHeight = async () => {
|
|
const info = await this.execNode('getblockchaininfo');
|
|
return info.blocks;
|
|
};
|
|
}
|
|
|
|
exports.Context = Context;
|
|
|
|
exports.staticPassphraseGetter = function (passphrase) {
|
|
return () => new Promise((resolve) => resolve(passphrase));
|
|
}; |