1 | #!/usr/bin/env node
|
2 |
|
3 | var fs = require('fs')
|
4 | var path = require('path')
|
5 | var underscore = require('underscore')
|
6 | var url = require('url')
|
7 | var uuid = require('uuid')
|
8 |
|
9 | const balance = require('bat-balance')
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var usage = function () {
|
18 | console.log('usage: node ' + path.basename(process.argv[1]) +
|
19 | ' [-1] [ -d ] [ -f file | -p personaID | -P] [ -l ] [ -[s|b] https://... ] [ -v ]')
|
20 | process.exit(1)
|
21 | }
|
22 |
|
23 | var options, server
|
24 | var argv = process.argv.slice(2)
|
25 | var configFile = process.env.CONFIGFILE
|
26 | var personaID = process.env.PERSONA
|
27 | var debugP = process.env.DEBUG || false
|
28 | var loggingP = process.env.LOGGING || false
|
29 | var verboseP = process.env.VERBOSE || false
|
30 | var version = process.env.LEDGER_VERSION || 'v2'
|
31 |
|
32 | while (argv.length > 0) {
|
33 | if (argv[0].indexOf('-') !== 0) break
|
34 |
|
35 | if (argv[0] === '-1') {
|
36 | version = 'v1'
|
37 | argv = argv.slice(1)
|
38 | continue
|
39 | }
|
40 | if (argv[0] === '-d') {
|
41 | debugP = true
|
42 | argv = argv.slice(1)
|
43 | continue
|
44 | }
|
45 | if (argv[0] === '-l') {
|
46 | loggingP = true
|
47 | argv = argv.slice(1)
|
48 | continue
|
49 | }
|
50 | if (argv[0] === '-P') {
|
51 | personaID = ''
|
52 | argv = argv.slice(1)
|
53 | continue
|
54 | }
|
55 | if (argv[0] === '-v') {
|
56 | verboseP = true
|
57 | argv = argv.slice(1)
|
58 | continue
|
59 | }
|
60 |
|
61 | if (argv.length === 1) usage()
|
62 |
|
63 | if (argv[0] === '-f') configFile = argv[1]
|
64 | else if (argv[0] === '-s') server = argv[1]
|
65 | else if (argv[0] === '-p') personaID = argv[1].toLowerCase()
|
66 | else if (argv[0] === '-b') {
|
67 | let entry = underscore.findWhere(balance.providers, { environment: 'production' })
|
68 | if (!entry) {
|
69 | console.log('bat-balance module is mis-configured, no entry for the "production" environment')
|
70 | process.exit(1)
|
71 | }
|
72 | entry.site = entry.server = argv[1]
|
73 | } else usage()
|
74 |
|
75 | argv = argv.slice(2)
|
76 | }
|
77 | if ((!configFile) && (typeof personaID === 'undefined')) usage()
|
78 | if (!configFile) configFile = 'config.json'
|
79 |
|
80 | if (!server) server = process.env.SERVER || 'https://ledger-staging.mercury.basicattentiontoken.org'
|
81 | if (server.indexOf('http') !== 0) server = 'https://' + server
|
82 | server = url.parse(server)
|
83 |
|
84 | options = { server: server, debugP: debugP, loggingP: loggingP, verboseP: verboseP, version: version }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | var client
|
93 |
|
94 | var callback = function (err, result, delayTime) {
|
95 | var entries = client.report()
|
96 |
|
97 | if (err) oops('client', err)
|
98 | if (verboseP) console.log('callback delayTime=' + delayTime + ' resultP=' + (!!result))
|
99 |
|
100 | if (!result) return run(delayTime)
|
101 |
|
102 | if (entries) entries.forEach((entry) => { console.log('*** ' + JSON.stringify(entry)) })
|
103 |
|
104 | if (result) {
|
105 | client.getWalletProperties(5, 'USD', (err, body) => {
|
106 | if (err) return console.log('wallet properties error=' + JSON.stringify(err, null, 2))
|
107 |
|
108 | console.log('!!! wallet properties=' + JSON.stringify(body, null, 2))
|
109 | if (body.balance > 10.0) {
|
110 | setTimeout(() => {
|
111 | const now = underscore.now()
|
112 |
|
113 | if (result.reconcileStamp < now) return console.log('already preparing for reconciliation')
|
114 |
|
115 | client.setTimeUntilReconcile(underscore.now(), (err, result) => {
|
116 | if (err) return console.log('setTimeUntilReconcile error=' + err.toString())
|
117 |
|
118 | console.log('preparing for reconciliation')
|
119 | }, 0)
|
120 | })
|
121 | }
|
122 | })
|
123 | }
|
124 |
|
125 | if (result.paymentInfo) {
|
126 | console.log(JSON.stringify(result.paymentInfo, null, 2))
|
127 | if (result.paymentInfo.addresses) {
|
128 | console.log('\nplease click here for payment: ether:' + result.paymentInfo.addresses.BAT + '?token=BAT&amount=' +
|
129 | result.paymentInfo.BAT + '\n')
|
130 | } else {
|
131 | console.log('\nplease click here for payment: bitcoin:' + result.paymentInfo.address + '?amount=' +
|
132 | result.paymentInfo.btc + '\n')
|
133 | }
|
134 | }
|
135 | delete result.publishersV2
|
136 | delete result.rulesetV2
|
137 | fs.writeFile(configFile, JSON.stringify(result, null, 2), { encoding: 'utf8', mode: parseInt('644', 8) }, function (err) {
|
138 | if (err) oops(configFile, err)
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | if ((result.transactions) && (result.transactions.length) &&
|
144 | (result.transactions[0].count === result.transactions[0].votes) &&
|
145 | (!result.ballots.length)) process.exit(0)
|
146 |
|
147 | run(delayTime)
|
148 | })
|
149 | }
|
150 |
|
151 | fs.readFile(typeof personaID !== 'undefined' ? '/dev/null' : configFile, { encoding: 'utf8' }, function (err, data) {
|
152 | var state = err ? null : data ? JSON.parse(data) : {}
|
153 |
|
154 | client = require('./index.js')(personaID, options, state)
|
155 | if (client.sync(callback) === true) run(10 * 1000)
|
156 |
|
157 | client.publisherTimestamp((err, result) => {
|
158 | if (err) console.log('timestamp: ' + err.toString)
|
159 | if (result) console.log('timestamp: ' + JSON.stringify(result, null, 2))
|
160 | })
|
161 |
|
162 | client.publisherInfo('google.com', (err, result) => {
|
163 | if (err) console.log('google.com: ' + err.toString)
|
164 | if (result) console.log('google.com: ' + JSON.stringify(result, null, 2))
|
165 | })
|
166 | })
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | var reconcileP = false
|
175 |
|
176 | var run = function (delayTime) {
|
177 | var viewingId = uuid.v4().toLowerCase()
|
178 |
|
179 | if (delayTime > 0) return setTimeout(() => { if (client.sync(callback)) return run(0) }, delayTime)
|
180 |
|
181 | if (!client.isReadyToReconcile()) return client.reconcile(viewingId, callback)
|
182 | if (reconcileP) {
|
183 | console.log('already reconciling.\n')
|
184 | return run(60 * 1000)
|
185 | }
|
186 |
|
187 | reconcileP = true
|
188 | client.reconcile(viewingId, callback)
|
189 | }
|
190 |
|
191 | var oops = function (s, err) {
|
192 | console.log(s + ': ' + err.toString())
|
193 | console.log(err.stack)
|
194 | process.exit(1)
|
195 | }
|