BangleApps/apps/pinsafe/app.js

164 lines
5.4 KiB
JavaScript
Raw Permalink Normal View History

2023-01-17 08:40:27 +00:00
var Layout = require("Layout");
var myTimeout;
var pins = Object.assign(require('Storage').readJSON("pinsafe.json", true) || {});
function mainMenu() {
if (myTimeout) clearTimeout(myTimeout);
var menu = {
"" : {title : "Decode:"},
"< Back" : Bangle.load
};
if (Object.keys(pins).length==0) Object.assign(menu, {"NO CARDS":""});
2023-01-18 08:37:20 +00:00
else for (let id in pins) {
let p=pins[id];
menu[id]=()=>{decode(p);};
2023-01-17 08:40:27 +00:00
}
2023-01-18 08:37:20 +00:00
menu["Add Card"]=addCard;
menu["Remove Card"]=removeCard;
g.clear();
2023-01-17 08:40:27 +00:00
E.showMenu(menu);
}
function decode(pin) {
showNumpad("Key:", function() {
if (key.length==0) mainMenu();
else {
var pinDecrypted="";
for (let n=0;n<pin.length;n++) {
pinDecrypted+=(10+Number(pin[n])-Number(key[n%key.length]))%10;
}
var showPin = new Layout ({
type:"v", c: [
{type:"txt", font:"30%", pad:1, fillx:1, filly:1, label:"PIN:"},
{type:"txt", font:100/pinDecrypted.length+"%", pad:1, fillx:1, filly:1, label: pinDecrypted},
{type:"btn", font:"30%", pad:1, fillx:1, filly:1, label:"OK", cb:l=>{mainMenu();}}
], lazy:true});
g.clear();
showPin.render();
myTimeout = setTimeout(Bangle.load, 10000);
}
});
}
function showNumpad(text, callback) {
E.showMenu();
key="";
function addDigit(digit) {
key+=digit;
Bangle.buzz(20);
update();
}
function update() {
2023-01-18 08:37:20 +00:00
g.reset();
g.clearRect(0,0,g.getWidth(),23);
g.setFont("Vector:24").setFontAlign(1,0).drawString(text+key,g.getWidth(),12);
2023-01-17 08:40:27 +00:00
}
2023-01-18 08:37:20 +00:00
ds="12%";
2023-01-17 08:40:27 +00:00
var numPad = new Layout ({
type:"v", c: [{
2023-01-18 08:37:20 +00:00
type:"v", c: [
{type:"", height:24},
{type:"h",filly:1, c: [
{type:"btn", font:ds, width:58, label:"7", cb:l=>{addDigit("7");}},
{type:"btn", font:ds, width:58, label:"8", cb:l=>{addDigit("8");}},
{type:"btn", font:ds, width:58, label:"9", cb:l=>{addDigit("9");}}
2023-01-17 08:40:27 +00:00
]},
2023-01-18 08:37:20 +00:00
{type:"h",filly:1, c: [
{type:"btn", font:ds, width:58, label:"4", cb:l=>{addDigit("4");}},
{type:"btn", font:ds, width:58, label:"5", cb:l=>{addDigit("5");}},
{type:"btn", font:ds, width:58, label:"6", cb:l=>{addDigit("6");}}
2023-01-17 08:40:27 +00:00
]},
2023-01-18 08:37:20 +00:00
{type:"h",filly:1, c: [
{type:"btn", font:ds, width:58, label:"1", cb:l=>{addDigit("1");}},
{type:"btn", font:ds, width:58, label:"2", cb:l=>{addDigit("2");}},
{type:"btn", font:ds, width:58, label:"3", cb:l=>{addDigit("3");}}
2023-01-17 08:40:27 +00:00
]},
2023-01-18 08:37:20 +00:00
{type:"h",filly:1, c: [
{type:"btn", font:ds, width:58, label:"0", cb:l=>{addDigit("0");}},
{type:"btn", font:ds, width:58, label:"C", cb:l=>{key=key.slice(0,-1); update();}},
{type:"btn", font:ds, width:58, id:"OK", label:"OK", cb:callback}
2023-01-17 08:40:27 +00:00
]}
]}
], lazy:true});
g.clear();
numPad.render();
2023-01-18 08:37:20 +00:00
update();
2023-01-17 08:40:27 +00:00
}
function removeCard() {
var menu = {
"" : {title : "select card"},
"< Back" : mainMenu
};
if (Object.keys(pins).length==0) Object.assign(menu, {"NO CARDS":""});
else for (let c in pins) {
let card=c;
menu[c]=()=>{
E.showMenu();
var confirmRemove = new Layout (
{type:"v", c: [
{type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:"Delete"},
{type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:card+"?"},
{type:"h", c: [
2023-01-18 08:37:20 +00:00
{type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "YES", cb:l=>{
delete pins[card];
require('Storage').writeJSON("pinsafe.json", pins);
mainMenu();
}},
{type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " NO", cb:l=>{mainMenu();}}
2023-01-17 08:40:27 +00:00
]}
], lazy:true});
g.clear();
confirmRemove.render();
};
}
E.showMenu(menu);
}
function addCard() {
2023-01-18 08:37:20 +00:00
var infoScreen = new Layout({
2023-01-17 08:40:27 +00:00
type:"v", c: [
2023-01-18 08:37:20 +00:00
{type:"txt", font:"10%", label:"Enter a name, PIN\nand key for your\ncard. The PIN will\nbe stored on your\ndevice in encrypted\nform.\nTap to continue!", cb:l=>{
2023-01-17 08:40:27 +00:00
require("textinput").input({text:""}).then(result => {
if (pins[result]!=undefined) {
E.showMenu();
var alreadyExists = new Layout (
{type:"v", c: [
{type:"txt", font:Math.min(15,100/result.length)+"%", pad:1, fillx:1, filly:1, label:result},
{type:"txt", font:"12%", pad:1, fillx:1, filly:1, label:"already exists."},
{type:"h", c: [
{type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "REPLACE", cb:l=>{encodeCard(result);}},
{type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "CANCEL", cb:l=>{mainMenu();}}
]}
], lazy:true});
g.clear();
alreadyExists.render();
} else
encodeCard(result);
});
}}
]
});
g.clear();
2023-01-18 08:37:20 +00:00
infoScreen.render();
2023-01-17 08:40:27 +00:00
}
function encodeCard(name) {
showNumpad("PIN:", function() {
if (key.length==0) mainMenu();
else {
var pin=key;
showNumpad("Key:", function() {
if (key.length==0) mainMenu();
else {
var pinEncrypted="";
for (let n=0;n<pin.length;n++) {
a=(10+Number(pin[n])+Number(key[n%key.length]))%10;
pinEncrypted+=a;
}
pins[name]=pinEncrypted;
require('Storage').writeJSON("pinsafe.json", pins);
mainMenu();
}
});
}
});
}
g.reset();
Bangle.setUI();
mainMenu();