Skip to content

Commit 5e4ff09

Browse files
authored
Create pirate.js
1 parent 475acfa commit 5e4ff09

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

App/scripts/pirate.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const IPFS = require('ipfs-api');
2+
const ipfs = IPFS({ host: 'ipfs.infura.io', port: 5001, protocol: 'https' });
3+
4+
async function uploadData(data) {
5+
const buffer = Buffer.from(JSON.stringify(data));
6+
const result = await ipfs.add(buffer);
7+
console.log('Data uploaded to IPFS:', result[0].hash);
8+
return result[0].hash;
9+
}
10+
11+
async function downloadData(hash) {
12+
const files = await ipfs.cat(hash);
13+
const data = JSON.parse(files.toString());
14+
console.log('Data downloaded from IPFS:', data);
15+
return data;
16+
}
17+
18+
// Example usage:
19+
const data = { x: 10, y: 10, vx: 1, vy: 1 };
20+
uploadData(data).then(hash => {
21+
downloadData(hash);
22+
});

0 commit comments

Comments
 (0)