Skip to content

Commit 45d55f3

Browse files
committed
Merge branch 'main' into feature_create_wallet
2 parents 7487f6d + e0863e9 commit 45d55f3

19 files changed

+593
-29
lines changed

requirements-dev.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ charset-normalizer==3.1.0
44
idna==3.4
55
requests==2.31.0
66
urllib3==2.0.2
7+
web3
8+
eth-accounts

src/lighthouseweb3/__init__.py

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
import os
44
import io
5-
from .functions import (
5+
from .functions import (
66
upload as d,
77
deal_status,
88
get_uploads as getUploads,
99
download as _download,
10-
create_wallet as createWallet
10+
get_file_info as getFileInfo,
11+
get_balance as getBalance,
12+
get_api_key as getApiKey,
13+
ipns_generate_key as ipnsGenerateKey,
14+
ipns_publish_record as ipnsPublishRecord,
15+
get_ipns_record as getIpnsRecord,
16+
remove_ipns_record as removeIpnsRecord,
1117
)
1218

1319

@@ -44,20 +50,63 @@ def uploadBlob(self, source: io.BufferedReader, filename: str, tag: str = ''):
4450
return d.uploadBlob(source, filename, self.token, tag)
4551
except Exception as e:
4652
raise e
53+
54+
def getBalance(self):
55+
"""
56+
Retrieve the balance information of a user from the Lighthouse.
57+
:param publicKey: str, The public key of the user.
58+
:return: dict[str, any], A dictionary containing the data usage and data limit details.
59+
"""
60+
try:
61+
return getBalance.get_balance(self.token)
62+
except Exception as e:
63+
raise e
64+
65+
def generateKey(self):
66+
"""
67+
Generate a new IPNS key for the authenticated user.
68+
:return: dict, The generated IPNS key information.
69+
"""
70+
try:
71+
return ipnsGenerateKey.ipns_generate_key(self.token)
72+
except Exception as e:
73+
raise e
4774

48-
@staticmethod
49-
def createWallet(password: str):
75+
def publishRecord(self, cid: str, keyName: str):
5076
"""
51-
Creates a new wallet using the provided password.
77+
Publish an IPNS record for a given CID and key name.
78+
:param cid: str, Content Identifier to publish
79+
:param keyName: str, Name of the IPNS key to use
80+
:return: dict, The published IPNS record information
81+
"""
82+
try:
83+
return ipnsPublishRecord.ipns_publish_record(self.token, cid, keyName)
84+
except Exception as e:
85+
raise e
5286

53-
:param password: str, The password to secure the wallet.
54-
:return: dict, The wallet encrypted with the passowrd
87+
def getAllKeys(self):
88+
"""
89+
Retrieves all IPNS records associated with the current token.
90+
return: list A list of IPNS records retrieved using the provided token.
5591
"""
92+
5693
try:
57-
return createWallet.create_wallet(password)
94+
return getIpnsRecord.get_ipns_records(self.token)
5895
except Exception as e:
5996
raise e
60-
97+
98+
def removeKey(self, keyName: str):
99+
"""
100+
Remove IPNS record of the given keyName
101+
:param keyName: str, Name of the IPNS key to use
102+
:return: dict, A dict of removed IPNS record.
103+
"""
104+
105+
try:
106+
return removeIpnsRecord.remove_ipns_record(self.token, keyName)
107+
except Exception as e:
108+
raise e
109+
61110
@staticmethod
62111
def downloadBlob(dist: io.BufferedWriter, cid: str, chunk_size=1024*1024*10):
63112
"""
@@ -87,18 +136,16 @@ def getDealStatus(cid: str):
87136
return deal_status.get_deal_status(cid)
88137
except Exception as e:
89138
raise e
90-
91-
@staticmethod
92-
def getUploads(publicKey: str, pageNo: int = 1):
139+
140+
def getUploads(self, lastKey: str = None):
93141
"""
94142
Get uploads from the Lighthouse.
95143
96-
:param publicKey: str, public key
97-
:param pageNo: int, page number (default: 1)
144+
:param lastKey: To navigate to different pages of results
98145
:return: List[t.DealData], list of deal data
99146
"""
100147
try:
101-
return getUploads.get_uploads(publicKey, pageNo)
148+
return getUploads.get_uploads(self.token, lastKey)
102149
except Exception as e:
103150
raise e
104151

@@ -115,6 +162,34 @@ def download(cid: str):
115162
return _download.get_file(cid)
116163
except Exception as e:
117164
raise e
165+
166+
@staticmethod
167+
def getFileInfo(cid: str):
168+
"""
169+
Retrieves information about a file using its CID (Content Identifier).
170+
:param cid: str, Content Identifier for the data to be downloaded
171+
returns: dict, A dictionary containing file information.
172+
"""
173+
174+
try:
175+
return getFileInfo.get_file_info(cid)
176+
except Exception as e:
177+
raise e
178+
179+
@staticmethod
180+
def getApiKey(publicKey: str, signedMessage: str):
181+
"""
182+
Generates and returns an API key for the given public key and signed message.
183+
:param publicKey: str, The public key associated with the user.
184+
:param signedMessage: str, The message signed by the user's private key.
185+
:return: dict, A dict with generated API key.
186+
"""
187+
188+
189+
try:
190+
return getApiKey.get_api_key(publicKey, signedMessage)
191+
except Exception as e:
192+
raise e
118193

119194
def getTagged(self, tag: str):
120195
"""
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def get_api_key(publicKey: str, signedMessage: str):
5+
url = f"{Config.lighthouse_api}/api/auth/create_api_key"
6+
7+
data = {
8+
"publicKey": publicKey,
9+
"signedMessage": signedMessage
10+
}
11+
12+
try:
13+
response = req.post(url, data=data)
14+
except Exception as e:
15+
raise Exception("Failed to create api key")
16+
17+
if response.status_code != 200:
18+
return response.json()
19+
20+
apiKey = response.json()
21+
22+
result = {
23+
"data": {
24+
"apiKey" : apiKey
25+
}
26+
}
27+
28+
return result
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def get_balance(token:str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/user/user_data_usage"
9+
try:
10+
response = req.get(url, headers=headers)
11+
except Exception as e:
12+
raise Exception("Failed to get account balance")
13+
14+
return response.json()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def get_file_info(cid: str):
5+
url = f"{Config.lighthouse_api}/api/lighthouse/file_info?cid={cid}"
6+
try:
7+
response = req.get(url)
8+
except Exception as e:
9+
raise Exception("Failed to get file metadata")
10+
11+
return response.json()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def get_ipns_records(token: str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/ipns/get_ipns_records"
9+
try:
10+
response = req.get(url, headers=headers)
11+
except Exception as e:
12+
raise Exception("Failed to get ipns records")
13+
14+
if response.status_code != 200:
15+
return response.json()
16+
17+
return {
18+
"data": response.json()
19+
}

src/lighthouseweb3/functions/get_uploads.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ def bytes_to_size(bytes_size):
1111
return f"{round(bytes_size, 2)} {units[index]}"
1212

1313

14-
def get_uploads(publicKey: str, pageNo: int = 1) :
14+
def get_uploads(token: str, lastKey: str = None) :
15+
headers = {
16+
"Authorization": f"Bearer {token}",
17+
}
18+
1519
try:
16-
url = f"{Config.lighthouse_api}/api/user/files_uploaded?publicKey={publicKey}&pageNo={pageNo}"
17-
response = requests.get(url)
20+
url = f"{Config.lighthouse_api}/api/user/files_uploaded?lastKey={lastKey}"
21+
response = requests.get(url, headers=headers)
1822
response.raise_for_status()
1923
return response.json()
2024
except requests.HTTPError as error:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def ipns_generate_key(token: str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/ipns/generate_key"
9+
try:
10+
response = req.get(url, headers=headers)
11+
except Exception as e:
12+
raise Exception("Failed to ipns generate key")
13+
14+
if response.status_code != 200:
15+
return response.json()
16+
17+
return {
18+
"data": response.json()
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def ipns_publish_record(token: str, cid: str, keyName: str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/ipns/publish_record?cid={cid}&keyName={keyName}"
9+
try:
10+
response = req.get(url, headers=headers)
11+
except Exception as e:
12+
raise Exception("Failed to ipns generate key")
13+
14+
if response.status_code != 200:
15+
return response.json()
16+
17+
return {
18+
"data": response.json()
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def remove_ipns_record(token: str, keyName: str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/ipns/remove_key?keyName={keyName}"
9+
try:
10+
response = req.delete(url, headers=headers)
11+
except Exception as e:
12+
raise Exception("Failed to remove ipns record")
13+
14+
if response.status_code != 200:
15+
return response.json()
16+
17+
return {
18+
"data": response.json()
19+
}

0 commit comments

Comments
 (0)