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

Commit 00b1cdf

Browse files
committed
fix: libp2p records for ipns should be signed
1 parent 6d960f3 commit 00b1cdf

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

src/core/ipns/publisher.js

+36-22
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class IpnsPublisher {
4040
return callback(err)
4141
}
4242

43-
this._putRecordToRouting(record, peerId, callback)
43+
this._putRecordToRouting(record, peerId, privKey, callback)
4444
})
4545
})
4646
}
@@ -50,7 +50,7 @@ class IpnsPublisher {
5050
this.publishWithEOL(privKey, value, defaultRecordTtl, callback)
5151
}
5252

53-
_putRecordToRouting (record, peerId, callback) {
53+
_putRecordToRouting (record, peerId, privKey, callback) {
5454
if (!(peerId instanceof PeerId)) {
5555
const errMsg = `peerId received is not valid`
5656

@@ -74,10 +74,10 @@ class IpnsPublisher {
7474
}
7575

7676
series([
77-
(cb) => this._publishEntry(keys.ipnsKey, embedPublicKeyRecord || record, peerId, cb),
77+
(cb) => this._publishEntry(keys.ipnsKey, embedPublicKeyRecord || record, peerId, privKey, cb),
7878
// Publish the public key if a public key cannot be extracted from the ID
7979
// We will be able to deprecate this part in the future, since the public keys will be only in the peerId
80-
(cb) => embedPublicKeyRecord ? this._publishPublicKey(keys.pkKey, publicKey, peerId, cb) : cb()
80+
(cb) => embedPublicKeyRecord ? this._publishPublicKey(keys.pkKey, publicKey, peerId, privKey, cb) : cb()
8181
], (err) => {
8282
if (err) {
8383
log.error(err)
@@ -89,7 +89,7 @@ class IpnsPublisher {
8989
})
9090
}
9191

92-
_publishEntry (key, entry, peerId, callback) {
92+
_publishEntry (key, entry, peerId, privKey, callback) {
9393
if (!(key instanceof Key)) {
9494
const errMsg = `datastore key does not have a valid format`
9595

@@ -108,21 +108,28 @@ class IpnsPublisher {
108108
return callback(err)
109109
}
110110

111-
// TODO Routing - this should be replaced by a put to the DHT
112-
this._repo.datastore.put(key, rec.serialize(), (err, res) => {
111+
rec.serializeSigned(privKey, (err, serializedRecord) => {
113112
if (err) {
114-
const errMsg = `ipns record for ${key.toString()} could not be stored in the routing`
115-
116-
log.error(errMsg)
117-
return callback(errcode(new Error(errMsg), 'ERR_STORING_IN_DATASTORE'))
113+
log.error(err)
114+
return callback(err)
118115
}
119116

120-
log(`ipns record for ${key.toString()} was stored in the routing`)
121-
callback(null, res)
117+
// TODO Routing - this should be replaced by a put to the DHT
118+
this._repo.datastore.put(key, serializedRecord, (err, res) => {
119+
if (err) {
120+
const errMsg = `ipns record for ${key.toString()} could not be stored in the routing`
121+
122+
log.error(errMsg)
123+
return callback(errcode(new Error(errMsg), 'ERR_STORING_IN_DATASTORE'))
124+
}
125+
126+
log(`ipns record for ${key.toString()} was stored in the routing`)
127+
callback(null, res)
128+
})
122129
})
123130
}
124131

125-
_publishPublicKey (key, publicKey, peerId, callback) {
132+
_publishPublicKey (key, publicKey, peerId, privKey, callback) {
126133
if (!(key instanceof Key)) {
127134
const errMsg = `datastore key does not have a valid format`
128135

@@ -146,17 +153,24 @@ class IpnsPublisher {
146153
return callback(err)
147154
}
148155

149-
// TODO Routing - this should be replaced by a put to the DHT
150-
this._repo.datastore.put(key, rec.serialize(), (err, res) => {
156+
rec.serializeSigned(privKey, (err, serializedRecord) => {
151157
if (err) {
152-
const errMsg = `public key for ${key.toString()} could not be stored in the routing`
153-
154-
log.error(errMsg)
155-
return callback(errcode(new Error(errMsg), 'ERR_STORING_IN_DATASTORE'))
158+
log.error(err)
159+
return callback(err)
156160
}
157161

158-
log(`public key for ${key.toString()} was stored in the routing`)
159-
callback(null, res)
162+
// TODO Routing - this should be replaced by a put to the DHT
163+
this._repo.datastore.put(key, serializedRecord, (err, res) => {
164+
if (err) {
165+
const errMsg = `public key for ${key.toString()} could not be stored in the routing`
166+
167+
log.error(errMsg)
168+
return callback(errcode(new Error(errMsg), 'ERR_STORING_IN_DATASTORE'))
169+
}
170+
171+
log(`public key for ${key.toString()} was stored in the routing`)
172+
callback(null, res)
173+
})
160174
})
161175
}
162176

src/core/ipns/resolver.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,20 @@ class IpnsResolver {
133133
return callback(err)
134134
}
135135

136-
// Record validation
137-
ipns.validate(pubKey, ipnsEntry, (err) => {
136+
// libp2p record signature validation
137+
record.verifySignature(pubKey, (err) => {
138138
if (err) {
139139
return callback(err)
140140
}
141141

142-
callback(null, ipnsEntry.value.toString())
142+
// IPNS entry validation
143+
ipns.validate(pubKey, ipnsEntry, (err) => {
144+
if (err) {
145+
return callback(err)
146+
}
147+
148+
callback(null, ipnsEntry.value.toString())
149+
})
143150
})
144151
})
145152
})

0 commit comments

Comments
 (0)