-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathkeypass-json.ts
154 lines (126 loc) · 4.49 KB
/
keypass-json.ts
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
/* based on https://github.com/input-output-hk/cardano-crypto/blob/master/cbits/encrypted_sign.c */
import {cardanoMemoryCombine, blake2b, scrypt} from 'cardano-crypto.js'
import derivationSchemes from './helpers/derivation-schemes'
import {HARDENED_THRESHOLD} from './constants'
import {decodeCbor, encodeCbor} from './helpers/cbor'
function transformPassword(password) {
return password ? blake2b(Buffer.from(password), 32) : Buffer.from([])
}
async function verifyPassword(passwordToVerify, passwordHash) {
const passwordHashSplit = decodeCbor(passwordHash)
.toString('ascii')
.split('|')
const n = 1 << parseInt(passwordHashSplit[0], 10)
const r = parseInt(passwordHashSplit[1], 10)
const p = parseInt(passwordHashSplit[2], 10)
const salt = Buffer.from(passwordHashSplit[3], 'base64')
const expectedHash = Buffer.from(passwordHashSplit[4], 'base64')
const passwordToVerifyHash = await new Promise((resolve, reject) => {
scrypt(
encodeCbor(transformPassword(passwordToVerify)),
salt,
{
N: n,
r,
p,
dkLen: expectedHash.length,
encoding: 'base64',
interruptStep: 1000,
},
(hash) => resolve(hash)
)
})
return passwordToVerifyHash === expectedHash.toString('base64')
}
function getRandomSaltForPasswordHash() {
const randBytes = Buffer.alloc(32)
window.crypto.getRandomValues(randBytes)
return encodeCbor(randBytes)
}
async function hashPasswordAndPack(password, salt) {
const [n, r, p, hashLen] = [14, 8, 1, 32]
const hash = await new Promise((resolve, reject) => {
scrypt(
encodeCbor(transformPassword(password)),
salt,
{
N: 1 << n,
r,
p,
dkLen: hashLen,
encoding: 'base64',
interruptStep: 1000,
},
(hash) => resolve(hash)
)
})
return [n.toString(), r.toString(), p.toString(), salt.toString('base64'), hash].join('|')
}
// wallet secret encryption/decryption is self-inverse
const [encryptWalletSecret, decryptWalletSecret] = Array(2).fill((walletSecret, password) => {
const secretKey = walletSecret.slice(0, 64)
const extendedPublicKey = walletSecret.slice(64, 128)
return Buffer.concat([cardanoMemoryCombine(secretKey, password), extendedPublicKey])
})
function parseWalletExportObj(walletExportObj) {
if (walletExportObj.fileType !== 'WALLETS_EXPORT') {
throw new Error('Invalid file type')
}
const {passwordHash: b64PasswordHash, walletSecretKey: b64WalletSecret} = walletExportObj.wallet
const passwordHash = Buffer.from(b64PasswordHash, 'base64')
const derivationScheme = Object.values(derivationSchemes).find(
(x) => x.keyfileVersion === walletExportObj.fileVersion
)
if (derivationScheme === undefined) {
throw new Error(`Invalid file version: ${walletExportObj.fileVersion}`)
}
const walletSecretDef = {
rootSecret: decodeCbor(Buffer.from(b64WalletSecret, 'base64')),
derivationScheme,
}
return {
passwordHash,
walletSecretDef,
}
}
async function isWalletExportEncrypted(walletExportObj) {
const {passwordHash} = parseWalletExportObj(walletExportObj)
const isPasswordVerified = await verifyPassword('', passwordHash)
return !isPasswordVerified
}
async function importWalletSecretDef(walletExportObj, password) {
const {passwordHash, walletSecretDef} = parseWalletExportObj(walletExportObj)
const isPasswordVerified = await verifyPassword(password, passwordHash)
if (!isPasswordVerified) {
throw new Error('Wrong password')
}
if (!password) {
return walletSecretDef
}
walletSecretDef.rootSecret = decryptWalletSecret(walletSecretDef.rootSecret, password)
return walletSecretDef
}
async function exportWalletSecretDef(walletSecretDef, password, walletName) {
const encryptedWalletSecret = encryptWalletSecret(walletSecretDef.rootSecret, password)
const packedPasswordHash = await hashPasswordAndPack(password, getRandomSaltForPasswordHash())
return {
wallet: {
accounts: [
{
name: 'Initial account',
index: HARDENED_THRESHOLD,
},
],
walletSecretKey: encodeCbor(encryptedWalletSecret).toString('base64'),
walletMeta: {
name: walletName,
assurance: 'normal',
unit: 'ADA',
},
passwordHash: encodeCbor(Buffer.from(packedPasswordHash, 'ascii')).toString('base64'),
},
fileType: 'WALLETS_EXPORT',
fileVersion: walletSecretDef.derivationScheme.keyfileVersion,
}
}
export {importWalletSecretDef, exportWalletSecretDef, isWalletExportEncrypted}