Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

fix: support uint8arrays in place of node buffers #23

Merged
merged 3 commits into from
Jul 29, 2020

Conversation

achingbrain
Copy link
Member

Allows passing Uint8Arrays in place of Node Buffers

BREAKING CHANGE: takes Uint8Arrays as well as Node Buffers

Allows passing Uint8Arrays in place of Node Buffers

BREAKING CHANGE: takes Uint8Arrays as well as Node Buffers
@achingbrain
Copy link
Member Author

achingbrain commented Jul 28, 2020

cc @Gozala (can't add you to the reviewers list)

Copy link

@Gozala Gozala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure 🚨 isn't introducing a regression, because I'm not exactly sure.

src/record.js Outdated Show resolved Hide resolved
@@ -16,7 +18,8 @@ const bestRecord = (selectors, k, records) => {
throw errcode(new Error(errMsg), 'ERR_NO_RECORDS_RECEIVED')
}

const parts = k.toString().split('/')
const kStr = utf8Decoder.decode(k)
const parts = kStr.split('/')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Seems like factoring out logic to count / char codes would be make sense here, which also could be then improved to avoid string encoding splitting etc...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to leave this in place as it's just doing what was being done before, but it's a valid point.

@@ -12,7 +13,8 @@ const errcode = require('err-code')
*/
const verifyRecord = (validators, record) => {
const key = record.key
const parts = key.toString().split('/')
const keyString = utf8Decoder.decode(key)
const parts = keyString.split('/')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 This is the second place were / char codes are counted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to leave this in place as it's just doing what was being done before, but it's a valid point.

throw errcode(new Error('invalid public key record'), 'ERR_INVALID_RECORD_KEY_TOO_SHORT')
}

const prefix = key.slice(0, 4).toString()
const keyString = utf8Decoder.decode(key)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Seems like const prefix = utf8Decoder.decode(key.subarray(0, 4)) would have kept same profile.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made this change

throw errcode(new Error('key was not prefixed with /pk/'), 'ERR_INVALID_RECORD_KEY_BAD_PREFIX')
}

const keyhash = key.slice(4)

const publicKeyHash = await multihashing(publicKey, 'sha2-256')

if (!keyhash.equals(publicKeyHash)) {
if (!keyhash.every((val, i) => val === publicKeyHash[i])) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 This does not take byteLength into account.
📝 Seems like equality check should be factored out into separate function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are Uint8Arrays so unless I'm missing something .length should always equal .byteLength so we should be good to use .every and friends?

That said it could be optimised to compare lengths before iterating over the values which would worth pulling into it's own function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pulled it into it's own function.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are Uint8Arrays so unless I'm missing something .length should always equal .byteLength so we should be good to use .every and friends?

They do. I was just trying to say that keyhash and publicKeyHash could have different length which this code was not taking into account.

achingbrain added a commit to ipfs/js-datastore-pubsub that referenced this pull request Jul 28, 2020
Pulls in the latest interface-datastore and replaces use of node
Buffers with Uint8Arrays

Depends on:

- [ ] ipfs/interface-datastore#43
- [ ] ipfs/js-datastore-core#27
- [ ] libp2p/js-libp2p-record#23
Copy link
Member

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@vasco-santos vasco-santos merged commit 3b99ee1 into master Jul 29, 2020
@vasco-santos vasco-santos deleted the fix/support-uint8arrays-in-place-of-buffers branch July 29, 2020 14:27
@vasco-santos
Copy link
Member

0.8.0 is shipped

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants