-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
64 lines (53 loc) · 1.6 KB
/
main.js
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
56
57
58
59
60
61
62
63
64
const { ethers } = require('ethers')
const fs = require('fs');
const path = require('path');
const { create } = require('ipfs-http-client');
const request = require('request');
const gwEndpoint = 'https://crustipfs.xyz';
const psEndpoint = 'https://pin.crustcode.com'
const filePath = './test.txt';
async function main() {
// 1. Upload the file through GW
const authHeader = await getPolygonAuthHeader();
//console.log(authHeader)
const cid = await uploadToGW(authHeader);
// console.log(cid);
// 2. Pin to Crust through PS
const rst = await pinToCrust(cid, authHeader);
console.log(rst);
}
async function getPolygonAuthHeader() {
const pair = ethers.Wallet.createRandom();
const sig = await pair.signMessage(pair.address);
const authHeaderRaw = `eth-${pair.address}:${sig}`;
const authHeader = Buffer.from(authHeaderRaw).toString('base64')
return authHeader;
}
async function uploadToGW(h) {
// 1. Create IPFS instance
const ipfs = create({
url: `${gwEndpoint}/api/v0`,
headers: {
authorization: `Basic ${h}`
}
})
// 2. Get file content
const fileContent = await fs.readFileSync(path.resolve(__dirname, filePath));
// 3. IPFS add through GW
const { cid } = await ipfs.add(fileContent);
return cid.toV0().toString();
}
async function pinToCrust(cid, h) {
const { body } = request.post({
url: `${psEndpoint}/psa/pins`,
headers: {
authorization: `Bearer ${h}`
},
json: {
cid,
name: 'test.txt'
}
});
return body;
}
main()