-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests to identity keys
- Loading branch information
Showing
1 changed file
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |