-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.js
executable file
·225 lines (178 loc) · 7.3 KB
/
app.js
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
225
const hiveClient = new dhive.Client(['https://api.hive.blog', 'https://api.ha.deathwing.me']);
const steemClient = new dsteem.Client('https://api.steemit.com');
// Checking if the already exists
async function checkAccountName(client, username) {
const ac = await client.database.call('lookup_account_names', [[username]]);
return (ac[0] === null) ? true : false;
}
// Returns an account's Resource Credits data
async function getRC(client, username) {
return client.call('rc_api', 'find_rc_accounts', { accounts: [username] });
}
// Generates Aall Private Keys from username and password
function getPrivateKeys(username, password, roles = ['owner', 'active', 'posting', 'memo']) {
const privKeys = {};
roles.forEach((role) => {
privKeys[role] = dsteem.PrivateKey.fromLogin(username, password, role).toString();
privKeys[`${role}Pubkey`] = dsteem.PrivateKey.from(privKeys[role]).createPublic().toString();
});
return privKeys;
};
// Creates a suggested password
function suggestPassword() {
const array = new Uint32Array(10);
window.crypto.getRandomValues(array);
return 'P' + dsteem.PrivateKey.fromSeed(array).toString();
}
$(document).ready(async function () {
// Checks and shows an account's RC
$('#username').keyup(async function () {
const parent = $(this).parent('.form-group');
const steem = await getRC(steemClient, $(this).val());
const hive = await getRC(hiveClient, $(this).val());
const notifyDiv = parent.find('.text-muted');
notifyDiv.empty();
let message = '';
if (hive.rc_accounts.length > 0) {
message += 'HIVE RC: ' + Number(hive.rc_accounts[0].rc_manabar.current_mana).toLocaleString() + ' ';
}
if (steem.rc_accounts.length > 0) {
message += 'STEEM RC: ' + Number(steem.rc_accounts[0].rc_manabar.current_mana).toLocaleString();
}
notifyDiv.text(message)
});
// Check if the name is available
$('#new-account').keyup(async function () {
const notifyDiv = $(this).parent('.form-group').find('.message');
notifyDiv.text('Enter the username you want to create.');
if ($(this).val().length >= 3) {
const steem = await checkAccountName(steemClient, $(this).val());
const hive = await checkAccountName(hiveClient, $(this).val());
let message = '';
message += (hive) ? '<span class="text-success">Availble on HIVE.</span> ' : '<span class="text-danger">Not available on HIVE.</span> ';
message += (steem) ? '<span class="text-success">Availble on STEEM.</span> ' : '<span class="text-danger">Not available on STEEM.</span> ';
notifyDiv.html(message);
}
});
// Auto fills password field
$('#password').val(suggestPassword());
// Processisng claim account form
$('#claim-account-steem').click(function (e) {
$('#claim-account-chain').val('steem');
});
$('#claim-account-hive').click(function (e) {
$('#claim-account-chain').val('hive');
});
$('#claim-account').submit(async function (e) {
e.preventDefault();
const chain = $('#claim-account-chain').val();
const username = $('#username').val();
const activeKey = $('#active-key').val();
const feedbackDiv = $('#claim-account-feedback');
feedbackDiv.removeClass('alert-success').removeClass('alert-danger').empty();
const lib = (chain === 'hive') ? dhive : dsteem;
const op = ['claim_account', {
creator: username,
fee: lib.Asset.from(`0.000 ${(chain === 'hive') ? 'HIVE' : 'STEEM'}`),
extensions: [],
}];
if (activeKey === '') {
op[1].fee = op[1].fee.toString();
const keychain = (chain === 'hive') ? window.hive_keychain : window.steem_keychain;
if (keychain) {
keychain.requestBroadcast(username, [op], 'active', function (response) {
if (response.success) {
feedbackDiv.addClass('alert-success').text('You have successfully claimed a discounted account!');
} else {
feedbackDiv.addClass('alert-danger').text(response.message);
}
});
} else {
alert('STEEM and/or HIVE Keychain was not found.');
}
} else {
const client = (chain === 'hive') ? hiveClient : steemClient;
client.broadcast.sendOperations([op], lib.PrivateKey.from(activeKey))
.then((r) => {
console.log(r);
feedbackDiv.addClass('alert-success').text('You have successfully claimed a discounted account!');
})
.catch(e => {
console.log(e);
feedbackDiv.addClass('alert-danger').text(e.message);
});
}
});
$('#create-account-steem').click(function (e) {
$('#create-account-chain').val('steem');
});
$('#create-account-hive').click(function (e) {
$('#create-account-chain').val('hive');
});
// Processing create account form
$('#create-account').submit(async function (e) {
e.preventDefault();
const chain = $('#create-account-chain').val();
const username = $('#new-account').val();
const password = $('#password').val();
const creator = $('#creator').val();
const sp = parseFloat($('#delegation').val()).toFixed(3);
const active = $('#creator-key').val();
const feedbackDiv = $('#create-account-feedback');
if (!username || !password || !creator) {
return alert('Username, Password, Creator account is required.')
}
const client = (chain === 'hive') ? hiveClient : steemClient;
const lib = (chain === 'hive') ? dhive : dsteem;
const ops = [];
const keys = getPrivateKeys(username, password);
const create_op = ['create_claimed_account', {
active: lib.Authority.from(keys.activePubkey),
creator,
extensions: [],
json_metadata: '',
memo_key: keys.memoPubkey,
new_account_name: username,
owner: lib.Authority.from(keys.ownerPubkey),
posting: lib.Authority.from(keys.postingPubkey),
}];
ops.push(create_op);
if (sp > 0) {
// Converting SP to VESTS
const delegation = (lib.getVestingSharePrice(await client.database.getDynamicGlobalProperties()))
.convert({ amount: sp, symbol: (chain === 'hive') ? 'HIVE' : 'STEEM' });
const delegate_op = ['delegate_vesting_shares', {
delegatee: username,
delegator: creator,
vesting_shares: delegation,
}];
ops.push(delegate_op);
}
feedbackDiv.removeClass('alert-success').removeClass('alert-danger').empty();
if (active === '') {
const keychain = (chain === 'hive') ? window.hive_keychain : window.steem_keychain;
if (keychain) {
keychain.requestBroadcast(creator, ops, 'active', function (response) {
console.log(response);
if (response.success) {
feedbackDiv.addClass('alert-success').text('Account: ' + username + ' has been created successfully.');
} else {
feedbackDiv.addClass('alert-danger').text(response.message);
}
});
} else {
alert('STEEM and/or HIVE Keychain was not found.');
}
} else {
client.broadcast.sendOperations(ops, lib.PrivateKey.from(active))
.then((r) => {
console.log(r);
feedbackDiv.addClass('alert-success').text('Account: ' + username + ' has been created successfully.');
})
.catch(e => {
console.log(e);
feedbackDiv.addClass('alert-danger').text(e.message);
});
}
});
});