Skip to content

Commit

Permalink
refactor: use nextjs framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Burtey committed Sep 17, 2023
1 parent db15975 commit 1eb6936
Show file tree
Hide file tree
Showing 39 changed files with 620 additions and 321 deletions.
1 change: 1 addition & 0 deletions apps/boltcard-galoy/.next/server/app-paths-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
6 changes: 6 additions & 0 deletions apps/boltcard-galoy/.next/server/middleware-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sortedMiddleware": [],
"middleware": {},
"functions": {},
"version": 2
}
1 change: 1 addition & 0 deletions apps/boltcard-galoy/.next/server/pages-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
self.__RSC_SERVER_MANIFEST="{\n \"node\": {},\n \"edge\": {}\n}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"node": {},
"edge": {}
}
1 change: 1 addition & 0 deletions apps/boltcard-galoy/.next/types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type": "module"}
3 changes: 3 additions & 0 deletions apps/boltcard/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
35 changes: 35 additions & 0 deletions apps/boltcard/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
34 changes: 34 additions & 0 deletions apps/boltcard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
3 changes: 1 addition & 2 deletions apps/boltcard/TODO.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
- rate limits
- proper link of .gql (instead of copy)
- Tilt setup
- next-apisation?
- Tilt setup
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import express from "express"

import { gql, GraphQLClient } from "graphql-request"

import { boltcardRouter } from "./router"
import { fetchByCardId, fetchByK1 } from "./knex"
import { apiUrl } from "./config"
import { NextRequest, NextResponse } from "next/server"

import { fetchByCardId, fetchByK1 } from "../../knex"
import { coreUrl } from "../../config"

type GetUsdWalletIdQuery = {
readonly __typename: "Query"
Expand Down Expand Up @@ -71,28 +70,26 @@ const lnInvoicePaymentSendMutation = gql`
}
`

boltcardRouter.get("/callback", async (req: express.Request, res: express.Response) => {
const k1 = req?.query?.k1
const pr = req?.query?.pr

console.log({ k1, pr })
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url)
const k1 = searchParams.get("k1")
const pr = searchParams.get("pr")

if (!k1 || !pr) {
res.status(400).send({ status: "ERROR", reason: "missing k1 or pr" })
return
}

if (typeof k1 !== "string" || typeof pr !== "string") {
res.status(400).send({ status: "ERROR", reason: "invalid k1 or pr" })
return
return NextResponse.json(
{ status: "ERROR", reason: "missing k1 or pr" },
{ status: 400 },
)
}

const payment = await fetchByK1(k1)
console.log({ payment, k1 })

const { cardId } = payment

const card = await fetchByCardId(cardId)

const graphQLClient = new GraphQLClient(apiUrl, {
const graphQLClient = new GraphQLClient(coreUrl, {
headers: {
authorization: `Bearer ${card.token}`,
},
Expand All @@ -103,15 +100,19 @@ boltcardRouter.get("/callback", async (req: express.Request, res: express.Respon
data = await graphQLClient.request<GetUsdWalletIdQuery>(getUsdWalletIdQuery)
} catch (error) {
console.error(error)
res.status(400).send({ status: "ERROR", reason: "issue fetching walletId" })
return
return NextResponse.json(
{ status: "ERROR", reason: "issue fetching walletId" },
{ status: 400 },
)
}

const wallets = data.me?.defaultAccount.wallets

if (!wallets) {
res.status(400).send({ status: "ERROR", reason: "no wallets found" })
return
return NextResponse.json(
{ status: "ERROR", reason: "no wallets found" },
{ status: 400 },
)
}

const usdWallet = wallets.find((wallet) => wallet.walletCurrency === "USD")
Expand All @@ -120,8 +121,10 @@ boltcardRouter.get("/callback", async (req: express.Request, res: express.Respon
console.log({ usdWallet, wallets })

if (!usdWalletId) {
res.status(400).send({ status: "ERROR", reason: "no usd wallet found" })
return
return NextResponse.json(
{ status: "ERROR", reason: "no usd wallet found" },
{ status: 400 },
)
}

let result: LnInvoicePaymentSendMutation
Expand All @@ -132,19 +135,21 @@ boltcardRouter.get("/callback", async (req: express.Request, res: express.Respon
})
} catch (error) {
console.error(error)
res.status(400).send({ status: "ERROR", reason: "payment failed" })
return
return NextResponse.json(
{ status: "ERROR", reason: "payment failed" },
{ status: 400 },
)
}

if (result.lnInvoicePaymentSend.status === "SUCCESS") {
res.json({ status: "OK" })
return NextResponse.json({ status: "OK" })
} else {
res.status(400).send({
status: "ERROR",
reason: `payment failed: ${result.lnInvoicePaymentSend.errors[0].message}`,
})
return NextResponse.json(
{
status: "ERROR",
reason: `payment failed: ${result.lnInvoicePaymentSend.errors[0].message}`,
},
{ status: 400 },
)
}
})

const callback = "dummy"
export { callback }
}
63 changes: 63 additions & 0 deletions apps/boltcard/app/api/createboltcard/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { randomBytes } from "crypto"

import { NextRequest, NextResponse } from "next/server"

import { serverUrl } from "../../config"
import { createCardInit } from "../../knex"

function randomHex(): string {
try {
const bytes: Buffer = randomBytes(16)
return bytes.toString("hex")
} catch (error) {
if (error instanceof Error) {
console.warn(error.message)
throw error
}
}
}

export async function GET(req: NextRequest) {
// should be pass with POST? not sure if this would be compatible
// with the wallet that can create cards

const { searchParams } = new URL(req.url)
const token = searchParams.get("token")

if (!token) {
return NextResponse.json(
{ status: "ERROR", reason: "token missing" },
{ status: 400 },
)
}

// TODO: token validation

const oneTimeCode = randomHex()
const k0AuthKey = randomHex()
const k2CmacKey = randomHex()
const k3 = randomHex()
const k4 = randomHex()

const result = await createCardInit({
oneTimeCode,
k0AuthKey,
k2CmacKey,
k3,
k4,
token,
})

if (result instanceof Error) {
return NextResponse.json(
{ status: "ERROR", reason: "impossible to create card" },
{ status: 400 },
)
}

const url = `${serverUrl}/new?a=${oneTimeCode}`
return NextResponse.json({
status: "OK",
url,
})
}
5 changes: 5 additions & 0 deletions apps/boltcard/app/api/health/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextRequest, NextResponse } from "next/server"

export async function GET(req: NextRequest) {
return NextResponse.json({ status: "OK" })
}
Loading

0 comments on commit 1eb6936

Please sign in to comment.