From 27227d84d952c5a51f061f95a4b55c324b6b745d Mon Sep 17 00:00:00 2001 From: daviroo Date: Tue, 20 Dec 2022 08:21:10 +0000 Subject: [PATCH] Revert "fix: Fix lnurl payment when wallet is usd (#372)" (#378) This reverts commit 156534f5226805524ec748b96d8ddb10bfee17ed. --- package.json | 2 +- pages/api/lnurlp/[username].ts | 124 +++++++++------------------------ yarn.lock | 8 +-- 3 files changed, 39 insertions(+), 95 deletions(-) diff --git a/package.json b/package.json index d1132ada..65af0411 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ }, "dependencies": { "@apollo/client": "^3.4.16", - "@galoymoney/client": "^0.1.60", + "@galoymoney/client": "^0.1.42", "bech32": "^2.0.0", "bitcoinjs-lib": "5.0.0", "bolt11": "1.3.4", diff --git a/pages/api/lnurlp/[username].ts b/pages/api/lnurlp/[username].ts index b3e65c94..8f6eee53 100644 --- a/pages/api/lnurlp/[username].ts +++ b/pages/api/lnurlp/[username].ts @@ -34,13 +34,9 @@ const client = new ApolloClient({ cache: new InMemoryCache(), }) -const ACCOUNT_DEFAULT_WALLET = gql` - query accountDefaultWallet($username: Username!) { - accountDefaultWallet(username: $username) { - __typename - id - walletCurrency - } +const USER_WALLET_ID = gql` + query userDefaultWalletId($username: Username!) { + userDefaultWalletId(username: $username) } ` @@ -67,61 +63,32 @@ const LNURL_INVOICE = gql` } ` -const LNURL_USD_INVOICE = gql` - mutation lnUsdInvoiceCreateOnBehalfOfRecipient( - $walletId: WalletId! - $amount: SatAmount! - $descriptionHash: Hex32Bytes! - ) { - mutationData: lnUsdInvoiceCreateOnBehalfOfRecipient( - input: { - recipientWalletId: $walletId - amount: $amount - descriptionHash: $descriptionHash - } - ) { - errors { - message - } - invoice { - paymentRequest - } - } - } -` - export default async function (req: NextApiRequest, res: NextApiResponse) { const { username, amount } = req.query const url = originalUrl(req) - let accountUsername: string - if (username == undefined) { - accountUsername = "" - } else { - accountUsername = username.toString() - } + + console.log({ headers: req.headers }, "request to NextApiRequest") + let walletId - let walletCurrency try { const { data } = await client.query({ - query: ACCOUNT_DEFAULT_WALLET, - variables: { username: accountUsername }, + query: USER_WALLET_ID, + variables: { username }, context: { "x-real-ip": req.headers["x-real-ip"], "x-forwarded-for": req.headers["x-forwarded-for"], }, }) - walletId = data?.accountDefaultWallet?.id ? data?.accountDefaultWallet?.id : "" - walletCurrency = data?.accountDefaultWallet?.walletCurrency - } catch (error) { + + walletId = data.userDefaultWalletId + } catch (err) { return res.json({ status: "ERROR", reason: `Couldn't find user '${username}'.`, }) } - console.log({ headers: req.headers }, "request to NextApiRequest") - const metadata = JSON.stringify([ ["text/plain", `Payment to ${username}`], ["text/identifier", `${username}@${url.hostname}`], @@ -142,55 +109,32 @@ export default async function (req: NextApiRequest, res: NextApiResponse) { try { const descriptionHash = crypto.createHash("sha256").update(metadata).digest("hex") - if (walletCurrency === "BTC") { - const { - data: { - mutationData: { errors, invoice }, - }, - } = await client.mutate({ - mutation: LNURL_INVOICE, - variables: { - walletId, - amount: amountSats, - descriptionHash, - }, - }) - if (errors && errors.length) { - console.log("error getting invoice", errors) - return res.json({ - status: "ERROR", - reason: `Failed to get invoice: ${errors[0].message}`, - }) - } - return res.json({ - pr: invoice.paymentRequest, - routes: [], - }) - } else { - const { - data: { - mutationData: { errors, invoice }, - }, - } = await client.mutate({ - mutation: LNURL_USD_INVOICE, - variables: { - walletId, - amount: amountSats, - descriptionHash, - }, - }) - if (errors && errors.length) { - console.log("error getting invoice", errors) - return res.json({ - status: "ERROR", - reason: `Failed to get invoice: ${errors[0].message}`, - }) - } + + const { + data: { + mutationData: { errors, invoice }, + }, + } = await client.mutate({ + mutation: LNURL_INVOICE, + variables: { + walletId, + amount: amountSats, + descriptionHash, + }, + }) + + if (errors && errors.length) { + console.log("error getting invoice", errors) return res.json({ - pr: invoice.paymentRequest, - routes: [], + status: "ERROR", + reason: `Failed to get invoice: ${errors[0].message}`, }) } + + res.json({ + pr: invoice.paymentRequest, + routes: [], + }) } catch (err: unknown) { console.log("unexpected error getting invoice", err) res.json({ diff --git a/yarn.lock b/yarn.lock index 3a16658b..00acf702 100644 --- a/yarn.lock +++ b/yarn.lock @@ -101,10 +101,10 @@ minimatch "^3.1.2" strip-json-comments "^3.1.1" -"@galoymoney/client@^0.1.60": - version "0.1.60" - resolved "https://registry.yarnpkg.com/@galoymoney/client/-/client-0.1.60.tgz#10a708b2ea53584503f658f4af6f011fae1313e3" - integrity sha512-DOveDf+DmNW+9Ob9ljqcVwWFVTJkNygno8JVzpwlHgB7Ndh/iA2+RcM4kY4r2A+eQslxd0yTSpMrZjCCvcOJmA== +"@galoymoney/client@^0.1.42": + version "0.1.43" + resolved "https://registry.yarnpkg.com/@galoymoney/client/-/client-0.1.43.tgz#76693be7b652d789b529a5f40ffab3da12ce5db6" + integrity sha512-3yyYbNITcXkLSBmBLWoi2BEc8shxNcX0MlvBEav3FHeWhEg6DV9fs2K8z0ORZdcUA5EDbxi3128VYsyS/9qHNg== "@graphql-typed-document-node/core@^3.1.1": version "3.1.1"