Compare commits

...

5 Commits

Author SHA1 Message Date
bumi 9472f3edb0 moar 2020-10-22 23:15:52 +02:00
bumi ff35c727d4 Optional constructor parameter
this makes the interface easier when we only want to get a new address:

new LnMe().newAddress().then(console.log)
2020-10-22 20:44:11 +02:00
bumi e1882b8fcf Add support to get an onchain Address 2020-10-22 20:36:27 +02:00
bumi 456ae2eb50 Add endpoint to get ne onchain BTC address 2020-10-22 20:15:34 +02:00
bumi 01460b2c5c Disable inputs after submit
otherwise the user might change the values but the invoice does not get updated.
2020-10-22 20:14:14 +02:00
4 changed files with 67 additions and 11 deletions

View File

@ -48,14 +48,14 @@ var paymentConfirmationTemplate = `<h1 class="lnme-header lnme-confirmation">Pay
class LnMe {
constructor(options) {
options = options || {};
this.script = document.getElementById('lnme-script');
if (options.baseURL) {
this.baseURL = options.baseURL;
} else if (this.script.dataset.lnmeBaseUrl) {
} else if (this.script && this.script.dataset.lnmeBaseUrl) {
this.baseURL = this.script.dataset.lnmeBaseUrl;
} else {
let url = new URL(this.script.src);
this.baseURL = `${url.protocol}//${url.host}`;
this.baseURL = `${document.location.protocol}//${document.location.host}`;
}
this.value = parseInt(options.value || 0);
this.memo = options.memo || '';
@ -66,7 +66,7 @@ class LnMe {
loadStylesheet() {
if (document.getElementById('lnme-style')) { return; }
// get the CSS file from the same source as the JS widget file
let source = this.script.src.replace(/\.js$/, ".css");
let source = `${this.baseURL}/lnme/lnme.css`;
let head = document.getElementsByTagName('head')[0];
let css = document.createElement('link');
css.id = "lnme-style";
@ -155,6 +155,19 @@ class LnMe {
});
}
newAddress() {
let args = {
method: 'POST',
mode: 'cors',
header: { 'Content-Type': 'application/json' }
};
return this._fetch(`${this.baseURL}/v1/newaddress`, args)
.then(address => {
this.address = address;
return address;
});
}
requestPayment() {
return this.addInvoice().then((invoice) => {
if (typeof webln !== 'undefined') {

View File

@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Send me some Sats</title>
<title>Send me some Sats</title>
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:wght@400;700&display=swap" rel="stylesheet" />
<style>
html {
@ -24,6 +24,9 @@
margin: 1em auto;
text-align: center;
}
.note {
font-size: 0.2em;
}
.wrapper h1 {
font-weight: 700;
}
@ -102,11 +105,15 @@
<br>
for
<br>
<input type="text" class="memo" id="memo" autocomplete="off">
<input type="text" class="memo" id="memo" placeholder="message" autocomplete="off">
</p>
<button id="send-button">
Send
Send
</button>
<p id="onchain" class="note">
<a href="#" id="get-new-address" class="onchain">Prefer onchain Bitcoin? Click here!</a>
</p>
<div id="lnme-wrapper" class="lnme-wrapper" style="display:none">
<div class="lnme-qrcode"></div>
<div class="lnme-details">
@ -128,6 +135,15 @@
<script src="/lnme/lnme.js" id="lnme-script"></script>
<script>
document.getElementById("get-new-address").addEventListener('click', function(e) {
e.preventDefault();
var lnme = new LnMe({});
lnme.newAddress().then(address => {
document.getElementById("onchain").innerHTML = address;
});
});
var urlParams = new URLSearchParams(window.location.search);
if (urlParams.get('amount')) {
document.getElementById('amount').value = urlParams.get('amount');
@ -137,11 +153,17 @@
}
document.getElementById('send-button').addEventListener('click', function(e) {
e.preventDefault();
e.target.setAttribute('disabled', true);
document.getElementById('loader').style.display = 'block';
var amount = document.getElementById('amount').value;
var memo = document.getElementById('memo').value;
var lnme = new LnMe({ value: amount, memo: memo, target: document.getElementById('lnme-wrapper') });
var amountElement = document.getElementById('amount');
var memoElement = document.getElementById('memo');
e.target.setAttribute('disabled', true);
amountElement.setAttribute('disabled', true);
memoElement.setAttribute('disabled', true);
var lnme = new LnMe({ value: amountElement.value, memo: memoElement.value, target: document.getElementById('lnme-wrapper') });
lnme.showPaymentRequest = function() {
document.getElementById('send-button').style.display = 'none';
document.getElementById('lnme-wrapper').style.display = 'block';

View File

@ -50,6 +50,18 @@ func (c LNDclient) AddInvoice(value int64, memo string) (Invoice, error) {
return result, nil
}
func (c LNDclient) NewAddress() (string, error) {
stdOutLogger.Printf("Getting a new BTC address")
request := lnrpc.NewAddressRequest{
Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH,
}
res, err := c.lndClient.NewAddress(c.ctx, &request)
if err != nil {
return "", err
}
return res.Address, nil
}
// GetInvoice takes an invoice ID and returns the invoice details including settlement details
// An error is returned if no corresponding invoice was found.
func (c LNDclient) GetInvoice(paymentHashStr string) (Invoice, error) {

View File

@ -102,6 +102,15 @@ func main() {
return c.JSON(http.StatusOK, invoice)
})
e.POST("/v1/newaddress", func(c echo.Context) error {
address, err := lnClient.NewAddress()
if err != nil {
stdOutLogger.Printf("Error getting a new BTC address: %s", err)
return c.JSON(http.StatusInternalServerError, "Error getting address")
}
return c.JSON(http.StatusOK, address)
})
e.GET("/v1/invoice/:invoiceId", func(c echo.Context) error {
invoiceId := c.Param("invoiceId")
invoice, err := lnClient.GetInvoice(invoiceId)