diff --git a/.gitignore b/.gitignore
index ff746b1..c5e2c6a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,3 +34,4 @@ yarn.lock
yarn-error.log
dist/
src/coverage/
+.DS_Store
\ No newline at end of file
diff --git a/README.md b/README.md
index de885b1..1c0aee2 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.
@@ -11,51 +11,61 @@ npm install -g @lighthouse-web3/sdk
## CLI Usage
```bash
-# create-wallet
-lighthouse-web3 create-wallet
-
-# import-wallet
-lighthouse-web3 import-wallet --key
-
-# wallet-forget
-lighthouse-web3 wallet-forget
-
-# api-key
-lighthouse-web3 api-key --new
-
-# balance
-lighthouse-web3 balance
-
-# upload
-lighthouse-web3 upload
-
-# status
-lighthouse-web3 status
-
-# get-uploads
-lighthouse-web3 get-uploads
-
-# wallet
-lighthouse-web3 wallet
+# Wallet management
+lighthouse-web3 create-wallet # Create a new wallet
+lighthouse-web3 import-wallet --key # Import an existing wallet
+lighthouse-web3 wallet-forget # Remove previously saved wallet
+lighthouse-web3 reset-password # Change password of your wallet
+lighthouse-web3 wallet # Returns wallet public address
+
+# API Key management
+lighthouse-web3 api-key --new # Generate a new API key
+lighthouse-web3 api-key --import # Import an existing API key
+
+# Storage and uploads
+lighthouse-web3 upload # Upload a file
+lighthouse-web3 upload-encrypted # Upload a file encrypted
+lighthouse-web3 decrypt-file # Decrypt and download a file
+
+# Data usage and balance
+lighthouse-web3 balance # Get your data usage
+
+# File and deal status
+lighthouse-web3 deal-status # Get filecoin deal status of a CID
+
+# File management
+lighthouse-web3 get-uploads # Get details of files uploaded
+
+# Sharing and access control
+lighthouse-web3 share-file # Share access to another user
+lighthouse-web3 revoke-access # Revoke access on a file
+
+# IPNS (InterPlanetary Naming System)
+lighthouse-web3 ipns --generate-key # Generate IPNS Key
+lighthouse-web3 ipns --publish --cid --key # Publish CID to IPNS
+lighthouse-web3 ipns --list # List all IPNS keys
+lighthouse-web3 ipns --remove # Remove an IPNS key
+
+# Proof of Data Segment Inclusion (PODSI)
+lighthouse-web3 podsi # Show Proof of Data Segment Inclusion for a CID
```
## NodeJs Example
```javascript
-import lighthouse from "@lighthouse-web3/sdk";
+import lighthouse from '@lighthouse-web3/sdk'
// Create wallet
-const wallet = await lighthouse.createWallet(
- "Password for wallet encryption"
-);
+const wallet = await lighthouse.createWallet('Password for wallet encryption')
// Get wallet balance
-const balance = await lighthouse.getBalance(publicKey);
+const balance = await lighthouse.getBalance(wallet.data.publicKey)
// Upload File
-const uploadResponse = await lighthouse.upload('/home/cosmos/Desktop/wow.jpg', 'YOUR_API_KEY');
-
-```
+const uploadResponse = await lighthouse.upload(
+ '/home/cosmos/Desktop/wow.jpg',
+ 'YOUR_API_KEY'
+)
> Refer [GitBook](https://docs.lighthouse.storage/lighthouse-1/)
diff --git a/package-lock.json b/package-lock.json
index 75a90aa..39d1d53 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@lighthouse-web3/sdk",
- "version": "0.3.7",
+ "version": "0.4.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@lighthouse-web3/sdk",
- "version": "0.3.7",
+ "version": "0.4.0",
"license": "MIT",
"dependencies": {
"@lighthouse-web3/kavach": "^0.1.9",
diff --git a/package.json b/package.json
index 2c3f716..fa7870a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@lighthouse-web3/sdk",
- "version": "0.3.7",
+ "version": "0.4.0",
"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/deal-status.ts b/src/Commands/deal-status.ts
index 09bfb61..d8e33ea 100644
--- a/src/Commands/deal-status.ts
+++ b/src/Commands/deal-status.ts
@@ -6,11 +6,7 @@ const showResponse = (cid: string, dealStatus: any) => {
console.log(
yellow('\r\nCID:') +
Array(9).fill('\xa0').join('') +
- cid +
- yellow('\r\nSize:') +
- Array(8).fill('\xa0').join('') +
- bytesToSize(dealStatus[0]['content']) +
- '\r\n'
+ cid
)
console.log(
@@ -21,19 +17,19 @@ const showResponse = (cid: string, dealStatus: any) => {
)
for (let i = 0; i < dealStatus.length; i++) {
- const gap = 10 + (8 - dealStatus[i]['miner'].length)
+ const gap = 10 + (8 - dealStatus[i]['Provider'].length)
console.log(
Array(20).fill('\xa0').join('') +
- dealStatus[i]['miner'] +
+ dealStatus[i]['Provider'] +
Array(gap).fill('\xa0').join('') +
- dealStatus[i]['dealId']
+ dealStatus[i]['DealID']
)
}
console.log(
green('\r\nView deals at filfox URL:\r\n') +
Array(4).fill('\xa0').join('') +
'https://filfox.info/en/deal/' +
- dealStatus[0]['dealId']
+ dealStatus[0]['DealID']
)
}
diff --git a/src/Commands/index.ts b/src/Commands/index.ts
index bd8ad81..40014e0 100644
--- a/src/Commands/index.ts
+++ b/src/Commands/index.ts
@@ -17,7 +17,6 @@ import importWallet from './import-wallet'
import revokeAccess from './revoke-access'
import resetPassword from './reset-password'
import uploadEncrypted from './upload-encrypted'
-import podsi from './podsi'
const widgets = new Command('lighthouse-web3')
@@ -72,7 +71,7 @@ Command.prototype.helpInformation = function (context: any) {
}
widgets.addHelpText('before', 'Welcome to lighthouse-web3')
-widgets.version('0.3.7')
+widgets.version('0.4.0')
widgets
.command('wallet')
@@ -116,12 +115,6 @@ widgets
.description('IPNS service')
.action(ipns)
-widgets
- .command('podsi')
- .description('Show Proof of data segment inclusion')
- .argument('', 'CID of the file previously uploaded')
- .action(podsi)
-
widgets
.command('upload')
.description('Upload a file')
diff --git a/src/Lighthouse/tests/dealStatus.test.ts b/src/Lighthouse/tests/dealStatus.test.ts
index f352277..ec0295b 100644
--- a/src/Lighthouse/tests/dealStatus.test.ts
+++ b/src/Lighthouse/tests/dealStatus.test.ts
@@ -7,11 +7,9 @@ describe('dealStatus', () => {
'QmaiauHSgTDMy2NtLbsygL3iKmLXBqHf39SBA1nAQFSSey'
)
).data
-
- expect(response[0]).toHaveProperty('dealStatus')
- expect(response[0]).toHaveProperty('dealUUID')
- expect(response[0]).toHaveProperty('dealId')
- expect(response[0]).toHaveProperty('miner')
+
+ expect(response[0]).toHaveProperty('DealID')
+ expect(response[0]).toHaveProperty('Provider')
}, 20000)
it('should not retrieve deal status when invalid CID provided', async () => {
diff --git a/src/Lighthouse/tests/encryption.test.ts b/src/Lighthouse/tests/encryption.test.ts
index 05c409b..9f3660b 100644
--- a/src/Lighthouse/tests/encryption.test.ts
+++ b/src/Lighthouse/tests/encryption.test.ts
@@ -13,21 +13,23 @@ const signAuthMessage = async (privateKey: string) => {
describe('encryption', () => {
describe('getAuthMessage', () => {
it('should get auth message when valid public key is provided', async () => {
+ console.log('testing getAuthMessage')
const response = await lighthouse.getAuthMessage(
'0x1Ec09D4B3Cb565b7CCe2eEAf71CC90c9b46c5c26'
)
expect(response.data.message).toMatch(
/^Please sign this message to prove/
)
- }, 60000)
+ }, 6000)
it('should not get auth message when invalid public key is provided', async () => {
try {
+ console.log('testing getAuthMessage with invalid public key')
const response = await lighthouse.getAuthMessage('invalidPublicKey')
} catch (error) {
expect(error.message).toBe('Invalid public Key')
}
- }, 60000)
+ }, 6000)
})
describe('fetchEncryptionKey', () => {
const publicKey = '0xa3c960b3ba29367ecbcaf1430452c6cd7516f588'
@@ -36,6 +38,7 @@ describe('encryption', () => {
const cid = 'QmVkHgHnYVUfvTXsaJisHRgc89zsrgVL6ATh9mSiegRYrX'
it('should fetch encryption key when correct public-private key pair is provided', async () => {
+ console.log('testing fetchEncryptionKey')
const signed_message = await signAuthMessage(privateKey)
const response = await lighthouse.fetchEncryptionKey(
cid,
@@ -43,10 +46,11 @@ describe('encryption', () => {
signed_message
)
expect(typeof response.data.key).toBe('string')
- }, 80000)
+ }, 8000)
it('should not fetch encryption key when incorrect public-private key pair is provided', async () => {
try {
+ console.log('testing fetchEncryptionKey with incorrect key pair')
const randomPublicKey = '0x1ccEF158Dcbe6643F1cC577F236af79993F4D066'
const signed_message = await signAuthMessage(privateKey)
const response = await lighthouse.fetchEncryptionKey(
@@ -57,7 +61,7 @@ describe('encryption', () => {
} catch (error) {
expect(typeof error.message).toBe('string')
}
- }, 60000)
+ }, 6000)
it('should not fetch encryption key when incorrect CID is provided', async () => {
try {
@@ -72,7 +76,7 @@ describe('encryption', () => {
} catch (error) {
expect(error).toBe('cid not found')
}
- }, 60000)
+ }, 6000)
it('should not fetch encryption key when incorrect signature is provided', async () => {
try {
@@ -86,7 +90,7 @@ describe('encryption', () => {
} catch (error) {
expect(error.message).toBe('Invalid Signature')
}
- }, 60000)
+ }, 6000)
})
describe('shareFile', () => {
const publicKey = '0x969e19A952A9aeF004e4F711eE481D72A59470B1'
@@ -105,7 +109,7 @@ describe('encryption', () => {
expect(response.data.cid).toEqual(cid)
expect(response.data.shareTo).toEqual(shareTo)
expect(response.data.status).toBe('Success')
- }, 60000)
+ }, 10000)
it('should deny access for sharing file uploaded by other account', async () => {
try {
@@ -118,9 +122,9 @@ describe('encryption', () => {
signed_message
)
} catch (error) {
- expect(error.message).toEqual('Access Denied')
+ expect(error.message.message.message).toEqual('access denied')
}
- }, 60000)
+ }, 10000)
})
describe('applyAccessCondition', () => {
const publicKey = '0x969e19A952A9aeF004e4F711eE481D72A59470B1'
@@ -162,7 +166,7 @@ describe('encryption', () => {
)
expect(response.data.status).toEqual('Success')
expect(response.data.cid).toEqual(cid)
- }, 60000)
+ }, 10000)
})
describe('revokeFileAccess', () => {
it('should revoke file access to provided public address', async () => {
@@ -182,7 +186,7 @@ describe('encryption', () => {
expect(response.data.cid).toEqual(cid)
expect(response.data.status).toEqual('Success')
expect(response.data.revokeTo).toEqual(revokeTo)
- }, 20000)
+ }, 10000)
})
describe('getAccessConditions', () => {
it('should retrieve access conditions from provided cid', async () => {
@@ -194,6 +198,6 @@ describe('encryption', () => {
expect(response.data).toHaveProperty('conditions')
expect(response.data).toHaveProperty('sharedTo')
expect(response.data.cid).toEqual(cid)
- }, 20000)
+ }, 10000)
})
})
diff --git a/src/Lighthouse/tests/podsi.test.ts b/src/Lighthouse/tests/podsi.test.ts
deleted file mode 100644
index 34a8816..0000000
--- a/src/Lighthouse/tests/podsi.test.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import lighthouse from '..'
-
-describe('podsi', () => {
- it('should get PoDSI when CID whose deal is created is provided', async () => {
- const response = (
- await lighthouse.posdi('QmaiauHSgTDMy2NtLbsygL3iKmLXBqHf39SBA1nAQFSSey')
- ).data
-
- expect(response).toHaveProperty('pieceCID')
- expect(typeof response.dealInfo[0].dealId).toBe('number')
- expect(response).toHaveProperty('dealInfo')
- }, 20000)
-
- it('should not get PoDSI when CID whose deal is not created is provided', async () => {
- try {
- const response = (
- await lighthouse.posdi('QmbnJh4KMARvXcSqNCaz3gv6KuJ8v19RYi55YVqPg4zZV8')
- ).data
- } catch (error) {
- expect(error.message).toBe('Request failed with status code 404')
- }
- }, 20000)
-})
diff --git a/src/Lighthouse/tests/upload.test.ts b/src/Lighthouse/tests/upload.test.ts
index 88415bc..4ac22f5 100644
--- a/src/Lighthouse/tests/upload.test.ts
+++ b/src/Lighthouse/tests/upload.test.ts
@@ -24,8 +24,7 @@ describe('uploadFiles', () => {
it('should upload folder to ipfs when correct path is provided', async () => {
const path = resolve(process.cwd(), 'src/Lighthouse/tests/testImages')
- const full_deployResponse = (await lighthouse.upload(path, apiKey))
- .data
+ const full_deployResponse = (await lighthouse.upload(path, apiKey)).data
const deployResponse = full_deployResponse
expect(deployResponse).toHaveProperty('Name')
@@ -54,7 +53,7 @@ describe('uploadFiles', () => {
)
await lighthouse.upload(path, 'random apiKey')
} catch (error) {
- expect(error.message).toBe('Request failed with status code 500')
+ expect(error.message).toBe('Error: Authentication failed')
}
}, 60000)
})
diff --git a/src/Lighthouse/tests/uploadBuffer.test.ts b/src/Lighthouse/tests/uploadBuffer.test.ts
index 671b73c..9bb3d16 100644
--- a/src/Lighthouse/tests/uploadBuffer.test.ts
+++ b/src/Lighthouse/tests/uploadBuffer.test.ts
@@ -25,7 +25,7 @@ describe('uploadBuffer', () => {
await lighthouse.uploadBuffer(image, 'invalid.apiKey')
).data
} catch (error) {
- expect(error.message).toBe('Request failed with status code 500')
+ expect(error.message).toBe('Error: Authentication failed')
}
}, 60000)
})
diff --git a/src/Lighthouse/tests/uploadEncrypted.test.ts b/src/Lighthouse/tests/uploadEncrypted.test.ts
index 7ad2651..b5fe18c 100644
--- a/src/Lighthouse/tests/uploadEncrypted.test.ts
+++ b/src/Lighthouse/tests/uploadEncrypted.test.ts
@@ -81,7 +81,7 @@ describe('uploadEncrypted', () => {
signedMessageEncryption
)
} catch (error) {
- expect(error.message).toBe('Error encrypting file')
+ expect(error.message).toBe('Error: Error encrypting file')
}
}, 60000)
@@ -96,7 +96,7 @@ describe('uploadEncrypted', () => {
signedMessageEncryption
)
} catch (error) {
- expect(error.message).toBe('Request failed with status code 500')
+ expect(error.message).toBe('Error: Authentication failed')
}
}, 60000)
})
diff --git a/src/Lighthouse/tests/uploadText.test.ts b/src/Lighthouse/tests/uploadText.test.ts
index 6c6e427..4e0671b 100644
--- a/src/Lighthouse/tests/uploadText.test.ts
+++ b/src/Lighthouse/tests/uploadText.test.ts
@@ -28,7 +28,7 @@ describe('uploadText', () => {
await lighthouse.uploadText(text, 'invalid.apiKey', 'sample')
).data
} catch (error) {
- expect(error.message).toBe('Request failed with status code 500')
+ expect(error.message).toBe('Error: Authentication failed')
}
}, 60000)
})
diff --git a/src/Lighthouse/upload/buffer/browser.ts b/src/Lighthouse/upload/buffer/browser.ts
index 0230dd7..fa8543b 100644
--- a/src/Lighthouse/upload/buffer/browser.ts
+++ b/src/Lighthouse/upload/buffer/browser.ts
@@ -1,9 +1,9 @@
import { lighthouseConfig } from '../../../lighthouse.config'
-export default async (blob: any, apiKey: string, mimeType = '') => {
+export default async (blob: any, apiKey: string, cidVersion: number) => {
try {
const token = 'Bearer ' + apiKey
- const endpoint = lighthouseConfig.lighthouseNode + '/api/v0/add'
+ const endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}`
// Upload file
const formData = new FormData()
@@ -13,19 +13,19 @@ export default async (blob: any, apiKey: string, mimeType = '') => {
method: 'POST',
body: formData,
headers: {
- 'Mime-Type': mimeType,
Authorization: token,
},
})
if (!response.ok) {
- throw new Error(`Request failed with status code ${response.status}`)
+ const res = (await response.json())
+ throw new Error(res.error)
}
const data = await response.json()
return { data }
} catch (error: any) {
- throw new Error(error?.message)
+ throw new Error(error)
}
}
diff --git a/src/Lighthouse/upload/buffer/index.ts b/src/Lighthouse/upload/buffer/index.ts
index e1761ed..9e79829 100644
--- a/src/Lighthouse/upload/buffer/index.ts
+++ b/src/Lighthouse/upload/buffer/index.ts
@@ -1,12 +1,12 @@
import uploadBuffer from './node'
import uploadTypedArray from './browser'
-export default async (buffer: any, apiKey: string) => {
+export default async (buffer: any, apiKey: string, cidVersion: number = 1) => {
// Upload File to IPFS
//@ts-ignore
if (typeof window === 'undefined') {
- return await uploadBuffer(buffer, apiKey)
+ return await uploadBuffer(buffer, apiKey, cidVersion)
} else {
- return await uploadTypedArray(buffer, apiKey)
+ return await uploadTypedArray(buffer, apiKey, cidVersion)
}
}
diff --git a/src/Lighthouse/upload/buffer/node.ts b/src/Lighthouse/upload/buffer/node.ts
index a480bb7..11be7f5 100644
--- a/src/Lighthouse/upload/buffer/node.ts
+++ b/src/Lighthouse/upload/buffer/node.ts
@@ -1,9 +1,9 @@
import { lighthouseConfig } from '../../../lighthouse.config'
-export default async (buffer: any, apiKey: string, mimeType = '') => {
+export default async (buffer: any, apiKey: string, cidVersion: number) => {
try {
const token = 'Bearer ' + apiKey
- const endpoint = lighthouseConfig.lighthouseNode + '/api/v0/add'
+ const endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}`
// Upload file
const blob = new Blob([buffer])
@@ -14,17 +14,18 @@ export default async (buffer: any, apiKey: string, mimeType = '') => {
method: 'POST',
body: formData,
headers: {
- Authorization: token,
- 'Mime-Type': mimeType,
+ Authorization: token
},
})
- if (!response.ok)
- throw new Error(`Request failed with status code ${response.status}`)
+ if (!response.ok) {
+ const res = (await response.json())
+ throw new Error(res.error)
+ }
const data = await response.json()
return { data }
} catch (error: any) {
- throw new Error(error?.message)
+ throw new Error(error)
}
}
diff --git a/src/Lighthouse/upload/files/browser.ts b/src/Lighthouse/upload/files/browser.ts
index 4286048..505347d 100644
--- a/src/Lighthouse/upload/files/browser.ts
+++ b/src/Lighthouse/upload/files/browser.ts
@@ -2,24 +2,23 @@
import { lighthouseConfig } from '../../../lighthouse.config'
import {
IUploadProgressCallback,
- UploadFileReturnType,
- DealParameters,
+ IFileUploadedResponse
} from '../../../types'
import { fetchWithTimeout } from '../../utils/util'
// eslint-disable-next-line @typescript-eslint/no-empty-function
-export default async (
+export default async (
files: any,
accessToken: string,
- dealParameters: DealParameters | undefined,
+ cidVersion: number,
uploadProgressCallback?: (data: IUploadProgressCallback) => void
-): Promise<{ data: UploadFileReturnType }> => {
+): Promise<{ data: IFileUploadedResponse }> => {
try {
const isDirectory = [...files].some(file => file.webkitRelativePath)
- let endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=false`
+ let endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=false&cid-version=${cidVersion}`
if(!isDirectory && files.length > 1) {
- endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=true`
+ endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=true&cid-version=${cidVersion}`
}
const formData = new FormData()
@@ -30,10 +29,7 @@ export default async (
const token = 'Bearer ' + accessToken
const headers = new Headers({
- Authorization: token,
- 'X-Deal-Parameter': dealParameters
- ? JSON.stringify(dealParameters)
- : 'null',
+ Authorization: token
})
const response = uploadProgressCallback
@@ -56,12 +52,13 @@ export default async (
})
if (!response.ok) {
- throw new Error(`Request failed with status code ${response.status}`)
+ const res = (await response.json())
+ throw new Error(res.error)
}
- const responseText = await response.text()
- return { data: JSON.parse(responseText) }
+ const responseData = (await response.json())
+ return { data: responseData }
} catch (error: any) {
- throw new Error(error?.message)
+ throw new Error(error)
}
}
diff --git a/src/Lighthouse/upload/files/index.ts b/src/Lighthouse/upload/files/index.ts
index a436f59..d55de93 100644
--- a/src/Lighthouse/upload/files/index.ts
+++ b/src/Lighthouse/upload/files/index.ts
@@ -2,39 +2,24 @@ import uploadFile from './node'
import uploadFileBrowser from './browser'
import {
IUploadProgressCallback,
- IFileUploadedResponse,
- DealParameters,
+ IFileUploadedResponse
} from '../../../types'
-async function uploadFiles(
- sourcePath: string | any,
- apiKey: string,
- dealParameters?: DealParameters,
- uploadProgressCallback?: (data: IUploadProgressCallback) => void
-): Promise<{ data: IFileUploadedResponse }>
-
-async function uploadFiles(
- sourcePath: string | any,
- apiKey: string,
- dealParameters?: DealParameters,
- uploadProgressCallback?: (data: IUploadProgressCallback) => void
-): Promise<{ data: IFileUploadedResponse[] }>
-
async function uploadFiles(
path: string | any,
apiKey: string,
- dealParameters?: DealParameters,
+ cidVersion: number = 1,
uploadProgressCallback?: (data: IUploadProgressCallback) => void
-) {
+): Promise<{ data: IFileUploadedResponse }> {
// Upload File to IPFS
//@ts-ignore
if (typeof window === 'undefined') {
- return await uploadFile(path, apiKey, dealParameters)
+ return await uploadFile(path, apiKey, cidVersion)
} else {
return await uploadFileBrowser(
path,
apiKey,
- dealParameters,
+ cidVersion,
uploadProgressCallback
)
}
diff --git a/src/Lighthouse/upload/files/node.ts b/src/Lighthouse/upload/files/node.ts
index 2420571..3dfc731 100644
--- a/src/Lighthouse/upload/files/node.ts
+++ b/src/Lighthouse/upload/files/node.ts
@@ -1,8 +1,7 @@
import basePathConvert from '../../utils/basePathConvert'
import { lighthouseConfig } from '../../../lighthouse.config'
-import { UploadFileReturnType, DealParameters } from '../../../types'
import { fetchWithTimeout } from '../../utils/util'
-
+import { IFileUploadedResponse } from '../../../types'
export async function walk(dir: string) {
const { readdir, stat } = eval(`require`)('fs-extra')
let results: string[] = []
@@ -22,11 +21,11 @@ export async function walk(dir: string) {
return results
}
-export default async (
+export default async (
sourcePath: string,
apiKey: string,
- dealParameters: DealParameters | undefined
-): Promise<{ data: UploadFileReturnType }> => {
+ cidVersion: number
+): Promise<{ data: IFileUploadedResponse }> => {
const { createReadStream, lstatSync } = eval(`require`)('fs-extra')
const path = eval(`require`)('path')
@@ -35,7 +34,7 @@ export default async (
try {
const endpoint =
lighthouseConfig.lighthouseNode +
- `/api/v0/add?wrap-with-directory=false`
+ `/api/v0/add?wrap-with-directory=false&cid-version=${cidVersion}`
if (stats.isFile()) {
const data = new FormData()
const stream = createReadStream(sourcePath)
@@ -52,20 +51,16 @@ export default async (
body: data,
timeout: 7200000,
headers: {
- Authorization: token,
- 'X-Deal-Parameter': dealParameters
- ? JSON.stringify(dealParameters)
- : 'null',
+ Authorization: token
},
})
if (!response.ok) {
- throw new Error(`Request failed with status code ${response.status}`)
+ const res = (await response.json())
+ throw new Error(res.error)
}
- let responseData = (await response.text()) as any
- responseData = JSON.parse(responseData)
-
+ const responseData = (await response.json())
return { data: responseData }
} else {
const files = await walk(sourcePath)
@@ -91,23 +86,19 @@ export default async (
body: data,
timeout: 7200000,
headers: {
- Authorization: token,
- 'X-Deal-Parameter': dealParameters
- ? JSON.stringify(dealParameters)
- : 'null',
+ Authorization: token
},
})
if (!response.ok) {
- throw new Error(`Request failed with status code ${response.status}`)
+ const res = (await response.json())
+ throw new Error(res.error)
}
- let responseData = (await response.text()) as any
- responseData = JSON.parse(responseData)
-
+ const responseData = (await response.json())
return { data: responseData }
}
} catch (error: any) {
- throw new Error(error.message)
+ throw new Error(error)
}
}
diff --git a/src/Lighthouse/upload/text/browser.ts b/src/Lighthouse/upload/text/browser.ts
index d7d8fdd..f46b90e 100644
--- a/src/Lighthouse/upload/text/browser.ts
+++ b/src/Lighthouse/upload/text/browser.ts
@@ -1,10 +1,10 @@
import { lighthouseConfig } from '../../../lighthouse.config'
import { fetchWithTimeout } from '../../utils/util'
-export default async (text: string, apiKey: string, name: string) => {
+export default async (text: string, apiKey: string, name: string, cidVersion: number) => {
try {
const token = 'Bearer ' + apiKey
- const endpoint = lighthouseConfig.lighthouseNode + '/api/v0/add'
+ const endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}`
// Upload file
const formData = new FormData()
@@ -16,18 +16,18 @@ export default async (text: string, apiKey: string, name: string) => {
body: formData,
timeout: 7200000,
headers: {
- 'Mime-Type': 'text/plain',
Authorization: token,
},
})
if (!response.ok) {
- throw new Error(`Request failed with status code ${response.status}`)
+ const res = (await response.json())
+ throw new Error(res.error)
}
const data = await response.json()
return { data }
} catch (error: any) {
- throw new Error(error?.message)
+ throw new Error(error)
}
}
diff --git a/src/Lighthouse/upload/text/index.ts b/src/Lighthouse/upload/text/index.ts
index 07c3d0a..4d645a9 100644
--- a/src/Lighthouse/upload/text/index.ts
+++ b/src/Lighthouse/upload/text/index.ts
@@ -1,12 +1,12 @@
import uploadTextServer from './node'
import uploadTextBrowser from './browser'
-export default async (text: string, apiKey: string, name = 'text') => {
+export default async (text: string, apiKey: string, name = 'text', cidVersion: number = 1) => {
// Upload File to IPFS
//@ts-ignore
if (typeof window === 'undefined') {
- return await uploadTextServer(text, apiKey, name)
+ return await uploadTextServer(text, apiKey, name, cidVersion)
} else {
- return await uploadTextBrowser(text, apiKey, name)
+ return await uploadTextBrowser(text, apiKey, name, cidVersion)
}
}
diff --git a/src/Lighthouse/upload/text/node.ts b/src/Lighthouse/upload/text/node.ts
index 85bbbc0..8c2257c 100644
--- a/src/Lighthouse/upload/text/node.ts
+++ b/src/Lighthouse/upload/text/node.ts
@@ -1,10 +1,10 @@
import { lighthouseConfig } from '../../../lighthouse.config'
import { fetchWithTimeout } from '../../utils/util'
-export default async (text: string, apiKey: string, name: string) => {
+export default async (text: string, apiKey: string, name: string, cidVersion: number) => {
try {
const token = 'Bearer ' + apiKey
- const endpoint = lighthouseConfig.lighthouseNode + '/api/v0/add'
+ const endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?cid-version=${cidVersion}`
// Upload file
const formData = new FormData()
@@ -18,18 +18,18 @@ export default async (text: string, apiKey: string, name: string) => {
credentials: 'include',
timeout: 7200000,
headers: {
- 'Mime-Type': 'text/plain',
Authorization: token,
},
})
if (!response.ok) {
- throw new Error(`Request failed with status code ${response.status}`)
+ const res = (await response.json())
+ throw new Error(res.error)
}
const data = await response.json()
return { data }
} catch (error: any) {
- throw new Error(error?.message)
+ throw new Error(error)
}
}
diff --git a/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts b/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts
index a39822c..c833343 100644
--- a/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts
+++ b/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts
@@ -29,6 +29,7 @@ export default async (
apiKey: string,
publicKey: string,
auth_token: string,
+ cidVersion: number,
uploadProgressCallback?: (data: IUploadProgressCallback) => void
): Promise<{ data: IFileUploadedResponse[] }> => {
try {
@@ -38,7 +39,7 @@ export default async (
mimeType = files[0].type
}
const endpoint =
- lighthouseConfig.lighthouseNode + '/api/v0/add?wrap-with-directory=false'
+ lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=false&cid-version=${cidVersion}`
const token = 'Bearer ' + apiKey
const fileArr = []
@@ -98,27 +99,13 @@ export default async (
},
})
if (!response.ok) {
- throw new Error(`HTTP error! status: ${response.status}`)
+ const res = (await response.json())
+ throw new Error(res.error)
}
-
- // const reader = response.body?.getReader()
- // let chunks = []
- // while (true) {
- // const { done, value } = await reader!.read()
- // if (done) {
- // break
- // }
- // chunks.push(value)
- // }
-
- // let responseData = new TextDecoder('utf-8').decode(
- // new Uint8Array(chunks.flatMap((chunk) => [...chunk]))
- // ) as any
+
const responseText = await response.text()
const jsondata = JSON.parse(responseText) as IFileUploadedResponse[]
- // responseData = JSON.parse(responseData)
-
const savedKey = await Promise.all(
jsondata.map(async (data: IFileUploadedResponse) => {
return saveShards(publicKey, data.Hash, auth_token, keyMap[data.Name])
diff --git a/src/Lighthouse/uploadEncrypted/encrypt/file/index.ts b/src/Lighthouse/uploadEncrypted/encrypt/file/index.ts
index aacc026..cb996fe 100644
--- a/src/Lighthouse/uploadEncrypted/encrypt/file/index.ts
+++ b/src/Lighthouse/uploadEncrypted/encrypt/file/index.ts
@@ -6,18 +6,20 @@ export default async (
apiKey: string,
publicKey: string,
signedMessage: string,
+ cidVersion: number = 1,
uploadProgressCallback?: (data: any) => void
) => {
// Upload File to IPFS
//@ts-ignore
if (typeof window === 'undefined') {
- return await uploadFile(path, apiKey, publicKey, signedMessage)
+ return await uploadFile(path, apiKey, publicKey, signedMessage, cidVersion)
} else {
return await browser(
path,
apiKey,
publicKey,
signedMessage,
+ cidVersion,
uploadProgressCallback ||
(() => {
return
diff --git a/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts b/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts
index 3ffa2e8..0a9d803 100644
--- a/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts
+++ b/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts
@@ -9,12 +9,14 @@ export default async (
sourcePath: any,
apiKey: string,
publicKey: string,
- auth_token: string
+ auth_token: string,
+ cidVersion: number
): Promise<{ data: IFileUploadedResponse[] }> => {
const fs = eval('require')('fs-extra')
const token = 'Bearer ' + apiKey
const endpoint =
- lighthouseConfig.lighthouseNode + '/api/v0/add?wrap-with-directory=false'
+ lighthouseConfig.lighthouseNode +
+ `/api/v0/add?wrap-with-directory=false&cid-version=${cidVersion}`
const stats = fs.lstatSync(sourcePath)
if (stats.isFile()) {
@@ -39,7 +41,8 @@ export default async (
})
if (!response.ok) {
- throw new Error(`Request failed with status code ${response.status}`)
+ const res = await response.json()
+ throw new Error(res.error)
}
const responseData = (await response.json()) as any
@@ -56,7 +59,7 @@ export default async (
return { data: responseData }
} catch (error: any) {
- throw new Error(error.message)
+ throw new Error(error)
}
} else {
const files = await walk(sourcePath)
@@ -92,11 +95,19 @@ export default async (
})
if (!response.ok) {
- throw new Error(`Request failed with status code ${response.status}`)
+ const res = await response.json()
+ throw new Error(res.error)
}
const responseText = await response.text()
- const jsondata = JSON.parse(responseText) as IFileUploadedResponse[]
+ let jsondata: IFileUploadedResponse[] = []
+
+ const match = responseText.match(/\[.*\]$/s)
+ if (match) {
+ jsondata = JSON.parse(match[0])
+ } else {
+ throw new Error('No JSON array found in response')
+ }
const savedKey = await Promise.all(
jsondata.map(async (data) => {
diff --git a/src/Lighthouse/uploadEncrypted/encrypt/text/node.ts b/src/Lighthouse/uploadEncrypted/encrypt/text/node.ts
index edf594d..8b42a24 100644
--- a/src/Lighthouse/uploadEncrypted/encrypt/text/node.ts
+++ b/src/Lighthouse/uploadEncrypted/encrypt/text/node.ts
@@ -47,7 +47,7 @@ export default async (
const { error } = await saveShards(
publicKey,
- responseData.Hash,
+ responseData[0].Hash,
signedMessage,
keyShards
)
diff --git a/src/lighthouse.config.ts b/src/lighthouse.config.ts
index 7d93b6b..4866896 100644
--- a/src/lighthouse.config.ts
+++ b/src/lighthouse.config.ts
@@ -1,6 +1,6 @@
const defaultConfig = {
lighthouseAPI: 'https://api.lighthouse.storage',
- lighthouseNode: 'https://node.lighthouse.storage',
+ lighthouseNode: 'https://upload.lighthouse.storage',
lighthouseGateway: 'https://gateway.lighthouse.storage',
lighthouseBLSNode: 'https://encryption.lighthouse.storage',
network: 'polygon',
diff --git a/src/types.ts b/src/types.ts
index c677af2..523afe2 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -8,15 +8,6 @@ export interface IFileUploadedResponse {
Size: string
}
-export type DealParameters = {
- miner: string[]
- num_copies: number
- repair_threshold: number
- renew_threshold: number
- deal_duration: number
- network: string
-}
-
export type UploadFileReturnType = T extends true
? IFileUploadedResponse[]
: IFileUploadedResponse