Skip to content

Commit 7487f6d

Browse files
committed
feature: create wallet API added and tested
1 parent 678e49e commit 7487f6d

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ certifi==2023.5.7
22
charset-normalizer==3.1.0
33
idna==3.4
44
requests==2.31.0
5-
urllib3==2.0.2
5+
urllib3==2.0.2
6+
eth-account==0.13.7

src/lighthouseweb3/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import os
44
import io
5-
from .functions import upload as d,deal_status, get_uploads as getUploads, download as _download
5+
from .functions import (
6+
upload as d,
7+
deal_status,
8+
get_uploads as getUploads,
9+
download as _download,
10+
create_wallet as createWallet
11+
)
612

713

814
class Lighthouse:
@@ -39,6 +45,19 @@ def uploadBlob(self, source: io.BufferedReader, filename: str, tag: str = ''):
3945
except Exception as e:
4046
raise e
4147

48+
@staticmethod
49+
def createWallet(password: str):
50+
"""
51+
Creates a new wallet using the provided password.
52+
53+
:param password: str, The password to secure the wallet.
54+
:return: dict, The wallet encrypted with the passowrd
55+
"""
56+
try:
57+
return createWallet.create_wallet(password)
58+
except Exception as e:
59+
raise e
60+
4261
@staticmethod
4362
def downloadBlob(dist: io.BufferedWriter, cid: str, chunk_size=1024*1024*10):
4463
"""
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import requests as req
2+
from .config import Config
3+
4+
from eth_account import Account
5+
6+
def create_wallet(password: str):
7+
wallet = Account.create()
8+
9+
url = f"{Config.lighthouse_api}/api/auth/get_auth_message?publicKey={wallet.address}"
10+
11+
try:
12+
response = req.get(url)
13+
except Exception as e:
14+
raise Exception("Failed to create wallet")
15+
16+
if response.status_code != 200:
17+
return response.json()
18+
19+
encrypted_wallet = wallet.encrypt(password)
20+
21+
del wallet
22+
23+
return encrypted_wallet

tests/test_create_wallet.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
import unittest
3+
from src.lighthouseweb3 import Lighthouse
4+
from eth_account import Account
5+
6+
7+
class TestCreateWallet(unittest.TestCase):
8+
9+
def test_create_wallet(self):
10+
"""test static create wallet function"""
11+
password = 'TestPassword'
12+
encrypted_wallet = Lighthouse.createWallet(password)
13+
14+
private_key = Account.decrypt(encrypted_wallet, password)
15+
wallet = Account.from_key(private_key)
16+
self.assertEqual(wallet.address, f"0x{encrypted_wallet['address']}", 'Both public key must be same')
17+
18+
def test_create_wallet_different_password(self):
19+
"""test static create wallet function use different password for decryption"""
20+
encrypted_wallet = Lighthouse.createWallet('TestPassword')
21+
with self.assertRaises(Exception) as context:
22+
private_key = Account.decrypt(encrypted_wallet, 'RandomPassword')
23+
self.assertIn("valueerror", str(context).lower())
24+

0 commit comments

Comments
 (0)