-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
212 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
declare global { | ||
// var prisma: PrismaClient; | ||
var prisma: PrismaClient | undefined; | ||
} | ||
|
||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- CreateTable | ||
CREATE TABLE "Token" ( | ||
"id" TEXT NOT NULL, | ||
"token" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"redeemedAt" TIMESTAMP(3), | ||
|
||
CONSTRAINT "Token_pkey" PRIMARY KEY ("id") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import prisma from './prisma'; | ||
|
||
export const createTokenInDb = async (token: string, txid: string) => { | ||
return await prisma.token.create({ | ||
data: { | ||
token, | ||
id: txid, | ||
}, | ||
}); | ||
}; | ||
|
||
export const findTokenByTxId = async (txid: string) => { | ||
return await prisma.token.findUnique({ | ||
where: { | ||
id: txid, | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import { GetTokenResponse } from '@/types'; | ||
import { findTokenByTxId } from '@/lib/tokenModels'; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
const { txid } = req.query; | ||
|
||
if (typeof txid !== 'string') { | ||
return res.status(400).json({ message: 'Txid is required as a string' }); | ||
} | ||
|
||
if (req.method === 'GET') { | ||
try { | ||
const token = await findTokenByTxId(txid); | ||
if (!token) { | ||
return res.status(404).json({ message: 'Token not found' }); | ||
} | ||
return res.status(200).json(token as GetTokenResponse); | ||
} catch (error: any) { | ||
return res.status(500).json({ message: error.message }); | ||
} | ||
} else { | ||
res.setHeader('Allow', ['POST']); | ||
res.status(405).end(`Method ${req.method} Not Allowed`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { createTokenInDb } from '@/lib/tokenModels'; | ||
import { PostTokenRequest, PostTokenResponse } from '@/types'; | ||
import { computeTxId, getProofsFromToken } from '@/utils/cashu'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
console.log('req', req.body); | ||
if (req.method === 'POST') { | ||
try { | ||
const { token } = req.body as PostTokenRequest; | ||
|
||
if (!token) { | ||
return res.status(400).json({ message: 'Token is required' }); | ||
} | ||
|
||
console.log('creating token in db', token); | ||
|
||
const proofs = getProofsFromToken(token); | ||
|
||
const txid = computeTxId(proofs); | ||
|
||
await createTokenInDb(token, txid); | ||
|
||
return res.status(200).json({ txid } as PostTokenResponse); | ||
} catch (error: any) { | ||
return res.status(500).json({ message: error.message }); | ||
} | ||
} else { | ||
res.setHeader('Allow', ['POST']); | ||
res.status(405).end(`Method ${req.method} Not Allowed`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.