diff --git a/README.md b/README.md index 5515cb6e..b364c77e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Lighthouse +# Lighthouse Lighthouse is a permanent decentralized file storage protocol that allows the ability to pay once and store forever. While traditionally, users need to repeatedly keep track and pay for their storage after every fixed amount of time, Lighthouse manages this for them and makes sure that user files are stored forever. The aim is to move users from a rent-based cost model where they are renting their own files on cloud storage to a permanent ownership model. It is built on top of IPFS, Filecoin, and Polygon. It uses the existing miner network and storage capacity of the filecoin network. diff --git a/package-lock.json b/package-lock.json index a354dc79..188c6ac8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@lighthouse-web3/sdk", - "version": "0.4.5", + "version": "0.4.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@lighthouse-web3/sdk", - "version": "0.4.5", + "version": "0.4.6", "license": "MIT", "dependencies": { "@lighthouse-web3/kavach": "^0.1.9", diff --git a/package.json b/package.json index 578c9e6f..3e9e1037 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lighthouse-web3/sdk", - "version": "0.4.5", + "version": "0.4.6", "description": "NPM package and CLI tool to interact with lighthouse protocol", "main": "./dist/Lighthouse/index.js", "types": "./dist/Lighthouse/index.d.ts", diff --git a/src/Commands/index.ts b/src/Commands/index.ts index 0f05948e..68c89dcc 100644 --- a/src/Commands/index.ts +++ b/src/Commands/index.ts @@ -72,7 +72,7 @@ Command.prototype.helpInformation = function (context: any) { } widgets.addHelpText('before', 'Welcome to lighthouse-web3') -widgets.version('0.4.5') +widgets.version('0.4.6') widgets .command('wallet') @@ -120,7 +120,13 @@ widgets .command('upload') .description('Upload a file') .argument('', 'Path to file') - .action(upload) + .option( + '-s, --storage-type ', + 'Storage backend to use (e.g. ipfs, walrus)' + ) + .action((path: string, options: { storageType?: string }) => + upload(path, options?.storageType) + ) widgets .command('upload-encrypted') diff --git a/src/Commands/upload.ts b/src/Commands/upload.ts index b2948635..c6fb1363 100644 --- a/src/Commands/upload.ts +++ b/src/Commands/upload.ts @@ -7,7 +7,12 @@ import lighthouse from '../Lighthouse' import bytesToSize from './utils/byteToSize' import { config } from './utils/getNetwork' -const getQuote = async (path: string, apiKey: string, Spinner: any) => { +const getQuote = async ( + path: string, + apiKey: string, + Spinner: any, + storageType?: string +) => { const spinner = new Spinner('Getting Quote...') spinner.start() @@ -58,21 +63,25 @@ const getQuote = async (path: string, apiKey: string, Spinner: any) => { bytesToSize(quoteResponse.totalSize) ) + const isWalrus = storageType === 'walrus' + const dataLimit = parseInt( + isWalrus ? quoteResponse.walrusDataLimit : quoteResponse.dataLimit + ) + const dataUsed = parseInt( + isWalrus ? quoteResponse.walrusDataUsed : quoteResponse.dataUsed + ) + console.log( 'Data Limit: ' + - bytesToSize(parseInt(quoteResponse.dataLimit)) + + bytesToSize(dataLimit) + '\r\nData Used : ' + - bytesToSize(parseInt(quoteResponse.dataUsed)) + + bytesToSize(dataUsed) + '\r\nAfter Upload: ' + - bytesToSize( - parseInt(quoteResponse.dataLimit) - - (parseInt(quoteResponse.dataUsed) + quoteResponse.totalSize) - ) + bytesToSize(dataLimit - (dataUsed + quoteResponse.totalSize)) ) const remainingAfterUpload = - parseInt(quoteResponse.dataLimit) - - (parseInt(quoteResponse.dataUsed) + quoteResponse.totalSize) + dataLimit - (dataUsed + quoteResponse.totalSize) return { fileName: quoteResponse.metaData[0].fileName, @@ -83,11 +92,20 @@ const getQuote = async (path: string, apiKey: string, Spinner: any) => { } } -const uploadFile = async (path: string, apiKey: string) => { +const uploadFile = async ( + path: string, + apiKey: string, + storageType?: string +) => { const spinner = new Spinner('Uploading...') spinner.start() - const uploadResponse = (await lighthouse.upload(path, apiKey)).data + const uploadResponse = ( + await lighthouse.upload(path, apiKey, { + cidVersion: 1, + ...(storageType ? { headers: { storageType } } : {}), + }) + ).data spinner.stop() process.stdout.clearLine(-1) @@ -99,14 +117,14 @@ const uploadFile = async (path: string, apiKey: string) => { process.exit() } + const gatewayBase = + storageType === 'walrus' + ? 'https://gateway-walrus.lighthouse.storage/ipfs/' + : 'https://gateway.lighthouse.storage/ipfs/' + console.log( green('File Uploaded, visit following url to view content!\r\n') + - cyan( - 'Visit: ' + - 'https://gateway.lighthouse.storage/ipfs/' + - uploadResponse.Hash + - '\r\n' - ) + + cyan('Visit: ' + gatewayBase + uploadResponse.Hash + '\r\n') + cyan( Array(7).fill('\xa0').join('') + 'https://ipfs.io/ipfs/' + @@ -118,18 +136,22 @@ const uploadFile = async (path: string, apiKey: string) => { return } -export default async function (_path: string) { +export default async function (_path: string, storageType?: string) { if (!_path) { console.log( - 'lighthouse-web3 upload \r\n\r\n' + + 'lighthouse-web3 upload [--storage-type ]\r\n\r\n' + green('Description: ') + 'Upload a file\r\n\r\n' + cyan('Options:\r\n') + Array(3).fill('\xa0').join('') + - '--path: Required, path to file\r\n\r\n' + + '--path: Required, path to file\r\n' + + Array(3).fill('\xa0').join('') + + '-s, --storage-type: Optional, storage backend (e.g. ipfs, walrus)\r\n\r\n' + magenta('Example:') + Array(3).fill('\xa0').join('') + - 'lighthouse-web3 upload /home/cosmos/Desktop/ILoveAnime.jpg\r\n' + 'lighthouse-web3 upload /home/cosmos/Desktop/ILoveAnime.jpg\r\n' + + Array(3).fill('\xa0').join('') + + 'lighthouse-web3 upload /home/cosmos/Desktop/ILoveAnime.jpg --storage-type walrus\r\n' ) } else { try { @@ -140,7 +162,8 @@ export default async function (_path: string) { const quoteResponse = await getQuote( path, config.get('LIGHTHOUSE_GLOBAL_API_KEY') as string, - Spinner + Spinner, + storageType ) // Upload @@ -192,7 +215,7 @@ export default async function (_path: string) { } const apiKey = config.get('LIGHTHOUSE_GLOBAL_API_KEY') as string - await uploadFile(path, apiKey) + await uploadFile(path, apiKey, storageType) } catch (error: any) { console.log(red(error.message)) process.exit(0) diff --git a/src/Lighthouse/getBalance/index.ts b/src/Lighthouse/getBalance/index.ts index 8552d995..a63fd3a8 100644 --- a/src/Lighthouse/getBalance/index.ts +++ b/src/Lighthouse/getBalance/index.ts @@ -4,6 +4,10 @@ export type balanceResponse = { data: { dataLimit: number dataUsed: number + dataLimitPermanent: number + dataUsedPermanent: number + walrusDataLimit: number + walrusDataUsed: number } } diff --git a/src/Lighthouse/getQuote/index.ts b/src/Lighthouse/getQuote/index.ts index cc7a5df4..a5637d2b 100644 --- a/src/Lighthouse/getQuote/index.ts +++ b/src/Lighthouse/getQuote/index.ts @@ -35,6 +35,10 @@ const getCosting = async (path: string, apiKey: string) => { metaData: metaData, dataLimit: user_data_usage.dataLimit, dataUsed: user_data_usage.dataUsed, + dataLimitPermanent: user_data_usage.dataLimitPermanent, + dataUsedPermanent: user_data_usage.dataUsedPermanent, + walrusDataLimit: user_data_usage.walrusDataLimit, + walrusDataUsed: user_data_usage.walrusDataUsed, totalSize: totalSize, }, } @@ -57,6 +61,10 @@ const getCosting = async (path: string, apiKey: string) => { metaData: metaData, dataLimit: user_data_usage.dataLimit, dataUsed: user_data_usage.dataUsed, + dataLimitPermanent: user_data_usage.dataLimitPermanent, + dataUsedPermanent: user_data_usage.dataUsedPermanent, + walrusDataLimit: user_data_usage.walrusDataLimit, + walrusDataUsed: user_data_usage.walrusDataUsed, totalSize: fileSizeInBytes, }, } diff --git a/src/Lighthouse/tests/ipns.test.ts b/src/Lighthouse/tests/ipns.test.ts index d69285b9..3bd6086d 100644 --- a/src/Lighthouse/tests/ipns.test.ts +++ b/src/Lighthouse/tests/ipns.test.ts @@ -37,7 +37,7 @@ describe('ipns', () => { await lighthouse.publishRecord(cid, 'invalid ipnsName', apiKey) ).data } catch (error) { - expect(error.message).toBe('Request failed with status code 400') + expect(error.message).toBe('Request failed with status code 500') } }, 20000) }) @@ -63,7 +63,7 @@ describe('ipns', () => { try { const response = (await lighthouse.removeKey(ipnsName, apiKey)).data } catch (error) { - expect(error.message).toBe('Request failed with status code 400') + expect(error.message).toBe('Request failed with status code 500') } }) }) diff --git a/src/Lighthouse/upload/buffer/browser.ts b/src/Lighthouse/upload/buffer/browser.ts index 73fefa0d..b1b417fe 100644 --- a/src/Lighthouse/upload/buffer/browser.ts +++ b/src/Lighthouse/upload/buffer/browser.ts @@ -9,8 +9,13 @@ export default async ( ) => { try { const token = 'Bearer ' + apiKey - const endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}` + let endpoint = '' const storageType = headers?.storageType + if (storageType === 'walrus') { + endpoint = lighthouseConfig.lighthouseWalrusNode + `/api/v0/add?cid-version=${cidVersion}` + } else { + endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}` + } // Upload file const formData = new FormData() diff --git a/src/Lighthouse/upload/buffer/node.ts b/src/Lighthouse/upload/buffer/node.ts index f31a1f33..58f1dd51 100644 --- a/src/Lighthouse/upload/buffer/node.ts +++ b/src/Lighthouse/upload/buffer/node.ts @@ -9,8 +9,13 @@ export default async ( ) => { try { const token = 'Bearer ' + apiKey - const endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}` + let endpoint = '' const storageType = headers?.storageType + if (storageType === 'walrus') { + endpoint = lighthouseConfig.lighthouseWalrusNode + `/api/v0/add?cid-version=${cidVersion}` + } else { + endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}` + } // Upload file const blob = new Blob([buffer]) diff --git a/src/Lighthouse/upload/car/browser.ts b/src/Lighthouse/upload/car/browser.ts index e9ef458c..b9fa5793 100644 --- a/src/Lighthouse/upload/car/browser.ts +++ b/src/Lighthouse/upload/car/browser.ts @@ -20,13 +20,18 @@ export default async ( throw new Error('File must have a .car extension') } - const endpoint = lighthouseConfig.lighthouseNode + '/api/v0/dag/import' + let endpoint = '' + const storageType = headers?.storageType + if (storageType === 'walrus') { + endpoint = lighthouseConfig.lighthouseWalrusNode + '/api/v0/dag/import' + } else { + endpoint = lighthouseConfig.lighthouseNode + '/api/v0/dag/import' + } const formData = new FormData() formData.append('file', file) const token = 'Bearer ' + accessToken - const storageType = headers?.storageType const requestHeaders = { Authorization: token, diff --git a/src/Lighthouse/upload/car/node.ts b/src/Lighthouse/upload/car/node.ts index 1ad05810..52e6e8a5 100644 --- a/src/Lighthouse/upload/car/node.ts +++ b/src/Lighthouse/upload/car/node.ts @@ -25,11 +25,16 @@ export default async ( } try { - const endpoint = lighthouseConfig.lighthouseNode + '/api/v0/dag/import' + let endpoint = '' + const storageType = headers?.storageType + if (storageType === 'walrus') { + endpoint = lighthouseConfig.lighthouseWalrusNode + '/api/v0/dag/import' + } else { + endpoint = lighthouseConfig.lighthouseNode + '/api/v0/dag/import' + } const boundary = '----WebKitFormBoundary' + Math.random().toString(16).substr(2) - - const storageType = headers?.storageType + const requestHeaders = { Authorization: token, ...(storageType ? { 'X-Storage-Type': storageType } : {}), diff --git a/src/Lighthouse/upload/files/browser.ts b/src/Lighthouse/upload/files/browser.ts index c667c7e1..9c3d6f04 100644 --- a/src/Lighthouse/upload/files/browser.ts +++ b/src/Lighthouse/upload/files/browser.ts @@ -28,7 +28,12 @@ export default async ( 'wrap-with-directory': 'false', ...baseParams, }) - let endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?${queryParams.toString()}` + let endpoint = '' + if (storageType === 'walrus') { + endpoint = lighthouseConfig.lighthouseWalrusNode + `/api/v0/add?${queryParams.toString()}` + } else { + endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?${queryParams.toString()}` + } if(!isDirectory && files.length > 1) { const wrapParams = new URLSearchParams({ diff --git a/src/Lighthouse/upload/files/node.ts b/src/Lighthouse/upload/files/node.ts index d1714fb5..3d6b11de 100644 --- a/src/Lighthouse/upload/files/node.ts +++ b/src/Lighthouse/upload/files/node.ts @@ -42,8 +42,12 @@ export default async ( 'wrap-with-directory': 'false', 'cid-version': String(cidVersion), }) - const endpoint = - lighthouseConfig.lighthouseNode + `/api/v0/add?${queryParams.toString()}` + let endpoint = '' + if (storageType === 'walrus') { + endpoint = lighthouseConfig.lighthouseWalrusNode + `/api/v0/add?${queryParams.toString()}` + } else { + endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?${queryParams.toString()}` + } const boundary = '----WebKitFormBoundary' + Math.random().toString(16).slice(2) diff --git a/src/Lighthouse/upload/text/browser.ts b/src/Lighthouse/upload/text/browser.ts index 72aadfbe..eae8eac4 100644 --- a/src/Lighthouse/upload/text/browser.ts +++ b/src/Lighthouse/upload/text/browser.ts @@ -11,8 +11,13 @@ export default async ( ) => { try { const token = 'Bearer ' + apiKey - const endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}` + let endpoint = '' const storageType = headers?.storageType + if (storageType === 'walrus') { + endpoint = lighthouseConfig.lighthouseWalrusNode + `/api/v0/add?cid-version=${cidVersion}` + } else { + endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}` + } // Upload file const formData = new FormData() diff --git a/src/Lighthouse/upload/text/node.ts b/src/Lighthouse/upload/text/node.ts index bc8e862d..21327812 100644 --- a/src/Lighthouse/upload/text/node.ts +++ b/src/Lighthouse/upload/text/node.ts @@ -11,8 +11,13 @@ export default async ( ) => { try { const token = 'Bearer ' + apiKey - const endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}` + let endpoint = '' const storageType = headers?.storageType + if (storageType === 'walrus') { + endpoint = lighthouseConfig.lighthouseWalrusNode + `/api/v0/add?cid-version=${cidVersion}` + } else { + endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}` + } // Upload file const formData = new FormData() diff --git a/src/lighthouse.config.ts b/src/lighthouse.config.ts index 48668968..d6d1d743 100644 --- a/src/lighthouse.config.ts +++ b/src/lighthouse.config.ts @@ -1,7 +1,9 @@ const defaultConfig = { lighthouseAPI: 'https://api.lighthouse.storage', lighthouseNode: 'https://upload.lighthouse.storage', + lighthouseWalrusNode: 'https://upload-walrus.lighthouse.storage', lighthouseGateway: 'https://gateway.lighthouse.storage', + lighthouseWalrusGateway: 'https://gateway-walrus.lighthouse.storage', lighthouseBLSNode: 'https://encryption.lighthouse.storage', network: 'polygon', fantom: {