-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
290 lines (275 loc) · 9.7 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
var emvTagsConfig = require('./tags');
var emvTagsMap = emvTagsConfig.map;
var emvLongTags = emvTagsConfig.longTags;
var emvDolNumericTypes = emvTagsConfig.dolNumericTypes;
function emvEncodeMapTags() {
emvTagsConfig.map.encode = Object.keys(emvTagsConfig.map.decode)
.map((e) => {
let o = {};
o[emvTagsConfig.map.decode[e]] = e;
return o;
})
.reduce((accum, cur) => Object.assign(accum, cur), {});
}
function translateTagEncode(tag = '') {
// if the tag has 'emv' prepended, returns input value with 'emv' removed
if (tag.startsWith('emv')) {
return tag.substr(3);
}
return emvTagsMap.encode[tag];
}
function translateTagDecode(tag = '') {
// if the tag is unknown, returns 'emv' prepended input value
return emvTagsMap.decode[tag.toUpperCase()] || `emv${tag}`;
}
function getNumVal(val, len, dolLenDiff, compressedNumeric) {
if (dolLenDiff < 0) { // left truncated
return val.slice(len * -2);
} else {
if (!compressedNumeric) {
return ((new Array(len)).fill('00').join('') + val).slice(len * -2);
}
return (val + (new Array(dolLenDiff)).fill('FF').join(''));
}
};
function getNonNumVal(val, len, dolLenDiff) {
if (dolLenDiff < 0) { // right truncated
return val.slice(len * 2);
} else {
return (val + (new Array(len)).fill('00').join('')).slice(len * 2);
}
};
/**
* @param {Object} tagObj decoded emv tag
* @param {string} tagObj.val decoded emv tag value
* @param {string} tagObj.len decoded emv tag value bytes length (/2)
*/
function getValueHexLength(tagObj) {
let len;
if (!tagObj || (!tagObj.len && !tagObj.val)) {
len = 0;
} else {
if (!tagObj.val || !tagObj.val.length) {
len = tagObj.len || 0;
} else {
len = (tagObj.val.length / 2);
}
}
len = len.toString(16).toUpperCase();
return (len.length % 2) ? `0${len}` : len;
}
/**
* @param {string} emvString emv input string
* @param {Object} result object to store results in
* @param {number=} dolIdx value position in data object list
*/
function tagsDecode(emvString, result, dolIdx) {
let tag;
let len;
let val;
let isDol = false;
emvString = emvString.toUpperCase();
if (emvLongTags.includes(emvString.substr(0, 2).toLowerCase())) { // 2 bytes tag
tag = emvString.substr(0, 4);
emvString = emvString.substr(4);
} else {
tag = emvString.substr(0, 2);
emvString = emvString.substr(2);
}
let tagTranslated = translateTagDecode(tag);
result[tagTranslated] = {tag};
if (tagTranslated.includes('DOL')) {
isDol = true;
}
if (dolIdx) {
result[tagTranslated].idx = dolIdx - 1;
}
let lenStr = emvString.substr(0, 2);
len = (lenStr === '') ? 0 : parseInt(emvString.substr(0, 2), 16);
emvString = emvString.substr(2);
if (len >= 128) { // size is big
let byteNumSize = 0;
let cur = 128;
while (cur >= 1) { // calculate big size
cur = cur >> 1;
if ((len & cur) === cur) {
byteNumSize = byteNumSize | cur;
}
}
len = parseInt(emvString.substr(0, byteNumSize * 2), 16);
emvString = emvString.substr(byteNumSize * 2);
}
if (!dolIdx && len > emvString.length / 2) {
throw new Error('Data integrity error');
}
result[tagTranslated].len = len;
if (!len) {
len = 0;
val = '';
} else {
if (dolIdx) {
val = '';
} else {
val = emvString.substr(0, len * 2);
emvString = emvString.substr(len * 2);
}
}
let constructedTagByte = (Buffer.from(tag, 'hex')).slice(0, 1)[0];
if ((constructedTagByte & 32) === 32) {
result[tagTranslated].val = (isDol ? tagsDecode(val, {}, 1) : tagsDecode(val, {}, (dolIdx ? dolIdx + 1 : dolIdx)));
} else {
result[tagTranslated].val = (isDol ? tagsDecode(val, {}, 1) : val);
}
if (emvString.length) {
return tagsDecode(emvString, result, (dolIdx ? dolIdx + 1 : dolIdx));
}
return result;
};
/**
* EMV 4.3 Book 3 5 Data Elements and Files
* Application Specification 5.4 Rules for Using a Data Object List (DOL)
* @param {Object} emvTags decoded emv tags map
*/
function dolDecode(emvTags) {
let data = Object.assign({}, emvTags);
let mainTags = Object.keys(data);
let dolTags = mainTags.filter((t) => (t.includes('DOL')));
if (dolTags.length) {
data = dolTags
.map((t) => ({tag: t, data: data[t].val, internalTags: Object.keys(data[t].val)}))
.reduce((allTags, dol) => {
allTags[dol.tag].val = dol.internalTags.reduce((dolTags, dolInt) => {
let extTag = allTags[dolInt];
let dolTag = dolTags[dolInt];
if (!extTag) { // no tag found in root tags list
dolTags[dolInt].val = (new Array(dolTags[dolInt].len)).fill('00').join('');
} else if (extTag.len === dolTag.len) {
dolTags[dolInt].val = extTag.val;
} else {
let extNumType = emvDolNumericTypes[dolTag.tag];
if (extNumType === 'n') { // non numeric value
dolTags[dolInt].val = getNonNumVal(extTag.val, dolTag.len, dolTag.len - extTag.len, false);
} else if (extNumType === 'nc') { // non numeric value
dolTags[dolInt].val = getNonNumVal(extTag.val, dolTag.len, dolTag.len - extTag.len, true);
} else { // numeric value
dolTags[dolInt].val = getNumVal(extTag.val, dolTag.len, dolTag.len - extTag.len);
}
}
return dolTags;
}, dol.data);
return allTags;
}, data);
}
return data;
};
/**
* @param {Object} emvTags decoded emv tags map
*/
function tagsEncode(emvTags) {
let data = Object.assign({}, emvTags);
let dolOrder = ['CDOL1', 'CDOL2', 'TDOL', 'PDOL', 'DDOL'];
let result = '';
let allTags = Object.keys(data)
.sort((a, b) => {
if ((!data[a].idx && data[a].idx !== 0) || (!data[b].idx && data[b].idx !== 0)) {
return 0;
}
return data[a].idx - data[b].idx;
});
// transform data in dols
data = allTags
.filter((k) => (k.includes('DOL')))
.map((k) => {
let dol = data[k];
return {
tag: dol.tag,
data: Object.keys(dol.val)
.sort((a, b) => {
return dol.val[a].idx > dol.val[b].idx;
})
.map((k) => {
let tagObj = dol.val[k];
let tagTranslated = translateTagEncode(k);
let tagLength = getValueHexLength(tagObj);
return [tagTranslated, tagLength].join('');
})
};
})
.reduce((data, dol) => {
data[dol.tag] = dol.data.join('');
return data;
}, data);
// make sure that dols are constructed in order
let allDols = dolOrder
.map((dol) => (allTags.includes(dol) ? dol : 0))
.filter((e) => e);
// append dols to result
result = allDols
.reduce((r, dol) => {
let tagTranslated = translateTagEncode(dol);
let d = Object.assign({}, data[dol], {val: data[tagTranslated]});
let tagLength = getValueHexLength(d);
return `${r}${tagTranslated}${tagLength}${d.val}`;
}, '');
// cleanup dols
data = allDols
.reduce((r, dol) => {
let tagTranslated = translateTagEncode(dol);
delete r[dol];
delete r[tagTranslated];
return r;
}, data);
// append all fields left to result
return result + Object.keys(data)
.sort((a, b) => data[a].idx - data[b].idx)
.map((e) => {
let tagTranslated = translateTagEncode(e);
let tagObj = data[e];
let tagLength = getValueHexLength(tagObj);
let tagValue = tagObj.val;
let constructedTagByte = (Buffer.from(tagTranslated, 'hex')).slice(0, 1)[0];
if ((constructedTagByte & 32) === 32) {
tagValue = tagsEncode(tagObj.val);
}
return `${tagTranslated}${tagLength}${tagValue}`;
}).join('');
}
/**
* Reduce emv to {{key: value}, ...}
*/
function flatten(emv = {}) {
return Object.keys(emv)
.reduce((acc, key) => {
acc[key] = emv[key].val;
return acc;
}, {});
}
/**
* Expand emv from {{key: value}, ...} to {{tag, len, val}, ...}
*/
function unflatten(emv = {}) {
return Object.keys(emv)
.reduce((acc, key) => {
let entity = {
tag: translateTagEncode(key),
val: emv[key]
};
acc[key] = {
...entity,
len: getValueHexLength(entity)
};
return acc;
}, {});
}
module.exports = (function emv() {
if (!emvTagsConfig.map.encode) {
emvEncodeMapTags();
}
return {
tagsDecode,
dolDecode,
tagsEncode,
flatten,
unflatten
};
})();