-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
67 lines (59 loc) · 2.04 KB
/
index.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
var crypto = require('crypto')
var eccrypto = require('eccrypto')
module.exports = SPSClient
var prefix = '-----BEGIN SPS SCREED TEXT-----'
var sigPrefix = '-----BEGIN SPS SCREED SIG-----'
var publicKeyPrefix = '-----BEGIN SPS SCREED PUBLIC KEY-----'
var keySigPrefix = '-----BEGIN SPS KEY SIG-----'
var suffix = '-----END SPS SCREED-----'
function SPSClient (opts) {
if (!(this instanceof SPSClient)) return new SPSClient(opts)
if (!opts) opts = {}
this.privateKey = opts.privateKey
this.publicKey = opts.publicKey
}
SPSClient.prototype.generateKeypair = function () {
this.privateKey = eccrypto.generatePrivate() // A new random 32-byte private key.
this.publicKey = eccrypto.getPublic(this.privateKey) // Corresponding uncompressed (65-byte) public key.
}
SPSClient.prototype._sign = function (str, cb) {
var self = this
var msg = crypto.createHash("sha256").update(str).digest();
eccrypto.sign(self.privateKey, msg).then(function (sig) {
cb(null, sig)
}).catch(function (err) {
cb(err)
})
}
SPSClient.prototype.screed = function (msg, regSig, cb) {
var self = this
self._sign(msg, function (err, sig) {
if (err) return cb(err)
var screed = ''
screed += prefix + '\n'
screed += msg + '\n'
screed += sigPrefix + '\n'
screed += new Buffer(new Uint8Array(sig)).toString('base64') + '\n'
screed += publicKeyPrefix + '\n'
screed += new Buffer(new Uint8Array(self.publicKey)).toString('base64') + '\n'
screed += keySigPrefix + '\n'
screed += regSig + '\n'
screed += suffix
return cb(null, screed)
})
}
SPSClient.prototype.displayScreed = function (msg, regSig, cb) {
var self = this
self._sign(msg, function (err, sig) {
if (err) return cb(err)
var screed = {
index: Math.floor((Math.random() * 100) + 1),
opinion: msg,
screedSig: new Buffer(new Uint8Array(sig)).toString('base64'),
screedPubKey: new Buffer(new Uint8Array(self.publicKey)).toString('base64'),
regSig: regSig
}
console.log(JSON.stringify(screed))
return cb(null, screed)
})
}