-
Notifications
You must be signed in to change notification settings - Fork 11
/
zpost
executable file
·86 lines (80 loc) · 2.5 KB
/
zpost
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
#!/usr/bin/env node
const {WalletClient, NodeClient} = require('hs-client');
const {Network} = require('hsd');
const fs = require('fs');
const constants = require('./lib/constants.js');
const ConsoleIO = new (require("consoleinout"))(console);
const network = Network.get('main');
const nodeOptions = {
network: network.type,
port: network.rpcPort,
apiKey: fs.readFileSync(__dirname+"/keys/node").toString().trim()
}
const walletOptions = {
network: network.type,
port: network.walletPort,
apiKey: fs.readFileSync(__dirname+"/keys/wallet").toString().trim()
}
const nodeClient = new NodeClient(nodeOptions);
const walletClient = new WalletClient(walletOptions);
async function getNameResource(name) {
let rr = await nodeClient.execute('getnameresource', [name])
if(rr)
return rr.records;
return null;
}
async function _writeZooko(name,message) {
if(message.length+constants.MAX_TXT>constants.MAGIC_ZOOKO)
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;
}
else {
returnRecord.push({type:"TXT",txt:[message]});
return returnRecord;
}
}
async function writeZooko(name, message) { return await _writeZooko(name, constants.MAGIC_ZOOKO+message) }
(async function() {
const argc = process.argv.length;
const argv = process.argv;
if(argc != 5) {
console.log("Usage: ",argv[1],"<wallet-id>","<name>","\"<message>\"");
process.exit(0);
}
const wallet = walletClient.wallet(argv[2]);
if(!wallet) {
console.log("Wallet ",argv[2],"not found");
process.exit(0);
}
console.output("Enter Password: ");
const password = await console.input(true);
const records = await writeZooko(argv[3],argv[4]);
const options = {passphrase:password,name:argv[3],broadcast:true,sign:true,data:{records: records}};
if(!options.data.records) {
console.log("Zooko failed (message too long?)");
process.exit(0);
}
let result;
try {
result = await wallet.createUpdate(options);
} catch(e) {
console.log("Zooko failed (incorrect password or need to wait until next block?)");
process.exit(0);
}
console.log(result);
process.exit(0);
})();