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

Commit

Permalink
fix: use Key.asKey instead of instanceOf
Browse files Browse the repository at this point in the history
Using `instanceOf` is not reliable since instances of classes can
cross the CLS/ESM boundary and/or come from different browser bundles.

Instead see if we can treat what we've been handed as a `Key` by
using the `Key.asKey` method.

Fixes #3852
  • Loading branch information
achingbrain committed Sep 17, 2021
1 parent 88c9921 commit 57587c9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/ipfs-core-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
],
"license": "(Apache-2.0 OR MIT)",
"dependencies": {
"interface-datastore": "^5.0.0",
"interface-datastore": "^5.2.0",
"multiaddr": "^10.0.0",
"multiformats": "^9.4.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"hamt-sharding": "^2.0.0",
"hashlru": "^2.3.0",
"interface-blockstore": "^1.0.0",
"interface-datastore": "^5.0.0",
"interface-datastore": "^5.2.0",
"ipfs-bitswap": "^6.0.0",
"ipfs-core-types": "^0.7.2",
"ipfs-core-utils": "^0.10.4",
Expand Down
18 changes: 11 additions & 7 deletions packages/ipfs-core/src/ipns/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class IpnsPublisher {
* @param {IPNSEntry} entry
*/
async _publishEntry (key, entry) {
if (!(key instanceof Key)) {
const k = Key.asKey(key)

if (!k) {
const errMsg = 'datastore key does not have a valid format'

log.error(errMsg)
Expand All @@ -112,12 +114,12 @@ class IpnsPublisher {

// Add record to routing (buffer key)
try {
const res = await this._routing.put(key.uint8Array(), entryData)
log(`ipns record for ${uint8ArrayToString(key.uint8Array(), 'base64')} was stored in the routing`)
const res = await this._routing.put(k.uint8Array(), entryData)
log(`ipns record for ${uint8ArrayToString(k.uint8Array(), 'base64')} was stored in the routing`)

return res
} catch (err) {
const errMsg = `ipns record for ${uint8ArrayToString(key.uint8Array(), 'base64')} could not be stored in the routing`
const errMsg = `ipns record for ${uint8ArrayToString(k.uint8Array(), 'base64')} could not be stored in the routing`
log.error(errMsg)
log.error(err)

Expand All @@ -130,7 +132,9 @@ class IpnsPublisher {
* @param {PublicKey} publicKey
*/
async _publishPublicKey (key, publicKey) {
if (!(key instanceof Key)) {
const k = Key.asKey(key)

if (!k) {
const errMsg = 'datastore key does not have a valid format'
log.error(errMsg)

Expand All @@ -146,12 +150,12 @@ class IpnsPublisher {

// Add public key to routing (buffer key)
try {
const res = await this._routing.put(key.uint8Array(), publicKey.bytes)
const res = await this._routing.put(k.uint8Array(), publicKey.bytes)
log(`public key for ${uint8ArrayToString(key.uint8Array(), 'base64')} was stored in the routing`)

return res
} catch (err) {
const errMsg = `public key for ${uint8ArrayToString(key.uint8Array(), 'base64')} could not be stored in the routing`
const errMsg = `public key for ${uint8ArrayToString(k.uint8Array(), 'base64')} could not be stored in the routing`
log.error(errMsg)
log.error(err)

Expand Down

0 comments on commit 57587c9

Please sign in to comment.