Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 21276ae

Browse files
author
Alan Shaw
committedDec 17, 2019
refactor: apply async peer ID changes
From #2588 thanks to @lidel
1 parent 2fe568f commit 21276ae

File tree

11 files changed

+24
-35
lines changed

11 files changed

+24
-35
lines changed
 

‎README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,9 @@ Instead of a boolean, you may provide an object with custom initialization optio
314314
```js
315315
// Generating a Peer ID:
316316
const PeerId = require('peer-id')
317-
PeerId.create({ bits: 2048 }, (err, peerId) => {
318-
// Generates a new Peer ID, complete with public/private keypair
319-
// See https://github.com/libp2p/js-peer-id
320-
})
317+
// Generates a new Peer ID, complete with public/private keypair
318+
// See https://github.com/libp2p/js-peer-id
319+
const peerId = await PeerId.create({ bits: 2048 })
321320
```
322321
- `pass` (string) A passphrase to encrypt keys. You should generally use the [top-level `pass` option](#optionspass) instead of the `init.pass` option (this one will take its value from the top-level option if not set).
323322
- `profiles` (Array) Apply profile settings to config.

‎src/core/components/dht.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module.exports = (self) => {
9898
*/
9999
findPeer: callbackify(async (peer) => { // eslint-disable-line require-await
100100
if (typeof peer === 'string') {
101-
peer = PeerId.createFromB58String(peer)
101+
peer = PeerId.createFromCID(peer)
102102
}
103103

104104
return self.libp2p.peerRouting.findPeer(peer)
@@ -160,7 +160,7 @@ module.exports = (self) => {
160160
query: callbackify(async (peerId) => {
161161
if (typeof peerId === 'string') {
162162
try {
163-
peerId = PeerId.createFromB58String(peerId)
163+
peerId = PeerId.createFromCID(peerId)
164164
} catch (err) {
165165
log.error(err)
166166

‎src/core/components/init.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const log = require('debug')('ipfs:components:init')
44
const PeerId = require('peer-id')
55
const PeerInfo = require('peer-info')
66
const mergeOptions = require('merge-options')
7-
const promisify = require('promisify-es6')
87
const getDefaultConfig = require('../runtime/config-nodejs.js')
98
const createRepo = require('../runtime/repo-nodejs')
109
const Keychain = require('libp2p-keychain')
@@ -72,7 +71,7 @@ module.exports = ({
7271

7372
const { peerId, config, keychain } = isInitialized
7473
? await initExistingRepo(repo, options)
75-
: await initNewRepo(repo, options)
74+
: await initNewRepo(repo, { ...options, print })
7675

7776
log('peer created')
7877
const peerInfo = new PeerInfo(peerId)
@@ -242,7 +241,7 @@ async function initExistingRepo (repo, { config: newConfig, profiles, pass }) {
242241
log('keychain constructed')
243242
}
244243

245-
const peerId = await promisify(PeerId.createFromPrivKey)(config.Identity.PrivKey)
244+
const peerId = await PeerId.createFromPrivKey(config.Identity.PrivKey)
246245

247246
// Import the private key as 'self', if needed.
248247
if (pass) {
@@ -262,11 +261,11 @@ function createPeerId ({ privateKey, bits, print }) {
262261
log('using user-supplied private-key')
263262
return typeof privateKey === 'object'
264263
? privateKey
265-
: promisify(PeerId.createFromPrivKey)(Buffer.from(privateKey, 'base64'))
264+
: PeerId.createFromPrivKey(Buffer.from(privateKey, 'base64'))
266265
} else {
267266
// Generate peer identity keypair + transform to desired format + add to config.
268267
print('generating %s-bit RSA keypair...', bits)
269-
return promisify(PeerId.create)({ bits })
268+
return PeerId.create({ bits })
270269
}
271270
}
272271

‎src/core/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async function create (options) {
4242
const apiManager = new ApiManager()
4343

4444
const { api } = apiManager.update({
45-
init: Components.init({ apiManager, print, constructorOptions: options }),
45+
init: Components.init({ apiManager, print, options }),
4646
dns: Components.dns(),
4747
isOnline: Components.isOnline({})
4848
}, () => { throw new NotInitializedError() })

‎src/core/ipns/publisher.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const PeerId = require('peer-id')
44
const { Key, Errors } = require('interface-datastore')
55
const errcode = require('err-code')
6-
const promisify = require('promisify-es6')
76
const debug = require('debug')
87
const log = debug('ipfs:ipns:publisher')
98
log.error = debug('ipfs:ipns:publisher:error')
@@ -26,7 +25,7 @@ class IpnsPublisher {
2625
throw errcode(new Error('invalid private key'), 'ERR_INVALID_PRIVATE_KEY')
2726
}
2827

29-
const peerId = await promisify(PeerId.createFromPrivKey)(privKey.bytes)
28+
const peerId = await PeerId.createFromPrivKey(privKey.bytes)
3029
const record = await this._updateOrCreateRecord(privKey, value, lifetime, peerId)
3130

3231
return this._putRecordToRouting(record, peerId)

‎src/core/ipns/republisher.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const ipns = require('ipns')
44
const crypto = require('libp2p-crypto')
55
const PeerId = require('peer-id')
66
const errcode = require('err-code')
7-
const promisify = require('promisify-es6')
87

98
const debug = require('debug')
109
const log = debug('ipfs:ipns:republisher')
@@ -132,7 +131,7 @@ class IpnsRepublisher {
132131
}
133132

134133
try {
135-
const peerId = await promisify(PeerId.createFromPrivKey)(privateKey.bytes)
134+
const peerId = await PeerId.createFromPrivKey(privateKey.bytes)
136135
const value = await this._getPreviousValue(peerId)
137136
await this._publisher.publishWithEOL(privateKey, value, defaultRecordLifetime)
138137
} catch (err) {

‎src/core/ipns/resolver.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const ipns = require('ipns')
44
const crypto = require('libp2p-crypto')
55
const PeerId = require('peer-id')
66
const errcode = require('err-code')
7-
const CID = require('cids')
8-
97
const debug = require('debug')
108
const log = debug('ipfs:ipns:resolver')
119
log.error = debug('ipfs:ipns:resolver:error')
@@ -75,7 +73,7 @@ class IpnsResolver {
7573

7674
// resolve ipns entries from the provided routing
7775
async _resolveName (name) {
78-
const peerId = PeerId.createFromBytes(new CID(name).multihash) // TODO: change to `PeerId.createFromCID` when https://github.com/libp2p/js-peer-id/pull/105 lands and js-ipfs switched to async peer-id lib
76+
const peerId = PeerId.createFromCID(name)
7977
const { routingKey } = ipns.getIdKeys(peerId.toBytes())
8078
let record
8179

‎test/cli/bitswap.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ describe('bitswap', () => runOn((thing) => {
2222
ipfs('block get ' + key1).catch(() => {})
2323
})
2424

25-
before(function (done) {
26-
PeerId.create({ bits: 512 }, (err, peer) => {
27-
expect(err).to.not.exist()
28-
peerId = peer.toB58String()
29-
done()
30-
})
25+
before(async function () {
26+
const peer = await PeerId.create({ bits: 512 })
27+
peerId = peer.toB58String()
3128
})
3229

3330
before(async () => {

‎test/cli/swarm.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,9 @@ describe('swarm', () => {
8383
}
8484

8585
describe('addrs', () => {
86-
before((done) => {
87-
PeerId.create({ bits: 512 }, (err, peerId) => {
88-
if (err) return done(err)
89-
peerInfo = new PeerInfo(peerId)
90-
done()
91-
})
86+
before(async () => {
87+
const peerId = await PeerId.create({ bits: 512 })
88+
peerInfo = new PeerInfo(peerId)
9289
})
9390

9491
it('should return addresses for all peers', (done) => {

‎test/core/interface.spec.js

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ describe('interface-ipfs-core tests', function () {
3232
}
3333
const commonFactory = createFactory(commonOptions, overrides)
3434

35+
tests.root(commonFactory)
36+
3537
tests.bitswap(commonFactory)
3638

3739
tests.block(commonFactory)

‎test/core/name-pubsub.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ const hat = require('hat')
66
const { expect } = require('interface-ipfs-core/src/utils/mocha')
77
const base64url = require('base64url')
88
const { fromB58String } = require('multihashes')
9-
const peerId = require('peer-id')
9+
const PeerId = require('peer-id')
1010
const isNode = require('detect-node')
1111
const ipns = require('ipns')
1212
const waitFor = require('../utils/wait-for')
1313
const delay = require('delay')
14-
const promisify = require('promisify-es6')
1514

1615
const factory = require('../utils/factory')
1716

@@ -157,8 +156,8 @@ describe('name-pubsub', function () {
157156
await nodeB.pubsub.subscribe(topic, checkMessage)
158157
await nodeA.name.publish(ipfsRef, { resolve: false, key: testAccountName })
159158
await waitFor(alreadySubscribed)
160-
const messageKey = await promisify(peerId.createFromPubKey)(publishedMessage.key)
161-
const pubKeyPeerId = await promisify(peerId.createFromPubKey)(publishedMessageData.pubKey)
159+
const messageKey = await PeerId.createFromPubKey(publishedMessage.key)
160+
const pubKeyPeerId = await PeerId.createFromPubKey(publishedMessageData.pubKey)
162161

163162
expect(pubKeyPeerId.toB58String()).not.to.equal(messageKey.toB58String())
164163
expect(pubKeyPeerId.toB58String()).to.equal(testAccount.id)

0 commit comments

Comments
 (0)