Skip to content

Updated the example demos#10

Open
rodkeys wants to merge 3 commits into
OpenBazaar:masterfrom
rodkeys:master
Open

Updated the example demos#10
rodkeys wants to merge 3 commits into
OpenBazaar:masterfrom
rodkeys:master

Conversation

@rodkeys

@rodkeys rodkeys commented Nov 13, 2018

Copy link
Copy Markdown
Contributor

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

@hoffmabc

Copy link
Copy Markdown
Member

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

@rmisio

rmisio commented Nov 25, 2019

Copy link
Copy Markdown

@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