1 | "use strict";
|
2 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
11 | return new (P || (P = Promise))(function (resolve, reject) {
|
12 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
13 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
14 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
15 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
16 | });
|
17 | };
|
18 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
19 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
20 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
21 | function verb(n) { return function (v) { return step([n, v]); }; }
|
22 | function step(op) {
|
23 | if (f) throw new TypeError("Generator is already executing.");
|
24 | while (_) try {
|
25 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
26 | if (y = 0, t) op = [op[0] & 2, t.value];
|
27 | switch (op[0]) {
|
28 | case 0: case 1: t = op; break;
|
29 | case 4: _.label++; return { value: op[1], done: false };
|
30 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
31 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
32 | default:
|
33 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
34 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
35 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
36 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
37 | if (t[2]) _.ops.pop();
|
38 | _.trys.pop(); continue;
|
39 | }
|
40 | op = body.call(thisArg, _);
|
41 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
42 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
43 | }
|
44 | };
|
45 | var Keygen = require('eosjs-keygen').Keygen;
|
46 | var ACCOUNT_NAME_MAX_LENGTH = 12;
|
47 | var BASE = 31;
|
48 |
|
49 | function newAccountTransaction(name, ownerPublicKey, activePublicKey, orePayerAccountName, options) {
|
50 | if (options === void 0) { options = {}; }
|
51 | var _a = __assign({ broadcast: true, bytes: 2048, permission: 'active', stakedCpu: '0.1000', stakedNet: '0.1000', tokenSymbol: this.chainName === 'ore' ? 'SYS' : 'EOS', transfer: false }, options), broadcast = _a.broadcast, bytes = _a.bytes, permission = _a.permission, stakedCpu = _a.stakedCpu, stakedNet = _a.stakedNet, transfer = _a.transfer, tokenSymbol = _a.tokenSymbol;
|
52 | var actions = [{
|
53 | account: 'eosio',
|
54 | name: 'newaccount',
|
55 | authorization: [{
|
56 | actor: orePayerAccountName,
|
57 | permission: permission,
|
58 | }],
|
59 | data: {
|
60 | creator: orePayerAccountName,
|
61 | name: name,
|
62 | newact: name,
|
63 | owner: {
|
64 | threshold: 1,
|
65 | keys: [{
|
66 | key: ownerPublicKey,
|
67 | weight: 1,
|
68 | }],
|
69 | accounts: [],
|
70 | waits: [],
|
71 | },
|
72 | active: {
|
73 | threshold: 1,
|
74 | keys: [{
|
75 | key: activePublicKey,
|
76 | weight: 1,
|
77 | }],
|
78 | accounts: [],
|
79 | waits: [],
|
80 | },
|
81 | },
|
82 | },
|
83 | {
|
84 | account: 'eosio',
|
85 | name: 'buyrambytes',
|
86 | authorization: [{
|
87 | actor: orePayerAccountName,
|
88 | permission: permission,
|
89 | }],
|
90 | data: {
|
91 | payer: orePayerAccountName,
|
92 | receiver: name,
|
93 | bytes: bytes,
|
94 | },
|
95 | },
|
96 | {
|
97 | account: 'eosio',
|
98 | name: 'delegatebw',
|
99 | authorization: [{
|
100 | actor: orePayerAccountName,
|
101 | permission: permission,
|
102 | }],
|
103 | data: {
|
104 | from: orePayerAccountName,
|
105 | receiver: name,
|
106 | stake_net_quantity: stakedNet + " " + tokenSymbol,
|
107 | stake_cpu_quantity: stakedCpu + " " + tokenSymbol,
|
108 | transfer: transfer,
|
109 | },
|
110 | }];
|
111 | return this.transact(actions, broadcast);
|
112 | }
|
113 | function timestampEosBase32() {
|
114 |
|
115 | return eosBase32(Date.now().toString(BASE));
|
116 | }
|
117 | function randomEosBase32() {
|
118 |
|
119 | return eosBase32(Math.random().toString(BASE).substr(2));
|
120 | }
|
121 | function generateAccountNameString(prefix) {
|
122 | if (prefix === void 0) { prefix = ''; }
|
123 | return (prefix + timestampEosBase32() + randomEosBase32()).substr(0, 12);
|
124 | }
|
125 | function encrypted(key) {
|
126 | if (key.match(/^\{.*\}$/)) {
|
127 | return true;
|
128 | }
|
129 | return false;
|
130 | }
|
131 | function encryptKeys(keys, password, salt) {
|
132 | var privateKeys = keys.privateKeys, publicKeys = keys.publicKeys;
|
133 | var encryptedKeys = {
|
134 | privateKeys: {
|
135 | owner: encrypted(keys.privateKeys.owner) ? keys.privateKeys.owner : this.encrypt(keys.privateKeys.owner, password, salt).toString(),
|
136 | active: encrypted(keys.privateKeys.active) ? keys.privateKeys.active : this.encrypt(keys.privateKeys.active, password, salt).toString()
|
137 | },
|
138 | publicKeys: __assign({}, publicKeys)
|
139 | };
|
140 | return encryptedKeys;
|
141 | }
|
142 | function getAccountPermissions(oreAccountName) {
|
143 | return __awaiter(this, void 0, void 0, function () {
|
144 | var account, permissions;
|
145 | return __generator(this, function (_a) {
|
146 | switch (_a.label) {
|
147 | case 0: return [4 , this.eos.rpc.get_account(oreAccountName)];
|
148 | case 1:
|
149 | account = _a.sent();
|
150 | permissions = account.permissions;
|
151 | return [2 , permissions];
|
152 | }
|
153 | });
|
154 | });
|
155 | }
|
156 | function weightedKey(key, weight) {
|
157 | if (weight === void 0) { weight = 1; }
|
158 | return { key: key, weight: weight };
|
159 | }
|
160 | function weightKeys(keys, weight) {
|
161 | if (weight === void 0) { weight = 1; }
|
162 | return keys.map(function (key) { return weightedKey(key, weight); });
|
163 | }
|
164 | function newPermissionDetails(keys, threshold, weights) {
|
165 | if (threshold === void 0) { threshold = 1; }
|
166 | if (weights === void 0) { weights = 1; }
|
167 | return {
|
168 | accounts: [],
|
169 | keys: weightKeys.bind(this)(keys, weights),
|
170 | threshold: threshold,
|
171 | waits: [],
|
172 | };
|
173 | }
|
174 | function newPermission(keys, permName, parent, threshold, weights) {
|
175 | if (parent === void 0) { parent = 'active'; }
|
176 | if (threshold === void 0) { threshold = 1; }
|
177 | if (weights === void 0) { weights = 1; }
|
178 | return {
|
179 | parent: parent,
|
180 | perm_name: permName,
|
181 | required_auth: newPermissionDetails.bind(this)(keys, threshold, weights),
|
182 | };
|
183 | }
|
184 | function appendPermission(oreAccountName, keys, permName, parent, threshold, weights) {
|
185 | if (parent === void 0) { parent = 'active'; }
|
186 | if (threshold === void 0) { threshold = 1; }
|
187 | if (weights === void 0) { weights = 1; }
|
188 | return __awaiter(this, void 0, void 0, function () {
|
189 | var perms, existingPerm, weightedKeys, newPerm;
|
190 | return __generator(this, function (_a) {
|
191 | switch (_a.label) {
|
192 | case 0: return [4 , getAccountPermissions.bind(this)(oreAccountName)];
|
193 | case 1:
|
194 | perms = _a.sent();
|
195 | existingPerm = perms.find(function (perm) { return perm.perm_name === permName; });
|
196 | if (existingPerm) {
|
197 | weightedKeys = weightKeys.bind(this)(keys, weights);
|
198 | existingPerm.required_auth.keys = existingPerm.required_auth.keys.concat(weightedKeys);
|
199 | existingPerm.parent = parent;
|
200 | return [2 , existingPerm];
|
201 | }
|
202 | else {
|
203 | newPerm = newPermission.bind(this)(keys, permName, parent, threshold, weights);
|
204 | return [2 , newPerm];
|
205 | }
|
206 | return [2 ];
|
207 | }
|
208 | });
|
209 | });
|
210 | }
|
211 |
|
212 | function generateAuthKeys(oreAccountName, permName, code, action, broadcast) {
|
213 | return __awaiter(this, void 0, void 0, function () {
|
214 | var authKeys, options;
|
215 | return __generator(this, function (_a) {
|
216 | switch (_a.label) {
|
217 | case 0: return [4 , Keygen.generateMasterKeys()];
|
218 | case 1:
|
219 | authKeys = _a.sent();
|
220 | options = { broadcast: broadcast, authPermission: 'owner', links: [{ code: code, type: action }] };
|
221 | return [4 , addPermission.bind(this)(oreAccountName, [authKeys.publicKeys.active], permName, 'active', options)];
|
222 | case 2:
|
223 | _a.sent();
|
224 | return [2 , authKeys];
|
225 | }
|
226 | });
|
227 | });
|
228 | }
|
229 | function createOreAccountWithKeys(activePublicKey, ownerPublicKey, orePayerAccountName, options) {
|
230 | if (options === void 0) { options = {}; }
|
231 | return __awaiter(this, void 0, void 0, function () {
|
232 | var oreAccountName, _a, transaction;
|
233 | var _this = this;
|
234 | return __generator(this, function (_b) {
|
235 | switch (_b.label) {
|
236 | case 0:
|
237 | options = __assign({ confirm: true, accountNamePrefix: 'ore' }, options);
|
238 | _a = options.oreAccountName;
|
239 | if (_a) return [3 , 2];
|
240 | return [4 , generateAccountName.bind(this)(options.accountNamePrefix)];
|
241 | case 1:
|
242 | _a = (_b.sent());
|
243 | _b.label = 2;
|
244 | case 2:
|
245 | oreAccountName = _a;
|
246 | if (!options.confirm) return [3 , 4];
|
247 | return [4 , this.awaitTransaction(function () { return newAccountTransaction.bind(_this)(oreAccountName, ownerPublicKey, activePublicKey, orePayerAccountName, options); })];
|
248 | case 3:
|
249 | transaction = _b.sent();
|
250 | return [3 , 6];
|
251 | case 4: return [4 , newAccountTransaction.bind(this)(oreAccountName, ownerPublicKey, activePublicKey, orePayerAccountName, options)];
|
252 | case 5:
|
253 | transaction = _b.sent();
|
254 | _b.label = 6;
|
255 | case 6: return [2 , { oreAccountName: oreAccountName, transaction: transaction }];
|
256 | }
|
257 | });
|
258 | });
|
259 | }
|
260 | function generateOreAccountAndEncryptedKeys(password, salt, ownerPublicKey, orePayerAccountName, options) {
|
261 | if (options === void 0) { options = {}; }
|
262 | return __awaiter(this, void 0, void 0, function () {
|
263 | var keys, _a, oreAccountName, transaction;
|
264 | return __generator(this, function (_b) {
|
265 | switch (_b.label) {
|
266 | case 0: return [4 , generateEncryptedKeys.bind(this)(password, salt, options.keys)];
|
267 | case 1:
|
268 | keys = _b.sent();
|
269 | return [4 , createOreAccountWithKeys.bind(this)(keys.publicKeys.active, ownerPublicKey, orePayerAccountName, options)];
|
270 | case 2:
|
271 | _a = _b.sent(), oreAccountName = _a.oreAccountName, transaction = _a.transaction;
|
272 | return [2 , { oreAccountName: oreAccountName, transaction: transaction, keys: keys }];
|
273 | }
|
274 | });
|
275 | });
|
276 | }
|
277 |
|
278 | function createAccount(password, salt, ownerPublicKey, orePayerAccountName, options) {
|
279 | if (options === void 0) { options = {}; }
|
280 | return __awaiter(this, void 0, void 0, function () {
|
281 | var broadcast, _a, oreAccountName, transaction, keys;
|
282 | return __generator(this, function (_b) {
|
283 | switch (_b.label) {
|
284 | case 0:
|
285 | options = __assign({ broadcast: true }, options);
|
286 | broadcast = options.broadcast;
|
287 | return [4 , generateOreAccountAndEncryptedKeys.bind(this)(password, salt, ownerPublicKey, orePayerAccountName, options)];
|
288 | case 1:
|
289 | _a = _b.sent(), oreAccountName = _a.oreAccountName, transaction = _a.transaction, keys = _a.keys;
|
290 | return [2 , {
|
291 | oreAccountName: oreAccountName,
|
292 | privateKey: keys.privateKeys.active,
|
293 | publicKey: keys.publicKeys.active,
|
294 | keys: keys,
|
295 | transaction: transaction,
|
296 | }];
|
297 | }
|
298 | });
|
299 | });
|
300 | }
|
301 |
|
302 | function addPermission(authAccountName, keys, permissionName, parentPermission, options) {
|
303 | if (options === void 0) { options = {}; }
|
304 | return __awaiter(this, void 0, void 0, function () {
|
305 | var authPermission, _a, links, _b, broadcast, perm, permission, parent, auth, actions;
|
306 | return __generator(this, function (_c) {
|
307 | switch (_c.label) {
|
308 | case 0:
|
309 | options = __assign({ authPermission: 'active' }, options);
|
310 | authPermission = options.authPermission, _a = options.links, links = _a === void 0 ? [] : _a, _b = options.broadcast, broadcast = _b === void 0 ? true : _b;
|
311 | return [4 , appendPermission.bind(this)(authAccountName, keys, permissionName, parentPermission)];
|
312 | case 1:
|
313 | perm = _c.sent();
|
314 | permission = perm.perm_name, parent = perm.parent, auth = perm.required_auth;
|
315 | actions = [{
|
316 | account: 'eosio',
|
317 | name: 'updateauth',
|
318 | authorization: [{
|
319 | actor: authAccountName,
|
320 | permission: authPermission,
|
321 | }],
|
322 | data: {
|
323 | account: authAccountName,
|
324 | permission: permission,
|
325 | parent: parent,
|
326 | auth: auth,
|
327 | },
|
328 | }];
|
329 | links.forEach(function (link) {
|
330 | var code = link.code, type = link.type;
|
331 | actions.push({
|
332 | account: 'eosio',
|
333 | name: 'linkauth',
|
334 | authorization: [{
|
335 | actor: authAccountName,
|
336 | permission: authPermission,
|
337 | }],
|
338 | data: {
|
339 | account: authAccountName,
|
340 | code: code,
|
341 | type: type,
|
342 | requirement: permission,
|
343 | }
|
344 | });
|
345 | });
|
346 | return [2 , this.transact(actions, broadcast)];
|
347 | }
|
348 | });
|
349 | });
|
350 | }
|
351 |
|
352 | function createKeyPair(password, salt, authAccountName, permissionName, options) {
|
353 | if (options === void 0) { options = {}; }
|
354 | return __awaiter(this, void 0, void 0, function () {
|
355 | var _a, keys, parentPermission;
|
356 | var _this = this;
|
357 | return __generator(this, function (_b) {
|
358 | switch (_b.label) {
|
359 | case 0:
|
360 | _a = { confirm: true, parentPermission: 'active' };
|
361 | return [4 , generateEncryptedKeys.bind(this)(password, salt)];
|
362 | case 1:
|
363 | options = __assign.apply(void 0, [(_a.keys = _b.sent(), _a), options]);
|
364 | keys = options.keys, parentPermission = options.parentPermission;
|
365 | if (!options.confirm) return [3 , 3];
|
366 | return [4 , this.awaitTransaction(function () {
|
367 | return addPermission.bind(_this)(authAccountName, [keys.publicKeys.active], permissionName, parentPermission, options);
|
368 | })];
|
369 | case 2:
|
370 | _b.sent();
|
371 | return [3 , 5];
|
372 | case 3: return [4 , addPermission.bind(this)(authAccountName, [keys.publicKeys.active], permissionName, parentPermission, options)];
|
373 | case 4:
|
374 | _b.sent();
|
375 | _b.label = 5;
|
376 | case 5: return [2 , keys];
|
377 | }
|
378 | });
|
379 | });
|
380 | }
|
381 | function createBridgeAccount(password, salt, authorizingAccount, options) {
|
382 | return __awaiter(this, void 0, void 0, function () {
|
383 | var _a, transaction, keys;
|
384 | var _this = this;
|
385 | return __generator(this, function (_b) {
|
386 | switch (_b.label) {
|
387 | case 0:
|
388 | _a = {};
|
389 | return [4 , generateAccountName.bind(this)(options.accountNamePrefix)];
|
390 | case 1:
|
391 | options = __assign.apply(void 0, [(_a.oreAccountName = _b.sent(), _a.confirm = true, _a), options]);
|
392 | return [4 , generateEncryptedKeys.bind(this)(password, salt, options.keys)];
|
393 | case 2:
|
394 | keys = _b.sent();
|
395 | if (!options.confirm) return [3 , 4];
|
396 | return [4 , this.awaitTransaction(function () {
|
397 | return _this.createNewAccount(authorizingAccount, keys, options);
|
398 | })];
|
399 | case 3:
|
400 | transaction = _b.sent();
|
401 | return [3 , 6];
|
402 | case 4: return [4 , this.createNewAccount(authorizingAccount, keys, options)];
|
403 | case 5:
|
404 | transaction = _b.sent();
|
405 | _b.label = 6;
|
406 | case 6: return [2 , {
|
407 | oreAccountName: options.oreAccountName,
|
408 | privateKey: keys.privateKeys.active,
|
409 | publicKey: keys.publicKeys.active,
|
410 | keys: keys,
|
411 | transaction: transaction,
|
412 | }];
|
413 | }
|
414 | });
|
415 | });
|
416 | }
|
417 |
|
418 | function createOreAccount(password, salt, ownerPublicKey, orePayerAccountName, options) {
|
419 | if (options === void 0) { options = {}; }
|
420 | return __awaiter(this, void 0, void 0, function () {
|
421 | var broadcast, returnInfo, verifierAuthKeys;
|
422 | return __generator(this, function (_a) {
|
423 | switch (_a.label) {
|
424 | case 0:
|
425 | broadcast = options.broadcast;
|
426 | return [4 , createAccount.bind(this)(password, salt, ownerPublicKey, orePayerAccountName, options)];
|
427 | case 1:
|
428 | returnInfo = _a.sent();
|
429 | if (!(this.chainName === 'ore')) return [3 , 3];
|
430 | return [4 , generateAuthKeys.bind(this)(returnInfo.oreAccountName, 'authverifier', 'token.ore', 'approve', broadcast)];
|
431 | case 2:
|
432 | verifierAuthKeys = _a.sent();
|
433 | returnInfo.verifierAuthKey = verifierAuthKeys.privateKeys.active;
|
434 | returnInfo.verifierAuthPublicKey = verifierAuthKeys.publicKeys.active;
|
435 | _a.label = 3;
|
436 | case 3: return [2 , returnInfo];
|
437 | }
|
438 | });
|
439 | });
|
440 | }
|
441 | function eosBase32(base32String) {
|
442 |
|
443 | return base32String
|
444 | .replace(/0/g, 'v')
|
445 | .replace(/6/g, 'w')
|
446 | .replace(/7/g, 'x')
|
447 | .replace(/8/g, 'y')
|
448 | .replace(/9/g, 'z');
|
449 | }
|
450 |
|
451 | function generateAccountName(prefix, checkIfNameUsedOnChain) {
|
452 | if (prefix === void 0) { prefix = ''; }
|
453 | if (checkIfNameUsedOnChain === void 0) { checkIfNameUsedOnChain = true; }
|
454 | return __awaiter(this, void 0, void 0, function () {
|
455 | var accountName, nameAlreadyExists;
|
456 | return __generator(this, function (_a) {
|
457 | switch (_a.label) {
|
458 | case 0:
|
459 | accountName = generateAccountNameString.bind(this)(prefix);
|
460 | nameAlreadyExists = false;
|
461 | if (!checkIfNameUsedOnChain) return [3 , 2];
|
462 | return [4 , getNameAlreadyExists.bind(this)(accountName)];
|
463 | case 1:
|
464 | nameAlreadyExists = _a.sent();
|
465 | _a.label = 2;
|
466 | case 2:
|
467 | if (nameAlreadyExists) {
|
468 | return [2 , generateAccountName.bind(this)()];
|
469 | }
|
470 | else {
|
471 | return [2 , accountName];
|
472 | }
|
473 | return [2 ];
|
474 | }
|
475 | });
|
476 | });
|
477 | }
|
478 | function generateEncryptedKeys(password, salt, predefinedKeys) {
|
479 | if (predefinedKeys === void 0) { predefinedKeys = {}; }
|
480 | return __awaiter(this, void 0, void 0, function () {
|
481 | var keys, _a, publicKeys, _b, privateKeys, encryptedKeys;
|
482 | return __generator(this, function (_c) {
|
483 | switch (_c.label) {
|
484 | case 0: return [4 , Keygen.generateMasterKeys()];
|
485 | case 1:
|
486 | keys = _c.sent();
|
487 | _a = predefinedKeys.publicKeys, publicKeys = _a === void 0 ? {} : _a, _b = predefinedKeys.privateKeys, privateKeys = _b === void 0 ? {} : _b;
|
488 | keys = __assign({}, keys, { publicKeys: __assign({}, keys.publicKeys, publicKeys), privateKeys: __assign({}, keys.privateKeys, privateKeys) });
|
489 | encryptedKeys = encryptKeys.bind(this)(keys, password, salt);
|
490 | return [2 , encryptedKeys];
|
491 | }
|
492 | });
|
493 | });
|
494 | }
|
495 | function getNameAlreadyExists(accountName) {
|
496 | return __awaiter(this, void 0, void 0, function () {
|
497 | var e_1;
|
498 | return __generator(this, function (_a) {
|
499 | switch (_a.label) {
|
500 | case 0:
|
501 | _a.trys.push([0, 2, , 3]);
|
502 | return [4 , this.eos.rpc.get_account(accountName)];
|
503 | case 1:
|
504 | _a.sent();
|
505 | return [2 , true];
|
506 | case 2:
|
507 | e_1 = _a.sent();
|
508 | return [2 , false];
|
509 | case 3: return [2 ];
|
510 | }
|
511 | });
|
512 | });
|
513 | }
|
514 | module.exports = {
|
515 | addPermission: addPermission,
|
516 | createKeyPair: createKeyPair,
|
517 | createBridgeAccount: createBridgeAccount,
|
518 | createOreAccount: createOreAccount,
|
519 | eosBase32: eosBase32,
|
520 | generateAccountName: generateAccountName,
|
521 | generateEncryptedKeys: generateEncryptedKeys,
|
522 | getNameAlreadyExists: getNameAlreadyExists
|
523 | };
|
524 |
|
\ | No newline at end of file |