Skip to content

Commit

Permalink
Merge pull request #109 from lighthouse-web3/v0.3.1
Browse files Browse the repository at this point in the history
V0.3.1
  • Loading branch information
ravish1729 authored Apr 17, 2024
2 parents 4504f6e + d498c55 commit 2680c17
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 117 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Lighthouse <img src="https://img.shields.io/badge/BETA-v0.3.0-green"/>
# Lighthouse <img src="https://img.shields.io/badge/BETA-v0.3.1-green"/>

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.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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')
Expand Down
32 changes: 4 additions & 28 deletions src/Commands/podsi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/upload-encrypted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
)

Expand Down
6 changes: 3 additions & 3 deletions src/Commands/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
)

Expand Down
12 changes: 9 additions & 3 deletions src/Lighthouse/dealStatus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,6 +34,7 @@ export default async (cid: string): Promise<dealResponse> => {
`/api/lighthouse/deal_status?cid=${cid}`
)
).data

return { data: dealStatus }
} catch (error: any) {
throw new Error(error.message)
Expand Down
2 changes: 1 addition & 1 deletion src/Lighthouse/getApiKey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions src/Lighthouse/getBalance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ export type balanceResponse = {
}
}

export default async (publicKey: string): Promise<balanceResponse> => {
export default async (apiKey: string): Promise<balanceResponse> => {
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
/*
Expand Down
8 changes: 4 additions & 4 deletions src/Lighthouse/getQuote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 4 additions & 6 deletions src/Lighthouse/getUploads/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export type uploadsResponseType = {

export default async (
authToken: string,
pageNo = 1
lastKey: string|null = null
): Promise<uploadsResponseType> => {
try {
const uploads = (
await axios.get(
lighthouseConfig.lighthouseAPI +
`/api/user/files_uploaded?pageNo=${pageNo}`,
`/api/user/files_uploaded?lastKey=${lastKey}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
Expand All @@ -41,8 +41,7 @@ export default async (

/*
{
data: {
"fileList":[
"fileList":[
{
publicKey: '0xa3c960b3ba29367ecbcaf1430452c6cd7516f588',
fileName: 'flow1.png',
Expand All @@ -57,8 +56,7 @@ export default async (
encryption: true
},
],
"totalFiles": 75
}
"totalFiles": 75
}
*/
return { data: uploads }
Expand Down
37 changes: 35 additions & 2 deletions src/Lighthouse/podsi/index.ts
Original file line number Diff line number Diff line change
@@ -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}`
Expand Down
16 changes: 8 additions & 8 deletions src/Lighthouse/tests/getBalance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
})
Loading

0 comments on commit 2680c17

Please sign in to comment.