Transform nsec1 better

This commit is contained in:
Alex Gleason 2023-03-04 22:54:57 -06:00
parent 48f04e48de
commit 73884c212f
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 2 additions and 5 deletions

View File

@ -3,18 +3,15 @@ import converter from 'npm:bech32-converting';
import { validator, z } from '@/deps.ts'; import { validator, z } from '@/deps.ts';
const createTokenSchema = z.object({ const createTokenSchema = z.object({
password: z.string(), password: z.string().transform((v) => v.startsWith('nsec1') ? converter('nsec').toHex(v).slice(2) : v),
}); });
const createTokenController = validator('json', (value, c) => { const createTokenController = validator('json', (value, c) => {
const result = createTokenSchema.safeParse(value); const result = createTokenSchema.safeParse(value);
if (result.success) { if (result.success) {
const password = result.data.password;
const token = password.startsWith('nsec1') ? converter('nsec').toHex(password).slice(2) : password;
return c.json({ return c.json({
access_token: token, access_token: result.data.password,
token_type: 'Bearer', token_type: 'Bearer',
scope: 'read write follow push', scope: 'read write follow push',
created_at: Math.floor(new Date().getTime() / 1000), created_at: Math.floor(new Date().getTime() / 1000),