-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·175 lines (148 loc) · 4.37 KB
/
index.js
File metadata and controls
executable file
·175 lines (148 loc) · 4.37 KB
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
#!/usr/bin/env node --harmony
var fs = require('fs')
var minimist = require('minimist')
var colors = require('colors/safe')
const btcConvert = require('bitcoin-convert')
const utils = require('./utils')
const api = require('@proofofexistence/api-client')
const logger = require('./logger')
var argv = minimist(process.argv, {
alias: {
v: 'version',
h: 'host',
p: 'port',
n: 'network',
V: 'verbose'
},
default: {
p: null,
h: 'https://proofofexistence.com',
n: 'testnet'
},
boolean: ['version', 'help', 'verbose']
})
const version = require('./package').version
const filename = argv._[2]
const url = `${argv.host}${argv.port ? ":" + argv.port : ''}`
if (argv.verbose) {
logger.level('debug')
}
console.log(colors.blue("Proof of Existence - Verify your documents"))
if (argv.version) {
console.log(version)
process.exit(0)
}
if (argv.help || (process.stdin.isTTY && !filename)) {
console.error(
'Usage: proofx [filename] [options]\n\n' +
' --host,-h URL of the proofx instance\n' +
' --port,-p Port where proofx is running\n' +
' --verbose,-V Print out more logs\n' +
' --version,-v Print out the installed version\n' +
' --help Show this help\n'
)
process.exit(1)
}
// check if is a hash
if (utils.isValidSHA256(filename)) { // read existing hash
console.log(colors.gray("Recognized an existing hash (sha256)"))
register(filename)
} else if (filename === '-' || !filename) { // read from stdin
utils.hashFile(process.stdin, function(hash) {
register(hash);
})
} else if (fs.existsSync(filename)) { // read data from file
utils.hashFile(fs.createReadStream(filename), function(hash) {
register(hash);
})
} else { // error
console.error(colors.red('Sorry, file: %s does not exist'), filename)
process.exit(2)
}
function getDocStatus (status) {
if (status.pending === true && !status.txstamp) {
return 'paymentRequired'
} else if (status.txstamp && !status.blockstamp) {
return 'confirming'
} else if (status.blockstamp) {
return 'confirmed'
}
}
function register(sha256) {
if (!utils.isValidSHA256(sha256)) {
console.error(colors.red('Please pass a valid hash.'))
process.exit(0)
}
logger.debug(sha256)
console.log(colors.gray(`Connection to ${url}...\n`))
api.register(sha256, {baseURL : url})
.then(resp => {
const { success, reason } = resp.data
// console.log(sha256, resp.data);
if (success) { // new record
console.log(
colors.green('New document registered!')
)
api.updateStatus(sha256, { baseURL: url })
.then(statusResp => showStatus(statusResp.data))
.catch(error => showError(error))
} else if (success === false && reason === 'existing') { // record already exist in local DB
console.log(
colors.yellow('This document exists in our registery\n')
)
// update by default
api.getStatus(sha256, { baseURL: url })
.then(resp => showStatus(resp.data) )
.catch(error => showError(error))
}
})
.catch(error => showError(error) )
}
/*
* Human-readable errors on the screen
*
*/
function showError(error) {
console.log(error.status, error.statusText)
}
/*
* Display human-readable information on the screen
*
*/
function showStatus(resp) {
const status = getDocStatus(resp)
const {
payment_address,
price,
pending,
tx,
txstamp,
blockstamp,
} = resp
const mBTCPrice = btcConvert(price, 'Satoshi', 'mBTC')
switch (status) {
case "paymentRequired":
console.log(
`Please pay the fee for the certification to continue\nSend ${colors.green(mBTCPrice+ ' mBTC')} to ${colors.green(payment_address)}
`
)
break;
case "confirming":
console.log(
colors.green(
`Congrats! The transaction has been succesfully recorded.\n`
)
+
`\nNow we just need to wait for the block to be confirmed by the miners.\nYour transaction id is ${colors.green(tx)}.\nCheck progresses at http://insight.bitpay.com/tx/${tx}`
)
break;
case "confirmed":
console.log(
colors.green('The existence of this document is already confirmed.\n')
+
`The transaction id is ${colors.green(tx)} in the block ${blockstamp}.`
)
break;
default:
}
}