-
-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zebedee send attachment #1742
Open
riccardobl
wants to merge
6
commits into
stackernews:master
Choose a base branch
from
riccardobl:zbd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Zebedee send attachment #1742
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d0c49a
zbd
riccardobl af43fab
Merge branch 'master' into zbd
riccardobl 2b5c7bf
Merge branch 'master' into zbd
huumn 0949059
zbd send only attachment
riccardobl f5761d1
Merge branch 'master' into zbd
riccardobl ab6676c
make code more explicit
riccardobl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 24 additions & 0 deletions
24
prisma/migrations/20241219120508_zebedee_attachment/migration.sql
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,24 @@ | ||
-- AlterEnum | ||
ALTER TYPE "WalletType" ADD VALUE 'ZEBEDEE'; | ||
|
||
-- CreateTable | ||
CREATE TABLE "WalletZebedee" ( | ||
"id" SERIAL NOT NULL, | ||
"walletId" INTEGER NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"gamerTagId" TEXT, | ||
|
||
CONSTRAINT "WalletZebedee_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "WalletZebedee_walletId_key" ON "WalletZebedee"("walletId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "WalletZebedee" ADD CONSTRAINT "WalletZebedee_walletId_fkey" FOREIGN KEY ("walletId") REFERENCES "Wallet"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- Update wallet json | ||
CREATE TRIGGER wallet_zebedee_as_jsonb | ||
AFTER INSERT OR UPDATE ON "WalletZebedee" | ||
FOR EACH ROW EXECUTE PROCEDURE wallet_wallet_type_as_jsonb(); |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,63 @@ | ||
import { API_URL, PREIMAGE_AWAIT_TIMEOUT_MS } from '@/wallets/zebedee' | ||
import { assertContentTypeJson } from '@/lib/url' | ||
import { callWithTimeout } from '@/lib/time' | ||
import { fetchWithTimeout } from '@/lib/fetch' | ||
|
||
export * from '@/wallets/zebedee' | ||
|
||
export async function testSendPayment ({ apiKey }, { signal }) { | ||
const wallet = await apiCall('wallet', { apiKey, method: 'GET' }, { signal }) | ||
if (!wallet.data) throw new Error('wallet not found') | ||
} | ||
|
||
export async function sendPayment (bolt11, { apiKey }, { signal }) { | ||
const res = await apiCall('payments', { body: { invoice: bolt11 }, apiKey }, { signal }) | ||
const { id, preimage } = res?.data | ||
if (preimage) return preimage | ||
// the api might return before the invoice is paid, so we'll wait for the preimage | ||
return await waitForPreimage(id, { apiKey }, { signal }) | ||
} | ||
|
||
async function waitForPreimage (id, { apiKey }, { signal }) { | ||
return await callWithTimeout(async () => { | ||
let preimage | ||
while (true) { | ||
const res = await apiCall('payments/{id}', { body: { id }, apiKey, method: 'GET' }, { signal }) | ||
preimage = res?.data?.preimage | ||
if (preimage) break | ||
await new Promise(resolve => setTimeout(resolve, 10)) | ||
} | ||
return preimage | ||
}, PREIMAGE_AWAIT_TIMEOUT_MS) | ||
} | ||
|
||
export async function apiCall (api, { body, apiKey, method = 'POST' }, { signal }) { | ||
const headers = { | ||
apikey: apiKey, | ||
'Content-Type': 'application/json' | ||
} | ||
if (method === 'GET' && body) { | ||
for (const [k, v] of Object.entries(body)) { | ||
api = api.replace('{' + k + '}', v) | ||
} | ||
} | ||
const res = await fetchWithTimeout(API_URL + api, { | ||
method, | ||
headers, | ||
signal, | ||
body: method === 'POST' ? JSON.stringify(body) : undefined | ||
}) | ||
// https://zbd.dev/api-reference/errors | ||
if (res.status !== 200) { | ||
let error | ||
try { | ||
assertContentTypeJson(res) | ||
const json = await res.json() | ||
if (json?.message) error = json.message | ||
} catch (e) { | ||
error = res.statusText || 'error ' + res.status | ||
} | ||
throw new Error(error) | ||
} | ||
return res.json() | ||
} |
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,42 @@ | ||
import { string } from '@/lib/yup' | ||
|
||
export const PREIMAGE_AWAIT_TIMEOUT_MS = 1_200 | ||
export const STATIC_CHARGE_URL = 'https://api.zebedee.io/v0/process-static-charges/' | ||
export const DASHBOARD_URL = 'https://dashboard.zebedee.io/' | ||
export const GAMER_TAG_LNADDR_BASEURL = 'https://zbd.gg/.well-known/lnurlp/' | ||
export const API_URL = 'https://api.zebedee.io/v0/' | ||
export const ZEBEDEE_LNDOMAIN = 'zbd.gg' | ||
|
||
export const name = 'zebedee' | ||
export const walletType = 'ZEBEDEE' | ||
export const walletField = 'walletZebedee' | ||
|
||
export const fields = [ | ||
{ | ||
name: 'apiKey', | ||
label: 'api key', | ||
type: 'password', | ||
optional: 'for sending', | ||
help: `you can get an API key from [Zebedee Dashboard](${DASHBOARD_URL}) from \n\`Project->API->Live\``, | ||
clientOnly: true, | ||
requiredWithout: 'gamerTagId', | ||
validate: string() | ||
}, | ||
{ | ||
name: 'gamerTagId', | ||
label: 'gamer tag or id', | ||
type: 'text', | ||
optional: 'for receiving', | ||
help: `you can find your Gamertag in the [Zebedee Dashboard](${DASHBOARD_URL}) under \n\`Account->Gamertag\`\n section, or in the Zebedee app on the Wallet card.\nNote: You can also use your \`@${ZEBEDEE_LNDOMAIN}\` Lightning address here.`, | ||
serverOnly: true, | ||
requiredWithout: 'apiKey', | ||
validate: string() | ||
} | ||
] | ||
|
||
export const card = { | ||
title: 'Zebedee', | ||
subtitle: 'use [Zebedee](https://zebedee.io) for payments', | ||
image: { src: '/wallets/zbd.svg' } | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect we could do a little better for validation.
Same for the gamer tag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see the zebedee api key today is an alphanumeric 32 characters long string, however there is nothing in the doc that implies this is and always be its shape, so i just added a reasonable min and max length.
Are you happy with that?