Skip to content

Commit 1174ba2

Browse files
chore(wrapped-keys-lit-actions): LIT-3920 - Add sync-actions-to-ipfs script that publishes current lit actions to IPFS and prints out common + by-network CID repository entries for wrapped-keys on completion
1 parent 3cf7e5e commit 1174ba2

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const axios = require('axios');
2+
const FormData = require('form-data');
3+
4+
const {
5+
litActionRepository,
6+
litActionRepositoryCommon,
7+
} = require('./dist/src/index');
8+
9+
const JWT = process.env.LIT_IPFS_JWT || '';
10+
if (!JWT) {
11+
throw new Error('Missing Pinata IPFS JWT in LIT_IPFS_JWT env variable');
12+
}
13+
14+
async function pinFileToIPFS(actionName, code) {
15+
const formData = new FormData();
16+
formData.append('file', Buffer.from(code), { filename: actionName + '.js' });
17+
18+
formData.append(
19+
'pinataMetadata',
20+
JSON.stringify({
21+
name: 'File name',
22+
})
23+
);
24+
25+
formData.append(
26+
'pinataOptions',
27+
JSON.stringify({
28+
cidVersion: 0,
29+
})
30+
);
31+
32+
const res = await axios.post(
33+
'https://api.pinata.cloud/pinning/pinFileToIPFS',
34+
formData,
35+
{
36+
maxBodyLength: 'Infinity',
37+
headers: {
38+
'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
39+
Authorization: `Bearer ${JWT}`,
40+
},
41+
}
42+
);
43+
44+
console.log(actionName, res.data.IpfsHash);
45+
return res.data.IpfsHash;
46+
}
47+
48+
async function getCidRepository(codeRepository) {
49+
const cidRepository = {};
50+
51+
await Promise.all(
52+
Object.entries(codeRepository).map(([actionName, byNetworkObj]) => {
53+
cidRepository[actionName] = {};
54+
55+
return Promise.all(
56+
Object.entries(byNetworkObj).map(async ([networkName, codeStr]) => {
57+
console.log('setting', actionName, networkName, codeStr.length);
58+
59+
cidRepository[actionName][networkName] = await pinFileToIPFS(
60+
actionName,
61+
codeStr
62+
);
63+
})
64+
);
65+
})
66+
);
67+
68+
return cidRepository;
69+
}
70+
71+
async function getCidRepositoryCommon(codeRepository) {
72+
const cidRepository = {};
73+
74+
await Promise.all(
75+
Object.entries(codeRepository).map(async ([actionName, codeStr]) => {
76+
console.log('setting common', actionName, codeStr.length);
77+
cidRepository[actionName] = await pinFileToIPFS(actionName, codeStr);
78+
})
79+
);
80+
81+
return cidRepository;
82+
}
83+
84+
async function gogo() {
85+
const [cidRepoCommon, cidRepo] = await Promise.all([
86+
getCidRepositoryCommon(litActionRepositoryCommon),
87+
getCidRepository(litActionRepository),
88+
]);
89+
90+
console.log('common', cidRepoCommon);
91+
console.log('byNetwork', cidRepo);
92+
}
93+
94+
gogo();

0 commit comments

Comments
 (0)