Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/v0.4.4-green"/>
# Lighthouse <img src="https://img.shields.io/badge/v0.4.6-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.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",
Expand Down
10 changes: 8 additions & 2 deletions src/Commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -120,7 +120,13 @@ widgets
.command('upload')
.description('Upload a file')
.argument('<path>', 'Path to file')
.action(upload)
.option(
'-s, --storage-type <type>',
'Storage backend to use (e.g. ipfs, walrus)'
)
.action((path: string, options: { storageType?: string }) =>
upload(path, options?.storageType)
)

widgets
.command('upload-encrypted')
Expand Down
69 changes: 46 additions & 23 deletions src/Commands/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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/' +
Expand All @@ -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 <path>\r\n\r\n' +
'lighthouse-web3 upload <path> [--storage-type <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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/Lighthouse/getBalance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export type balanceResponse = {
data: {
dataLimit: number
dataUsed: number
dataLimitPermanent: number
dataUsedPermanent: number
walrusDataLimit: number
walrusDataUsed: number
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Lighthouse/getQuote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand All @@ -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,
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lighthouse/tests/ipns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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')
}
})
})
Expand Down
7 changes: 6 additions & 1 deletion src/Lighthouse/upload/buffer/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion src/Lighthouse/upload/buffer/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
9 changes: 7 additions & 2 deletions src/Lighthouse/upload/car/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions src/Lighthouse/upload/car/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } : {}),
Expand Down
7 changes: 6 additions & 1 deletion src/Lighthouse/upload/files/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 6 additions & 2 deletions src/Lighthouse/upload/files/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 6 additions & 1 deletion src/Lighthouse/upload/text/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion src/Lighthouse/upload/text/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions src/lighthouse.config.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
Loading