-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqrcode.js
54 lines (35 loc) · 1.29 KB
/
qrcode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require('dotenv').config()
const fs = require('fs');
const qrcode = require('qrcode');
const { bech32 } = require("bech32");
const getLnurl = (lightningAddress) => {
const lnAddressParts = lightningAddress.split('@')
const alias = lnAddressParts[0]
const domain = lnAddressParts[1]
return `https://${domain}/.well-known/lnurlp/${alias}`
}
const bech32EncodeLnurl = (lnurl) => {
const words = bech32.toWords(Buffer.from(lnurl, 'utf8'))
const bech32Encoded = bech32.encode("lnurl", words)
return bech32Encoded
}
const toUpperCaseIfNeeded = (data) => data.includes('lnurl') ? data.toUpperCase() : data
async function generateQrCode() {
const address = process.argv[2]
if (!address) {
throw new Error('Address must be defined')
}
const data = address.includes('@') ? bech32EncodeLnurl(getLnurl(address)) : address
console.log(data)
try {
const qrCodeDataUrl = await qrcode.toDataURL(toUpperCaseIfNeeded(data));
const qrCodeImage = qrCodeDataUrl.split(',')[1]; // Remove data URI prefix
console.log(qrCodeImage)
const fileName = `${address}.png`
fs.writeFileSync(fileName, qrCodeImage, 'base64');
console.log(`QR code generated and saved as ${fileName}`);
} catch (error) {
console.error('Error generating QR code:', error);
}
}
generateQrCode();