-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightningAddress.js
146 lines (133 loc) · 6.05 KB
/
lightningAddress.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const config = require("./config.js");
const umbril = require("./umbril");
const responseToTwit = require("./responseToTwit");
const updateTwitterBio = require("./updateTwitterBio");
const supabase = require("./supabase");
const axios = require("axios");
async function lightningAddress(tweet, twitContainURL, userID, twitterUserID, accountAge, userScrenName) {
try {
let twitText = tweet.text;
let checkEmail = validateEmail(twitText);
let validEmail = Object.keys(checkEmail).length >= 1;
if (validEmail) {
console.log("Lightning network email address detected");
let checkUserIDInDatabase = await supabase.QueryTwitterID(twitterUserID);
let checkUserIDinPendingPayout = await supabase.QueryTwitterIDPendingPayout(twitterUserID);
let checkUserIDinPaidPayout = await supabase.QueryTwitterIDPaidPayout(twitterUserID);
let time = Date.now();
console.log(`${new Date(time)} - User: ${userScrenName} User ID: ${twitterUserID} Account Age: ${accountAge} DB: ${checkUserIDInDatabase} URL: ${twitContainURL} `);
// check if there's URL and user ID is false and account older than 60 days
if (checkUserIDInDatabase && config.enableErrorTwit && validEmail) {
//send already PAID
responseToTwit.sendTwit(tweet, "alreadypaid");
}
if (validEmail && !checkUserIDInDatabase && accountAge >= 60) {
console.log("Email Address detected: ", checkEmail.email);
// want to check if we have sent user payment before. Need to store to DB
// need to create user elibility, twiiter account age etc.
try {
let lightningInvoice = await getLNURL(checkEmail.domain, checkEmail.userName, config.defaultPayOut);
if (lightningInvoice.length >= 90 && !checkUserIDinPendingPayout && !checkUserIDinPaidPayout) {
let timerStart = new Date();
//may want to show timer, time to take to pay
console.log("Wallet QR: ", lightningInvoice);
let cleanQRCode = lightningInvoice.replace("lightning:", "");
//send response
let MakePaymentAndReturnFee = await umbril.makePayment(cleanQRCode, config.defaultPayOut, checkUserIDinPendingPayout);
console.log("Fee PAID: ", MakePaymentAndReturnFee.fee);
let timerEnd = new Date() - timerStart;
console.log("Executed in: ", timerEnd);
supabase.InsertPaidTwitterID(twitterUserID, MakePaymentAndReturnFee.sat, MakePaymentAndReturnFee.fee, userScrenName);
console.log("User inserted");
//Update Twitter Bio
updateTwitterBio();
responseToTwit.sendTwit(tweet, "success", {
time: timerEnd,
amountandfee: {
fee: MakePaymentAndReturnFee.fee,
sat: MakePaymentAndReturnFee.sat,
},
});
// PAY OUT TIPPING!
} else if (checkUserIDinPendingPayout && !checkUserIDinPaidPayout && validEmail) {
let timerStart = new Date();
let pendingAmount = await supabase.QueryTwitterPendingPayoutAmount(twitterUserID);
let lightningInvoice = await getLNURL(checkEmail.domain, checkEmail.userName, pendingAmount);
//may want to show timer, time to take to pay
console.log("Pending payout Wallet QR: " + lightningInvoice + " Pending Amount Due: " + pendingAmount);
let cleanQRCode = lightningInvoice.replace("lightning:", "");
//send response
let MakePaymentAndReturnFee = await umbril.makePayment(cleanQRCode, pendingAmount, checkUserIDinPendingPayout);
console.log("Fee PAID: ", MakePaymentAndReturnFee.fee);
let timerEnd = new Date() - timerStart;
console.log("Executed in: ", timerEnd);
supabase.UpdatePendingPayout(twitterUserID, MakePaymentAndReturnFee.fee);
console.log("User inserted");
//Update Twitter Bio
updateTwitterBio();
responseToTwit.sendTwit(tweet, "success", {
time: timerEnd,
amountandfee: {
fee: MakePaymentAndReturnFee.fee,
sat: MakePaymentAndReturnFee.sat,
},
});
}
} catch (err) {
console.log("Unable to read QR Code: ", err);
if (config.enableErrorTwit) {
//Update database
console.log(err);
if (err.includes("routing")) {
supabase.UpdatePendingPayout(twitterUserID, 0);
responseToTwit.sendTwit(tweet, "paidbasic");
} else {
responseToTwit.sendTwit(tweet, "invalidqr", { err: err });
}
}
}
}
}
} catch (err) {
console.log("Error in TweetDetect: ", err);
}
}
function validateEmail(email) {
//let twitText = tweet.text;
var re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
let containsEmail = re.test(email);
if (containsEmail) {
console.log("yes, email found");
const emailAddress = email.match(re);
let domain = emailAddress[0].split("@");
console.log({
email: emailAddress[0],
domain: domain[1],
userName: domain[0],
});
return { email: emailAddress[0], domain: domain[1], userName: domain[0] };
}
return false;
}
async function getLNURL(domain, username, paymentAmount) {
try {
// fetch data from a url endpoint
const response = await axios.get(`https://${domain}/.well-known/lnurlp/${username}`);
const data = await response.data;
//console.log(response)
if (response.status == 200) {
// let callbackURL = await data.callback
let responseCallback = await axios.get(`${data.callback}?amount=${paymentAmount * 1000}`);
let dataCallback = await responseCallback.data;
console.log(dataCallback.pr);
return dataCallback.pr;
}
//
//
//return lnURL;
} catch (error) {
console.log("error", error);
// appropriately handle the error
}
}
module.exports = lightningAddress;