-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathots-cli.js
393 lines (359 loc) · 12.2 KB
/
ots-cli.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env node
// Dependencies
const fs = require('fs')
const program = require('commander')
const moment = require('moment-timezone')
const OpenTimestamps = require('./src/open-timestamps.js')
const Utils = require('./src/utils.js')
const DetachedTimestampFile = require('./src/detached-timestamp-file.js')
const Ops = require('./src/ops.js')
const Context = require('./src/context.js')
const Calendar = require('./src/calendar.js')
// Constants
const path = process.argv[1].split('/')
const title = path[path.length - 1]
let isExecuted = false
// Parse parameters
function collect (val, memo) {
memo.push(val)
return memo
}
program
.version(require('./package.json').version)
.option('-v, --verbose', 'Be more verbose.')
.option('-l, --whitelist <url>', 'Add a calendar to the whitelist.', collect, [])
.option('--no-default-whitelist', 'Do not load the default remote calendar whitelist; ' +
'contact only calendars that have been manually added with --whitelist')
const infoCommand = program
.command('info [FILE_OTS]')
.alias('i')
.description('Show information on a timestamp.')
.action((file, options) => {
isExecuted = true
if (!file) {
console.log(infoCommand.helpInformation())
return
}
options = parseCommon(options)
info(file, options)
})
const stampCommand = program
.command('stamp [FILE...]')
.alias('s')
.option('-c, --calendar [url]', 'Create timestamp with the aid of a remote calendar. May be specified multiple times', collect, [])
.option('-m <int>', 'Commitments are sent to remote calendars in the event of timeout the timestamp is considered done if at least M calendars replied')
.option('-d, --digest <digest>', 'Verify a (hex-encoded) digest rather than a file')
.option('-a, --algorithm <type>', 'Hash algorithm: sha1, sha256 (default), ripemd160')
.description('Create timestamp with the aid of a remote calendar, the output receipt will be saved with .ots')
.action((files, options) => {
isExecuted = true
if ((files === undefined || files.length < 1) && !options.digest) {
console.log(stampCommand.helpInformation())
return
}
if (options.calendar) {
options.calendars = options.calendar
}
if (options.algorithm === undefined) {
options.algorithm = 'sha256'
} else if (['sha1', 'sha256', 'ripemd160'].indexOf(options.algorithm.toLowerCase()) > -1) {
options.algorithm = options.algorithm.toLowerCase()
} else {
console.log('Create timestamp with the aid of a remote calendar.')
console.log(title + ' stamp: ' + options.algorithm + ' unsupported ')
return
}
options = parseCommon(options)
stamp(files, options)
})
const verifyCommand = program
.command('verify [FILE_OTS]')
.alias('v')
.option('-f, --file <file>', 'Specify target file explicitly (default: original file present in the same directory without .ots)')
.option('-d, --digest <digest>', 'Verify a (hex-encoded) digest rather than a file')
.option('-a, --algorithm <type>', 'Hash algorithm: sha1, sha256 (default), ripemd160')
.option('-i, --ignore-bitcoin-node', 'Ignore verification with bitcoin node, only with explorer')
.option('-t, --timeout <int>', 'Adjust the request timeout (default: 1000), only with explorer')
.description('Verify a timestamp')
.action((file, options) => {
isExecuted = true
if (!file) {
console.log(verifyCommand.helpInformation())
return
}
if (options.algorithm === undefined) {
options.algorithm = 'sha256'
} else if (['sha1', 'sha256', 'ripemd160'].indexOf(options.algorithm.toLowerCase()) > -1) {
options.algorithm = options.algorithm.toLowerCase()
} else {
console.log('Create timestamp with the aid of a remote calendar.')
console.log(title + ' verify: ' + options.algorithm + ' unsupported ')
return
}
if (!options.ignoreBitcoinNode) {
options.ignoreBitcoinNode = false
}
if (parseInt(options.timeout) < 0) {
console.log('Timeout value should be greater than zero.')
console.log(title + ' verify: ' + options.timeout + ' negative value ')
return
}
options = parseCommon(options)
verify(file, options)
})
const upgradeCommand = program
.command('upgrade [FILE_OTS]')
.alias('u')
.option('-c, --calendar [url]', 'Override calendars in timestamp', collect, [])
.description('Upgrade remote calendar timestamps to be locally verifiable')
.action((file, options) => {
isExecuted = true
if (!file) {
console.log(upgradeCommand.helpInformation())
return
}
options.calendars = options.calendar
options = parseCommon(options)
upgrade(file, options)
})
program.parse(process.argv)
if (!isExecuted) {
console.log(program.helpInformation())
}
// FUNCTIONS
function parseCommon (options) {
let whitelist = new Calendar.UrlWhitelist()
if (options.parent.defaultWhitelist) {
whitelist = Calendar.DEFAULT_CALENDAR_WHITELIST
}
options.parent.whitelist.forEach(url => {
whitelist.add(url)
})
options.whitelist = whitelist
if (options.parent.verbose) {
options.verbose = true
}
return options
}
function info (argsFileOts, options) {
const otsPromise = Utils.readFilePromise(argsFileOts, null)
Promise.all([otsPromise]).then(values => {
const ots = values[0]
try {
const detachedOts = DetachedTimestampFile.deserialize(ots)
const infoResult = OpenTimestamps.info(detachedOts, options)
console.log(infoResult)
} catch (err) {
if (err instanceof Context.BadMagicError) {
throw new Error('Error! ' + argsFileOts + ' is not a timestamp file.')
} else if (err instanceof Context.DeserializationError) {
throw new Error('Invalid timestamp file ' + argsFileOts)
} else {
throw err
}
}
}).catch(err => {
if (err.code === 'ENOENT') {
console.error('File not found \'' + err.path + '\'')
} else {
console.error(err.message)
}
process.exit(1)
})
}
function stamp (argsFiles, options) {
// check input params : file/hash
const filePromises = []
if (options.digest) {
// digest: convert to bytes
filePromises.push(Utils.hexToBytes(options.digest))
} else {
// file: read file in bytes format
argsFiles.forEach(argsFile => {
filePromises.push(Utils.readFilePromise(argsFile, null))
})
}
// check input params : algorithm
let op = new Ops.OpSHA256()
if (options.algorithm === 'sha1') {
op = new Ops.OpSHA1()
} else if (options.algorithm === 'sha256') {
op = new Ops.OpSHA256()
} else if (options.algorithm === 'ripemd160') {
op = new Ops.OpRIPEMD160()
}
// main promise
Promise.all(filePromises).then(values => {
const detaches = []
values.forEach(value => {
if (options.digest) {
try {
detaches.push(DetachedTimestampFile.fromHash(op, value))
} catch (err) {
throw new Error('Invalid hash ' + options.digest + ' for ' + options.algorithm)
}
} else {
detaches.push(DetachedTimestampFile.fromBytes(op, value))
}
})
OpenTimestamps.stamp(detaches, options).then(() => {
if (detaches === undefined) {
console.error('Invalid timestamp')
return
}
detaches.forEach((ots, i) => {
if (ots === undefined) {
console.error('Invalid timestamp')
return
}
let otsFilename
if (options.digest) {
otsFilename = options.digest + '.ots'
} else {
otsFilename = argsFiles[i] + '.ots'
}
const buffer = Buffer.from(ots.serializeToBytes())
saveOts(otsFilename, buffer)
})
}).catch(err => {
console.error(err.message)
process.exit(1)
})
}).catch(err => {
if (err.code === 'ENOENT') {
console.error('File not found \'' + err.path + '\'')
} else {
console.error(err.message)
}
process.exit(1)
})
}
function saveOts (otsFilename, buffer) {
fs.stat(otsFilename, (err, stats) => {
if (!err) {
console.log('The timestamp proof \'' + otsFilename + '\' already exists')
} else {
fs.writeFile(otsFilename, buffer, 'binary', err => {
if (err) {
return console.log(err)
}
console.log('The timestamp proof \'' + otsFilename + '\' has been created!')
})
}
})
}
function verify (argsFileOts, options) {
const files = []
files.push(Utils.readFilePromise(argsFileOts, null))
if (options.digest) {
// input is a digest
console.log('Assuming target hash is \'' + options.digest + '\'')
} else if (options.file) {
// defined input file
console.log('Assuming target filename is \'' + options.file + '\'')
files.push(Utils.readFilePromise(options.file, null))
} else {
// default input file
const argsFile = argsFileOts.replace('.ots', '')
console.log('Assuming target filename is \'' + argsFile + '\'')
files.push(Utils.readFilePromise(argsFile, null))
}
Promise.all(files).then(values => {
const fileOts = values[0]
// Read ots file and check hash function
let detachedOts
try {
detachedOts = DetachedTimestampFile.deserialize(fileOts)
} catch (err) {
if (err instanceof Context.BadMagicError) {
throw new Error('Error! ' + argsFileOts + ' is not a timestamp file.')
} else if (err instanceof Context.DeserializationError) {
throw new Error('Invalid timestamp file ' + argsFileOts)
} else {
throw err
}
}
// Read original file with same hash function of ots
let detached
if (options.digest) {
try {
detached = DetachedTimestampFile.fromHash(detachedOts.fileHashOp, Utils.hexToBytes(options.digest))
} catch (err) {
throw new Error('Invalid hash ' + options.digest + ' for ' + detachedOts.fileHashOp._HASHLIB_NAME())
}
} else {
const file = values[1]
detached = DetachedTimestampFile.fromBytes(detachedOts.fileHashOp, file)
}
// Opentimestamps verify
const verifyPromise = OpenTimestamps.verify(detachedOts, detached, options)
verifyPromise.then(results => {
if (results) {
Object.keys(results).forEach(chain => {
const date = moment(results[chain].timestamp * 1000).tz(moment.tz.guess()).format('YYYY-MM-DD z')
console.log('Success! ' + chain[0].toUpperCase() + chain.slice(1) + ' block ' + results[chain].height + ' attests existence as of ' + date)
})
}
}).catch(err => {
console.log(err.message)
process.exit(1)
})
}).catch(err => {
if (err.code === 'ENOENT') {
console.error('File not found \'' + err.path + '\'')
} else {
console.error(err.message)
}
process.exit(1)
})
}
function upgrade (argsFileOts, options) {
const otsPromise = Utils.readFilePromise(argsFileOts, null)
otsPromise.then(ots => {
let detachedOts
try {
detachedOts = DetachedTimestampFile.deserialize(ots)
} catch (err) {
if (err instanceof Context.BadMagicError) {
throw new Error('Error! ' + argsFileOts + ' is not a timestamp file.')
} else if (err instanceof Context.DeserializationError) {
throw new Error('Invalid timestamp file ' + argsFileOts)
} else {
throw err
}
}
const upgradePromise = OpenTimestamps.upgrade(detachedOts, options)
upgradePromise.then(changed => {
// check timestamp
if (changed) {
// console.log('Timestamp has been successfully upgraded!');
fs.writeFile(argsFileOts + '.bak', Buffer.from(ots), 'binary', err => {
if (err) {
return console.log(err)
}
console.log('The file .bak was saved!')
})
fs.writeFile(argsFileOts, Buffer.from(detachedOts.serializeToBytes()), 'binary', err => {
if (err) {
return console.log(err)
}
})
}
if (detachedOts.timestamp.isTimestampComplete()) {
console.log('Success! Timestamp complete')
} else {
console.log('Failed! Timestamp not complete')
}
}).catch(err => {
console.log(err.message)
process.exit(1)
})
}).catch(err => {
if (err.code === 'ENOENT') {
console.error('File not found \'' + err.path + '\'')
} else {
console.error(err.message)
}
process.exit(1)
})
}