Skip to content

Commit

Permalink
chore: create cardId earlier in the flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Burtey committed Sep 23, 2023
1 parent ca9554e commit 2c39b34
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 45 deletions.
2 changes: 1 addition & 1 deletion apps/boltcard/app/api/activate/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function GET(req: NextRequest) {
warning: warningReusedCode,
protocol_name: "create_bolt_card_response",
protocol_version: 2,
card_name: "",
card_name: cardKeysSetup.cardId,
lnurlw_base: lnurlwBase,
k0: cardKeysSetup.k0AuthKey,
k1: k1DecryptKey,
Expand Down
41 changes: 41 additions & 0 deletions apps/boltcard/app/api/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,44 @@ import { serverUrl } from "@/services/config"

const randomHex = (): string => randomBytes(16).toString("hex")

function generateReadableCode(numDigits: number, separator: number = 4): string {
const allowedNumbers = ["3", "4", "6", "7", "9"]
const allowedLetters = [
"A",
"C",
"D",
"E",
"F",
"G",
"H",
"J",
"K",
"M",
"N",
"P",
"Q",
"R",
"T",
"U",
"V",
"W",
"X",
"Y",
]

const allowedChars = [...allowedNumbers, ...allowedLetters]
let code = ""
for (let i = 0; i < numDigits; i++) {
if (i > 0 && i % separator === 0) {
code += "_"
}
const randomIndex = Math.floor(Math.random() * allowedChars.length)
code += allowedChars[randomIndex]
}

return code
}

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
Expand All @@ -29,13 +67,16 @@ export async function GET(req: NextRequest) {
const k3 = randomHex()
const k4 = randomHex()

const cardId = generateReadableCode(12)

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

if (result instanceof Error) {
Expand Down
40 changes: 1 addition & 39 deletions apps/boltcard/app/api/ln/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,6 @@ gql`
}
`

function generateReadableCode(numDigits: number, separator: number = 4): string {
const allowedNumbers = ["3", "4", "6", "7", "9"]
const allowedLetters = [
"A",
"C",
"D",
"E",
"F",
"G",
"H",
"J",
"K",
"M",
"N",
"P",
"Q",
"R",
"T",
"U",
"V",
"W",
"X",
"Y",
]

const allowedChars = [...allowedNumbers, ...allowedLetters]
let code = ""
for (let i = 0; i < numDigits; i++) {
if (i > 0 && i % separator === 0) {
code += "_"
}
const randomIndex = Math.floor(Math.random() * allowedChars.length)
code += allowedChars[randomIndex]
}

return code
}

function generateSecureRandomString(length: number): string {
return randomBytes(Math.ceil(length / 2))
.toString("hex")
Expand Down Expand Up @@ -208,7 +170,7 @@ const setupCard = async ({
)
}

const id = generateReadableCode(12)
const id = cardKeysSetup.cardId
const username = `card_${id}`
console.log({ id, username }, "activate card id")

Expand Down
4 changes: 2 additions & 2 deletions apps/boltcard/app/card/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ export default async function Card({ params }: { params: { id: string } }) {
<section className="my-4">
<h2>Wipe Card:</h2>
<p>
<strong>Warning:</strong> This will wipe the card and remove all funds. This
action cannot be undone.
<strong>Warning:</strong> This will wipe the card and reset it to factory
settings. Any remaining funds will be unaccessible.
</p>
<p>
<Image
Expand Down
9 changes: 8 additions & 1 deletion apps/boltcard/bats/e2e-test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ random_phone() {
CALLBACK_API_URL=$(echo $RESPONSE | jq -r '.apiActivationUrl')
CALLBACK_UI_URL=$(echo $RESPONSE | jq -r '.uiActivationUrl')

echo "RESPONSE: $RESPONSE"
echo "CALLBACK_API_URL: $CALLBACK_API_URL"
echo "CALLBACK_UI_URL: $CALLBACK_UI_URL"

[[ $(echo $CALLBACK_API_URL) != "null" ]] || exit 1
[[ $(echo $CALLBACK_UI_URL) != "null" ]] || exit 1

# TODO: test CALLBACK_UI_URL

# Making the follow-up curl request
Expand Down Expand Up @@ -61,7 +68,7 @@ random_phone() {
cardId=$(curl -s http://localhost:3000/api/card/uid/${uid} | jq -r '.id')
cache_value "cardId" "$cardId"

amount="0.01"
amount="0.001"
token_name=$(read_value "alice")

bitcoin_cli sendtoaddress "$address" "$amount"
Expand Down
4 changes: 3 additions & 1 deletion apps/boltcard/services/db/card-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ export interface CardKeysSetupInput {
k3: string
k4: string
token: string
cardId: string
}

export async function createCardKeysSetup(cardData: CardKeysSetupInput) {
try {
const { oneTimeCode, k0AuthKey, k2CmacKey, k3, k4, token } = cardData
const { oneTimeCode, k0AuthKey, k2CmacKey, k3, k4, token, cardId } = cardData

const result = await knex("CardKeysSetup").insert({
oneTimeCode,
Expand All @@ -20,6 +21,7 @@ export async function createCardKeysSetup(cardData: CardKeysSetupInput) {
k3,
k4,
token,
cardId,
})

return result
Expand Down
5 changes: 4 additions & 1 deletion apps/boltcard/services/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ async function createTables() {
if (!hasCardKeysSetupTable) {
await knex.schema.createTable("CardKeysSetup", (table) => {
table.string("oneTimeCode").notNullable().index().unique()

table.timestamp("created_at").defaultTo(knex.fn.now())

table.string("status").defaultTo("init") // init, fetched, used
table.string("token").notNullable()

table.string("cardId").notNullable().unique()

table.string("k0AuthKey").notNullable()
table.string("k2CmacKey").notNullable().index() // .unique() enforcing uniqueness would ensure there is no reusage of keys
table.string("k2CmacKey").notNullable().unique()
table.string("k3").notNullable()
table.string("k4").notNullable()
})
Expand Down

0 comments on commit 2c39b34

Please sign in to comment.