Created
November 5, 2022 19:09
-
-
Save leesalminen/0d30bc00215beeea1e8746b54438f24a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vue.component("LNURLWithdrawContactless", { | |
data: function () { | |
return { | |
supported: ('NDEFReader' in window && window.self === window.top), | |
scanning: false, | |
submitting: false, | |
readerAbortController: null, | |
} | |
}, | |
methods: { | |
startScan: async function () { | |
try { | |
if (this.scanning || this.submitting) { | |
return; | |
} | |
const self = this; | |
self.submitting = false; | |
self.scanning = true; | |
if (!this.supported) { | |
const result = prompt("enter lnurl withdraw"); | |
if (result) { | |
self.sendData.bind(self)(result); | |
return; | |
} | |
self.scanning = false; | |
} | |
ndef = new NDEFReader() | |
self.readerAbortController = new AbortController() | |
await ndef.scan({signal: self.readerAbortController.signal}) | |
ndef.addEventListener('readingerror', () => { | |
self.scanning = false; | |
self.readerAbortController.abort() | |
}) | |
ndef.addEventListener('reading', ({message, serialNumber}) => { | |
//Decode NDEF data from tag | |
const record = message.records[0] | |
const textDecoder = new TextDecoder('utf-8') | |
const lnurl = textDecoder.decode(record.data) | |
//User feedback, show loader icon | |
self.scanning = false; | |
self.sendData.bind(self)(lnurl); | |
//Play a sound for the user. | |
self.playSound("https://storage.googleapis.com/bitcoin-jungle-branding/sounds/lnurlnfcsuccess.wav") | |
}) | |
} catch(e) { | |
self.scanning = false; | |
self.submitting = false; | |
} | |
}, | |
sendData: function (lnurl) { | |
this.submitting = true; | |
//Post LNURLW data to server | |
var xhr = new XMLHttpRequest() | |
xhr.open('POST', window.lnurlWithdrawSubmitUrl, true) | |
xhr.setRequestHeader('Content-Type', 'application/json') | |
xhr.send(JSON.stringify({lnurl, destination: this.$parent.srvModel.btcAddress})) | |
const self = this; | |
//User feedback, reset on failure | |
xhr.onload = function () { | |
if (xhr.readyState === xhr.DONE) { | |
console.log(xhr.response); | |
console.log(xhr.responseText); | |
self.scanning = false; | |
self.submitting = false; | |
if(self.readerAbortController) { | |
self.readerAbortController.abort() | |
} | |
if(xhr.response){ | |
alert(xhr.response) | |
} else { | |
self.playSound("https://praiabitcoin.org/bitcoinparadise_teaser.mp3") | |
} | |
} | |
} | |
}, | |
playSound: function (url) { | |
// audio supported? | |
if (typeof window.Audio === 'function') { | |
var audioElem = new Audio(url); | |
audioElem.play().catch(function(){ | |
debugger; | |
}) | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment