Skip to content
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

Use nostr build apiv2 #440

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/js/components/buttons/Upload.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from 'react';

import Key from '@/nostr/Key';
import { uploadFile } from '@/utils/uploadFile';

const Upload = (props) => {
Expand All @@ -18,6 +19,7 @@ const Upload = (props) => {
(errorMsg) => {
setError(errorMsg);
},
Key,
);
}
};
Expand Down
45 changes: 36 additions & 9 deletions src/js/components/create/CreateNoteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,47 @@ function CreateNoteForm({
const handleFileAttachments = useCallback((files) => {
if (!files) return;

const uploadApiUrl = 'https://nostr.build/api/v2/upload/files';

for (let i = 0; i < files.length; i++) {
const file = files[i];

const formData = new FormData();
formData.append('fileToUpload', file);

fetch('https://nostr.build/api/upload/iris.php', {
method: 'POST',
body: formData,
})
const authEvent = {
kind: 27235,
created_at: Math.floor(Date.now() / 1000),
content: '',
tags: [
['u', uploadApiUrl],
['method', 'POST'],
],
pubkey: Key.getPubKey(),
sig: '',
id: '',
};

Key.sign(authEvent)
.then((signature) => {
authEvent.sig = signature;
const authHeader = `Nostr ${btoa(JSON.stringify(authEvent))}`;

const formData = new FormData();
formData.append('file', file);

return fetch(uploadApiUrl, {
method: 'POST',
body: formData,
headers: {
Authorization: authHeader,
},
});
})
.then(async (response) => {
const url = await response.json();
if (url) {
const uploadResult = await response.json();
if (response.status === 200 && uploadResult?.status === 'success') {
const url = uploadResult.data[0].url;
setText((prevText) => (prevText ? `${prevText}\n\n${url}` : url));
} else {
console.error('Upload failed', uploadResult);
}
})
.catch((error) => {
Expand Down
1 change: 1 addition & 0 deletions src/js/components/create/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const TextArea: React.FC<TextAreaProps> = ({
blob,
(url) => setValue(value ? `${value}\n\n${url}` : url),
(errorMsg) => console.error(errorMsg),
Key,
);
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/js/nostr/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { bech32 } from 'bech32';
import {
Event,
generatePrivateKey,
getEventHash,
getPublicKey,
getSignature,
nip04,
signEvent,
UnsignedEvent,
} from 'nostr-tools';

Expand Down Expand Up @@ -124,8 +125,15 @@ export default {
},
sign: async function (event: Event | UnsignedEvent): Promise<string> {
const priv = this.getPrivKey();
if ('id' in event) {
event.id = getEventHash(event);
} else {
const hash = getEventHash(event);
(event as Event).id = hash;
}
if (priv) {
return signEvent(event, priv);
(event as Event).sig = getSignature(event, priv);
return JSON.stringify(event);
} else if (window.nostr) {
return new Promise((resolve) => {
this.processWindowNostr({ op: 'sign', data: event, callback: resolve });
Expand Down
44 changes: 35 additions & 9 deletions src/js/utils/uploadFile.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
export const uploadFile = (file, onUrlCallback, onErrorCallback) => {
const formData = new FormData();
formData.append('fileToUpload', file);
export const uploadFile = (file, onUrlCallback, onErrorCallback, key) => {
const uploadApiUrl = 'https://nostr.build/api/v2/upload/files';

fetch('https://nostr.build/api/upload/iris.php', {
method: 'POST',
body: formData,
})
const authEvent = {
kind: 27235,
created_at: Math.floor(Date.now() / 1000),
content: '',
tags: [
['u', uploadApiUrl],
['method', 'POST'],
],
pubkey: key.getPubKey(), // assuming 'key' is an instance of your Key class or has a similar interface
};

key
.sign(authEvent) // assuming 'key' is an instance of your Key class or has a similar interface
.then((signature) => {
(authEvent as any).sig = signature;
const authHeader = `Nostr ${btoa(JSON.stringify(authEvent))}`;

const formData = new FormData();
formData.append('fileToUpload', file);

return fetch(uploadApiUrl, {
method: 'POST',
body: formData,
headers: {
Authorization: authHeader,
},
});
})
.then(async (response) => {
const url = await response.json();
if (url && onUrlCallback) {
const uploadResult = await response.json();
if (response.status === 200 && uploadResult?.status === 'success' && onUrlCallback) {
const url = uploadResult.data[0].url;
onUrlCallback(url);
} else {
throw new Error('Upload failed: ' + JSON.stringify(uploadResult));
}
})
.catch((error) => {
Expand Down