-
Notifications
You must be signed in to change notification settings - Fork 22
/
keys.js
211 lines (190 loc) · 6.82 KB
/
keys.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
'use strict'
const utils = require('ethereumjs-util')
const { Map } = require('immutable')
const assert = require('chai').assert
const { Logger } = require('demo-utils')
const LOGGER = new Logger('keys')
const keys = {}
keys.keythereum = require('keythereum')
keys.randombytes = require('randombytes')
keys.Buffer = Buffer
/**
* Key and initialization vector lengths for Ethereum account generation.
* @memberof module:keys
*/
keys.PARAMS = { keyBytes: 32, ivBytes: 16 }
/**
* Create a new account map with private key, public key, address, salt, and init vector.
*
* @method create
* @memberof module:keys
* @return an Immutable {Map} representing an Ethereum account.
*/
keys.create = () => {
const account = keys.keythereum.create(keys.PARAMS)
const privateString = account.privateKey.toString('hex')
const ivString = keys.hex(account.iv)
const saltString = keys.hex(account.salt)
return keys.createFromPrivateString(privateString, ivString, saltString)
}
/**
* Convert the given buffers to an Immutable {Map}
*
* @method bufferToMap
* @memberof module:keys
* @param _privateBuffer Buffer of a private key (non-prefixed)
* @param _publicBuffer Buffer of an Ethereum public key (non-prefixed)
* @param _addressBuffer Buffer of an Ethereum address (non-prefixed)
* @param _ivBuffer Buffer of an initialization vector
* @param _saltBuffer Buffer of a password salt
* @return an Immutable {Map} representing an Ethereum account.
*/
keys.bufferToMap =
(_privateBuffer, _publicBuffer, _addressBuffer, _ivBuffer, _saltBuffer) => {
assert.equal(_ivBuffer.length, keys.PARAMS.ivBytes)
assert.equal(_saltBuffer.length, keys.PARAMS.keyBytes)
const privateString = keys.hex(_privateBuffer)
const publicString = keys.hex(_publicBuffer)
const addressPrefixed = utils.toChecksumAddress(keys.hex(_addressBuffer))
const ivString = keys.hex(_ivBuffer)
const saltString = keys.hex(_saltBuffer)
return new Map({
privateString,
publicString,
addressString : addressPrefixed.slice(2),
ivString,
saltString,
privatePrefixed : keys.prefixed(privateString),
publicPrefixed : keys.prefixed(publicString),
addressPrefixed,
})
}
/**
* @method createFromPrivateString
* @memberof module:keys
* @param _privateString {String} an Ethereum private key, not prefixed with `0x`
* @return an Immutable {Map} representing an Ethereum account associated with the given
* private key string.
*/
keys.createFromPrivateString = (privateString, ivString, saltString) => {
const privateBuffer = keys.buffer(privateString)
assert(utils.isValidPrivate(privateBuffer))
const publicBuffer = utils.privateToPublic(privateBuffer)
const addressBuffer = utils.privateToAddress(privateBuffer)
const _ivString = ivString || keys.hex(keys.randombytes(keys.PARAMS.ivBytes))
const _saltString = saltString || keys.hex(keys.randombytes(32))
const privatePrefixed = keys.prefixed(privateString)
const publicPrefixed = keys.prefixed(publicBuffer.toString('hex'))
const addressPrefixed = utils.toChecksumAddress(addressBuffer.toString('hex'))
return Map({
privatePrefixed,
publicPrefixed,
addressPrefixed,
privateString,
publicString : publicPrefixed.slice(2),
addressString : addressPrefixed.slice(2),
ivString : _ivString,
saltString : _saltString,
})
}
/**
* @method createFromPrivateBuffer
* @memberof module:keys
* @param _privateBuffer {Buffer} the private Ethereum key
* @param _ivBuffer {Buffer} the initialization vector from private key generation
* @param _salt {Buffer} a random salt for later enciphering.
* @return an Immutable {Map} representing an Ethereum account associated with the given
* private parameters.
*/
keys.createFromPrivateBuffer = (_privateBuffer, _ivBuffer, _saltBuffer) => {
const publicBuffer = utils.privateToPublic(_privateBuffer)
const addressBuffer = utils.privateToAddress(_privateBuffer)
return keys.bufferToMap(_privateBuffer, publicBuffer, addressBuffer, _ivBuffer, _saltBuffer)
}
/**
* @method isAccount
* @memberof module:keys
* @return true if the given object is an Immutable {Map} representing an Ethereum account.
*/
keys.isAccount = (_map) => {
return (Map.isMap(_map) &&
_map.get('privateString' ) &&
_map.get('publicString' ) &&
_map.get('addressString' ) &&
_map.get('privatePrefixed') &&
_map.get('publicPrefixed' ) &&
_map.get('addressPrefixed') &&
utils.isValidPrivate(Buffer.from(_map.get('privateString'), 'hex')) &&
utils.isValidPublic(Buffer.from(_map.get('publicString'), 'hex')) &&
utils.isValidAddress(_map.get('addressPrefixed'))
)
}
/**
* Keythereum options for enciphered password dumps and loads.
* @property OPTIONS
* @memberof module:keys
*/
keys.OPTIONS = {
kdf: 'pbkdf2',
cipher: 'aes-128-ctr',
kdfparams: {
c: 262144,
dklen: 32,
prf: 'hmac-sha256'
}
}
/**
* @method accountToEncryptedJSON
* @memberof module:keys
* @param account an Immutable {Map} the Ethereum account to encipher.
* @param password {String} a password to create an encrypted dump with Keythereum.
* @return a JSON file representing the given Ethereum account in encipher form.
*/
keys.accountToEncryptedJSON = ({ account, password }) => {
return keys.keythereum.dump( password,
keys.buffer(account.get('privateString')),
keys.buffer(account.get('saltString')),
keys.buffer(account.get('ivString')), keys.OPTIONS)
}
/**
* @method encryptedJSONToAccount
* @memberof module:keys
* @param encryptedJSON {JSON} the enciphered form of an Ethereum account
* @param password {String} for deciphering the encrypted JSON.
* @return an Immutable {Map} representing the deciphered Ethereum account.
*/
keys.encryptedJSONToAccount = ({ encryptedJSON, password }) => {
LOGGER.debug('encryptedJSONToAccount', encryptedJSON, password)
const privateBuffer = keys.keythereum.recover(password, encryptedJSON)
const ivString = encryptedJSON.crypto.cipherparams.iv
assert.equal(ivString.length, keys.PARAMS.ivBytes * 2)
const saltString = encryptedJSON.crypto.kdfparams.salt
assert.equal(saltString.length, keys.PARAMS.keyBytes * 2)
return keys.createFromPrivateBuffer(
privateBuffer, keys.buffer(ivString), keys.buffer(saltString))
}
/**
* @method hex
* @memberof module:keys
* @return the given buffer as a hex string (without a `0x` prefix)
*/
keys.hex = (_buffer) => {
return _buffer.toString('hex')
}
/**
* @method buffer
* @memberof module:keys
* @return the given hex string as a browser-friendly Buffer
*/
keys.buffer = (_hex) => {
return keys.keythereum.str2buf(_hex, 'hex')
}
/**
* @method prefixed
* @memberof module:keys
* @return the given string with a `0x` prefix
*/
keys.prefixed = (_str) => {
return '0x' + _str
}
module.exports = keys