Skip to content

Commit 1e77c8e

Browse files
Merge pull request #24 from unniznd/feature_create_wallet
feature: create wallet API added and tested
2 parents e0863e9 + f9ba7b1 commit 1e77c8e

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
ipns_publish_record as ipnsPublishRecord,
1515
get_ipns_record as getIpnsRecord,
1616
remove_ipns_record as removeIpnsRecord,
17+
create_wallet as createWallet
1718
)
1819

1920

@@ -106,6 +107,18 @@ def removeKey(self, keyName: str):
106107
return removeIpnsRecord.remove_ipns_record(self.token, keyName)
107108
except Exception as e:
108109
raise e
110+
111+
@staticmethod
112+
def createWallet(password: str):
113+
"""
114+
Creates a new wallet using the provided password.
115+
:param password: str, The password to secure the wallet.
116+
:return: dict, The wallet encrypted with the passowrd
117+
"""
118+
try:
119+
return createWallet.create_wallet(password)
120+
except Exception as e:
121+
raise e
109122

110123
@staticmethod
111124
def downloadBlob(dist: io.BufferedWriter, cid: str, chunk_size=1024*1024*10):
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)