-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreateAccount.js
More file actions
420 lines (343 loc) · 9.33 KB
/
createAccount.js
File metadata and controls
420 lines (343 loc) · 9.33 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// 创建账号数量
const NUMBER = 1000;
const Sha = require('jssha')
const crypto = require('crypto')
const elliptic = require('elliptic')
const jsSha3 = require('js-sha3')
const EC = elliptic.ec
const keccak256 = jsSha3.keccak256
const fs = require('fs')
const path = require('path')
const ENCRYPTION_ALGORITHM = 'aes-256-ctr'
const ONE_TRX = 1000000
const TRON_CONSTANTS_MAINNET = {
ADD_PRE_FIX_BYTE: 0x41,
ADD_PRE_FIX_STRING: '41'
}
TRON_CONSTANTS_TESTNET = {
ADD_PRE_FIX_BYTE: 0xa0,
ADD_PRE_FIX_STRING: 'a0'
}
class ByteArray {
static toHexString (bytes = false) {
if (!bytes) {
return ''
}
return Array.from(bytes, byte => (
// Pad for exactly two digits
(`0${(byte & 0xFF).toString(16)}`).slice(-2)
)).join('')
}
static charToByte (c) {
if (c >= 'A' && c <= 'F') {
return c.charCodeAt(0) - 'A'.charCodeAt(0) + 10
}
if (c >= 'a' && c <= 'f') {
return c.charCodeAt(0) - 'a'.charCodeAt(0) + 10
} else if (c >= '0' && c <= '9') {
return c.charCodeAt(0) - '0'.charCodeAt(0)
}
return 0
}
static fromHexString (str) {
const byteArray = []
let d = 0
let j = 0
let k = 0
for (let i = 0; i < str.length; i++) {
const c = str.charAt(i)
d <<= 4
d += this.charToByte(c)
j++
if ((j % 2) === 0) {
byteArray[k++] = d
d = 0
}
}
return byteArray
}
}
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
class utils {
static sha256 (string) {
const shaObj = new Sha('SHA-256', 'HEX')
shaObj.update(string)
return shaObj.getHash('HEX')
}
/**
* hash password to save local
* @param {password} string
*/
static hashPassword (string) {
return this.sha256(this.stringToHex(string + '!~%$#^&*'))
}
static validateAddress (address) {
if (address.length !== 34) {
return false
}
const prefix = this.base58ToHex(address).substr(0, 2)
if (prefix === TRON_CONSTANTS_MAINNET.ADD_PRE_FIX_STRING) {
return true
}
if (prefix === TRON_CONSTANTS_TESTNET.ADD_PRE_FIX_STRING) {
return true
}
return false
}
static privateKeyToPublicKey (privateKey) {
const ec = new EC('secp256k1')
const key = ec.keyFromPrivate(privateKey, 'bytes')
const publicKey = key.getPublic()
const { x, y } = publicKey
let xHex = x.toString('hex')
let yHex = y.toString('hex')
while (xHex.length < 64) {
xHex = `0${xHex}`
}
while (yHex.length < 64) {
yHex = `0${yHex}`
}
const publicKeyHex = `04${xHex}${yHex}`
return ByteArray.fromHexString(publicKeyHex)
}
static privateKeyToAddress (privateKey) {
const privateKeyBytes = ByteArray.fromHexString(privateKey)
const publicKeyBytes = this.privateKeyToPublicKey(privateKeyBytes)
const addressBytes = this.publicKeyToAddress(publicKeyBytes)
return this.hexToBase58(
ByteArray.toHexString(addressBytes)
)
}
static publicKeyToAddress (pubKey) {
const publicKey = (pubKey.length === 65) ? pubKey.slice(1) : pubKey
const hash = keccak256(publicKey).toString()
const address = TRON_CONSTANTS_MAINNET.ADD_PRE_FIX_STRING + hash.substring(24)
return ByteArray.fromHexString(address)
}
static validatePrivateKey (privateKey) {
try {
const address = this.privateKeyToAddress(privateKey)
return this.validateAddress(address)
} catch (e) {
return false
}
}
static isFunction (obj) {
return typeof obj === 'function'
}
static isHex (string) {
return typeof string === 'string' && !isNaN(parseInt(string, 16))
}
static isInteger (number) {
return Number.isInteger(
Number(number)
)
}
static isString (string) {
return Object.prototype.toString.call(string) === '[object String]'
}
static stringToHex (string) {
return Buffer.from(string).toString('hex')
}
static hexToString (hex) {
return Buffer.from(hex, 'hex').toString()
}
static encrypt (data, password, algorithm = ENCRYPTION_ALGORITHM) {
const cipher = crypto.createCipher(algorithm, password)
let crypted = cipher.update(data, 'utf8', 'hex')
crypted += cipher.final('hex')
return crypted
}
static decrypt (data, password, algorithm = ENCRYPTION_ALGORITHM) {
const decipher = crypto.createDecipher(algorithm, password)
let decrypted = decipher.update(data, 'hex', 'utf8')
decrypted += decipher.final('utf8')
return decrypted
}
static injectPromise (func, ...args) {
return new Promise((resolve, reject) => {
func(...args, (err, res) => {
if (err) {
reject(err)
} else {
resolve(res)
}
})
})
}
static strippedHost () {
let host = window.location.hostname
if (host.indexOf('www.') === 0) host = host.replace('www.', '')
return host
}
// get system language
static getLanguage () {
let lang = ''
if (window.navigator.appName === 'Netscape') {
lang = window.navigator.language
} else {
lang = window.navigator.browserLanguage
}
if (lang.indexOf('zh') > -1) {
lang = 'zh'
} else if (lang.indexOf('en') > -1) {
lang = 'en'
} else {
lang = 'en'
}
return lang
}
static toUtf8 (hex) {
hex = hex.replace(/^0x/, '')
return Buffer.from(hex, 'hex').toString('utf8')
}
static getTokenAmount (rawAmount) {
return rawAmount / ONE_TRX
}
static getTokenRawAmount (amount) {
return amount * ONE_TRX
}
static byte2hexStr (byte) {
const hexByteMap = '0123456789ABCDEF'
let str = ''
str += hexByteMap.charAt(byte >> 4)
str += hexByteMap.charAt(byte & 0x0f)
return str
}
static byteArray2hexStr (byteArray) {
return byteArray.reduce((string, byte) => {
return string + this.byte2hexStr(byte)
}, '')
}
static hexToBase58 (string) {
const primary = this.sha256(string)
const secondary = this.sha256(primary)
const buffer = ByteArray.fromHexString(string + secondary.slice(0, 8))
const digits = [0]
for (let i = 0; i < buffer.length; i++) {
for (let j = 0; j < digits.length; j++) {
digits[j] <<= 8
}
digits[0] += buffer[i]
let carry = 0
for (let j = 0; j < digits.length; ++j) {
digits[j] += carry
carry = (digits[j] / 58) | 0
digits[j] %= 58
}
while (carry) {
digits.push(carry % 58)
carry = (carry / 58) | 0
}
}
for (let i = 0; buffer[i] === 0 && i < buffer.length - 1; i++) {
digits.push(0)
}
return digits.reverse().map(digit => ALPHABET[digit]).join('')
}
static base58ToHex (string) {
const bytes = [0]
for (let i = 0; i < string.length; i++) {
const char = string[i]
if (!ALPHABET.includes(char)) {
throw new Error('Non-base58 character')
}
for (let j = 0; j < bytes.length; j++) {
bytes[j] *= 58
}
bytes[0] += ALPHABET.indexOf(char)
let carry = 0
for (let j = 0; j < bytes.length; ++j) {
bytes[j] += carry
carry = bytes[j] >> 8
bytes[j] &= 0xff
}
while (carry) {
bytes.push(carry & 0xff)
carry >>= 8
}
}
for (let i = 0; string[i] === '1' && i < string.length - 1; i++) {
bytes.push(0)
}
return bytes.reverse().slice(0, 21).map(byte => {
let temp = byte.toString(16)
if (temp.length === 1) {
temp = `0${temp}`
}
return temp
}).join('')
}
static base64ToHex (string) {
const bin = atob(string.replace(/[ \r\n]+$/, ''))
const hex = []
for (let i = 0; i < bin.length; i++) {
let temp = bin.charCodeAt(i).toString(16)
if (temp.length === 1) {
temp = `0${temp}`
}
hex.push(temp)
}
return hex.join('')
}
static transformAddress (address) {
if (!this.isString(address)) {
return false
}
switch (address.length) {
case 42: {
// hex -> base58
return this.transformAddress(
this.hexToBase58(address)
)
}
case 28: {
// base64 -> base58
const hex = this.base64ToHex(address)
const base58 = this.hexToBase58(hex)
return this.transformAddress(base58)
}
case 34: {
// base58
const isAddressValid = this.validateAddress(address)
if (isAddressValid) {
return address
}
return false
}
}
}
// gen Ecc priKey for bytes
static genPriKey () {
let ec = new EC('secp256k1')
let key = ec.genKeyPair()
let priKey = key.getPrivate()
let priKeyHex = priKey.toString('hex')
while (priKeyHex.length < 64) {
priKeyHex = '0' + priKeyHex
}
return ByteArray.fromHexString(priKeyHex)
}
static generateAccount () {
let priKeyBytes = this.genPriKey()
let privateKey = ByteArray.toHexString(priKeyBytes)
let address = this.privateKeyToAddress(privateKey)
// let password = base64EncodeToString(priKeyBytes)
return {
pk: privateKey,
address: address
}
}
}
function createAccounts () {
let account = {
rows: []
}
for (let i = 0; i < NUMBER; i++) {
account.rows.push(utils.generateAccount())
console.log(i);
}
// console.log(account.rows.length);
fs.writeFileSync(path.join(__dirname, './', 'account.json'), JSON.stringify(account, null, 2))
}
createAccounts()