Implement Bangle1 support

pull/901/head
Andrew Gregory 2021-11-13 17:44:32 +08:00 committed by GitHub
parent 04af09e52a
commit fe345a35ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 71 additions and 36 deletions

View File

@ -87,16 +87,22 @@ function hotp(token) {
var v = new DataView(msg.buffer); var v = new DataView(msg.buffer);
v.setUint32(0, tick >> 16 >> 16); v.setUint32(0, tick >> 16 >> 16);
v.setUint32(4, tick & 0xFFFFFFFF); v.setUint32(4, tick & 0xFFFFFFFF);
var hash = do_hmac(b32decode(token.secret), msg, token.algorithm.toUpperCase()); var ret = "";
var ret = "" + hash % Math.pow(10, token.digits); try {
while (ret.length < token.digits) { var hash = do_hmac(b32decode(token.secret), msg, token.algorithm.toUpperCase());
ret = "0" + ret; ret = "" + hash % Math.pow(10, token.digits);
while (ret.length < token.digits) {
ret = "0" + ret;
}
} catch(err) {
ret = "Not supported";
} }
return {hotp:ret, next:((token.period > 0) ? ((tick + 1) * token.period * 1000) : d.getTime() + 30000)}; return {hotp:ret, next:((token.period > 0) ? ((tick + 1) * token.period * 1000) : d.getTime() + 30000)};
} }
var state = { var state = {
listy: 0, listy: 0,
prevcur:0,
curtoken:-1, curtoken:-1,
nextTime:0, nextTime:0,
otp:"", otp:"",
@ -143,7 +149,7 @@ function drawToken(id, r) {
adj = 5; adj = 5;
} }
// digits just below label // digits just below label
g.setFont("Vector", (tokens[id].digits > 8) ? 26 : 30); g.setFont("Vector", (state.otp.length > 8) ? 26 : 30);
g.drawString(state.otp, (x1 + x2) / 2 + adj, y1 + 16, false); g.drawString(state.otp, (x1 + x2) / 2 + adj, y1 + 16, false);
} }
// shaded lines top and bottom // shaded lines top and bottom
@ -160,20 +166,18 @@ function draw() {
if (d.getTime() > state.nextTime) { if (d.getTime() > state.nextTime) {
if (state.hide == 0) { if (state.hide == 0) {
// auto-hide the current token // auto-hide the current token
state.curtoken = -1; if (state.curtoken != -1) {
state.prevcur = state.curtoken;
state.curtoken = -1;
}
state.nextTime = 0; state.nextTime = 0;
} else { } else {
// time to generate a new token // time to generate a new token
try { var r = hotp(t);
var r = hotp(t); state.nextTime = r.next;
state.nextTime = r.next; state.otp = r.hotp;
state.otp = r.hotp; if (t.period <= 0) {
if (t.period <= 0) { state.hide = 1;
state.hide = 1;
}
} catch (err) {
state.nextTime = 0;
state.otp = "Not supported";
} }
state.hide--; state.hide--;
} }
@ -203,7 +207,10 @@ function draw() {
} }
} else { } else {
// de-select the current token if it is scrolled out of view // de-select the current token if it is scrolled out of view
state.curtoken = -1; if (state.curtoken != -1) {
state.prevcur = state.curtoken;
state.curtoken = -1;
}
state.nexttime = 0; state.nexttime = 0;
} }
} else { } else {
@ -214,26 +221,28 @@ function draw() {
} }
function onTouch(zone, e) { function onTouch(zone, e) {
var id = Math.floor((state.listy + (e.y - Bangle.appRect.y)) / tokenentryheight); if (e) {
if (id == state.curtoken) { var id = Math.floor((state.listy + (e.y - Bangle.appRect.y)) / tokenentryheight);
id = -1; if (id == state.curtoken) {
} id = -1;
if (state.curtoken != id) { }
if (id != -1) { if (state.curtoken != id) {
var y = id * tokenentryheight - state.listy; if (id != -1) {
if (y < 0) { var y = id * tokenentryheight - state.listy;
state.listy += y; if (y < 0) {
y = 0; state.listy += y;
} y = 0;
y += tokenentryheight; }
if (y > Bangle.appRect.h) { y += tokenentryheight;
state.listy += (y - Bangle.appRect.h); if (y > Bangle.appRect.h) {
} state.listy += (y - Bangle.appRect.h);
}
}
state.nextTime = 0;
state.curtoken = id;
state.hide = 2;
draw();
} }
state.nextTime = 0;
state.curtoken = id;
state.hide = 2;
draw();
} }
} }
@ -261,9 +270,35 @@ function onSwipe(e) {
} }
} }
function bangle1Btn(e) {
if (tokens.length > 0) {
if (state.curtoken == -1) {
state.nextTime = 0;
state.curtoken = state.prevcur;
} else {
switch (e) {
case -1: state.curtoken--; break;
case 1: state.curtoken++; break;
}
state.nextTime = 0;
}
state.curtoken = Math.max(state.curtoken, 0);
state.curtoken = Math.min(state.curtoken, tokens.length - 1);
var fakee = {};
fakee.y = state.curtoken * tokenentryheight - state.listy + Bangle.appRect.y;
state.curtoken = -1;
onTouch(0, fakee);
}
}
Bangle.on('touch', onTouch); Bangle.on('touch', onTouch);
Bangle.on('drag' , onDrag ); Bangle.on('drag' , onDrag );
Bangle.on('swipe', onSwipe); Bangle.on('swipe', onSwipe);
if (typeof BTN2 == 'number') {
setWatch(function(){bangle1Btn(-1);}, BTN1, {edge:"rising", debounce:50, repeat:true});
setWatch(function(){ onSwipe(-1);}, BTN2, {edge:"rising", debounce:50, repeat:true});
setWatch(function(){bangle1Btn( 1);}, BTN3, {edge:"rising", debounce:50, repeat:true});
}
Bangle.loadWidgets(); Bangle.loadWidgets();
// Clear the screen once, at startup // Clear the screen once, at startup