1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | var crypto = require('crypto')
|
6 | var stdin = require('get-stdin')
|
7 | var eccrypto = require('eccrypto')
|
8 |
|
9 | var command = process.argv[0]
|
10 | var script = process.argv[1]
|
11 | var private_key = process.argv[2]
|
12 | var message = process.argv[3]
|
13 |
|
14 | var pk, m
|
15 |
|
16 | stdin.buffer().then(function(s) {
|
17 |
|
18 | if (!s.length && typeof message === 'undefined') {
|
19 | return process.stdout.write(JSON.stringify({
|
20 | error: 'message not given'
|
21 | }), function() {
|
22 | process.exit(1)
|
23 | })
|
24 | }
|
25 |
|
26 | m = crypto.createHash('sha256').update(s || message).digest()
|
27 |
|
28 | if (!private_key) {
|
29 | return process.stdout.write(JSON.stringify({
|
30 | error: 'private key not given'
|
31 | }), function() {
|
32 | process.exit(1)
|
33 | })
|
34 | }
|
35 |
|
36 | try {
|
37 | pk = Buffer(private_key, 'hex')
|
38 | } catch (err) {
|
39 | return process.stdout.write(JSON.stringify({
|
40 | error: 'private key is not a hexadecimal string'
|
41 | }), function() {
|
42 | process.exit(1)
|
43 | })
|
44 | }
|
45 |
|
46 | return eccrypto.sign(pk, m)
|
47 |
|
48 | }).then(function(signature) {
|
49 | process.stdout.write(JSON.stringify({
|
50 | signature: signature.toString('hex')
|
51 | }), function() {
|
52 | process.exit(0)
|
53 | })
|
54 |
|
55 | }).catch(function(err) {
|
56 | process.stdout.write(JSON.stringify({
|
57 | error: 'signature creation failed',
|
58 | eccrypto_error: JSON.stringify(err || true)}
|
59 | ), function() {
|
60 | process.exit(1)
|
61 | })
|
62 | })
|