Skip to content

Commit 9992e4e

Browse files
authored
Feat: enhance the integration test (#166)
* feat: create a extend private key use private key * Fix: the fail test * feat: add WALLET_ADDRESS to .env-example for configuration clarity * fix: increase sleep duration in transaction mining test to ensure reliability
1 parent ff4e31f commit 9992e4e

File tree

6 files changed

+84
-20
lines changed

6 files changed

+84
-20
lines changed

.env-example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
WALLET_PRIVATE_KEY=YOUR-PRIVATE-WALLET-KEY
2+
WALLET_ADDRESS=YOUR-WALLET-ADDRESS
23
RPC_PROVIDER_URL=YOUR_RPC_URL
34
ETHERSCAN_API_KEY=YOUR-ETHERSCAN-API-KEY-HERE

tests/integration/config/test_config.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
from dotenv import load_dotenv
44
from web3 import Web3
55

6+
from tests.integration.config.utils import (
7+
create_xprv_from_private_key,
8+
get_private_key_from_xprv,
9+
)
10+
611
# Load environment variables
712
load_dotenv(override=True)
813
private_key = os.getenv("WALLET_PRIVATE_KEY")
9-
private_key_2 = os.getenv("WALLET_PRIVATE_KEY_2")
1014
rpc_url = os.getenv("RPC_PROVIDER_URL")
1115
wallet_address = os.getenv("WALLET_ADDRESS")
12-
wallet_address_2 = os.getenv("WALLET_ADDRESS_2")
1316

1417
if not private_key:
1518
raise ValueError("WALLET_PRIVATE_KEY environment variable is not set")
@@ -23,8 +26,11 @@
2326

2427
# Set up the account with the private key
2528
account = web3.eth.account.from_key(private_key)
29+
# Create the secondary account via extended private key
30+
xprv = create_xprv_from_private_key(private_key)
31+
private_key_2 = get_private_key_from_xprv(xprv)
2632
account_2 = web3.eth.account.from_key(private_key_2)
27-
33+
wallet_address_2 = account_2.address
2834
# Export all configuration
2935
__all__ = [
3036
"web3",

tests/integration/config/utils.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import hashlib
2+
import hmac
23
import os
34

45
import base58
@@ -26,6 +27,44 @@
2627
CORE_METADATA_MODULE = "0x6E81a25C99C6e8430aeC7353325EB138aFE5DC16"
2728

2829

30+
def create_xprv_from_private_key(private_key: str) -> str:
31+
"""
32+
Create an extended private key (xprv) using a private key as seed.
33+
34+
Args:
35+
private_key: The private key (hex string with or without 0x prefix)
36+
37+
Returns:
38+
xprv string in base58check format
39+
"""
40+
seed = bytes.fromhex(private_key.removeprefix("0x"))
41+
hmac_result = hmac.new(b"Bitcoin seed", seed, hashlib.sha512).digest()
42+
43+
xprv_bytes = (
44+
bytes.fromhex("0488ADE4")
45+
+ bytes(9)
46+
+ hmac_result[32:]
47+
+ bytes([0])
48+
+ hmac_result[:32]
49+
)
50+
checksum = hashlib.sha256(hashlib.sha256(xprv_bytes).digest()).digest()[:4]
51+
return base58.b58encode(xprv_bytes + checksum).decode()
52+
53+
54+
def get_private_key_from_xprv(xprv: str) -> str:
55+
"""
56+
Extract the private key from an xprv.
57+
58+
Args:
59+
xprv: Extended private key in base58check format
60+
61+
Returns:
62+
Private key as hex string with 0x prefix
63+
"""
64+
decoded = base58.b58decode(xprv)
65+
return "0x" + decoded[46:78].hex()
66+
67+
2968
def get_story_client(web3: Web3, account) -> StoryClient:
3069
chain_id = 1315 # aeneid chain ID
3170
return StoryClient(web3, account, chain_id)

tests/integration/conftest.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
ROYALTY_POLICY_LAP_ADDRESS,
1212
ZERO_ADDRESS,
1313
)
14-
from tests.integration.config.test_config import account, account_2, web3
14+
from tests.integration.config.test_config import account, account_2, private_key, web3
1515
from tests.integration.config.utils import MockERC20, approve, get_story_client
1616
from tests.integration.setup_for_integration import PIL_LICENSE_TEMPLATE
1717

@@ -25,22 +25,25 @@ def story_client() -> StoryClient:
2525
@pytest.fixture(scope="session")
2626
def story_client_2() -> StoryClient:
2727
"""Fixture to provide the secondary story client"""
28-
return get_story_client(web3, account_2)
28+
if not private_key:
29+
raise ValueError("Private key is not set")
2930

30-
31-
@pytest.fixture(scope="module")
32-
def nft_collection(story_client: StoryClient):
33-
"""Fixture to provide the SPG NFT collection"""
34-
tx_data = story_client.NFTClient.create_nft_collection(
35-
name="test-collection",
36-
symbol="TEST",
37-
max_supply=100,
38-
is_public_minting=True,
39-
mint_open=True,
40-
contract_uri="test-uri",
41-
mint_fee_recipient=account.address,
42-
)
43-
return tx_data["nft_contract"]
31+
story_client_2 = get_story_client(web3, account_2)
32+
balance = story_client_2.get_wallet_balance()
33+
if balance < web3.to_wei(5, "ether"):
34+
tx = {
35+
"from": account.address,
36+
"to": account_2.address,
37+
"value": web3.to_wei(5, "ether"),
38+
"nonce": web3.eth.get_transaction_count(account.address),
39+
"gas": 21000,
40+
"gasPrice": web3.eth.gas_price,
41+
"chainId": web3.eth.chain_id,
42+
}
43+
signed_tx = account.sign_transaction(tx)
44+
tx_hash = web3.eth.send_raw_transaction(signed_tx.raw_transaction)
45+
web3.eth.wait_for_transaction_receipt(tx_hash)
46+
return story_client_2
4447

4548

4649
@pytest.fixture(scope="module")
@@ -124,3 +127,17 @@ def mint_and_approve_license_token(
124127
)
125128

126129
return license_token_ids
130+
131+
132+
@pytest.fixture(scope="module")
133+
def nft_collection(story_client: StoryClient):
134+
tx_data = story_client.NFTClient.create_nft_collection(
135+
name="test-collection",
136+
symbol="TEST",
137+
max_supply=100,
138+
is_public_minting=True,
139+
mint_open=True,
140+
contract_uri="test-uri",
141+
mint_fee_recipient=account.address,
142+
)
143+
return tx_data["nft_contract"]

tests/integration/test_integration_transaction_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,5 @@ def build_tx(tx_options):
301301
build_tx,
302302
tx_options={"wait_for_receipt": True, "timeout": 0.001},
303303
)
304+
305+
time.sleep(5) # Wait for the transaction to be mined

tests/unit/resources/test_ip_asset.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,6 @@ def test_batch_mint_transaction_success(
20612061
) as mock_build_multicall_transaction:
20622062
result = ip_asset.batch_mint_and_register_ip(requests=requests)
20632063

2064-
print(mock_build_multicall_transaction.call_args[0][0])
20652064
assert mock_build_multicall_transaction.call_args[0][0] == [
20662065
b"encoded_data",
20672066
b"encoded_data",

0 commit comments

Comments
 (0)