Skip to content

Commit

Permalink
Merge pull request #1755 from waku-org/feat/validate-signature
Browse files Browse the repository at this point in the history
  • Loading branch information
fryorcraken authored Dec 12, 2023
2 parents 411b760 + 853ac74 commit 8ad470f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
24 changes: 22 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/message-encryption/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"@waku/proto": "0.0.5",
"@waku/utils": "0.0.13",
"debug": "^4.3.4",
"js-sha3": "^0.9.2"
"js-sha3": "^0.9.2",
"uint8arrays": "^5.0.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
Expand Down
13 changes: 13 additions & 0 deletions packages/message-encryption/src/decoded_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
proto
} from "@waku/core/lib/message/version_0";
import type { IDecodedMessage } from "@waku/interfaces";
import { equals } from "uint8arrays/equals";

export class DecodedMessage
extends DecodedMessageV0
Expand All @@ -24,4 +25,16 @@ export class DecodedMessage
get payload(): Uint8Array {
return this._decodedPayload;
}

/**
* Verify the message's signature against the public key.
*
* @returns true if the signature matches the public key, false if not or if no signature is present.
*/
verifySignature(publicKey: Uint8Array): boolean {
if (this.signaturePublicKey) {
return equals(this.signaturePublicKey, publicKey);
}
return false;
}
}
6 changes: 4 additions & 2 deletions packages/message-encryption/src/ecies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ describe("Ecies Encryption", function () {
expect(result.version).to.equal(1);
expect(result?.payload).to.deep.equal(payload);
expect(result.signature).to.be.undefined;
expect(result.verifySignature(new Uint8Array())).to.be.false;
expect(result.signaturePublicKey).to.be.undefined;
}
)
);
});

it("R trip binary encryption [ecies, signature]", async function () {
this.timeout(4000);
it("Round trip binary encryption [ecies, signature]", async function () {
this.timeout(6000);

await fc.assert(
fc.asyncProperty(
Expand Down Expand Up @@ -78,6 +79,7 @@ describe("Ecies Encryption", function () {
expect(result.version).to.equal(1);
expect(result?.payload).to.deep.equal(payload);
expect(result.signature).to.not.be.undefined;
expect(result.verifySignature(alicePublicKey)).to.be.true;
expect(result.signaturePublicKey).to.deep.eq(alicePublicKey);
}
)
Expand Down
2 changes: 2 additions & 0 deletions packages/message-encryption/src/symmetric.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("Symmetric Encryption", function () {
expect(result.version).to.equal(1);
expect(result?.payload).to.deep.equal(payload);
expect(result.signature).to.be.undefined;
expect(result.verifySignature(new Uint8Array())).to.be.false;
expect(result.signaturePublicKey).to.be.undefined;
}
)
Expand Down Expand Up @@ -66,6 +67,7 @@ describe("Symmetric Encryption", function () {
expect(result.version).to.equal(1);
expect(result?.payload).to.deep.equal(payload);
expect(result.signature).to.not.be.undefined;
expect(result.verifySignature(sigPubKey)).to.be.true;
expect(result.signaturePublicKey).to.deep.eq(sigPubKey);
}
)
Expand Down

0 comments on commit 8ad470f

Please sign in to comment.