Compare commits
24
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61c2b898fa | ||
|
|
8d28a3de35 | ||
|
|
e8e5ddb0b3 | ||
|
|
70cb0a1251 | ||
|
|
303e6d8c41 | ||
|
|
46f7942022 | ||
|
|
c7adbc7689 | ||
|
|
c702aa2268 | ||
|
|
31d29df021 | ||
|
|
1599fc9704 | ||
|
|
0ecbb8d58c | ||
|
|
2642965052 | ||
|
|
e46470b127 | ||
|
|
7a8cf08f47 | ||
|
|
c03e68666d | ||
|
|
95aa2a0c92 | ||
|
|
77c0652e8a | ||
|
|
f6bb0ca47f | ||
|
|
4b4e518609 | ||
|
|
d2a63c07a3 | ||
|
|
5199e4c8c6 | ||
|
|
1167392140 | ||
|
|
6c06a4972e | ||
|
|
666eecf207 |
@@ -5,6 +5,7 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="utf-8" />
|
||||
<meta property="og:title" content="Pay to {{.Username}}@ln.tips">
|
||||
<meta property="og:site_name" content="{{.Username}}@ln.tips">
|
||||
<meta property="og:description" content="Send bitcoin to {{.Username}} over Lightning. Get your own Lightning address at https://ln.tips 👈">
|
||||
<meta property="og:type" content="article" />
|
||||
<meta property="og:image" content="{{.Image}}">
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package lnbits
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/tidwall/gjson"
|
||||
"time"
|
||||
|
||||
"github.com/imroc/req"
|
||||
@@ -40,8 +43,8 @@ func (c *Client) GetUser(userId string) (user User, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// CreateUserWithInitialWallet creates new user with initial wallet
|
||||
func (c *Client) CreateUserWithInitialWallet(userName, walletName, adminId string, email string) (wal User, err error) {
|
||||
// CreateUserWithWallet creates new user with wallet
|
||||
func (c *Client) CreateUserWithWallet(userName, walletName, adminId string, email string) (user User, err error) {
|
||||
resp, err := req.Post(c.url+"/usermanager/api/v1/users", c.header, req.BodyJSON(struct {
|
||||
WalletName string `json:"wallet_name"`
|
||||
AdminId string `json:"admin_id"`
|
||||
@@ -58,7 +61,14 @@ func (c *Client) CreateUserWithInitialWallet(userName, walletName, adminId strin
|
||||
err = reqErr
|
||||
return
|
||||
}
|
||||
err = resp.ToJSON(&wal)
|
||||
err = resp.ToJSON(&user)
|
||||
res := gjson.Get(resp.String(), "wallets.0")
|
||||
if res.Index == 0 {
|
||||
err = fmt.Errorf("could not create initial wallet")
|
||||
return
|
||||
}
|
||||
user.Wallet = &Wallet{}
|
||||
err = json.Unmarshal([]byte(res.Raw), user.Wallet)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,11 @@ type Client struct {
|
||||
InvoiceKey string
|
||||
}
|
||||
|
||||
// User is a bot user containing lnbits and telegram information.
|
||||
// This struct is used for orm persistence.
|
||||
// Users are created, by talking to the bot or by receiving funds (tips, receive, faucet)
|
||||
// The initialized flag is set true, if user already talked to the bot.
|
||||
// This is used for the '<- talk to this bot' message when sending tips to new users.
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name" gorm:"primaryKey"`
|
||||
@@ -122,8 +127,8 @@ type Wallet struct {
|
||||
type Payments []struct {
|
||||
CheckingID string `json:"checking_id"`
|
||||
Pending bool `json:"pending"`
|
||||
Amount int `json:"amount"`
|
||||
Fee int `json:"fee"`
|
||||
Amount int64 `json:"amount"`
|
||||
Fee int64 `json:"fee"`
|
||||
Memo string `json:"memo"`
|
||||
Time int `json:"time"`
|
||||
Bolt11 string `json:"bolt11"`
|
||||
|
||||
@@ -74,9 +74,9 @@ func newTelegramBot() *tb.Bot {
|
||||
// todo -- may want to derive user wallets from this specific bot wallet (master wallet), since lnbits usermanager extension is able to do that.
|
||||
func (bot TipBot) initBotWallet() error {
|
||||
botWalletInitialisation.Do(func() {
|
||||
_, err := bot.initWallet(bot.Telegram.Me)
|
||||
_, err := bot.userWithInitWallet(bot.Telegram.Me)
|
||||
if err != nil {
|
||||
log.Errorln(fmt.Sprintf("[initBotWallet] Could not initialize bot wallet: %s", err.Error()))
|
||||
log.Errorln(fmt.Sprintf("[initBotWallet] ❌ 🧨 ❌ Could not initialize bot wallet: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
@@ -3,6 +3,7 @@ package telegram
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept"
|
||||
|
||||
tb "gopkg.in/lightningtipbot/telebot.v3"
|
||||
@@ -60,11 +61,11 @@ func (bot TipBot) makeAdvancedHelpMessage(ctx context.Context, m *tb.Message) st
|
||||
// we print the anonymous ln address in the advanced help
|
||||
lnaddr, err := bot.UserGetAnonLightningAddress(fromUser)
|
||||
if err == nil {
|
||||
dynamicHelpMessage = dynamicHelpMessage + fmt.Sprintf("Anonymous lightning address: `%s`\n", lnaddr)
|
||||
dynamicHelpMessage = dynamicHelpMessage + fmt.Sprintf("Anonymous Lightning address: `%s`\n", lnaddr)
|
||||
}
|
||||
lnurl, err := UserGetLNURL(fromUser)
|
||||
lnurl, err := UserGetAnonLNURL(fromUser)
|
||||
if err == nil {
|
||||
dynamicHelpMessage = dynamicHelpMessage + fmt.Sprintf("LNURL: `%s`", lnurl)
|
||||
dynamicHelpMessage = dynamicHelpMessage + fmt.Sprintf("Anonymous LNURL: `%s`", lnurl)
|
||||
}
|
||||
|
||||
// this is so stupid:
|
||||
|
||||
@@ -133,7 +133,6 @@ func (bot *TipBot) UserGetAnonLightningAddress(user *lnbits.User) (string, error
|
||||
func UserGetLNURL(user *lnbits.User) (string, error) {
|
||||
name := fmt.Sprint(user.UUID)
|
||||
callback := fmt.Sprintf("%s/.well-known/lnurlp/%s", internal.Configuration.Bot.LNURLHostName, name)
|
||||
log.Debugf("[lnurlReceiveHandler] %s's LNURL: %s", GetUserStr(user.Telegram), callback)
|
||||
|
||||
lnurlEncode, err := lnurl.LNURLEncode(callback)
|
||||
if err != nil {
|
||||
@@ -142,6 +141,15 @@ func UserGetLNURL(user *lnbits.User) (string, error) {
|
||||
return lnurlEncode, nil
|
||||
}
|
||||
|
||||
func UserGetAnonLNURL(user *lnbits.User) (string, error) {
|
||||
callback := fmt.Sprintf("%s/.well-known/lnurlp/%s", internal.Configuration.Bot.LNURLHostName, fmt.Sprint(user.AnonIDSha256))
|
||||
lnurlEncode, err := lnurl.LNURLEncode(callback)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return lnurlEncode, nil
|
||||
}
|
||||
|
||||
// lnurlReceiveHandler outputs the LNURL of the user
|
||||
func (bot TipBot) lnurlReceiveHandler(ctx intercept.Context) (intercept.Context, error) {
|
||||
m := ctx.Message()
|
||||
|
||||
@@ -23,7 +23,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
registerNodeMessage = "📖 You did not register a node yet.\n\nCurrently supported backends: `lnd` and `lnbits`\nTo register a node, type: `/node add <Type> <Node_info>`\n*LND:* `/node add lnd <Host> <Macaroon> <Cert>`\n*LNbits:* `/node add lnbits <Host> <Key>`\n\nFor security reasons, you should *only use an invoice macaroon* for LND and an *invoice key* for LNbits."
|
||||
registerNodeMessage = "📖 Connect your Lightning node with your wallet.\n\nCurrently supported backends: `lnd` and `lnbits`\nTo register a node, type: `/node add <type> <info>`\n\n*LND (REST):* `/node add lnd <host> <macaroon> <cert>`\n*LNbits:* `/node add lnbits <host> <key>`\n\nℹ️ Always use `https://` for the `<host>`, even if you use a Tor node. Certificates and macaroons need to be in base64 format.\n\n⚠️ For security reasons, you should *only use an invoice macaroon* for LND and an *invoice key* for LNbits."
|
||||
nodeHelpMessage = "⚙️ *Commands:*\n`/node add <type> <info>` ✅ Add your node.\n`/node invoice <admount>` ⤵️ Fetch an invoice from your node.\n`/node proxy <amount>` 🔀 Proxy a payment to your node (privacy feature).\n`/node help` 📖 Show help."
|
||||
checkingInvoiceMessage = "⏳ Checking invoice on your node..."
|
||||
invoiceNotSettledMessage = "❌ Invoice has not settled yet."
|
||||
checkInvoiceButtonMessage = "🔄 Check invoice"
|
||||
@@ -49,29 +50,34 @@ func parseUserSettingInput(ctx intercept.Context, m *tb.Message) (satdress.Backe
|
||||
if splitlen < 4 {
|
||||
return params, fmt.Errorf("not enough arguments")
|
||||
}
|
||||
if strings.ToLower(splits[2]) == "lnd" {
|
||||
if splitlen < 6 || splitlen > 7 {
|
||||
return params, fmt.Errorf("wrong format. Use <Type> <Host> <Macaroon> <Cert>")
|
||||
switch strings.ToLower(splits[2]) {
|
||||
case "lnd":
|
||||
if splitlen < 5 || splitlen > 7 {
|
||||
return params, fmt.Errorf("wrong format. Use <Type> <Host> <Macaroon> [<Cert>]")
|
||||
}
|
||||
host := splits[3]
|
||||
macaroon := splits[4]
|
||||
cert := splits[5]
|
||||
var pem []byte
|
||||
if splitlen == 6 {
|
||||
cert := splits[5]
|
||||
pem := parseCertificateToPem(cert)
|
||||
if len(pem) < 1 {
|
||||
return params, fmt.Errorf("certificate has invalid format")
|
||||
}
|
||||
}
|
||||
|
||||
hostsplit := strings.Split(host, ".")
|
||||
if len(hostsplit) == 0 {
|
||||
return params, fmt.Errorf("host has wrong format")
|
||||
}
|
||||
pem := parseCertificateToPem(cert)
|
||||
if len(pem) < 1 {
|
||||
return params, fmt.Errorf("certificate has invalid format")
|
||||
}
|
||||
|
||||
return satdress.LNDParams{
|
||||
Cert: pem,
|
||||
Host: host,
|
||||
Macaroon: macaroon,
|
||||
CertString: string(pem),
|
||||
}, nil
|
||||
} else if strings.ToLower(splits[2]) == "lnbits" {
|
||||
case "lnbits":
|
||||
if splitlen < 5 || splitlen > 6 {
|
||||
return params, fmt.Errorf("wrong format. Use <Type> <Host> <Key>")
|
||||
}
|
||||
@@ -87,8 +93,9 @@ func parseUserSettingInput(ctx intercept.Context, m *tb.Message) (satdress.Backe
|
||||
Host: host,
|
||||
Key: key,
|
||||
}, nil
|
||||
default:
|
||||
return params, fmt.Errorf("unknown backend type. Supported types: `lnd`, `lnbits`")
|
||||
}
|
||||
return params, fmt.Errorf("unknown backend type. Supported types: `lnd`, `lnbits`")
|
||||
}
|
||||
|
||||
func nodeInfoString(node *lnbits.NodeSettings) (string, error) {
|
||||
@@ -110,6 +117,11 @@ func nodeInfoString(node *lnbits.NodeSettings) (string, error) {
|
||||
return fmt.Sprintf("ℹ️ *Your node information.*\n\n%s", node_info_str_filled), nil
|
||||
}
|
||||
|
||||
func (bot *TipBot) nodeHelpHandler(ctx intercept.Context) (intercept.Context, error) {
|
||||
bot.trySendMessage(ctx.Message().Sender, registerNodeMessage+"\n\n"+nodeHelpMessage)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (bot *TipBot) getNodeHandler(ctx intercept.Context) (intercept.Context, error) {
|
||||
m := ctx.Message()
|
||||
user, err := GetLnbitsUserWithSettings(m.Sender, *bot)
|
||||
@@ -119,14 +131,14 @@ func (bot *TipBot) getNodeHandler(ctx intercept.Context) (intercept.Context, err
|
||||
}
|
||||
|
||||
if user.Settings == nil {
|
||||
bot.trySendMessage(m.Sender, registerNodeMessage)
|
||||
bot.trySendMessage(m.Sender, registerNodeMessage+"\n\n"+nodeHelpMessage)
|
||||
return ctx, fmt.Errorf("no node registered")
|
||||
}
|
||||
|
||||
node_info_str, err := nodeInfoString(&user.Settings.Node)
|
||||
if err != nil {
|
||||
log.Infof("Could not get node info for user %s", GetUserStr(user.Telegram))
|
||||
bot.trySendMessage(m.Sender, registerNodeMessage)
|
||||
bot.trySendMessage(m.Sender, registerNodeMessage+"\n\n"+nodeHelpMessage)
|
||||
return ctx, err
|
||||
}
|
||||
bot.trySendMessage(m.Sender, node_info_str)
|
||||
@@ -140,17 +152,17 @@ func (bot *TipBot) nodeHandler(ctx intercept.Context) (intercept.Context, error)
|
||||
if len(splits) == 1 {
|
||||
return bot.getNodeHandler(ctx)
|
||||
} else if len(splits) > 1 {
|
||||
if splits[1] == "invoice" {
|
||||
switch strings.ToLower(splits[1]) {
|
||||
case "invoice":
|
||||
return bot.invHandler(ctx)
|
||||
}
|
||||
if splits[1] == "add" {
|
||||
case "add":
|
||||
return bot.registerNodeHandler(ctx)
|
||||
}
|
||||
if splits[1] == "check" {
|
||||
case "check":
|
||||
return bot.satdressCheckInvoiceHandler(ctx)
|
||||
}
|
||||
if splits[1] == "proxy" {
|
||||
case "proxy":
|
||||
return bot.satdressProxyHandler(ctx)
|
||||
case "help":
|
||||
return bot.nodeHelpHandler(ctx)
|
||||
}
|
||||
}
|
||||
return ctx, nil
|
||||
@@ -216,10 +228,12 @@ func (bot *TipBot) registerNodeHandler(ctx intercept.Context) (intercept.Context
|
||||
node_info_str, err := nodeInfoString(&user.Settings.Node)
|
||||
if err != nil {
|
||||
log.Infof("Could not get node info for user %s", GetUserStr(user.Telegram))
|
||||
bot.trySendMessage(m.Sender, registerNodeMessage)
|
||||
bot.trySendMessage(m.Sender, registerNodeMessage+"\n\n"+nodeHelpMessage)
|
||||
return ctx, err
|
||||
}
|
||||
bot.tryEditMessage(check_message, fmt.Sprintf("%s\n\n%s", node_info_str, nodeAddedMessage))
|
||||
|
||||
log.Infof("[node:add] Added node of user %s backend %s", GetUserStr(user.Telegram), user.Settings.Node.NodeType)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
@@ -242,10 +256,13 @@ func (bot *TipBot) invHandler(ctx intercept.Context) (intercept.Context, error)
|
||||
}
|
||||
}
|
||||
|
||||
log.Infof("[node:invoice] Getting invoice for user %s backend %s", GetUserStr(user.Telegram), user.Settings.Node.NodeType)
|
||||
|
||||
check_message := bot.trySendMessageEditable(user.Telegram, routingInvoiceMessage)
|
||||
var getInvoiceParams satdress.CheckInvoiceParams
|
||||
|
||||
if user.Settings.Node.NodeType == "lnd" {
|
||||
switch user.Settings.Node.NodeType {
|
||||
case "lnd":
|
||||
// get invoice from user's node
|
||||
getInvoiceParams, err = satdress.MakeInvoice(
|
||||
satdress.Params{
|
||||
@@ -258,7 +275,7 @@ func (bot *TipBot) invHandler(ctx intercept.Context) (intercept.Context, error)
|
||||
Description: fmt.Sprintf("Invoice by %s", GetUserStr(bot.Telegram.Me)),
|
||||
},
|
||||
)
|
||||
} else if user.Settings.Node.NodeType == "lnbits" {
|
||||
case "lnbits":
|
||||
// get invoice from user's node
|
||||
getInvoiceParams, err = satdress.MakeInvoice(
|
||||
satdress.Params{
|
||||
@@ -270,6 +287,8 @@ func (bot *TipBot) invHandler(ctx intercept.Context) (intercept.Context, error)
|
||||
Description: fmt.Sprintf("Invoice by %s", GetUserStr(bot.Telegram.Me)),
|
||||
},
|
||||
)
|
||||
default:
|
||||
return ctx, fmt.Errorf("unknown node type %s", user.Settings.Node.NodeType)
|
||||
}
|
||||
if err != nil {
|
||||
log.Errorln(err.Error())
|
||||
@@ -292,6 +311,8 @@ func (bot *TipBot) invHandler(ctx intercept.Context) (intercept.Context, error)
|
||||
// add the getInvoiceParams to cache to check it later
|
||||
bot.Cache.Set(fmt.Sprintf("invoice:%d", user.Telegram.ID), getInvoiceParams, &store.Options{Expiration: 24 * time.Hour})
|
||||
|
||||
log.Infof("[node:invoice] Invoice created for user %s backend %s", GetUserStr(user.Telegram), user.Settings.Node.NodeType)
|
||||
|
||||
// check if invoice settles
|
||||
return bot.satdressCheckInvoiceHandler(ctx)
|
||||
}
|
||||
@@ -427,6 +448,8 @@ func (bot *TipBot) satdressProxyHandler(ctx intercept.Context) (intercept.Contex
|
||||
return ctx, err
|
||||
}
|
||||
bot.trySendMessage(m.Sender, &tb.Photo{File: tb.File{FileReader: bytes.NewReader(qr)}, Caption: fmt.Sprintf("`%s`", invoice.PaymentRequest)})
|
||||
|
||||
log.Infof("[node] Proxy payment for user %s backend %s", GetUserStr(user.Telegram), user.Settings.Node.NodeType)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
@@ -439,6 +462,8 @@ func (bot *TipBot) satdressProxyRelayPaymentHandler(event Event) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Infof("[node:proxy] Relaying payment for user %s backend %s", GetUserStr(user.Telegram), user.Settings.Node.NodeType)
|
||||
|
||||
bot.notifyInvoiceReceivedEvent(invoiceEvent)
|
||||
|
||||
// now relay the payment to the user's node
|
||||
@@ -481,6 +506,8 @@ func (bot *TipBot) satdressProxyRelayPaymentHandler(event Event) {
|
||||
|
||||
// bot.trySendMessage(user.Telegram, fmt.Sprintf("PR: `%s`\n\nHash: `%s`\n\nStatus: `%s`", getInvoiceParams.PR, string(getInvoiceParams.Hash), getInvoiceParams.Status))
|
||||
|
||||
log.Infof("[node:proxy] Retrieved invoice for payment of user %s backend %s. Paying...", GetUserStr(user.Telegram), user.Settings.Node.NodeType)
|
||||
|
||||
// pay invoice
|
||||
invoice, err := user.Wallet.Pay(lnbits.PaymentParams{Out: true, Bolt11: getInvoiceParams.PR}, bot.Client)
|
||||
if err != nil {
|
||||
@@ -521,6 +548,8 @@ func (bot *TipBot) satdressProxyRelayPaymentHandler(event Event) {
|
||||
return
|
||||
}
|
||||
bot.tryEditMessage(check_message, invoiceRoutedMessage)
|
||||
|
||||
log.Infof("[node:proxy] Proxy paid for user %s backend %s.", GetUserStr(user.Telegram), user.Settings.Node.NodeType)
|
||||
// bot.trySendMessage(user.Telegram, fmt.Sprintf("PR: `%s`\n\nHash: `%s`\n\nStatus: `%s`", getInvoiceParams.PR, string(getInvoiceParams.Hash), getInvoiceParams.Status))
|
||||
|
||||
return
|
||||
|
||||
+40
-60
@@ -4,20 +4,16 @@ import (
|
||||
"context"
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/errors"
|
||||
|
||||
"github.com/LightningTipBot/LightningTipBot/internal"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/errors"
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/lnbits"
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/str"
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept"
|
||||
log "github.com/sirupsen/logrus"
|
||||
tb "gopkg.in/lightningtipbot/telebot.v3"
|
||||
"gorm.io/gorm"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (bot TipBot) startHandler(ctx intercept.Context) (intercept.Context, error) {
|
||||
@@ -29,7 +25,7 @@ func (bot TipBot) startHandler(ctx intercept.Context) (intercept.Context, error)
|
||||
// bot.helpHandler(m)
|
||||
log.Printf("[⭐️ /start] New user: %s (%d)\n", GetUserStr(ctx.Sender()), ctx.Sender().ID)
|
||||
walletCreationMsg := bot.trySendMessageEditable(ctx.Sender(), Translate(ctx, "startSettingWalletMessage"))
|
||||
user, err := bot.initWallet(ctx.Sender())
|
||||
user, err := bot.userWithInitWallet(ctx.Sender())
|
||||
if err != nil {
|
||||
log.Errorln(fmt.Sprintf("[startHandler] Error with initWallet: %s", err.Error()))
|
||||
bot.tryEditMessage(walletCreationMsg, Translate(ctx, "startWalletErrorMessage"))
|
||||
@@ -48,74 +44,58 @@ func (bot TipBot) startHandler(ctx intercept.Context) (intercept.Context, error)
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (bot TipBot) initWallet(tguser *tb.User) (*lnbits.User, error) {
|
||||
// userWithInitWallet will ensure that lnbits.User is initialized.
|
||||
// Initialized users have already talked to the bot,
|
||||
// therefor they are able to receive messages from the bot.
|
||||
func (bot TipBot) userWithInitWallet(tguser *tb.User) (*lnbits.User, error) {
|
||||
user, err := GetUser(tguser, bot)
|
||||
if stderrors.Is(err, gorm.ErrRecordNotFound) {
|
||||
user = &lnbits.User{Telegram: tguser}
|
||||
err = bot.createWallet(user)
|
||||
if err != nil {
|
||||
return user, err
|
||||
if err != nil {
|
||||
if stderrors.Is(err, gorm.ErrRecordNotFound) {
|
||||
user, err = bot.createWallet(tguser)
|
||||
if err != nil {
|
||||
return user, err
|
||||
}
|
||||
user.Initialized = true
|
||||
err = UpdateUserRecord(user, bot)
|
||||
if err != nil {
|
||||
return user, fmt.Errorf("[initWallet] error updating user: %v", err)
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
// set user initialized
|
||||
user, err := GetUser(tguser, bot)
|
||||
user.Initialized = true
|
||||
err = UpdateUserRecord(user, bot)
|
||||
if err != nil {
|
||||
log.Errorln(fmt.Sprintf("[initWallet] error updating user: %s", err.Error()))
|
||||
return user, err
|
||||
}
|
||||
} else if !user.Initialized {
|
||||
return nil, err
|
||||
}
|
||||
if !user.Initialized {
|
||||
// update all tip tooltips (with the "initialize me" message) that this user might have received before
|
||||
tipTooltipInitializedHandler(user.Telegram, bot)
|
||||
user.Initialized = true
|
||||
err = UpdateUserRecord(user, bot)
|
||||
if err != nil {
|
||||
log.Errorln(fmt.Sprintf("[initWallet] error updating user: %s", err.Error()))
|
||||
return user, err
|
||||
return user, fmt.Errorf("[initWallet] error updating user: %v", err)
|
||||
}
|
||||
} else if user.Initialized {
|
||||
// wallet is already initialized
|
||||
return user, nil
|
||||
} else {
|
||||
err = fmt.Errorf("could not initialize wallet")
|
||||
return user, err
|
||||
}
|
||||
// wallet is already initialized
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (bot TipBot) createWallet(user *lnbits.User) error {
|
||||
UserStr := GetUserStr(user.Telegram)
|
||||
u, err := bot.Client.CreateUserWithInitialWallet(strconv.FormatInt(user.Telegram.ID, 10),
|
||||
fmt.Sprintf("%d (%s)", user.Telegram.ID, UserStr),
|
||||
// createWallet will create a wallet for any telegram user.
|
||||
func (bot TipBot) createWallet(u *tb.User) (*lnbits.User, error) {
|
||||
userStr := GetUserStr(u)
|
||||
user, err := bot.Client.CreateUserWithWallet(strconv.FormatInt(u.ID, 10),
|
||||
fmt.Sprintf("%d (%s)", u.ID, userStr),
|
||||
internal.Configuration.Lnbits.AdminId,
|
||||
UserStr)
|
||||
userStr)
|
||||
if err != nil {
|
||||
errormsg := fmt.Sprintf("[createWallet] Create wallet error: %s", err.Error())
|
||||
log.Errorln(errormsg)
|
||||
return err
|
||||
return nil, fmt.Errorf("[createWallet] Create wallet error: %v", err)
|
||||
}
|
||||
user.Wallet = &lnbits.Wallet{}
|
||||
user.ID = u.ID
|
||||
user.Name = u.Name
|
||||
wallet, err := bot.Client.Wallets(*user)
|
||||
if err != nil {
|
||||
errormsg := fmt.Sprintf("[createWallet] Get wallet error: %s", err.Error())
|
||||
log.Errorln(errormsg)
|
||||
return err
|
||||
}
|
||||
user.Wallet = &wallet[0]
|
||||
|
||||
user.Telegram = u
|
||||
user.AnonID = fmt.Sprint(str.Int32Hash(user.ID))
|
||||
user.AnonIDSha256 = str.AnonIdSha256(user)
|
||||
user.UUID = str.UUIDSha256(user)
|
||||
user.AnonIDSha256 = str.AnonIdSha256(&user)
|
||||
user.UUID = str.UUIDSha256(&user)
|
||||
|
||||
user.Initialized = false
|
||||
user.CreatedAt = time.Now()
|
||||
err = UpdateUserRecord(user, bot)
|
||||
err = UpdateUserRecord(&user, bot)
|
||||
if err != nil {
|
||||
errormsg := fmt.Sprintf("[createWallet] Update user record error: %s", err.Error())
|
||||
log.Errorln(errormsg)
|
||||
return err
|
||||
return nil, fmt.Errorf("[createWallet] Update user record error: %v", err)
|
||||
}
|
||||
return nil
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
@@ -100,20 +100,14 @@ func (bot *TipBot) CreateWalletForTelegramUser(tbUser *tb.User) (*lnbits.User, e
|
||||
if _, exists := bot.UserExists(tbUser); exists {
|
||||
return nil, fmt.Errorf("user already exists")
|
||||
}
|
||||
user := &lnbits.User{Telegram: tbUser}
|
||||
userStr := GetUserStr(tbUser)
|
||||
log.Printf("[CreateWalletForTelegramUser] Creating wallet for user %s ... ", userStr)
|
||||
err := bot.createWallet(user)
|
||||
user, err := bot.createWallet(tbUser)
|
||||
if err != nil {
|
||||
errmsg := fmt.Sprintf("[CreateWalletForTelegramUser] Error: Could not create wallet for user %s", userStr)
|
||||
log.Errorln(errmsg)
|
||||
return user, err
|
||||
}
|
||||
// todo: remove this. we're doing this already in bot.createWallet().
|
||||
err = UpdateUserRecord(user, *bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("[CreateWalletForTelegramUser] Wallet created for user %s. ", userStr)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user