Skip to content

Commit 7492f43

Browse files
authored
Create token_storage.py
1 parent 445e20b commit 7492f43

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

tokenization/token_storage.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# tokenization/token_storage.py
2+
3+
import requests
4+
import logging
5+
6+
# Configure logging
7+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
8+
9+
class TokenStorage:
10+
def __init__(self, blockchain_url):
11+
"""
12+
Initialize the TokenStorage with the blockchain URL.
13+
14+
:param blockchain_url: The URL of the blockchain API.
15+
"""
16+
self.blockchain_url = blockchain_url
17+
18+
def store_token(self, token, metadata):
19+
"""
20+
Store a token in the blockchain along with its metadata.
21+
22+
:param token: The token to store.
23+
:param metadata: Additional metadata associated with the token.
24+
:return: Response from the blockchain API.
25+
"""
26+
payload = {
27+
"token": token,
28+
"metadata": metadata
29+
}
30+
31+
try:
32+
response = requests.post(f"{self.blockchain_url}/store_token", json=payload)
33+
response.raise_for_status() # Raise an error for bad responses
34+
logging.info(f"Token stored successfully: {token}")
35+
return response.json()
36+
except requests.exceptions.RequestException as e:
37+
logging.error(f"Failed to store token: {e}")
38+
return None
39+
40+
def retrieve_token(self, token_id):
41+
"""
42+
Retrieve a token from the blockchain using its identifier.
43+
44+
:param token_id: The identifier of the token to retrieve.
45+
:return: The token data if found, otherwise None.
46+
"""
47+
try:
48+
response = requests.get(f"{self.blockchain_url}/retrieve_token/{token_id}")
49+
response.raise_for_status() # Raise an error for bad responses
50+
logging.info(f"Token retrieved successfully: {token_id}")
51+
return response.json()
52+
except requests.exceptions.RequestException as e:
53+
logging.error(f"Failed to retrieve token: {e}")
54+
return None
55+
56+
if __name__ == "__main__":
57+
# Example usage
58+
blockchain_url = "http://localhost:5000/api" # Replace with your blockchain API URL
59+
token_storage = TokenStorage(blockchain_url)
60+
61+
# Sample token and metadata
62+
token = "example_token_123456"
63+
metadata = {
64+
"owner": "user_001",
65+
"timestamp": 1633072800,
66+
"data_type": "DNA",
67+
"description": "Sample DNA sequence token"
68+
}
69+
70+
# Store the token
71+
store_response = token_storage.store_token(token, metadata)
72+
print("Store Response:", store_response)
73+
74+
# Retrieve the token
75+
token_id = "example_token_123456" # Replace with the actual token ID
76+
retrieve_response = token_storage.retrieve_token(token_id)
77+
print("Retrieve Response:", retrieve_response)

0 commit comments

Comments
 (0)