-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhellothereum.module
More file actions
224 lines (194 loc) · 8.1 KB
/
hellothereum.module
File metadata and controls
224 lines (194 loc) · 8.1 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Implements hook_admin_form().
*/
function hellothereum_user_admin_form() {
$form = array();
$form['ethereum_user_register_drupal_deployed_contract_address'] = array(
'#type' => 'textfield',
'#title' => t('Deployed contract address for your Ethereum Drupal user registry'),
'#default_value' => variable_get('ethereum_user_register_drupal_deployed_contract_address'),
);
$form['ethereum_user_register_drupal_deployed_contract_abi'] = array(
'#type' => 'textarea',
'#title' => t('Deployed contract ABI for your Ethereum Drupal user registry'),
'#default_value' => variable_get('ethereum_user_register_drupal_deployed_contract_abi'),
);
$form['ethereum_user_register_drupal_fallback_node'] = array(
'#type' => 'textfield',
'#title' => t('Fallback Ethereum node'),
'#default_value' => variable_get('ethereum_user_register_drupal_fallback_node', 'http://localhost:8545'),
);
$form['blockchain_Token_deployed_contract_address_fallback'] = array(
'#type' => 'textfield',
'#title' => t('Token: deployed contract address fallback'),
'#default_value' => variable_get('blockchain_Token_deployed_contract_address_fallback'),
);
$form['blockchain_Token_deployed_contract_ABI'] = array(
'#type' => 'textarea',
'#title' => t('Token: deployed contract ABI'),
'#default_value' => variable_get('blockchain_Token_deployed_contract_ABI'),
);
return system_settings_form($form);
}
function hellothereum_menu() {
$items = array();
$items['admin/hellothereum'] = array(
'title' => 'Hellothereum',
'page callback' => 'drupal_get_form',
'page arguments' => array('hellothereum_user_admin_form'),
'access arguments' => array('administer ethereum'),
'type' => MENU_NORMAL_ITEM,
);
$items['hellothereum/register'] = array(
'title' => 'Register',
'page callback' => 'hellothereum_register',
'access arguments' => array('ethereum user'),
'menu_name' => 'user-menu',
'type' => MENU_NORMAL_ITEM,
);
$items['hellothereum/buy'] = array(
'title' => 'Buy token',
'page callback' => 'hellothereum_buy',
'access arguments' => array('ethereum user'),
'menu_name' => 'user-menu',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function hellothereum_permission() {
return array(
'ethereum user' => array('title' => t('Use Ethereum')),
'administer ethereum' => array('title' => t('Administer Ethereum')),
);
}
function hellothereum_buy() {
global $user;
$content = '';
$client = user_load($user->uid);
//kpr($client); die;
$content .= '<br><b>Client</b><br>hash: '.$client->hash.'<br>address: <span id="client-address"></span><br>eth balance : <span id="client-eth"></span><br>token balance: <span id="client-token"></span>';
$content .= '<br>Password: <input type="text" id="eth-password" /> <input id="eth-buy" type="button" value="Buy 1 Token for 0.001 ether">';
$content .= '<br>Contract token available : <span id="contract-token"></span>';
drupal_add_js(array(
'blockchain' => array(
'token_deployed_contract_address_fallback' => variable_get('blockchain_Token_deployed_contract_address_fallback'),
'token_deployed_contract_ABI' => variable_get('blockchain_Token_deployed_contract_ABI'),
'ethereum_fallback' => variable_get('ethereum_user_register_drupal_fallback_node'),
'clientHash' => $client->hash,
'clientAddress' => $client->field_ethaddress['und'][0]['value'],
),
), 'setting');
drupal_add_js(drupal_get_path('module', 'hellothereum') . '/js/web3.min.js', array('scope' => 'footer'));
drupal_add_js(drupal_get_path('module', 'hellothereum') . '/js/ethereum_token.js', array('scope' => 'footer'));
drupal_add_js(drupal_get_path('module', 'hellothereum').'/js/buffer.js');
drupal_add_js(drupal_get_path('module', 'hellothereum').'/js/lodash.min.js');
drupal_add_js(drupal_get_path('module', 'hellothereum').'/js/ethereumjs-tx-1.3.3.min.js');
return $content;
}
/**
* Implements hook_form().
*/
function hellothereum_user_form($form, &$form_state) {
$form = array();
global $user;
$account = user_load($user->uid);
$form['ethereum_user_address'] = array(
'#title' => t('Ethereum address'),
'#title_display' => 'invisible',
'#type' => 'textfield',
'#size' => 42,
'#maxlength' => 42,
'#default_value' => $account->field_ethaddress['und'][0]['value'],
'#description' => t('Ethereum addresses must begin with 0x and 40 hexadecimals characters must follow.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
$form['#attached']['css'] = array(
drupal_get_path('module', 'hellothereum') . '/css/ethereum_user_form.css',
);
$form['#attached']['js'] = array(
drupal_get_path('module', 'hellothereum') . '/js/ethereum_user_form.js',
);
return $form;
}
/**
* Implements hook_form_submit().
*/
function hellothereum_user_form_submit($form, &$form_state) {
$address = $form_state['input']['ethereum_user_address'];
global $user;
$account = user_load($user->uid);
$account->field_ethaddress['und'][0]['value'] = $address;
user_save($account);
}
function hellothereum_register() {
// Current user (who is viewing $this_user, an admin or the same person).
global $user;
$output = array();
$this_user = user_load($user->uid);
$this_user_ethereum_address = $this_user->field_ethaddress['und'][0]['value'];
// Ethereum user address.
$address = '<div id="ethereum_user_address">';
$address .= '<h2>' . t('Ethereum address') . '</h2>';
$address .= '<p>';
$address .= $this_user_ethereum_address ? $this_user_ethereum_address : t('Please add your Ethereum address');
$address .= ' <a href="#">' . t('edit') . '</a>';
$address .= '</p>';
$address .= '</div>';
$output['ethereum_user_address'] = array(
'#type' => 'markup',
'#markup' => $address,
);
$output['ethereum_user_address_form'] = drupal_get_form('hellothereum_user_form');
if ($this_user_ethereum_address) {
// User registry check.
$registry = '<div id="ethereum_user_registry">';
$registry .= '<h2>' . t('Ethereum user registry') . '</h2>';
$registry .= '<div id="ethereum_user_registry_wait"><p>' . t('Please wait while we check if you are in our user registry on Ethereum...') . '</p></diV>';
$registry .= '<p id="ethereum_user_registry_validated">' . t('Yes, you are in our Ethereum user registry!') . '</p>';
$register_link = ($user->uid == $this_user->uid) ? '<a href="#">' . t('To use Ethereum features with us, please sign our user registry.') . '</a>' : t('User did not sign the registry and you can not do it as an admin');
$registry .= '<p id="ethereum_user_registry_sign">' . $register_link . '</p>';
$registry .= '</div>';
$output['ethereum_user_registry'] = array(
'#type' => 'markup',
'#markup' => $registry,
'#attached' => array(
'css' => array(
drupal_get_path('module', 'hellothereum') . '/css/ethereum_user_registry.css',
),
'js' => array(
drupal_get_path('module', 'hellothereum') . '/js/ethereum_user_register_drupal.js',
),
),
);
// Generate user hash, if necessary.
if (!$this_user->hash) {
$data = array(
'@name' => $this_user->name,
'!id' => $this_user->uid,
);
$hash = user_hash_generate();
$this_user = user_save($this_user, array('hash' => $hash));
}
// Pass variables to JS.
drupal_add_js(array(
'ethereum_user' => array(
'contract' => array(
'address' => variable_get('ethereum_user_register_drupal_deployed_contract_address'),
'abi' => variable_get('ethereum_user_register_drupal_deployed_contract_abi'),
),
'fallback_node' => variable_get('ethereum_user_register_drupal_fallback_node'),
'token' => variable_get('ethereum_user_registry_list_token'),
'user' => array(
'hash' => $this_user->hash,
'address' => $this_user_ethereum_address,
),
),
), 'setting');
drupal_add_js(drupal_get_path('module', 'hellothereum') . '/js/web3.min.js', array('scope' => 'footer'));
}
return $output;
}