Skip to content

Commit

Permalink
feat: provide username when creating user
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jan 1, 2025
1 parent c67ff73 commit 35959e4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ App Connections have a 10 sat / 1% reserve to account for possible routing fees.

`POST /api/wallets`

body (optional):

```json
{
"username": "Bob"
}
```

returns:

```json
Expand Down
10 changes: 7 additions & 3 deletions app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export async function hasPassword() {
}

export async function createWallet(
password: string | undefined
request: { username: string | undefined } | undefined,
servicePassword: string | undefined
): Promise<{ wallet: Wallet | undefined; error: string | undefined }> {
try {
if (process.env.PASSWORD) {
if (password !== process.env.PASSWORD) {
if (servicePassword !== process.env.PASSWORD) {
return { wallet: undefined, error: "wrong password" };
}
}
Expand Down Expand Up @@ -114,7 +115,10 @@ export async function createWallet(
}
appId = appInfo.id;

const { username } = await saveConnectionSecret(newApp.pairingUri);
const { username } = await saveConnectionSecret(
request?.username,
newApp.pairingUri
);

const domain = getDomain();
const lightningAddress = username + "@" + domain;
Expand Down
19 changes: 17 additions & 2 deletions app/api/wallets/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@ import { createWallet } from "@/app/actions";
export async function POST(request: Request) {
const credentials = request.headers.get("Authorization")?.split(" ")?.[1];

const password = credentials
const servicePassword = credentials
? Buffer.from(credentials, "base64").toString().split(":")?.[1]
: undefined;

const { wallet, error } = await createWallet(password);
let createWalletRequest: { username: string | undefined } = {
username: undefined,
};
try {
if (request.body) {
const body = await request.json();
createWalletRequest.username = body.username;
}
} catch (error) {
console.error("Failed to parse request body", error);
}

const { wallet, error } = await createWallet(
createWalletRequest,
servicePassword
);

if (!wallet) {
return new Response(error, {
Expand Down
3 changes: 2 additions & 1 deletion app/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { getPublicKey } from "nostr-tools";
const prisma = new PrismaClient();

export async function saveConnectionSecret(
username: string | undefined,
connectionSecret: string
): Promise<{ username: string }> {
const parsed = nwc.NWCClient.parseWalletConnectUrl(connectionSecret);
if (!parsed.secret) {
throw new Error("no secret found in connection secret");
}
const pubkey = getPublicKey(parsed.secret);
const username = pubkey.substring(0, 6);
username = username || pubkey.substring(0, 6);

const result = await prisma.connectionSecret.create({
data: {
Expand Down

0 comments on commit 35959e4

Please sign in to comment.