|
7 | 7 | const mh = require('multihashes')
|
8 | 8 | const cryptoKeys = require('libp2p-crypto/src/keys')
|
9 | 9 | const assert = require('assert')
|
10 |
| -const waterfall = require('async/waterfall') |
11 | 10 | const withIs = require('class-is')
|
12 | 11 |
|
13 | 12 | class PeerId {
|
@@ -119,176 +118,111 @@ class PeerId {
|
119 | 118 | /*
|
120 | 119 | * Check if this PeerId instance is valid (privKey -> pubKey -> Id)
|
121 | 120 | */
|
122 |
| - isValid (callback) { |
123 |
| - // TODO Needs better checking |
124 |
| - if (this.privKey && |
| 121 | + isValid () { |
| 122 | + // TODO: needs better checking |
| 123 | + return Boolean(this.privKey && |
125 | 124 | this.privKey.public &&
|
126 | 125 | this.privKey.public.bytes &&
|
127 | 126 | Buffer.isBuffer(this.pubKey.bytes) &&
|
128 |
| - this.privKey.public.bytes.equals(this.pubKey.bytes)) { |
129 |
| - callback() |
130 |
| - } else { |
131 |
| - callback(new Error('Keys not match')) |
132 |
| - } |
| 127 | + this.privKey.public.bytes.equals(this.pubKey.bytes)) |
133 | 128 | }
|
134 | 129 | }
|
135 | 130 |
|
136 |
| -const PeerIdWithIs = withIs(PeerId, { className: 'PeerId', symbolName: '@libp2p/js-peer-id/PeerId' }) |
| 131 | +const PeerIdWithIs = withIs(PeerId, { |
| 132 | + className: 'PeerId', |
| 133 | + symbolName: '@libp2p/js-peer-id/PeerId' |
| 134 | +}) |
137 | 135 |
|
138 | 136 | exports = module.exports = PeerIdWithIs
|
139 | 137 |
|
140 | 138 | // generation
|
141 |
| -exports.create = function (opts, callback) { |
142 |
| - if (typeof opts === 'function') { |
143 |
| - callback = opts |
144 |
| - opts = {} |
145 |
| - } |
| 139 | +exports.create = async (opts) => { |
146 | 140 | opts = opts || {}
|
147 | 141 | opts.bits = opts.bits || 2048
|
148 | 142 |
|
149 |
| - waterfall([ |
150 |
| - (cb) => cryptoKeys.generateKeyPair('RSA', opts.bits, cb), |
151 |
| - (privKey, cb) => privKey.public.hash((err, digest) => { |
152 |
| - cb(err, digest, privKey) |
153 |
| - }) |
154 |
| - ], (err, digest, privKey) => { |
155 |
| - if (err) { |
156 |
| - return callback(err) |
157 |
| - } |
| 143 | + const key = await cryptoKeys.generateKeyPair('RSA', opts.bits) |
| 144 | + const digest = await key.public.hash() |
158 | 145 |
|
159 |
| - callback(null, new PeerIdWithIs(digest, privKey)) |
160 |
| - }) |
| 146 | + return new PeerIdWithIs(digest, key) |
161 | 147 | }
|
162 | 148 |
|
163 |
| -exports.createFromHexString = function (str) { |
| 149 | +exports.createFromHexString = (str) => { |
164 | 150 | return new PeerIdWithIs(mh.fromHexString(str))
|
165 | 151 | }
|
166 | 152 |
|
167 |
| -exports.createFromBytes = function (buf) { |
| 153 | +exports.createFromBytes = (buf) => { |
168 | 154 | return new PeerIdWithIs(buf)
|
169 | 155 | }
|
170 | 156 |
|
171 |
| -exports.createFromB58String = function (str) { |
| 157 | +exports.createFromB58String = (str) => { |
172 | 158 | return new PeerIdWithIs(mh.fromB58String(str))
|
173 | 159 | }
|
174 | 160 |
|
175 | 161 | // Public Key input will be a buffer
|
176 |
| -exports.createFromPubKey = function (key, callback) { |
177 |
| - if (typeof callback !== 'function') { |
178 |
| - throw new Error('callback is required') |
179 |
| - } |
180 |
| - |
181 |
| - let pubKey |
182 |
| - |
183 |
| - try { |
184 |
| - let buf = key |
185 |
| - if (typeof buf === 'string') { |
186 |
| - buf = Buffer.from(key, 'base64') |
187 |
| - } |
188 |
| - |
189 |
| - if (!Buffer.isBuffer(buf)) throw new Error('Supplied key is neither a base64 string nor a buffer') |
| 162 | +exports.createFromPubKey = async (key) => { |
| 163 | + let buf = key |
190 | 164 |
|
191 |
| - pubKey = cryptoKeys.unmarshalPublicKey(buf) |
192 |
| - } catch (err) { |
193 |
| - return callback(err) |
| 165 | + if (typeof buf === 'string') { |
| 166 | + buf = Buffer.from(key, 'base64') |
194 | 167 | }
|
195 | 168 |
|
196 |
| - pubKey.hash((err, digest) => { |
197 |
| - if (err) { |
198 |
| - return callback(err) |
199 |
| - } |
| 169 | + if (!Buffer.isBuffer(buf)) { |
| 170 | + throw new Error('Supplied key is neither a base64 string nor a buffer') |
| 171 | + } |
200 | 172 |
|
201 |
| - callback(null, new PeerIdWithIs(digest, null, pubKey)) |
202 |
| - }) |
| 173 | + const pubKey = await cryptoKeys.unmarshalPublicKey(buf) |
| 174 | + const digest = await pubKey.hash() |
| 175 | + return new PeerIdWithIs(digest, null, pubKey) |
203 | 176 | }
|
204 | 177 |
|
205 | 178 | // Private key input will be a string
|
206 |
| -exports.createFromPrivKey = function (key, callback) { |
207 |
| - if (typeof callback !== 'function') { |
208 |
| - throw new Error('callback is required') |
209 |
| - } |
210 |
| - |
| 179 | +exports.createFromPrivKey = async (key) => { |
211 | 180 | let buf = key
|
212 | 181 |
|
213 |
| - try { |
214 |
| - if (typeof buf === 'string') { |
215 |
| - buf = Buffer.from(key, 'base64') |
216 |
| - } |
| 182 | + if (typeof buf === 'string') { |
| 183 | + buf = Buffer.from(key, 'base64') |
| 184 | + } |
217 | 185 |
|
218 |
| - if (!Buffer.isBuffer(buf)) throw new Error('Supplied key is neither a base64 string nor a buffer') |
219 |
| - } catch (err) { |
220 |
| - return callback(err) |
| 186 | + if (!Buffer.isBuffer(buf)) { |
| 187 | + throw new Error('Supplied key is neither a base64 string nor a buffer') |
221 | 188 | }
|
222 | 189 |
|
223 |
| - waterfall([ |
224 |
| - (cb) => cryptoKeys.unmarshalPrivateKey(buf, cb), |
225 |
| - (privKey, cb) => privKey.public.hash((err, digest) => { |
226 |
| - cb(err, digest, privKey) |
227 |
| - }) |
228 |
| - ], (err, digest, privKey) => { |
229 |
| - if (err) { |
230 |
| - return callback(err) |
231 |
| - } |
| 190 | + const privKey = await cryptoKeys.unmarshalPrivateKey(buf) |
| 191 | + const digest = await privKey.public.hash() |
232 | 192 |
|
233 |
| - callback(null, new PeerIdWithIs(digest, privKey, privKey.public)) |
234 |
| - }) |
| 193 | + return new PeerIdWithIs(digest, privKey, privKey.public) |
235 | 194 | }
|
236 | 195 |
|
237 |
| -exports.createFromJSON = function (obj, callback) { |
238 |
| - if (typeof callback !== 'function') { |
239 |
| - throw new Error('callback is required') |
| 196 | +exports.createFromJSON = async (obj) => { |
| 197 | + let id = mh.fromB58String(obj.id) |
| 198 | + let rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64') |
| 199 | + let rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64') |
| 200 | + let pub = rawPubKey && await cryptoKeys.unmarshalPublicKey(rawPubKey) |
| 201 | + |
| 202 | + if (!rawPrivKey) { |
| 203 | + return new PeerIdWithIs(id, null, pub) |
240 | 204 | }
|
241 | 205 |
|
242 |
| - let id |
243 |
| - let rawPrivKey |
244 |
| - let rawPubKey |
245 |
| - let pub |
246 |
| - |
247 |
| - try { |
248 |
| - id = mh.fromB58String(obj.id) |
249 |
| - rawPrivKey = obj.privKey && Buffer.from(obj.privKey, 'base64') |
250 |
| - rawPubKey = obj.pubKey && Buffer.from(obj.pubKey, 'base64') |
251 |
| - pub = rawPubKey && cryptoKeys.unmarshalPublicKey(rawPubKey) |
252 |
| - } catch (err) { |
253 |
| - return callback(err) |
| 206 | + const privKey = await cryptoKeys.unmarshalPrivateKey(rawPrivKey) |
| 207 | + const privDigest = await privKey.public.hash() |
| 208 | + let pubDigest |
| 209 | + |
| 210 | + if (pub) { |
| 211 | + pubDigest = await pub.hash() |
254 | 212 | }
|
255 | 213 |
|
256 |
| - if (rawPrivKey) { |
257 |
| - waterfall([ |
258 |
| - (cb) => cryptoKeys.unmarshalPrivateKey(rawPrivKey, cb), |
259 |
| - (priv, cb) => priv.public.hash((err, digest) => { |
260 |
| - cb(err, digest, priv) |
261 |
| - }), |
262 |
| - (privDigest, priv, cb) => { |
263 |
| - if (pub) { |
264 |
| - pub.hash((err, pubDigest) => { |
265 |
| - cb(err, privDigest, priv, pubDigest) |
266 |
| - }) |
267 |
| - } else { |
268 |
| - cb(null, privDigest, priv) |
269 |
| - } |
270 |
| - } |
271 |
| - ], (err, privDigest, priv, pubDigest) => { |
272 |
| - if (err) { |
273 |
| - return callback(err) |
274 |
| - } |
275 |
| - |
276 |
| - if (pub && !privDigest.equals(pubDigest)) { |
277 |
| - return callback(new Error('Public and private key do not match')) |
278 |
| - } |
279 |
| - |
280 |
| - if (id && !privDigest.equals(id)) { |
281 |
| - return callback(new Error('Id and private key do not match')) |
282 |
| - } |
283 |
| - |
284 |
| - callback(null, new PeerIdWithIs(id, priv, pub)) |
285 |
| - }) |
286 |
| - } else { |
287 |
| - callback(null, new PeerIdWithIs(id, null, pub)) |
| 214 | + if (pub && !privDigest.equals(pubDigest)) { |
| 215 | + throw new Error('Public and private key do not match') |
288 | 216 | }
|
| 217 | + |
| 218 | + if (id && !privDigest.equals(id)) { |
| 219 | + throw new Error('Id and private key do not match') |
| 220 | + } |
| 221 | + |
| 222 | + return new PeerIdWithIs(id, privKey, pub) |
289 | 223 | }
|
290 | 224 |
|
291 |
| -exports.isPeerId = function (peerId) { |
| 225 | +exports.isPeerId = (peerId) => { |
292 | 226 | return Boolean(typeof peerId === 'object' &&
|
293 | 227 | peerId._id &&
|
294 | 228 | peerId._idB58String)
|
|
0 commit comments