Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
Blockchain Forensics using OSINT and Graph Temporal Logic
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mohamed feroz khan D
Blockchain Forensics using OSINT and Graph Temporal Logic
Commits
8b824388
Commit
8b824388
authored
1 year ago
by
Mohamed feroz khan D
Browse files
Options
Downloads
Patches
Plain Diff
Python Program for Visualization
parent
592122bb
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Assets/Temporal_Graph/Bitcoin/Visualization/Visualization_with_Four_Address/visual.py
+134
-0
134 additions, 0 deletions
...n/Visualization/Visualization_with_Four_Address/visual.py
with
134 additions
and
0 deletions
Assets/Temporal_Graph/Bitcoin/Visualization/Visualization_with_Four_Address/visual.py
0 → 100644
+
134
−
0
View file @
8b824388
import
requests
import
pandas
as
pd
import
networkx
as
nx
import
matplotlib.pyplot
as
plt
from
datetime
import
datetime
def
fetch_address_data
(
address
):
"""
Fetches address data from blockchain.info API.
Args:
address (str): The Bitcoin address to fetch data for.
Returns:
dict: The fetched address data in JSON format.
"""
url
=
f
"
https://blockchain.info/address/
{
address
}
?format=json
"
response
=
requests
.
get
(
url
)
data
=
response
.
json
()
return
data
def
transform_data
(
data
):
"""
Transforms address data to extract relevant information.
Args:
data (dict): The address data to be transformed.
Returns:
list: List of transactions with extracted information.
"""
transactions
=
[]
for
tx
in
data
[
"
txs
"
]:
if
"
inputs
"
in
tx
and
"
prev_out
"
in
tx
[
"
inputs
"
][
0
]
and
"
addr
"
in
tx
[
"
inputs
"
][
0
][
"
prev_out
"
]:
address_a
=
tx
[
"
inputs
"
][
0
][
"
prev_out
"
][
"
addr
"
]
else
:
address_a
=
None
for
out
in
tx
[
"
out
"
]:
if
"
addr
"
in
out
:
address_b
=
out
[
"
addr
"
]
transaction_id
=
tx
[
"
hash
"
]
transaction
=
{
"
Address A
"
:
address_a
,
"
Address B
"
:
address_b
,
"
Transaction ID
"
:
transaction_id
}
transactions
.
append
(
transaction
)
return
transactions
def
export_to_excel
(
data
,
filename
):
"""
Exports data to an Excel file.
Args:
data (list): The data to be exported.
filename (str): The name of the Excel file to be created.
Returns:
None
"""
df
=
pd
.
DataFrame
(
data
)
df
.
to_excel
(
filename
,
index
=
False
)
print
(
f
"
Data exported to
{
filename
}
"
)
def
create_and_visualize_graph
(
df
,
user_addresses
):
"""
Creates a graph and visualizes it using matplotlib.
Args:
df (DataFrame): The DataFrame containing the transaction data.
user_addresses (list): List of user addresses to highlight in the graph.
Returns:
None
"""
# Create a networkx graph
graph
=
nx
.
DiGraph
()
# Add nodes to the graph
for
address
in
df
[
'
Address A
'
].
unique
():
if
address
in
user_addresses
:
# Highlight user address nodes in red
graph
.
add_node
(
address
,
color
=
'
red
'
)
else
:
graph
.
add_node
(
address
,
color
=
'
skyblue
'
)
# Add edges with attributes to the graph
for
_
,
row
in
df
.
iterrows
():
source
=
str
(
row
[
'
Address A
'
])
target
=
str
(
row
[
'
Address B
'
])
if
source
in
user_addresses
or
target
in
user_addresses
:
# Highlight edges connected to user address in blue
graph
.
add_edge
(
source
,
target
,
color
=
'
blue
'
)
if
source
in
user_addresses
:
graph
.
nodes
[
source
][
'
color
'
]
=
'
red
'
if
target
in
user_addresses
:
graph
.
nodes
[
target
][
'
color
'
]
=
'
red
'
else
:
graph
.
add_edge
(
source
,
target
,
color
=
'
gray
'
)
# Draw the graph using matplotlib
plt
.
figure
(
figsize
=
(
10
,
6
))
pos
=
nx
.
spring_layout
(
graph
)
node_colors
=
[
graph
.
nodes
[
node
].
get
(
'
color
'
,
'
skyblue
'
)
for
node
in
graph
.
nodes
]
edge_colors
=
[
graph
.
edges
[
edge
][
'
color
'
]
for
edge
in
graph
.
edges
]
nx
.
draw_networkx
(
graph
,
pos
,
with_labels
=
True
,
node_size
=
500
,
font_size
=
8
,
node_color
=
node_colors
,
edge_color
=
edge_colors
)
# Show the graph
plt
.
tight_layout
()
plt
.
show
()
# Main program
num_addresses
=
int
(
input
(
"
Enter the Number of Addresses to Visualize (1-4):
"
))
user_addresses
=
[]
for
i
in
range
(
num_addresses
):
address
=
input
(
f
"
Enter address
{
i
+
1
}
of
{
num_addresses
}
:
"
)
user_addresses
.
append
(
address
)
# Fetch address data for each user address
all_transformed_data
=
[]
for
address
in
user_addresses
:
address_data
=
fetch_address_data
(
address
)
transformed_data
=
transform_data
(
address_data
)
all_transformed_data
.
extend
(
transformed_data
)
# Export all data to a single Excel file
export_to_excel
(
all_transformed_data
,
"
Data.xlsx
"
)
# Read the Excel file
df
=
pd
.
read_excel
(
'
Data.xlsx
'
)
# Create and visualize the graph, passing the user addresses as an argument
create_and_visualize_graph
(
df
,
user_addresses
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment