forked from opentimestamps/javascript-opentimestamps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathots-cli.js
263 lines (236 loc) · 8.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
#!/usr/bin/env node
// Dependencies
const fs = require('fs');
const program = require('commander');
const OpenTimestamps = require('./src/open-timestamps.js');
const Context = require('./src/context.js');
const Utils = require('./src/utils.js');
const DetachedTimestampFile = require('./src/detached-timestamp-file.js');
// Constants
const path = process.argv[1].split('/');
const title = path[path.length - 1];
// Parse parameters
program
.version(require('./package.json').version);
program
.command('info [file_ots]')
.alias('i')
.description('Show information on a timestamp.')
.action(file => {
if (!file) {
console.log('Show information on a timestamp given as argument.');
console.log(title + ' info: bad options number ');
return;
}
info(file);
});
program
.command('stamp [file]')
.alias('s')
.description('Create timestamp with the aid of a remote calendar, the output receipt will be saved with .ots .')
.action(file => {
if (!file) {
console.log('Create timestamp with the aid of a remote calendar.');
console.log(title + ' stamp: bad options number ');
return;
}
stamp(file);
});
program
.command('multistamp [files...]')
.alias('M')
.option('-c, --calendar <url>', 'Create timestamp with the aid of a remote calendar. May be specified multiple times.')
.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('-k, --key <file>', 'Signature key file of private remote calendars.')
.description('Create timestamp with the aid of a remote calendar, the output receipt will be saved with .ots .')
.action((files, options) => {
if (!files && files.size() < 1) {
console.log('Create timestamp with the aid of a remote calendar.');
console.log(title + ' stamp: bad options number ');
return;
}
const parameters = {};
if (options.calendar) {
parameters.publicCalendars = options.calendar;
}
if (options.key) {
parameters.privateCalendars = Utils.readSignatureFile(options.key);
}
if (options.M) {
parameters.m = options.M;
}
console.log(options);
multistamp(files, parameters);
});
program
.command('verify [file_ots]')
.alias('v')
.description('Verify the timestamp attestations, expect original file present in the same directory without .ots .')
.action(file => {
if (!file) {
console.log('Verify the timestamp attestations given as argument.');
console.log(title + ' verify: bad options number ');
return;
}
verify(file);
});
program
.command('upgrade [file_ots]')
.alias('u')
.description('Upgrade remote calendar timestamps to be locally verifiable.')
.action(file => {
if (!file) {
console.log('Upgrade the timestamp attestations given as argument.');
console.log(title + ' upgrade: bad options number ');
return;
}
upgrade(file);
});
program.parse(process.argv);
if (program.args.length === 0) {
console.log(program.helpInformation());
}
// FUNCTIONS
function info(argsFileOts) {
const otsPromise = Utils.readFilePromise(argsFileOts, null);
Promise.all([otsPromise]).then(values => {
const ots = values[0];
const infoResult = OpenTimestamps.info(ots);
console.log(infoResult);
}).catch(err => {
console.log('Error: ' + err);
});
}
function stamp(argsFile) {
const filePromise = Utils.readFilePromise(argsFile, null);
Promise.all([filePromise]).then(values => {
const file = values[0];
const timestampBytesPromise = OpenTimestamps.stamp(file);
timestampBytesPromise.then(timestampBytes => {
const ctx = new Context.StreamDeserialization(timestampBytes);
const detachedTimestampFile = DetachedTimestampFile.DetachedTimestampFile.deserialize(ctx);
if (detachedTimestampFile === undefined) {
console.error('Invalid timestamp');
return;
}
// console.log('STAMP result : ');
// console.log(Utils.bytesToHex(detachedTimestampFile.timestamp.msg));
const buffer = new Buffer(timestampBytes);
const otsFilename = argsFile + '.ots';
fs.exists(otsFilename, fileExist => {
if (fileExist) {
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!');
});
}
});
}).catch(err => {
console.log('Error: ' + err);
});
}).catch(err => {
console.log('Error: ' + err);
});
}
function multistamp(argsFiles, options) {
const filePromises = [];
argsFiles.forEach(argsFile => {
filePromises.push(Utils.readFilePromise(argsFile, null));
});
Promise.all(filePromises).then(values => {
const timestampBytesPromise = OpenTimestamps.multistamp(values, options);
timestampBytesPromise.then(timestams => {
if (timestams === undefined) {
console.error('Invalid timestamp');
return;
}
timestams.forEach((timestamp, i) => {
const ctx = new Context.StreamDeserialization(timestamp);
const detachedTimestampFile = DetachedTimestampFile.DetachedTimestampFile.deserialize(ctx);
if (detachedTimestampFile === undefined) {
console.error('Invalid timestamp');
return;
}
// console.log('STAMP result : ');
// console.log(Utils.bytesToHex(detachedTimestampFile.timestamp.msg));
const buffer = new Buffer(timestamp);
const otsFilename = argsFiles[i] + '.ots';
saveOts(otsFilename, buffer);
});
}).catch(err => {
console.log('Error: ' + err);
});
}).catch(err => {
console.log('Error: ' + err);
});
}
function saveOts(otsFilename, buffer) {
fs.exists(otsFilename, fileExist => {
if (fileExist) {
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) {
const argsFile = argsFileOts.replace('.ots', '');
const filePromise = Utils.readFilePromise(argsFile, null);
const filePromiseOts = Utils.readFilePromise(argsFileOts, null);
Promise.all([filePromise, filePromiseOts]).then(values => {
const file = values[0];
const fileOts = values[1];
console.log('Assuming target filename is \'' + argsFile + '\'');
const verifyPromise = OpenTimestamps.verify(fileOts, file, false);
verifyPromise.then(result => {
if (result === undefined) {
console.log('Pending or Bad attestation');
} else {
console.log('Success! Bitcoin attests data existed as of ' + (new Date(result * 1000)));
}
}).catch(err => {
console.log(err);
});
}).catch(err => {
console.log('Error: ' + err);
});
}
function upgrade(argsFileOts) {
const otsPromise = Utils.readFilePromise(argsFileOts, null);
otsPromise.then(ots => {
const upgradePromise = OpenTimestamps.upgrade(ots);
upgradePromise.then(timestampBytes => {
// check timestamp
if (Utils.arrEq(Utils.arrayToBytes(ots), Utils.arrayToBytes(timestampBytes))) {
console.log('Timestamp not changed');
} else {
console.log('Timestamp has been successfully upgraded!');
fs.writeFile(argsFileOts + '.bak', new Buffer(ots), 'binary', err => {
if (err) {
return console.log(err);
}
// console.log('The file .bak was saved!');
});
fs.writeFile(argsFileOts, new Buffer(timestampBytes), 'binary', err => {
if (err) {
return console.log(err);
}
// console.log('The file .ots was upgraded!');
});
}
}).catch(err => {
console.log('Error: ' + err);
});
}).catch(err => {
console.log('Error: ' + err);
});
}