@binance/fingerprint
Version:
binance web fingerprint
60 lines (59 loc) • 1.91 kB
JavaScript
import md5 from "blueimp-md5";
import { Base64 } from "js-base64";
import * as store from "./utils/store";
import { isClient } from "./utils/is";
var getDevices = function() {
return store.getCacheDevice();
};
export var getCurrentDeviceId = function(id) {
if (!id) return "";
var devices = getDevices();
var emailEncode = md5(id);
var device = devices[emailEncode];
return device && device.value || "";
};
export var saveCurrentDeviceId = function(id, value) {
var devices = getDevices();
var emailEncode = md5(id || "");
var device = devices[emailEncode];
if (!device) {
// delete first date.
var tmpList = Object.keys(devices).reduce(function(res, key) {
res.push({
key: key,
value: devices[key]
});
return res;
}, []);
if (tmpList.length >= 10) {
tmpList = tmpList.sort(function(prev, next) {
return prev.value.date - next.value.date;
});
var first = tmpList[0] || {};
delete devices[first.key];
}
}
devices[emailEncode] = {
date: new Date().getTime(),
value: value
};
store.setCacheDevice(devices);
};
export var withDeviceInfo = function(id) {
var fpInfo = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
var info = Object.assign({}, fpInfo);
if (isClient) {
// get device_id
info.device_id = getCurrentDeviceId(id);
// get related_device_ids
var emailMd5 = md5(id || "");
var devices = getDevices();
var keys = Object.keys(devices).filter(function(k) {
return k !== emailMd5;
});
info.related_device_ids = keys.map(function(k) {
return devices[k].value;
}).join(",");
}
return Base64.encode(JSON.stringify(info));
};