-
Notifications
You must be signed in to change notification settings - Fork 5
/
zmsg-broadcast
executable file
·98 lines (91 loc) · 2.93 KB
/
zmsg-broadcast
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
95
96
97
#!/usr/bin/env node
const {encrypt, decrypt} = require("hsencrypt");
const {WalletClient, NodeClient} = require('hs-client');
const {Network} = require('hsd');
const fs = require('fs');
const network = Network.get('main');
const ConsoleIO = new (require("consoleinout"))(console);
const constants = require("./lib/constants.js");
const nodeOptions = {
network: network.type,
port: network.rpcPort,
apiKey: fs.readFileSync("keys/node").toString().trim()
}
const walletOptions = {
port: network.walletPort,
apiKey: fs.readFileSync("keys/wallet").toString().trim()
}
const nodeClient = new NodeClient(nodeOptions);
const _walletClient = new WalletClient(walletOptions);
let walletClient;
(async function() {
const argv=process.argv;
const argc=argv.length;
if(argc!=6 && argc!=7) {
console.log("Encrypt Usage: ",argv[1],"<wallet>","<from>","<to>","\"<msg>\"");
console.log("");
console.log("Decrypt Usage: ",argv[1],"<wallet>","<from>","<to>","\"<encrypted-msg>\"", "\"d\"");
process.exit();
}
walletClient = _walletClient.wallet(argv[2]);
if(!walletClient) {
console.log("Wallet ",argv[2],"not found");
process.exit(0);
}
console.output("Enter Password: ");
const password = await console.input(true);
let bName,bMessage;
if(argc==7) {
let decrypted=await decrypt(walletClient, nodeClient, password, argv[4], argv[3], argv[5]);
console.log("Decrypted: ",decrypted);
process.exit(0);
}
else if(argc==6) {
let encrypted=await encrypt(walletClient, nodeClient, password, argv[3], argv[4], argv[5]);
console.log(encrypted);
bName = argv[3];
bMessage = encrypted;
}
const records = await writeZmsg(bName,bMessage);
const options = {passphrase:password,name:bName,broadcast:true,sign:true,data:{records: records}};
if(!options.data.records) {
console.log("zmsg broadcast failed");
process.exit(0);
}
let result;
try {
result = await walletClient.createUpdate(options);
} catch(e) {
console.log("zmsg failed (incorrect password or need to wait until next block?)");
process.exit(0);
}
console.log(result);
process.exit(0);
})()
async function getNameResource(name) {
let rr = await nodeClient.execute('getnameresource', [name])
if(rr)
return rr.records;
return null;
}
async function _writeZmsg(name,message) {
if(message.length+constants.MAX_TXT>constants.MAGIC_ZMSG)
return null;
let records = await getNameResource(name);
let returnRecord = [];
if(records) {
for(let x=0;x<records.length;x++) {
let rec = records[x];
if(rec.type=="TXT") {
if(rec.txt) {
if(rec.txt[0].startsWith(constants.MAGIC_ZOOKO) || rec.txt[0].startsWith(constants.MAGIC_ZMSG))
continue;
}
}
returnRecord.push(rec);
}
returnRecord.push({type:"TXT",txt:[message]});
return returnRecord;
}
}
async function writeZmsg(name, message) { return await _writeZmsg(name, constants.MAGIC_ZMSG+message) }