Skip to content

Commit

Permalink
feat: POC for NFC for Pay
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Burtey committed Feb 12, 2024
1 parent f317b06 commit 8de9828
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions apps/pay/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react"
import React, { useState } from "react"
import Card from "react-bootstrap/Card"
import Col from "react-bootstrap/Col"
import Container from "react-bootstrap/Container"
Expand Down Expand Up @@ -45,8 +45,61 @@ function Home() {
)
}

const [nfcMessage, setNfcMessage] = useState("")

const checkForLnurl = (text) => {
return text.toLowerCase().includes("lnurl")
}

const decodeNDEFRecord = (record) => {
// Ensure that the record's data is an instance of ArrayBuffer
if (record.data instanceof ArrayBuffer) {
const decoder = new TextDecoder(record.encoding || "utf-8")
return decoder.decode(record.data)
} else {
// If it's not an ArrayBuffer, it might be a DataView or another typed array.
// In that case, we can create a new Uint8Array from the buffer of the DataView.
const decoder = new TextDecoder(record.encoding || "utf-8")
return decoder.decode(new Uint8Array(record.data.buffer))
}
}

const handleNFCScan = () => {
if ("NDEFReader" in window) {
const ndef = new NDEFReader()
ndef
.scan()
.then(() => {
console.log("NFC scan started successfully.")

ndef.onreading = (event) => {
console.log("NFC tag read.")
const record = event.message.records[0]
const text = decodeNDEFRecord(record)

if (checkForLnurl(text)) {
setNfcMessage(text)
// Handle your "lnurl" logic here...
}
}

ndef.onreadingerror = () => {
console.log("Cannot read data from the NFC tag. Try another one?")
}
})
.catch((error) => {
console.log(`Error! Scan failed to start: ${error}.`)
})
} else {
console.log("NFC is not supported")
}
}

return (
<Container>
<button onClick={handleNFCScan}>Start NFC Scan</button>
{nfcMessage && <div>LNURL: {nfcMessage}</div>}

<br />
<Row>
<Col>
Expand All @@ -68,8 +121,8 @@ function Home() {
{error
? "Unavailable"
: loading
? "Loading..."
: data.globals.nodesIds[0]}
? "Loading..."
: data.globals.nodesIds[0]}
</p>
</ListGroup.Item>
<ListGroup.Item>
Expand Down

0 comments on commit 8de9828

Please sign in to comment.