Skip to content

Commit

Permalink
chore(core): address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Dec 18, 2023
1 parent be3e4b5 commit 250c0be
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 106 deletions.
2 changes: 1 addition & 1 deletion apps/pay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev -p 3002",
"start": "next start",
"start": "next start -p 3002",
"build": "next build",
"lint:fix": "eslint --fix --ext .ts,.tsx .",
"codegen": "graphql-codegen --config codegen.yml",
Expand Down
6 changes: 4 additions & 2 deletions core/api/src/domain/bitcoin/lnurl/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DomainError } from "@/domain/shared"
import { DomainError, ErrorLevel } from "@/domain/shared"

export class LnurlError extends DomainError {}

export class ErrorFetchingLnurlInvoice extends LnurlError {}
export class UnknownLnurlError extends LnurlError {
level = ErrorLevel.Critical
}
3 changes: 2 additions & 1 deletion core/api/src/domain/bitcoin/lnurl/index.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type LnurlError = import("@/domain/bitcoin/lnurl/errors").LnurlError
interface ILnurlPayService {
fetchInvoiceFromLnAddressOrLnurl(args: {
amount: BtcPaymentAmount
lnAddressOrLnurl: string
}): Promise<string | ApplicationError>
}): Promise<string | LnurlError>
}
3 changes: 2 additions & 1 deletion core/api/src/graphql/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ export const mapError = (error: ApplicationError): CustomGraphQLError => {
case "InvalidPaginatedQueryArgsError":
message = error.message
return new ValidationInternalError({ message, logger: baseLogger })
case "LnurlError":
case "ErrorFetchingLnurlInvoice":
message = error.message
return new LnurlRequestInvoiceError({ message, logger: baseLogger })
Expand Down Expand Up @@ -661,6 +660,7 @@ export const mapError = (error: ApplicationError): CustomGraphQLError => {
case "ExpectedAddressInfoMissingInEventError":
case "MissingCreatedAtKratosError":
case "MissingExpiredAtKratosError":
case "LnurlError":
case "InvalidIdentitySessionKratosError":
case "MissingTotpKratosError":
case "IncompatibleSchemaUpgradeError":
Expand Down Expand Up @@ -730,6 +730,7 @@ export const mapError = (error: ApplicationError): CustomGraphQLError => {
case "UnknownOnChainServiceError":
case "UnknownNotificationsServiceError":
case "UnknownIpFetcherServiceError":
case "UnknownLnurlError":
case "UnknownCacheServiceError":
case "UnknownPhoneProviderServiceError":
case "UnknownDealerPriceServiceError":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { GT } from "@/graphql/index"
import WalletId from "@/graphql/shared/types/scalar/wallet-id"
import SatAmount from "@/graphql/shared/types/scalar/sat-amount"
import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map"
import LnAddress from "@/graphql/shared/types/scalar/ln-address"

const LnAddressPaymentSendInput = GT.Input({
name: "LnAddressPaymentSendInput",
Expand All @@ -19,7 +18,7 @@ const LnAddressPaymentSendInput = GT.Input({
},
amount: { type: GT.NonNull(SatAmount), description: "Amount in satoshis." },
lnAddress: {
type: GT.NonNull(LnAddress),
type: GT.NonNull(GT.String),
description: "Lightning address to send to.",
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { GT } from "@/graphql/index"
import WalletId from "@/graphql/shared/types/scalar/wallet-id"
import SatAmount from "@/graphql/shared/types/scalar/sat-amount"
import { mapAndParseErrorForGqlResponse } from "@/graphql/error-map"
import Lnurl from "@/graphql/shared/types/scalar/lnurl"

const LnurlPaymentSendInput = GT.Input({
name: "LnurlPaymentSendInput",
Expand All @@ -19,7 +18,7 @@ const LnurlPaymentSendInput = GT.Input({
},
amount: { type: GT.NonNull(SatAmount), description: "Amount in satoshis." },
lnurl: {
type: GT.NonNull(Lnurl),
type: GT.NonNull(GT.String),
description: "Lnurl string to send to.",
},
}),
Expand Down
10 changes: 2 additions & 8 deletions core/api/src/graphql/public/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,12 @@ enum InvoicePaymentStatus {

scalar Language

"""LNURL Lightning address"""
scalar LnAddress

input LnAddressPaymentSendInput {
"""Amount in satoshis."""
amount: SatAmount!

"""Lightning address to send to."""
lnAddress: LnAddress!
lnAddress: String!

"""Wallet ID to send bitcoin from."""
walletId: WalletId!
Expand Down Expand Up @@ -757,15 +754,12 @@ input LnUsdInvoiceFeeProbeInput {
walletId: WalletId!
}

"""Lnurl string"""
scalar Lnurl

input LnurlPaymentSendInput {
"""Amount in satoshis."""
amount: SatAmount!

"""Lnurl string to send to."""
lnurl: Lnurl!
lnurl: String!

"""Wallet ID to send bitcoin from."""
walletId: WalletId!
Expand Down
26 changes: 0 additions & 26 deletions core/api/src/graphql/shared/types/scalar/ln-address.ts

This file was deleted.

26 changes: 0 additions & 26 deletions core/api/src/graphql/shared/types/scalar/lnurl.ts

This file was deleted.

10 changes: 7 additions & 3 deletions core/api/src/services/lnurl-pay/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { utils, requestInvoice } from "lnurl-pay"

import { toSats } from "@/domain/bitcoin"
import { ErrorFetchingLnurlInvoice } from "@/domain/bitcoin/lnurl/errors"
import {
ErrorFetchingLnurlInvoice,
LnurlError,
UnknownLnurlError,
} from "@/domain/bitcoin/lnurl/errors"

export const LnurlPayService = (): ILnurlPayService => {
const fetchInvoiceFromLnAddressOrLnurl = async ({
Expand All @@ -10,7 +14,7 @@ export const LnurlPayService = (): ILnurlPayService => {
}: {
amount: BtcPaymentAmount
lnAddressOrLnurl: string
}): Promise<string | ApplicationError> => {
}): Promise<string | LnurlError> => {
try {
const invoice = await requestInvoice({
lnUrlOrAddress: lnAddressOrLnurl,
Expand All @@ -28,7 +32,7 @@ export const LnurlPayService = (): ILnurlPayService => {
if (err instanceof Error) {
return new ErrorFetchingLnurlInvoice(err.message)
}
return new ErrorFetchingLnurlInvoice("Unknown error fetching LnUrl invoice")
return new UnknownLnurlError(err)
}
}

Expand Down
53 changes: 29 additions & 24 deletions core/api/test/integration/services/lnurl-pay.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
import { decodeInvoice } from "@/domain/bitcoin/lightning"
import { ErrorFetchingLnurlInvoice } from "@/domain/bitcoin/lnurl/errors"
import { LnurlPayService } from "@/services/lnurl-pay"
import { createRandomUserAndWallets } from "test/helpers"
import { setUsername, usernameAvailable } from "@/app/accounts"
import { checkedToUsername } from "@/domain/accounts"

const usernameForLnAddress = checkedToUsername("lnurl_test_user")
const lnurlForUsername =
"lnurl1dp68gup69uhkcmmrv9kxsmmnwsarxvpsxghjuam9d3kz66mwdamkutmvde6hymrs9akxuatjd30hgetnw30h2um9wg3wjc63" // http://localhost:3002/.well-known/lnurlp/lnurl_test_user
if (usernameForLnAddress instanceof Error) {
throw usernameForLnAddress
}

beforeAll(async () => {
const isUsernameAvailable = await usernameAvailable(usernameForLnAddress)
if (isUsernameAvailable instanceof Error) {
throw isUsernameAvailable
}
if (isUsernameAvailable) {
const newUser = await createRandomUserAndWallets()
const setUsernameRes = await setUsername({
accountId: newUser.btcWalletDescriptor.accountId,
username: usernameForLnAddress,
})

if (setUsernameRes instanceof Error) {
throw setUsernameRes
}
}
})

describe("LnurlPayService", () => {
const lnurlPayService = LnurlPayService()
Expand All @@ -12,30 +40,7 @@ describe("LnurlPayService", () => {
amount: BigInt(1000),
currency: "BTC",
} as BtcPaymentAmount,
lnAddressOrLnurl:
"lnurl1dp68gurn8ghj7urp0yhxymrfde4juumk9uh8wetvdskkkmn0wahz7mrww4excup0w3jhxaqlq07tq", // https://pay.blink.sv/.well-known/lnurlp/test
})

if (invoice instanceof Error) {
throw invoice
}

const decodedInvoice = await decodeInvoice(invoice)

if (decodedInvoice instanceof Error) {
throw decodedInvoice
}

expect(decodedInvoice.amount).toEqual(1000)
})

it("fetches an invoice from an ln address", async () => {
const invoice = await lnurlPayService.fetchInvoiceFromLnAddressOrLnurl({
amount: {
amount: BigInt(1000),
currency: "BTC",
} as BtcPaymentAmount,
lnAddressOrLnurl: "[email protected]",
lnAddressOrLnurl: lnurlForUsername,
})

if (invoice instanceof Error) {
Expand Down
12 changes: 2 additions & 10 deletions dev/config/apollo-federation/supergraph.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -723,18 +723,14 @@ enum link__Purpose {
EXECUTION
}

"""LNURL Lightning address"""
scalar LnAddress
@join__type(graph: PUBLIC)

input LnAddressPaymentSendInput
@join__type(graph: PUBLIC)
{
"""Amount in satoshis."""
amount: SatAmount!

"""Lightning address to send to."""
lnAddress: LnAddress!
lnAddress: String!

"""Wallet ID to send bitcoin from."""
walletId: WalletId!
Expand Down Expand Up @@ -946,18 +942,14 @@ type LnUpdate
walletId: WalletId! @deprecated(reason: "Deprecated in favor of transaction")
}

"""Lnurl string"""
scalar Lnurl
@join__type(graph: PUBLIC)

input LnurlPaymentSendInput
@join__type(graph: PUBLIC)
{
"""Amount in satoshis."""
amount: SatAmount!

"""Lnurl string to send to."""
lnurl: Lnurl!
lnurl: String!

"""Wallet ID to send bitcoin from."""
walletId: WalletId!
Expand Down

0 comments on commit 250c0be

Please sign in to comment.