/
crypto-services.js
147 lines (126 loc) · 4.69 KB
/
crypto-services.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
angular.module('cesium.es.crypto.services', ['ngResource', 'cesium.services'])
.factory('esCrypto', function($q, $rootScope, CryptoUtils) {
'ngInject';
function getBoxKeypair(keypair) {
if (!keypair) {
throw new Error('Missing keypair');
}
if (keypair.boxPk && keypair.boxSk) {
return $q.when(keypair);
}
return $q.all([
CryptoUtils.box.keypair.skFromSignSk(keypair.signSk),
CryptoUtils.box.keypair.pkFromSignPk(keypair.signPk)
])
.then(function(res) {
return {
boxSk: res[0],
boxPk: res[1]
};
});
}
function packRecordFields(record, keypair, recipientFieldName, cypherFieldNames, nonce) {
recipientFieldName = recipientFieldName || 'recipient';
if (!record[recipientFieldName]) {
return $q.reject({message:'ES_WALLET.ERROR.RECIPIENT_IS_MANDATORY'});
}
cypherFieldNames = cypherFieldNames || 'content';
if (typeof cypherFieldNames == 'string') {
cypherFieldNames = [cypherFieldNames];
}
// Work on a copy, to keep the original record (as it could be use again - fix #382)
record = angular.copy(record);
// Get recipient
var recipientPk = CryptoUtils.util.decode_base58(record[recipientFieldName]);
return $q.all([
getBoxKeypair(keypair),
CryptoUtils.box.keypair.pkFromSignPk(recipientPk),
nonce ? $q.when(nonce) : CryptoUtils.util.random_nonce()
])
.then(function(res) {
//var senderSk = res[0];
var boxKeypair = res[0];
var senderSk = boxKeypair.boxSk;
var boxRecipientPk = res[1];
var nonce = res[2];
return $q.all(
cypherFieldNames.reduce(function(res, fieldName) {
if (!record[fieldName]) return res; // skip undefined fields
return res.concat(
CryptoUtils.box.pack(record[fieldName], nonce, boxRecipientPk, senderSk)
);
}, []))
.then(function(cypherTexts){
// Replace field values with cypher texts
var i = 0;
_.forEach(cypherFieldNames, function(cypherFieldName) {
if (!record[cypherFieldName]) {
// Force undefined fields to be present in object
// This is better for ES storage, that always works on lazy update mode
record[cypherFieldName] = null;
}
else {
record[cypherFieldName] = cypherTexts[i++];
}
});
// Set nonce
record.nonce = CryptoUtils.util.encode_base58(nonce);
return record;
});
});
}
function openRecordFields(records, keypair, issuerFieldName, cypherFieldNames) {
issuerFieldName = issuerFieldName || 'issuer';
cypherFieldNames = cypherFieldNames || 'content';
if (typeof cypherFieldNames == 'string') {
cypherFieldNames = [cypherFieldNames];
}
var now = new Date().getTime();
var issuerBoxPks = {}; // a map used as cache
var jobs = [getBoxKeypair(keypair)];
return $q.all(records.reduce(function(jobs, message) {
var issuer = message[issuerFieldName];
if (!issuer) {throw 'Record has no ' + issuerFieldName;}
if (issuerBoxPks[issuer]) return res;
return jobs.concat(
CryptoUtils.box.keypair.pkFromSignPk(CryptoUtils.util.decode_base58(issuer))
.then(function(issuerBoxPk) {
issuerBoxPks[issuer] = issuerBoxPk; // fill box pk cache
}));
}, jobs))
.then(function(res){
var boxKeypair = res[0];
return $q.all(records.reduce(function(jobs, record) {
var issuerBoxPk = issuerBoxPks[record[issuerFieldName]];
var nonce = CryptoUtils.util.decode_base58(record.nonce);
record.valid = true;
return jobs.concat(
cypherFieldNames.reduce(function(res, cypherFieldName) {
if (!record[cypherFieldName]) return res;
return res.concat(CryptoUtils.box.open(record[cypherFieldName], nonce, issuerBoxPk, boxKeypair.boxSk)
.then(function(text) {
record[cypherFieldName] = text;
})
.catch(function(err){
console.error(err);
console.warn('[ES] [crypto] a record may have invalid cypher ' + cypherFieldName);
record.valid = false;
}));
}, []));
}, []));
})
.then(function() {
console.debug('[ES] [crypto] All record decrypted in ' + (new Date().getTime() - now) + 'ms');
return records;
});
}
// exports
return {
box: {
getKeypair: getBoxKeypair,
pack: packRecordFields,
open: openRecordFields
}
};
})
;