Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Revert "fix: Fix lnurl payment when wallet is usd (#372)" (#378)
Browse files Browse the repository at this point in the history
This reverts commit 156534f.
  • Loading branch information
daviroo authored Dec 20, 2022
1 parent 5467fe8 commit 27227d8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 95 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
124 changes: 34 additions & 90 deletions pages/api/lnurlp/[username].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
`

Expand All @@ -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}`],
Expand All @@ -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({
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 27227d8

Please sign in to comment.