Skip to content

Commit

Permalink
fix(qr): b64 padding and content addition
Browse files Browse the repository at this point in the history
also fixes the example where refresh count should be updated
  • Loading branch information
Vexcited committed Nov 5, 2024
1 parent 328d5e3 commit 00e32f9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion examples/generate-pay-qr-code.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as izly from "../src";
import { read } from "./_persisted-session";
import { persist, read } from "./_persisted-session";
import { toString as qrcode } from "qrcode";

void async function main () {
Expand All @@ -10,4 +10,6 @@ void async function main () {
qrcode(data, { type: "utf8" }, (_, qr) => {
console.log(qr);
});

await persist(identification);
}();
4 changes: 2 additions & 2 deletions src/api/private/otp.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { base64, base64url } from "@scure/base";
import { base64urlnopad } from "@scure/base";
import { packBigEndian } from "~/core/pack";
import { hashWithHMAC } from "~/core/hmac";

export const otp = (seed: string, counter: number): string => {
const packedCounter = packBigEndian(counter);
return base64url.encode(hashWithHMAC(packedCounter, base64.decode(seed)));
return base64urlnopad.encode(hashWithHMAC(packedCounter, base64urlnopad.decode(seed)));
};
6 changes: 4 additions & 2 deletions src/api/qr-pay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const signWithPrivateKey = (textToSign: string, pem: string): Uint8Array => {
*/
export const qrPay = (identification: Identification): string => {
// Replicate `SimpleDateFormat("yyyy-MM-dd HH:mm:ss")`
const dateFormatter = new Intl.DateTimeFormat("en-CA", { timeZone: "UTC", year: "numeric", month: "2-digit", day: "2-digit", hour12: false, second: "2-digit", minute: "2-digit", hour: "2-digit" });
const dateFormatter = new Intl.DateTimeFormat("fr-CA", { timeZone: "UTC", year: "numeric", month: "2-digit", day: "2-digit", hour12: false, second: "2-digit", minute: "2-digit", hour: "2-digit" });
const dateNowFormatted = dateFormatter.format(new Date()).replace(",", "");

let hotpCode = otp(identification.seed, identification.refreshCount);
Expand All @@ -45,5 +45,7 @@ export const qrPay = (identification: Identification): string => {
content = content + ";" + bytesToHex(hashWithHMAC(`${content}+${identification.nsse}`, hmacKey)) + ";";

const signed = signWithPrivateKey(content, privateKey);
return base64.encode(signed);
content += base64.encode(signed);

return content;
};

0 comments on commit 00e32f9

Please sign in to comment.