|
| 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