From 9f6abaf952723e07d5691a847ebca061e5b704c5 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 6 Oct 2021 12:51:36 +1100 Subject: [PATCH] Create util mod with `bufToHex` that includes 0x prefix As this is how the string is represented for Community id. --- packages/status-communities/src/identity.ts | 5 +++-- packages/status-communities/src/messenger.spec.ts | 6 +++--- packages/status-communities/src/utils.ts | 11 +++++++++++ .../src/wire/application_metadata_message.ts | 4 ++-- 4 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 packages/status-communities/src/utils.ts diff --git a/packages/status-communities/src/identity.ts b/packages/status-communities/src/identity.ts index 88ec6e80..368d505b 100644 --- a/packages/status-communities/src/identity.ts +++ b/packages/status-communities/src/identity.ts @@ -2,9 +2,10 @@ import { Buffer } from "buffer"; import { keccak256 } from "js-sha3"; import { generatePrivateKey } from "js-waku"; -import { utils } from "js-waku"; import * as secp256k1 from "secp256k1"; +import { hexToBuf } from "./utils"; + export class Identity { public constructor(public privateKey: Uint8Array) {} @@ -20,7 +21,7 @@ export class Identity { const hash = keccak256(payload); const { signature, recid } = secp256k1.ecdsaSign( - utils.hexToBuf(hash), + hexToBuf(hash), this.privateKey ); diff --git a/packages/status-communities/src/messenger.spec.ts b/packages/status-communities/src/messenger.spec.ts index 6d10a9eb..1cf734d2 100644 --- a/packages/status-communities/src/messenger.spec.ts +++ b/packages/status-communities/src/messenger.spec.ts @@ -1,9 +1,9 @@ import { expect } from "chai"; import debug from "debug"; -import { utils } from "js-waku"; import { Identity } from "./identity"; import { Messenger } from "./messenger"; +import { bufToHex } from "./utils"; import { ApplicationMetadataMessage } from "./wire/application_metadata_message"; import { ContentType } from "./wire/chat_message"; @@ -106,8 +106,8 @@ describe("Messenger", () => { const receivedMessage = await receivedMessagePromise; - expect(utils.bufToHex(receivedMessage.signer!)).to.eq( - utils.bufToHex(identityAlice.publicKey) + expect(bufToHex(receivedMessage.signer!)).to.eq( + bufToHex(identityAlice.publicKey) ); }); diff --git a/packages/status-communities/src/utils.ts b/packages/status-communities/src/utils.ts new file mode 100644 index 00000000..e8a4656d --- /dev/null +++ b/packages/status-communities/src/utils.ts @@ -0,0 +1,11 @@ +import { utils } from "js-waku"; + +const hexToBuf = utils.hexToBuf; +export { hexToBuf }; + +/** + * Return hex string with 0x prefix (commonly used for string format of a community id/public key. + */ +export function bufToHex(buf: Uint8Array): string { + return "0x" + utils.bufToHex(buf); +} diff --git a/packages/status-communities/src/wire/application_metadata_message.ts b/packages/status-communities/src/wire/application_metadata_message.ts index b9ce2d0b..f7458b57 100644 --- a/packages/status-communities/src/wire/application_metadata_message.ts +++ b/packages/status-communities/src/wire/application_metadata_message.ts @@ -1,11 +1,11 @@ import { keccak256 } from "js-sha3"; -import { utils } from "js-waku"; import { Reader } from "protobufjs"; import secp256k1 from "secp256k1"; import { Identity } from "../identity"; import * as proto from "../proto/status/v1/application_metadata_message"; import { ApplicationMetadataMessage_Type } from "../proto/status/v1/application_metadata_message"; +import { hexToBuf } from "../utils"; import { ChatMessage } from "./chat_message"; @@ -70,6 +70,6 @@ export class ApplicationMetadataMessage { const recid = this.signature.slice(64)[0]; const hash = keccak256(this.payload); - return secp256k1.ecdsaRecover(signature, recid, utils.hexToBuf(hash)); + return secp256k1.ecdsaRecover(signature, recid, hexToBuf(hash)); } }