-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·94 lines (83 loc) · 2.15 KB
/
Copy pathcli.js
File metadata and controls
executable file
·94 lines (83 loc) · 2.15 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
const argv = require('yargs')
.usage('Usage: $0 [host-name [port]]')
.default('host', 'localhost')
.default('port', 18444)
.argv
var net = require('net');
var fs = require('fs')
var history_file = '.client.history'
var history = (fs.existsSync(history_file)) ? fs.readFileSync('.client.history').toString().split("\n") : []
var client = new net.Socket();
client.on('data', function(data) {
console.log('Received: ' + data);
});
client.on('close', function() {
console.log('Connection closed');
});
var methods = {
test : function() {
console.log(arguments)
return 'hello'
},
connect: function(host, port) {
console.log("Connecting to ", host, port)
client.connect(port, host, function() {
return "connected"
});
},
version : function (){
var buff = Buffer.from('fabfb5da76657273696f6e000000000064000000358d493262ea0000010000000000000011b2d05000000000010000000000000000000000000000000000ffff000000000000000000000000000000000000000000000000ffff0000000000003b2eb35d8ce617650f2f5361746f7368693a302e372e322fc03e0300','hex')
try {
client.write(buff)
} catch (e) {
console.log(e)
}
},
disconnect: function() {
client.destroy()
},
quit : function() {
console.log('Have a great day!');
process.exit(0);
},
unknown : function() {
return "Unknown command"
}
};
function writeLog(data) {
if (data != '') {
fs.appendFile(history_file, data + "\n", function(err) {
history.push(data)
if(err) {
return console.log(err);
}
});
}
}
function handleCmd() {
rl.prompt();
rl.on('line', (line) => {
var answer = line.trim()
writeLog(answer)
var parts = answer.split(' ')
var method = (parts[0] in methods) ? parts[0] : 'unknown'
console.log(methods[method](...parts.splice(1)))
rl.prompt();
}).on('close', () => {
methods.quit()
i });
}
var rl = null
if (argv._.length < 1 ) {
const readline = require('readline');
rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'bitcoin> '
});
handleCmd()
} else {
methods['connect'](...argv._)
methods['version']()
}