/
uploadToIPFS.ts
55 lines (49 loc) · 1.35 KB
/
uploadToIPFS.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import RNFetchBlob from 'rn-fetch-blob';
import getCid from './getCid';
import Config from 'react-native-config';
const UPLOAD_ENDPOINT = 'https://api.nft.storage/upload';
export default async function uploadToIPFS(
imagePath: string,
name: string,
description: string,
) {
// Convert image into raw bytes
const imageBytesInBase64: string = await RNFetchBlob.fs.readFile(
imagePath,
'base64',
);
const bytes = Buffer.from(imageBytesInBase64, 'base64');
const cid = await getCid(bytes);
const headers = {
Accept: 'application/json',
Authorization: `Bearer ${Config.NFT_STORAGE_API_KEY}`,
};
// Upload the image to IPFS
const imageUpload = fetch(UPLOAD_ENDPOINT, {
method: 'POST',
headers: {
...headers,
'Content-Type': 'image/jpg',
},
body: bytes,
});
// Upload the NFT metadata according the Metaplex Metadata specification,
// including pre-computed cid of the image
const metadataUpload = fetch(UPLOAD_ENDPOINT, {
method: 'POST',
headers: {
...headers,
'Content-Type': 'application/json',
},
body: JSON.stringify({
name,
description,
image: `https://ipfs.io/ipfs/${cid}`,
}),
});
// Fire off both uploads aysnc
return Promise.all([
imageUpload.then(response => response.json()),
metadataUpload.then(response => response.json()),
]);
}