Skip to content

Commit

Permalink
test: add unit tests to identity keys
Browse files Browse the repository at this point in the history
  • Loading branch information
devceline committed Nov 23, 2023
1 parent d7dd65d commit 402861c
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions misc/identity-keys/test/identity-keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,86 @@
import "mocha";
import { expect } from "chai";
import { Core } from "@walletconnect/core";
import { Wallet } from "@ethersproject/wallet";
import { IdentityKeys, DEFAULT_KEYSERVER_URL } from "../src/";
import { encodeEd25519Key } from "@walletconnect/did-jwt";
import { ICore } from "@walletconnect/types";

const PROJECT_ID = process.env.TEST_PROJECT_ID;

describe("@walletconnect/identity-keys", () => {
it("needs tests", () => {
// needs tests
const statement = "Test statement";
const domain = "example.com";

let wallet: Wallet;
let accountId: string;
let onSign: (m: string) => Promise<string>;
let core: ICore;
let identityKeys: IdentityKeys;

beforeEach(async () => {
wallet = Wallet.createRandom();
accountId = `eip155:1:${wallet.address}`;
onSign = (m) => wallet.signMessage(m);

core = new Core({projectId: PROJECT_ID});

identityKeys = identityKeys = new IdentityKeys(core, DEFAULT_KEYSERVER_URL);

await core.start()
await identityKeys.init();
});

it("registers on keyserver", async () => {
const identity = await identityKeys.registerIdentity({
accountId,
statement,
onSign,
domain,
});

const encodedIdentity = encodeEd25519Key(identity).split(":")[2];

const fetchUrl = `${DEFAULT_KEYSERVER_URL}/identity?publicKey=${encodedIdentity}`;

const fetchedFromKeyServer = await fetch(fetchUrl);

expect(fetchedFromKeyServer.status).eq(200);
});

it("does not presist identity keys that failed to register", async () => {
// rejectedWith & rejected are not supported on this version of chai
let failMessage = "";
await identityKeys
.registerIdentity({
accountId,
statement,
onSign: () => Promise.resolve("badSignature"),
domain,
})
.catch((err) => (failMessage = err.message));

expect(failMessage).eq(`Failed to register on keyserver`);

const keys = identityKeys.identityKeys.getAll();
expect(keys.length).eq(0);
});

it("prevents registering with empty signatures", async () => {
// rejectedWith & rejected are not supported on this version of chai
let failMessage = "";
await identityKeys
.registerIdentity({
accountId,
statement,
onSign: () => Promise.resolve(""),
domain,
})
.catch((err) => (failMessage = err.message));

expect(failMessage).eq("Provided an empty signature");

const keys = identityKeys.identityKeys.getAll();
expect(keys.length).eq(0);
});
});

0 comments on commit 402861c

Please sign in to comment.