-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdapp c
171 lines (78 loc) · 3.72 KB
/
dapp c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
Dapp connector: ( Hivesigner)
from flask import Flask, jsonify, request, redirect, session, url_for
from flask_oauthlib.client import OAuth
import requests
app = Flask(__name__)
app.secret_key = 'secret_key'
# Set up the OAuth client for Hivesigner
oauth = OAuth(app)
hivesigner = oauth.remote_app(
'hivesigner',
consumer_key='YOUR_APP_CONSUMER_KEY',
consumer_secret='YOUR_APP_CONSUMER_SECRET',
request_token_params={'scope': 'vote,comment,delete_post'},
base_url='https://hivesigner.com/api/',
access_token_method='POST',
access_token_url='https://hivesigner.com/oauth2/token',
authorize_url='https://hivesigner.com/oauth2/authorize'
)
# Define an endpoint for getting the balance of an account on a specific blockchain
@app.route('/balance/<blockchain>/<account>')
def get_balance(blockchain, account):
# Get the balance of the account from the blockchain node
balance = get_balance_from_node(blockchain, account)
if balance is not None:
# Return the balance as a JSON response
return jsonify({'balance': balance})
else:
# Return an error message if the balance could not be retrieved
return jsonify({'error': f'Could not retrieve balance for {account} on {blockchain}'})
# Define an endpoint for logging in with Hivesigner
@app.route('/login')
def login():
return hivesigner.authorize(callback=url_for('authorized', _external=True))
# Define an endpoint for handling the callback from Hivesigner after a successful login
@app.route('/authorized')
def authorized():
resp = hivesigner.authorized_response()
if resp is None:
# Return an error message if the authorization was denied
return jsonify({'error': 'Access denied'})
else:
# Store the access token in the user's session and redirect to the home page
session['oauth_token'] = (resp['access_token'], '')
return redirect(url_for('index'))
# Define the home page for the API
@app.route('/')
def index():
# Check if the user is authenticated with Hivesigner
if 'oauth_token' in session:
# Get the user's account information from Hivesigner
account_info = get_account_info()
if account_info is not None:
# Return the user's account information as a JSON response
return jsonify(account_info)
else:
# Return an error message if the account information could not be retrieved
return jsonify({'error': 'Could not retrieve account information'})
else:
# Redirect the user to the Hivesigner login page
return redirect(url_for('login'))
# Define a function for getting the balance of an account on a specific blockchain
def get_balance_from_node(blockchain, account):
# Determine the URL of the blockchain node based on the blockchain name
if blockchain == 'hive':
node_url = 'https://api.hive.blog'
elif blockchain == 'steem':
node_url = 'https://api.steemit.com'
else:
# Return None if an invalid blockchain name is provided
return None
# Make a request to the blockchain node to get the account's balance
response = requests.post(node_url, json={
'jsonrpc': '2.0',
'method': 'condenser_api.get_account_balances',
'params': [account, []],
'id': 1
})
# Parse the response to get the account's