Skip to content

Commit ce98789

Browse files
committed
chore: downgrade peer-id to the same version used by libp2p
This is to ensure you can dial other peers on the network, otherwise libp2p doesn't think peer-ids are peer-ids.
1 parent 6497e38 commit ce98789

File tree

3 files changed

+44
-47
lines changed

3 files changed

+44
-47
lines changed

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,25 @@
3535
"dependencies": {
3636
"base32-encode": "^1.1.0",
3737
"debug": "^4.1.1",
38-
"err-code": "^1.1.2",
38+
"err-code": "^2.0.0",
3939
"interface-datastore": "~0.7.0",
4040
"left-pad": "^1.3.0",
41-
"libp2p-crypto": "~0.17.0",
41+
"libp2p-crypto": "libp2p/js-libp2p-crypto#v0.16.x-upgrade-node-forge",
4242
"multihashes": "~0.4.14",
43-
"peer-id": "~0.13.2",
43+
"peer-id": "^0.12.2",
44+
"promisify-es6": "^1.0.3",
4445
"protons": "^1.0.1",
4546
"timestamp-nano": "^1.0.0"
4647
},
4748
"devDependencies": {
48-
"aegir": "^18.0.3",
49+
"aegir": "^20.3.1",
4950
"chai": "^4.2.0",
5051
"chai-bytes": "~0.1.2",
5152
"chai-string": "^1.5.0",
5253
"dirty-chai": "^2.0.1",
53-
"ipfs": "~0.33.1",
54-
"ipfsd-ctl": "~0.40.2"
54+
"ipfs": "^0.37.1",
55+
"ipfs-http-client": "^33.1.1",
56+
"ipfsd-ctl": "^0.47.2"
5557
},
5658
"contributors": [
5759
"Diogo Silva <fsdiogo@gmail.com>",

src/index.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const crypto = require('libp2p-crypto')
77
const PeerId = require('peer-id')
88
const multihash = require('multihashes')
99
const errCode = require('err-code')
10+
const promisify = require('promisify-es6')
1011

1112
const debug = require('debug')
1213
const log = debug('jsipns')
@@ -91,13 +92,15 @@ const validate = async (publicKey, entry) => {
9192
// Validate Signature
9293
let isValid
9394
try {
94-
isValid = await publicKey.verify(dataForSignature, entry.signature)
95+
isValid = await promisify(publicKey.verify, {
96+
context: publicKey
97+
})(dataForSignature, entry.signature)
9598
} catch (err) {
9699
isValid = false
97100
}
98101
if (!isValid) {
99102
log.error('record signature verification failed')
100-
throw errCode('record signature verification failed', ERRORS.ERR_SIGNATURE_VERIFICATION)
103+
throw errCode(new Error('record signature verification failed'), ERRORS.ERR_SIGNATURE_VERIFICATION)
101104
}
102105

103106
// Validate according to the validity type
@@ -108,16 +111,16 @@ const validate = async (publicKey, entry) => {
108111
validityDate = parseRFC3339(validity.toString())
109112
} catch (e) {
110113
log.error('unrecognized validity format (not an rfc3339 format)')
111-
throw errCode('unrecognized validity format (not an rfc3339 format)', ERRORS.ERR_UNRECOGNIZED_FORMAT)
114+
throw errCode(new Error('unrecognized validity format (not an rfc3339 format)'), ERRORS.ERR_UNRECOGNIZED_FORMAT)
112115
}
113116

114117
if (validityDate < Date.now()) {
115118
log.error('record has expired')
116-
throw errCode('record has expired', ERRORS.ERR_IPNS_EXPIRED_RECORD)
119+
throw errCode(new Error('record has expired'), ERRORS.ERR_IPNS_EXPIRED_RECORD)
117120
}
118121
} else if (validityType) {
119122
log.error('unrecognized validity type')
120-
throw errCode('unrecognized validity type', ERRORS.ERR_UNRECOGNIZED_VALIDITY)
123+
throw errCode(new Error('unrecognized validity type'), ERRORS.ERR_UNRECOGNIZED_VALIDITY)
121124
}
122125

123126
log(`ipns entry for ${value} is valid`)
@@ -138,15 +141,15 @@ const validate = async (publicKey, entry) => {
138141
*/
139142
const embedPublicKey = async (publicKey, entry) => {
140143
if (!publicKey || !publicKey.bytes || !entry) {
141-
const error = 'one or more of the provided parameters are not defined'
144+
const error = new Error('one or more of the provided parameters are not defined')
142145
log.error(error)
143146
throw errCode(error, ERRORS.ERR_UNDEFINED_PARAMETER)
144147
}
145148

146149
// Create a peer id from the public key.
147150
let peerId
148151
try {
149-
peerId = await PeerId.createFromPubKey(publicKey.bytes)
152+
peerId = await promisify(PeerId.createFromPubKey)(publicKey.bytes)
150153
} catch (err) {
151154
throw errCode(err, ERRORS.ERR_PEER_ID_FROM_PUBLIC_KEY)
152155
}
@@ -183,7 +186,7 @@ const embedPublicKey = async (publicKey, entry) => {
183186
*/
184187
const extractPublicKey = (peerId, entry) => {
185188
if (!entry || !peerId) {
186-
const error = 'one or more of the provided parameters are not defined'
189+
const error = new Error('one or more of the provided parameters are not defined')
187190

188191
log.error(error)
189192
throw errCode(error, ERRORS.ERR_UNDEFINED_PARAMETER)
@@ -241,10 +244,13 @@ const getIdKeys = (pid) => {
241244
const sign = (privateKey, value, validityType, validity) => {
242245
try {
243246
const dataForSignature = ipnsEntryDataForSig(value, validityType, validity)
244-
return privateKey.sign(dataForSignature)
247+
248+
return promisify(privateKey.sign, {
249+
context: privateKey
250+
})(dataForSignature)
245251
} catch (error) {
246252
log.error('record signature creation failed')
247-
throw errCode('record signature creation failed: ' + error.message, ERRORS.ERR_SIGNATURE_CREATION)
253+
throw errCode(new Error('record signature creation failed: ' + error.message), ERRORS.ERR_SIGNATURE_CREATION)
248254
}
249255
}
250256

@@ -254,7 +260,7 @@ const getValidityType = (validityType) => {
254260
return 'EOL'
255261
}
256262

257-
const error = `unrecognized validity type ${validityType.toString()}`
263+
const error = new Error(`unrecognized validity type ${validityType.toString()}`)
258264
log.error(error)
259265
throw errCode(error, ERRORS.ERR_UNRECOGNIZED_VALIDITY)
260266
}
@@ -287,9 +293,7 @@ const validator = {
287293
validate: async (marshalledData, key) => {
288294
const receivedEntry = unmarshal(marshalledData)
289295
const bufferId = key.slice('/ipns/'.length)
290-
let peerId
291-
292-
peerId = PeerId.createFromBytes(bufferId)
296+
const peerId = PeerId.createFromBytes(bufferId)
293297

294298
// extract public key
295299
const pubKey = extractPublicKey(peerId, receivedEntry)

test/index.spec.js

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ chai.use(chaiBytes)
1111
chai.use(chaiString)
1212

1313
const ipfs = require('ipfs')
14+
const ipfsHttpClient = require('ipfs-http-client')
1415
const DaemonFactory = require('ipfsd-ctl')
1516
const crypto = require('libp2p-crypto')
1617
const { fromB58String } = require('multihashes')
18+
const promisify = require('promisify-es6')
1719

1820
const ipns = require('../src')
1921
const ERRORS = require('../src/errors')
2022

21-
const df = DaemonFactory.create({ type: 'proc', exec: ipfs })
23+
const df = DaemonFactory.create({
24+
type: 'proc',
25+
exec: ipfs,
26+
IpfsClient: ipfsHttpClient
27+
})
2228

2329
describe('ipns', function () {
2430
this.timeout(20 * 1000)
@@ -30,35 +36,18 @@ describe('ipns', function () {
3036
let ipfsId = null
3137
let rsa = null
3238

33-
const spawnDaemon = () => {
34-
return new Promise((resolve, reject) => {
35-
df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => {
36-
expect(err).to.not.exist()
37-
ipfsd = _ipfsd
38-
ipfs = ipfsd.api
39-
40-
ipfs.id((err, id) => {
41-
if (err) {
42-
return reject(err)
43-
}
44-
45-
ipfsId = id
46-
resolve()
47-
})
48-
})
49-
})
50-
}
51-
5239
before(async () => {
53-
rsa = await crypto.keys.generateKeyPair('RSA', 2048)
54-
return spawnDaemon()
40+
rsa = await promisify(crypto.keys.generateKeyPair, {
41+
context: crypto.keys
42+
})('RSA', 2048)
43+
ipfsd = await df.spawn({ initOptions: { bits: 512 } })
44+
ipfs = ipfsd.api
45+
ipfsId = await ipfs.id()
5546
})
5647

57-
after(function (done) {
48+
after(async () => {
5849
if (ipfsd) {
59-
ipfsd.stop(() => done())
60-
} else {
61-
done()
50+
await ipfsd.stop()
6251
}
6352
})
6453

@@ -191,7 +180,9 @@ describe('ipns', function () {
191180
const sequence = 0
192181
const validity = 1000000
193182

194-
const ed25519 = await crypto.keys.generateKeyPair('ed25519', 2048)
183+
const ed25519 = await promisify(crypto.keys.generateKeyPair, {
184+
context: crypto.keys
185+
})('ed25519', 2048)
195186
const entry = await ipns.create(ed25519, cid, sequence, validity)
196187
const entryWithKey = ipns.embedPublicKey(ed25519.public, entry)
197188
expect(entryWithKey).to.not.exist() // Should be null

0 commit comments

Comments
 (0)