Skip to content

Commit

Permalink
Merge pull request #106 from trustcrypto/fix-loop
Browse files Browse the repository at this point in the history
Fix loop
  • Loading branch information
onlykey authored Oct 11, 2019
2 parents 6244ec5 + 0ecd197 commit 00d5e98
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 87 deletions.
184 changes: 104 additions & 80 deletions app/scripts/onlyKey/OnlyKeyComm.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ var OnlyKeyHID = function (onlyKeyConfigWizard) {
return this.lastMessages[type] && this.lastMessages[type][0] && this.lastMessages[type][0].hasOwnProperty('text') ? this.lastMessages[type][0].text : '';
};

OnlyKey.prototype.getLastMessageIndex = function (type, index) {
return this.lastMessages[type] && this.lastMessages[type][index] && this.lastMessages[type][index].hasOwnProperty('text') ? this.lastMessages[type][index].text : '';
};

OnlyKey.prototype.flushMessage = function (callback = () => {}) {
const messageTypes = Object.keys(this.pendingMessages);
const pendingMessagesTypes = messageTypes.filter(type => this.pendingMessages[type] === true);
Expand Down Expand Up @@ -453,9 +457,20 @@ var OnlyKeyHID = function (onlyKeyConfigWizard) {
this.pendingMessages[msgId] = !this.pendingMessages[msgId];
const cb = poll ? pollForInput.bind(this, {}, callback) : callback;
console.info(`sendPinMessage ${msgId}`);
this.sendMessage({
msgId
}, cb);
if (myOnlyKey.getLastMessage('received') == 'Error PIN is not between 7 - 10 digits') {
this.setLastMessage('received', 'Canceled');
this.sendPinMessage({
msgId: 'OKSETPIN',
poll: false
}, cb);
} else {
this.sendMessage({
msgId
}, cb);
}
console.info('last messages');
console.info(myOnlyKey.getLastMessageIndex('received', 0));
console.info(myOnlyKey.getLastMessageIndex('received', 1));
};

OnlyKey.prototype.sendSetPin = function (callback) {
Expand Down Expand Up @@ -1839,6 +1854,92 @@ var OnlyKeyHID = function (onlyKeyConfigWizard) {
*/
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

function checkForNewFW(checkForNewFW, fwUpdateSupport, version) {
if (!fwchecked) {
return new Promise(resolve => {
fwchecked = true;
if (checkForNewFW == true && fwUpdateSupport == true) { //fw checking enabled and firmware version supports app updates
var r = request.get('https://github.com/trustcrypto/OnlyKey-Firmware/releases/latest', function (err, res, body) {
console.log(r.uri.href);
console.log(this.uri.href);
var latestver = this.uri.href.substr(this.uri.href.length - 11); //end of redirected URL is the version
console.info(version);
console.info(latestver);
var thisver_maj = version.slice(1,2) * 100;
console.info(thisver_maj);
var thisver_min = version.slice(3,4) * 10;
console.info(thisver_min);
var thisver_pat = version.slice(10,11);
var thisver_mod = version.slice(11,12);
console.info(thisver_mod);
var latestver_maj = latestver.slice(1,2) * 100;
console.info(latestver_maj);
var latestver_min = latestver.slice(3,4) * 10;
console.info(latestver_min);
if (latestver_maj==0) {
var latestver_pat = latestver.slice(10,11);
} else {
var latestver_pat = latestver.slice(5,6);
}
console.info(latestver_pat);

if ( (thisver_maj+thisver_min+thisver_pat) < (latestver_maj+latestver_min+latestver_pat) ) {
if (version[9] != '.' || version[10] > 6) {
//if (window.confirm('A new version of firware is available. Click OK to go to the firmware download page.')) {
// window.location.href = 'https://docs.crp.to/usersguide.html#loading-onlykey-firmware';
//};
if (thisver_mod == 'c') {

if (window.confirm('A new version of firware is available. Do you want to automatically download and install the standard edition OnlyKey firmware?')) {
// Download latest standard firmware for color from URL
// https://github.com/trustcrypto/OnlyKey-Firmware/releases/download/
var downloadurl = 'https://github.com/trustcrypto/OnlyKey-Firmware/releases/download/' + latestver + '/Signed_OnlyKey_';
downloadurl = latestver_maj ? downloadurl + latestver_maj + '_' + latestver_min + '_' + latestver_pat + '_STD_Color.txt' : downloadurl + 'Beta' + latestver_pat + '_STD_Color.txt';
console.info(downloadurl);
var req = request.get(downloadurl, async function (err, res, body) {

console.info(myOnlyKey.getLastMessage('received'));
if (myOnlyKey.getLastMessage('received').indexOf("UNINITIALIZEDv") >= 0 || window.confirm('To load new firmware file to your OnlyKey, hold down the #6 button on your OnlyKey for 5+ seconds and release. The OnlyKey light will turn off. Re-enter your PIN to enter config mode. Once this is completed your OnlyKey will flash red and you may click OK to load new firmware.')) {
if (req.responseContent.body) {
var contents = req.responseContent.body && req.responseContent.body.trim();
try {
console.info("unparsed contents", contents);
contents = parseFirmwareData(contents);
console.info("parsed contents", contents);
} catch (parseError) {
throw new Error('Could not parse firmware file.\n\n' + parseError);
}
console.info(contents);
onlyKeyConfigWizard.newFirmware = contents;
const temparray = "1234";
await submitFirmwareData(temparray, function (err) { //First send one message to kick OnlyKey (in config mode) into bootloader
console.info('Working...');
console.info('Firmware file sent to OnlyKey');
myOnlyKey.listen(handleMessage); //OnlyKey will respond with "SUCCESSFULL FW LOAD REQUEST, REBOOTING..." or "ERROR NOT IN CONFIG MODE, HOLD BUTTON 6 DOWN FOR 5 SEC"
});
resolve();
} else {
alert(`Firmware Download Failed`);
resolve();
return;
}
};
});
};
}
}
}
});
} else if (!fwUpdateSupport) {
if (window.confirm('This application is designed to work with a newer version of OnlyKey firmware. Click OK to go to the firmware download page.')) {
window.location.href = 'https://docs.crp.to/usersguide.html#loading-onlykey-firmware';
};
}
resolve();
});
}
}

function submitFirmwareData(firmwareData) {
return new Promise(async function (resolve, reject) {
// this function should recursively call itself until all bytes are sent in chunks
Expand Down Expand Up @@ -2164,83 +2265,6 @@ function byteToHex(value) {
return value.toString(16);
}

function checkForNewFW(checkForNewFW, fwUpdateSupport, version) {
if (!fwchecked) {
return new Promise(resolve => {
fwchecked = true;
if (checkForNewFW == true && fwUpdateSupport == true) { //fw checking enabled and firmware version supports app updates
var r = request.get('https://github.com/trustcrypto/OnlyKey-Firmware/releases/latest', function (err, res, body) {
console.log(r.uri.href);
console.log(res.request.uri.href);
console.log(this.uri.href);
var latestver = this.uri.href.substr(this.uri.href.length - 11); //end of redirected URL is the version
console.info(version);
console.info(latestver);
if (latestversion[3] > version[3] || (latestversion[3] == version[3] && (latestversion[9] == '.' && latestversion[10] > version[10]) || (latestversion[9] != '.' && latestversion[9] > version[9] || (latestversion[9] == version[9] && latestversion[10] > version[10])))) {
if (version[9] != '.' || version[10] > 6) {
if (window.confirm('A new version of firware is available. Click OK to go to the firmware download page.')) {
window.location.href = 'https://docs.crp.to/usersguide.html#loading-onlykey-firmware';
};
/*
if (confirm(`Version ${latestversion} firware is available. Do you want to automatically download and install the standard edition OnlyKey firmware?`)) {
if (version[11] == 'o') {
//Download latest standard firmware for original from URL
// https://github.com/trustcrypto/OnlyKey-Firmware/releases/download/
// + latestversion
// + /
// + OnlyKey_
// + latestversion
// + _STD_Original.txt
// downloaded file = contents
if (contents) {
onlyKeyConfigWizard.newFirmware = contents;
const temparray = "1234";
submitFirmwareData(temparray, function (err) { //First send one message to kick OnlyKey (in config mode) into bootloader
//TODO if OnlyKey responds with SUCCESSFULL then continue, if not exit
myOnlyKey.listen(handleMessage); //OnlyKey will respond with "SUCCESSFULL FW LOAD REQUEST, REBOOTING..." or "ERROR NOT IN CONFIG MODE, HOLD BUTTON 6 DOWN FOR 5 SEC"
});
} else {
alert(`Firmware Download Failed`);
return;
}
} else if (version[11] == 'c') {
// Download latest standard firmware for color from URL
// https://github.com/trustcrypto/OnlyKey-Firmware/releases/download/
// + latestversion
// + /
// + OnlyKey_
// + latestversion
// + _STD_Color.txt
// downloaded file = contents
if (contents) {
onlyKeyConfigWizard.newFirmware = contents;
const temparray = "1234";
submitFirmwareData(temparray, function (err) { //First send one message to kick OnlyKey (in config mode) into bootloader
//TODO if OnlyKey responds with SUCCESSFULL then continue, if not exit
myOnlyKey.listen(handleMessage); //OnlyKey will respond with "SUCCESSFULL FW LOAD REQUEST, REBOOTING..." or "ERROR NOT IN CONFIG MODE, HOLD BUTTON 6 DOWN FOR 5 SEC"
});
} else {
alert(`Firmware Download Failed`);
return;
}
}
}
}
*/
}
}
});
resolve();
} else if (!fwUpdateSupport) {
if (window.confirm('This application is designed to work with a newer version of OnlyKey firmware. Click OK to go to the firmware download page.')) {
window.location.href = 'https://docs.crp.to/usersguide.html#loading-onlykey-firmware';
};
}
resolve();
});
}
}


//nw.Window.get().on('new-win-policy', function(frame, url, policy) {
// // do not open the window
Expand Down
7 changes: 4 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "OnlyKey Configuration",
"manifest_version": 2,
"version": "5.1.0",
"version_name": "5.1-beta.0",
"version": "5.2.0",
"version_name": "5.2.0",
"minimum_chrome_version": "45.0.2439.3",
"app": {
"background": {
Expand All @@ -23,5 +23,6 @@
"16": "resources/onlykey_logo_16.png",
"48": "resources/onlykey_logo_48.png",
"128": "resources/onlykey_logo_128.png"
}
},
"manifestUrl": "https://s3.amazonaws.com/onlykey/apps/desktop/releases/manifest.json"
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"name": "OnlyKey",
"productName": "OnlyKey App",
"version": "5.2.0",
"version_name": "5.2.0",
"description": "Setup and configure OnlyKey",
"main": "app.js",
"dependencies": {
"auto-launch": "^5.0.5",
"fs-jetpack": "^2.2.2",
"nw": "^0.36.4-sdk",
"nw": "^0.36.4",
"nw-autoupdater": "^1.1.8",
"q": "^1.5.1",
"request": "^2.88.0"
Expand Down

0 comments on commit 00d5e98

Please sign in to comment.