File tree Expand file tree Collapse file tree 4 files changed +62
-1
lines changed
Expand file tree Collapse file tree 4 files changed +62
-1
lines changed Original file line number Diff line number Diff line change @@ -2,4 +2,5 @@ certifi==2023.5.7
22charset-normalizer == 3.1.0
33idna == 3.4
44requests == 2.31.0
5- urllib3 == 2.0.2
5+ urllib3 == 2.0.2
6+ eth-account == 0.13.7
Original file line number Diff line number Diff line change 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 ):
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments