Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the example demos #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

rodkeys
Copy link
Contributor

@rodkeys rodkeys commented Nov 13, 2018

Fixed issue where recent changes caused subscription demo to fail. Also added a simple chat message demo.

@hoffmabc
Copy link
Member

Thanks for the submission. I’ll take a look soon.

@rmisio
Copy link

rmisio commented Nov 25, 2019

@rodkeys I found that, given the same peerID, the subscription key in this example generated via generateSubscriptionKey does not match the one generated by the go code.

Here's a tweaked generateSubscriptionKey function which will make the keys match.

(FWIW, the keys matching is critical to messages being routed properly)

import {
  fromB58String,
  toB58String,
  decode,
  encode
} from 'multihashes';
import BufferShift from 'buffershift';

export async function generateSubscriptionKey(peerID) {
  const mh = fromB58String(peerID);
  const decodedMh = decode(mh);
  const digest = decodedMh.digest;
  const prefix = new Buffer(new Uint8Array(digest.slice(0, 8)));

  const shifted = new Buffer(prefix.length);
  prefix.copy(shifted);
  BufferShift.shr(shifted, 48);

  const checksum = await crypto.subtle.digest('SHA-256', shifted);
  const subscriptionKey = encode(Buffer.from(checksum), 'sha2-256');
  return toB58String(subscriptionKey);
}

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

Successfully merging this pull request may close these issues.

3 participants