-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.js
70 lines (61 loc) · 1.72 KB
/
context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const { NodeClient, WalletClient } = require('hs-client');
const Network = require('hsd/lib/protocol/network.js');
const promptly = require('promptly');
class Context {
constructor(
networkName,
walletId,
apiKey,
passphraseGetter = noopPassphraseGetter,
host = '127.0.0.1',
nodeApiKey,
) {
if (!(nodeApiKey || apiKey)) {
console.error('no api key')
}
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;
}
return await this.wallet.unlock(pass, 60);
};
}
exports.Context = Context;
exports.staticPassphraseGetter = function (passphrase) {
return () => new Promise((resolve) => resolve(passphrase));
};
function noopPassphraseGetter() {
return new Promise((resolve) => resolve(null));
}
exports.promptPassphraseGetter = function (
prefix = '>> Please enter your passphrase: '
) {
return async () => await promptly.password(prefix);
};