-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (53 loc) · 1.79 KB
/
index.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
const argon2 = require('argon2')
const argon2id = argon2.argon2id
const shake128 = require('js-sha3').shake128
const EthWallet = require('ethereumjs-wallet')
const core = require('@iota/core')
const converter = require('@iota/converter')
class UserPass {
constructor (){
this.SALT_LENGTH = 512
this.INTERNAL_LENGTH = 2048
this.PRIVATE_KEY_LENGTH = 256
}
async generateEthereumWallet(username, password){
const privateKey = await this.generatePrivateKey(username, password)
const wallet = EthWallet.fromPrivateKey(Buffer.from(privateKey, 'hex'))
return wallet
}
async generateIotaWallet(username, password, idx=0){
const privateKey = await this.generatePrivateKey(username, password, 324)
const trytes = converter.asciiToTrytes(privateKey)
const address = core.generateAddress(trytes, idx )
return {
privateKey: trytes,
address
}
}
async generatePrivateKey(username, password, length=256){
const hashedUsername = this.hash(username, this.SALT_LENGTH)
const hashedPassword = this.hash(password, this.SALT_LENGTH)
const user = await this.argon2(username, hashedPassword, this.INTERNAL_LENGTH)
const pass = await this.argon2(password, hashedUsername, this.INTERNAL_LENGTH)
const salt = this.hash(user + pass + 'USERPASS', this.SALT_LENGTH)
const privateKey = await this.argon2((user + pass), salt, length)
return privateKey
}
hash (input, length) {
return shake128(input, length)
}
argon2 (input, salt, length=2048) {
return new Promise((resolve, reject) => {
argon2.hash(input, {
salt: Buffer.from(salt, 'hex'),
time: 10,
type: argon2id
})
.then(res => {
resolve(shake128(res, length))
})
.catch(reject)
})
}
}
module.exports = new UserPass()