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:
parent
5d04f731a7
commit
9f6abaf952
|
@ -2,9 +2,10 @@ import { Buffer } from "buffer";
|
||||||
|
|
||||||
import { keccak256 } from "js-sha3";
|
import { keccak256 } from "js-sha3";
|
||||||
import { generatePrivateKey } from "js-waku";
|
import { generatePrivateKey } from "js-waku";
|
||||||
import { utils } from "js-waku";
|
|
||||||
import * as secp256k1 from "secp256k1";
|
import * as secp256k1 from "secp256k1";
|
||||||
|
|
||||||
|
import { hexToBuf } from "./utils";
|
||||||
|
|
||||||
export class Identity {
|
export class Identity {
|
||||||
public constructor(public privateKey: Uint8Array) {}
|
public constructor(public privateKey: Uint8Array) {}
|
||||||
|
|
||||||
|
@ -20,7 +21,7 @@ export class Identity {
|
||||||
const hash = keccak256(payload);
|
const hash = keccak256(payload);
|
||||||
|
|
||||||
const { signature, recid } = secp256k1.ecdsaSign(
|
const { signature, recid } = secp256k1.ecdsaSign(
|
||||||
utils.hexToBuf(hash),
|
hexToBuf(hash),
|
||||||
this.privateKey
|
this.privateKey
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
import { utils } from "js-waku";
|
|
||||||
|
|
||||||
import { Identity } from "./identity";
|
import { Identity } from "./identity";
|
||||||
import { Messenger } from "./messenger";
|
import { Messenger } from "./messenger";
|
||||||
|
import { bufToHex } from "./utils";
|
||||||
import { ApplicationMetadataMessage } from "./wire/application_metadata_message";
|
import { ApplicationMetadataMessage } from "./wire/application_metadata_message";
|
||||||
import { ContentType } from "./wire/chat_message";
|
import { ContentType } from "./wire/chat_message";
|
||||||
|
|
||||||
|
@ -106,8 +106,8 @@ describe("Messenger", () => {
|
||||||
|
|
||||||
const receivedMessage = await receivedMessagePromise;
|
const receivedMessage = await receivedMessagePromise;
|
||||||
|
|
||||||
expect(utils.bufToHex(receivedMessage.signer!)).to.eq(
|
expect(bufToHex(receivedMessage.signer!)).to.eq(
|
||||||
utils.bufToHex(identityAlice.publicKey)
|
bufToHex(identityAlice.publicKey)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -1,11 +1,11 @@
|
||||||
import { keccak256 } from "js-sha3";
|
import { keccak256 } from "js-sha3";
|
||||||
import { utils } from "js-waku";
|
|
||||||
import { Reader } from "protobufjs";
|
import { Reader } from "protobufjs";
|
||||||
import secp256k1 from "secp256k1";
|
import secp256k1 from "secp256k1";
|
||||||
|
|
||||||
import { Identity } from "../identity";
|
import { Identity } from "../identity";
|
||||||
import * as proto from "../proto/status/v1/application_metadata_message";
|
import * as proto from "../proto/status/v1/application_metadata_message";
|
||||||
import { ApplicationMetadataMessage_Type } 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";
|
import { ChatMessage } from "./chat_message";
|
||||||
|
|
||||||
|
@ -70,6 +70,6 @@ export class ApplicationMetadataMessage {
|
||||||
const recid = this.signature.slice(64)[0];
|
const recid = this.signature.slice(64)[0];
|
||||||
const hash = keccak256(this.payload);
|
const hash = keccak256(this.payload);
|
||||||
|
|
||||||
return secp256k1.ecdsaRecover(signature, recid, utils.hexToBuf(hash));
|
return secp256k1.ecdsaRecover(signature, recid, hexToBuf(hash));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue