Create util mod with `bufToHex` that includes 0x prefix

As this is how the string is represented for Community id.
This commit is contained in:
Franck Royer 2021-10-06 12:51:36 +11:00
parent 5d04f731a7
commit 9f6abaf952
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 19 additions and 7 deletions

View File

@ -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
);

View File

@ -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)
);
});

View File

@ -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);
}

View File

@ -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));
}
}