Skip to content

Commit 9752aff

Browse files
feat: add arweave upload service
1 parent d104c61 commit 9752aff

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

packages/sdk/src/config/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export const CHAIN_CONFIG: Record<ChainId, ChainConfig> = {
2727
};
2828

2929
export const DEFAULT_CHAIN_ID = 134;
30+
export const DEFAULT_ARWEAVE_UPLOAD_API = 'http://localhost:3000'; // TODO change this
31+
export const DEFAULT_ARWEAVE_GATEWAY = 'https://arweave.net';
3032
export const DEFAULT_DATA_NAME = '';
3133
export const SCONE_TAG = ['tee', 'scone'];
3234
export const DEFAULT_MAX_PRICE = 0;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {
2+
DEFAULT_ARWEAVE_GATEWAY,
3+
DEFAULT_ARWEAVE_UPLOAD_API,
4+
} from '../config/config.js';
5+
6+
interface AddOptions {
7+
arweaveUploadApi?: string;
8+
arweaveGateway?: string;
9+
}
10+
11+
const add = async (
12+
content: Uint8Array,
13+
{ arweaveGateway, arweaveUploadApi }: AddOptions = {}
14+
): Promise<string> => {
15+
let arweaveId: string;
16+
17+
try {
18+
const payload = new FormData();
19+
payload.append('file', new Blob([content]));
20+
const res = await fetch(
21+
`${arweaveUploadApi || DEFAULT_ARWEAVE_UPLOAD_API}/upload`,
22+
{
23+
method: 'POST',
24+
body: payload,
25+
}
26+
);
27+
if (!res.ok) {
28+
throw Error(`Arweave upload API answered with status ${res.status}`);
29+
}
30+
try {
31+
const data = await res.json();
32+
arweaveId = data?.arweaveId;
33+
if (!arweaveId) {
34+
throw Error('missing arweaveId');
35+
}
36+
} catch (e) {
37+
throw Error('Arweave upload API answered with an invalid response', {
38+
cause: e,
39+
});
40+
}
41+
} catch (e) {
42+
throw Error('Failed to add file on Arweave', { cause: e });
43+
}
44+
const publicUrl = `${arweaveGateway || DEFAULT_ARWEAVE_GATEWAY}/${arweaveId}`;
45+
await fetch(publicUrl)
46+
.then((res) => {
47+
if (!res.ok) {
48+
throw Error(`Arweave gateway answered with status ${res.status}`);
49+
}
50+
})
51+
.catch((e) => {
52+
throw Error(`Failed to load uploaded file at ${publicUrl}`, { cause: e });
53+
});
54+
55+
return arweaveId;
56+
};
57+
58+
export { add };

0 commit comments

Comments
 (0)