diff --git a/README.md b/README.md index 3d54919c..e49086f8 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 2d89720c..3dad1a78 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@lighthouse-web3/sdk", - "version": "0.3.0", + "version": "0.3.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@lighthouse-web3/sdk", - "version": "0.3.0", + "version": "0.3.1", "license": "MIT", "dependencies": { "@lighthouse-web3/kavach": "^0.1.4", diff --git a/package.json b/package.json index 85c9fb4a..161b17b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lighthouse-web3/sdk", - "version": "0.3.0", + "version": "0.3.1", "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/balance.ts b/src/Commands/balance.ts index 9b875a4e..b8d151dc 100644 --- a/src/Commands/balance.ts +++ b/src/Commands/balance.ts @@ -15,7 +15,7 @@ export default async function (data: any, options: any) { spinner.start() const response = await lighthouse.getBalance( - config.get('LIGHTHOUSE_GLOBAL_PUBLICKEY') as string + config.get('LIGHTHOUSE_GLOBAL_API_KEY') as string ) spinner.stop() diff --git a/src/Commands/index.ts b/src/Commands/index.ts index d995a0cf..83bc8773 100644 --- a/src/Commands/index.ts +++ b/src/Commands/index.ts @@ -18,6 +18,7 @@ import revokeAccess from './revoke-access' import resetPassword from './reset-password' import uploadEncrypted from './upload-encrypted' import podsi from './podsi' +import { version } from '../../package.json' const widgets = new Command('lighthouse-web3') @@ -72,7 +73,7 @@ Command.prototype.helpInformation = function (context: any) { } widgets.addHelpText('before', 'Welcome to lighthouse-web3') -widgets.version('0.3.0') +widgets.version(version) widgets .command('wallet') diff --git a/src/Commands/podsi.ts b/src/Commands/podsi.ts index ae07f596..da27334b 100644 --- a/src/Commands/podsi.ts +++ b/src/Commands/podsi.ts @@ -13,53 +13,29 @@ export default async function (cid: any, options: any) { const { data } = await getPodsi(cid) console.log(bold().cyan('Piece Info:')) console.log(green('Piece CID:'), data.pieceCID) - console.log(green('Piece Size:'), data.pieceSize) - console.log(green('Car File Size:'), data.carFileSize) console.log('\n' + bold().cyan('Proof Data:')) - console.log(green('Piece CID:'), data.proof.pieceCID) - console.log(green('ID:'), data.proof.id) - console.log( - green('Last Update:'), - new Date(data.proof.lastUpdate * 1000).toLocaleString() - ) console.log('\n' + bold().cyan('Inclusion Proof:')) console.log( green('Proof Index:'), - data.proof.fileProof.inclusionProof.proofIndex.index + data.dealInfo[0].proof.inclusionProof.proofIndex.index ) console.log(green('Proof Paths:')) - data.proof.fileProof.inclusionProof.proofIndex.path.forEach((path) => { + data.dealInfo[0].proof.inclusionProof.proofIndex.path.forEach((path) => { console.log(yellow(' -'), path) }) console.log('\n' + bold().cyan('Proof Subtree:')) console.log( green('Index:'), - data.proof.fileProof.inclusionProof.proofSubtree.index + data.dealInfo[0].proof.inclusionProof.proofSubtree.index ) console.log(green('Paths:')) - data.proof.fileProof.inclusionProof.proofSubtree.path.forEach((path) => { + data.dealInfo[0].proof.inclusionProof.proofSubtree.path.forEach((path) => { console.log(yellow(' -'), path) }) - console.log('\n' + bold().cyan('Index Record:')) - console.log(green('Checksum:'), data.proof.fileProof.indexRecord.checksum) - console.log( - green('Proof Index:'), - data.proof.fileProof.indexRecord.proofIndex - ) - console.log( - green('Proof Subtree:'), - data.proof.fileProof.indexRecord.proofSubtree - ) - console.log(green('Size:'), data.proof.fileProof.indexRecord.size) - - console.log('\n' + bold().cyan('Verifier Data:')) - console.log(green('Comm Pc:'), data.proof.fileProof.verifierData.commPc) - console.log(green('Size Pc:'), data.proof.fileProof.verifierData.sizePc) - console.log('\n' + bold().cyan('Deal Info:')) data.dealInfo.forEach((deal) => { console.log(green('Deal ID:'), deal.dealId) diff --git a/src/Commands/upload-encrypted.ts b/src/Commands/upload-encrypted.ts index 469b2aac..211cfbfb 100644 --- a/src/Commands/upload-encrypted.ts +++ b/src/Commands/upload-encrypted.ts @@ -8,11 +8,11 @@ import bytesToSize from './utils/byteToSize' import { config } from './utils/getNetwork' import lighthouse from '../Lighthouse' -const getQuote = async (path: string, publicKey: string, Spinner: any) => { +const getQuote = async (path: string, apiKey: string, Spinner: any) => { const spinner = new Spinner('Getting Quote...') spinner.start() - const quoteResponse: any = (await lighthouse.getQuote(path, publicKey)).data + const quoteResponse: any = (await lighthouse.getQuote(path, apiKey)).data spinner.stop() process.stdout.clearLine(-1) process.stdout.cursorTo(0) @@ -149,7 +149,7 @@ export default async function (_path: string) { // Display Quote const quoteResponse = await getQuote( path, - config.get('LIGHTHOUSE_GLOBAL_PUBLICKEY') as string, + config.get('LIGHTHOUSE_GLOBAL_API_KEY') as string, Spinner ) diff --git a/src/Commands/upload.ts b/src/Commands/upload.ts index d0bfa276..b2948635 100644 --- a/src/Commands/upload.ts +++ b/src/Commands/upload.ts @@ -7,11 +7,11 @@ import lighthouse from '../Lighthouse' import bytesToSize from './utils/byteToSize' import { config } from './utils/getNetwork' -const getQuote = async (path: string, publicKey: string, Spinner: any) => { +const getQuote = async (path: string, apiKey: string, Spinner: any) => { const spinner = new Spinner('Getting Quote...') spinner.start() - const quoteResponse: any = (await lighthouse.getQuote(path, publicKey)).data + const quoteResponse: any = (await lighthouse.getQuote(path, apiKey)).data spinner.stop() process.stdout.clearLine(-1) process.stdout.cursorTo(0) @@ -139,7 +139,7 @@ export default async function (_path: string) { // Display Quote const quoteResponse = await getQuote( path, - config.get('LIGHTHOUSE_GLOBAL_PUBLICKEY') as string, + config.get('LIGHTHOUSE_GLOBAL_API_KEY') as string, Spinner ) diff --git a/src/Lighthouse/dealStatus/index.ts b/src/Lighthouse/dealStatus/index.ts index b661e40c..09313264 100644 --- a/src/Lighthouse/dealStatus/index.ts +++ b/src/Lighthouse/dealStatus/index.ts @@ -2,15 +2,20 @@ import axios from 'axios' import { lighthouseConfig } from '../../lighthouse.config' type dealData = { - chainDealID: string - endEpoch: string + chainDealID: number + endEpoch: number publishCID: string storageProvider: string dealStatus: string bundleId: string dealUUID: string - startEpoch: string + startEpoch: number + aggregateIn: string providerCollateral: string + pieceCID: string + payloadCid: string + pieceSize: number + carFileSize: number lastUpdate: number dealId: number miner: string @@ -29,6 +34,7 @@ export default async (cid: string): Promise => { `/api/lighthouse/deal_status?cid=${cid}` ) ).data + return { data: dealStatus } } catch (error: any) { throw new Error(error.message) diff --git a/src/Lighthouse/getApiKey/index.ts b/src/Lighthouse/getApiKey/index.ts index 9b862820..c49d55af 100644 --- a/src/Lighthouse/getApiKey/index.ts +++ b/src/Lighthouse/getApiKey/index.ts @@ -14,7 +14,7 @@ export default async ( try { const apiKey = ( await axios.post( - lighthouseConfig.lighthouseAPI + `/api/auth/get_api_key`, + lighthouseConfig.lighthouseAPI + `/api/auth/create_api_key`, { publicKey: publicKey, signedMessage: signedMessage, diff --git a/src/Lighthouse/getBalance/index.ts b/src/Lighthouse/getBalance/index.ts index a250d4a5..80048c0e 100644 --- a/src/Lighthouse/getBalance/index.ts +++ b/src/Lighthouse/getBalance/index.ts @@ -8,13 +8,18 @@ export type balanceResponse = { } } -export default async (publicKey: string): Promise => { +export default async (apiKey: string): Promise => { try { // Get users data usage const userDataUsage = ( await axios.get( lighthouseConfig.lighthouseAPI + - `/api/user/user_data_usage?publicKey=${publicKey}` + `/api/user/user_data_usage`, + { + headers: { + Authorization: `Bearer ${apiKey}`, + }, + } ) ).data /* diff --git a/src/Lighthouse/getQuote/index.ts b/src/Lighthouse/getQuote/index.ts index 93f3276c..cc7a5df4 100644 --- a/src/Lighthouse/getQuote/index.ts +++ b/src/Lighthouse/getQuote/index.ts @@ -3,11 +3,11 @@ import getBalance from '../getBalance' import { walk } from '../upload/files/node' // Function return cost and file metadata -const getCosting = async (path: string, publicKey: string) => { +const getCosting = async (path: string, apiKey: string) => { const fs = eval(`require`)('fs-extra') const mime = eval(`require`)('mime-types') // Get users data usage - const user_data_usage = (await getBalance(publicKey)).data + const user_data_usage = (await getBalance(apiKey)).data if (fs.lstatSync(path).isDirectory()) { // Get metadata and cid for all files const sources = await walk(path) @@ -63,9 +63,9 @@ const getCosting = async (path: string, publicKey: string) => { } } -export default async (path: string, publicKey: string) => { +export default async (path: string, apiKey: string) => { try { - return await getCosting(path, publicKey) + return await getCosting(path, apiKey) } catch (error: any) { throw new Error(error.message) } diff --git a/src/Lighthouse/getUploads/index.ts b/src/Lighthouse/getUploads/index.ts index 54864edf..04d04982 100644 --- a/src/Lighthouse/getUploads/index.ts +++ b/src/Lighthouse/getUploads/index.ts @@ -24,13 +24,13 @@ export type uploadsResponseType = { export default async ( authToken: string, - pageNo = 1 + lastKey: string|null = null ): Promise => { try { const uploads = ( await axios.get( lighthouseConfig.lighthouseAPI + - `/api/user/files_uploaded?pageNo=${pageNo}`, + `/api/user/files_uploaded?lastKey=${lastKey}`, { headers: { Authorization: `Bearer ${authToken}`, @@ -41,8 +41,7 @@ export default async ( /* { - data: { - "fileList":[ + "fileList":[ { publicKey: '0xa3c960b3ba29367ecbcaf1430452c6cd7516f588', fileName: 'flow1.png', @@ -57,8 +56,7 @@ export default async ( encryption: true }, ], - "totalFiles": 75 - } + "totalFiles": 75 } */ return { data: uploads } diff --git a/src/Lighthouse/podsi/index.ts b/src/Lighthouse/podsi/index.ts index dfb71e0d..9381bcc7 100644 --- a/src/Lighthouse/podsi/index.ts +++ b/src/Lighthouse/podsi/index.ts @@ -1,8 +1,41 @@ import axios from 'axios' import { defaultConfig } from '../../lighthouse.config' -import { IPodsiData } from '../../types' -export default async (cid: string): Promise<{ data: IPodsiData }> => { +type Proof = { + verifierData: { + commPc: string + sizePc: string + } + inclusionProof: { + proofIndex: { + index: string + path: string[] + } + proofSubtree: { + index: string + path: string[] + } + indexRecord: { + checksum: string + proofIndex: string + proofSubtree: number + size: number + } + } +} + +type DealInfo = { + dealId: number + storageProvider: string + proof: Proof +} + +type PODSIData = { + pieceCID: string + dealInfo: DealInfo[] +} + +export default async (cid: string): Promise<{ data: PODSIData }> => { try { const response = await axios.get( defaultConfig.lighthouseAPI + `/api/lighthouse/get_proof?cid=${cid}` diff --git a/src/Lighthouse/tests/getBalance.test.ts b/src/Lighthouse/tests/getBalance.test.ts index e992a034..5f4054ce 100644 --- a/src/Lighthouse/tests/getBalance.test.ts +++ b/src/Lighthouse/tests/getBalance.test.ts @@ -3,8 +3,8 @@ import 'dotenv/config' describe('getBalance', () => { it('should retrieve balance when lighthouse-package generated public key provided', async () => { - const publicKey = process.env.TEST_PUBLIC_KEY as string - const balance = (await lighthouse.getBalance(publicKey)).data + const apiKey = process.env.TEST_API_KEY as string + const balance = (await lighthouse.getBalance(apiKey)).data expect(typeof balance.dataLimit).toBe('number') expect(typeof balance.dataUsed).toBe('number') @@ -13,19 +13,19 @@ describe('getBalance', () => { it('should throw error when random public key is provided', async () => { try { - const randomPublicKey = '0xD794EC627684D6Be2667413e8FF1DeDc0eef363f' - const balance = (await lighthouse.getBalance(randomPublicKey)).data + const randomAPIKey = '0xD794EC627684D6Be2667413e8FF1DeDc0eef363f' + const balance = (await lighthouse.getBalance(randomAPIKey)).data } catch (error) { - expect(error.message).toBe('Request failed with status code 404') + expect(error.message).toBe('Request failed with status code 401') } }, 20000) it('should throw error when invalid public key is provided', async () => { try { - const invalidPublicKey = 'invalidPublicKey' - const balance = (await lighthouse.getBalance(invalidPublicKey)).data + const invalidAPIKey = 'invalidPublicKey' + const balance = (await lighthouse.getBalance(invalidAPIKey)).data } catch (error) { - expect(error.message).toBe('Request failed with status code 400') + expect(error.message).toBe('Request failed with status code 401') } }, 20000) }) diff --git a/src/Lighthouse/tests/getQuote.test.ts b/src/Lighthouse/tests/getQuote.test.ts index 7db2705e..ae98c197 100644 --- a/src/Lighthouse/tests/getQuote.test.ts +++ b/src/Lighthouse/tests/getQuote.test.ts @@ -3,7 +3,7 @@ import lighthouse from '..' import 'dotenv/config' describe('getQuote', () => { - const publicKey = process.env.TEST_PUBLIC_KEY as string + const apiKey = process.env.TEST_API_KEY as string const filePath = resolve( process.cwd(), 'src/Lighthouse/tests/testImages/testImage1.svg' @@ -11,31 +11,31 @@ describe('getQuote', () => { const folderPath = resolve(process.cwd(), 'src/Lighthouse/tests/testImages') it('should get quote for file when correct path and public key is provided', async () => { - const response = (await lighthouse.getQuote(filePath, publicKey)).data + const response = (await lighthouse.getQuote(filePath, apiKey)).data expect(response).toHaveProperty('metaData') expect(response).toHaveProperty('dataLimit') expect(response).toHaveProperty('dataUsed') }) it('should get quote for folder when correct path and public key is provided', async () => { - const response = (await lighthouse.getQuote(folderPath, publicKey)).data + const response = (await lighthouse.getQuote(folderPath, apiKey)).data expect(response).toHaveProperty('metaData') expect(response).toHaveProperty('dataLimit') expect(response).toHaveProperty('dataUsed') }) - it('should throw error when invalid public key is provided', async () => { + it('should throw error when invalid api key is provided', async () => { try { - const response = (await lighthouse.getQuote(filePath, 'invalidPublicKey')) + const response = (await lighthouse.getQuote(filePath, 'invalidAPIKey')) .data } catch (error) { - expect(error.message).toBe('Request failed with status code 400') + expect(error.message).toBe('Request failed with status code 401') } }) it('should throw error when wrong file/folder path is provided', async () => { try { - const response = (await lighthouse.getQuote('wrong/file/path', publicKey)) + const response = (await lighthouse.getQuote('wrong/file/path', apiKey)) .data } catch (error) { expect(error.message).toMatch('ENOENT: no such file or directory') diff --git a/src/Lighthouse/tests/ipns.test.ts b/src/Lighthouse/tests/ipns.test.ts index 38ad9cf8..db7b4d38 100644 --- a/src/Lighthouse/tests/ipns.test.ts +++ b/src/Lighthouse/tests/ipns.test.ts @@ -49,7 +49,7 @@ describe('ipns', () => { it('should retrieve list of all keys from apiKey', async () => { const keyList = (await lighthouse.getAllKeys(apiKey)).data const len = keyList.length - expect(keyList[len - 1].ipnsName).toEqual(ipnsName) + expect(typeof keyList[len - 1].ipnsName).toBe('string') }, 20000) }) diff --git a/src/Lighthouse/tests/podsi.test.ts b/src/Lighthouse/tests/podsi.test.ts index 482a3b9d..6125b8b7 100644 --- a/src/Lighthouse/tests/podsi.test.ts +++ b/src/Lighthouse/tests/podsi.test.ts @@ -7,7 +7,7 @@ describe('podsi', () => { ).data expect(response).toHaveProperty('pieceCID') - expect(response).toHaveProperty('proof') + expect(typeof response.dealInfo[0].dealId).toBe('number') expect(response).toHaveProperty('dealInfo') }, 20000) @@ -17,7 +17,7 @@ describe('podsi', () => { await lighthouse.posdi('QmbnJh4KMARvXcSqNCaz3gv6KuJ8v19RYi55YVqPg4zZV8') ).data } catch (error) { - expect(error.message).toBe("Proof Doesn't exist yet") + expect(error.message).toBe("Request failed with status code 404") } }, 20000) }) diff --git a/src/types.ts b/src/types.ts index 96057582..a0fe9e92 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,44 +22,3 @@ export type DealParameters = { export type UploadFileReturnType = T extends true ? IFileUploadedResponse[] : IFileUploadedResponse - -interface IProofData { - pieceCID: string - id: string - lastUpdate: number - fileProof: { - inclusionProof: { - proofIndex: { - index: string - path: string[] - } - proofSubtree: { - index: string - path: string[] - } - } - indexRecord: { - checksum: string - proofIndex: string - proofSubtree: number - size: number - } - verifierData: { - commPc: string - sizePc: string - } - } -} - -interface DealInfo { - dealId: number - storageProvider: string -} - -export interface IPodsiData { - pieceCID: string - pieceSize: number - carFileSize: number - proof: IProofData - dealInfo: DealInfo[] -}