-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* breaking changes companion * rename the rest * fix lint errors * fix * change API * lint error fixes * fix metadata & rpc * everything working again * lint error fixes * switch to rococo * fix tests
- Loading branch information
Showing
39 changed files
with
990 additions
and
828 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
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
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
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,86 +1,86 @@ | ||
import IdentityKey from "../src/utils/identityKey"; | ||
|
||
describe("IdentityKey",() => { | ||
describe("IdentityKey", () => { | ||
test("Creating a new cipher works", () => { | ||
// Identity key would be stored in `window.localStorage`, but in the tests it | ||
// will simply be stored locally. | ||
let identityKey = ""; | ||
|
||
const polkadotNetworkId = 0; | ||
identityKey = IdentityKey.newCipher(identityKey, polkadotNetworkId); | ||
const polkadotChainId = 0; | ||
identityKey = IdentityKey.newCipher(identityKey, polkadotChainId); | ||
|
||
containsNetworkAndCipher(identityKey, polkadotNetworkId); | ||
containsChainAndCipher(identityKey, polkadotChainId); | ||
|
||
const moonbeamNetworkId = 1; | ||
// Generate a new cipher for the Moonbeam network. | ||
identityKey = IdentityKey.newCipher(identityKey, moonbeamNetworkId); | ||
const moonbeamChainId = 1; | ||
// Generate a new cipher for the Moonbeam chain. | ||
identityKey = IdentityKey.newCipher(identityKey, moonbeamChainId); | ||
|
||
// The identity Key should still have the Polkadot cipher. | ||
containsNetworkAndCipher(identityKey, polkadotNetworkId); | ||
containsChainAndCipher(identityKey, polkadotChainId); | ||
|
||
containsNetworkAndCipher(identityKey, moonbeamNetworkId); | ||
containsChainAndCipher(identityKey, moonbeamChainId); | ||
|
||
// Ciphers are randomly generated so the two ciphers cannot be the same. | ||
const polkadotCipher = IdentityKey.getNetworkCipher(identityKey, polkadotNetworkId); | ||
const moonbeamCipher = IdentityKey.getNetworkCipher(identityKey, moonbeamNetworkId); | ||
const polkadotCipher = IdentityKey.getChainCipher(identityKey, polkadotChainId); | ||
const moonbeamCipher = IdentityKey.getChainCipher(identityKey, moonbeamChainId); | ||
|
||
expect(polkadotCipher).not.toBe(moonbeamCipher); | ||
|
||
// Cannot create a new Cipher for the same network twice. | ||
expect(() => IdentityKey.newCipher(identityKey, moonbeamNetworkId)) | ||
.toThrow("There already exists a cipher that is attached to the provided networkId"); | ||
// Cannot create a new Cipher for the same chain twice. | ||
expect(() => IdentityKey.newCipher(identityKey, moonbeamChainId)) | ||
.toThrow("There already exists a cipher that is attached to the provided chainId"); | ||
}); | ||
|
||
test("Updating cipher works", () => { | ||
let identityKey = ""; | ||
|
||
const polkadotNetworkId = 0; | ||
const moonbeamNetworkId = 1; | ||
const polkadotChainId = 0; | ||
const moonbeamChainId = 1; | ||
|
||
identityKey = IdentityKey.newCipher(identityKey, polkadotNetworkId); | ||
identityKey = IdentityKey.newCipher(identityKey, moonbeamNetworkId); | ||
identityKey = IdentityKey.newCipher(identityKey, polkadotChainId); | ||
identityKey = IdentityKey.newCipher(identityKey, moonbeamChainId); | ||
|
||
containsNetworkAndCipher(identityKey, polkadotNetworkId); | ||
containsNetworkAndCipher(identityKey, moonbeamNetworkId); | ||
containsChainAndCipher(identityKey, polkadotChainId); | ||
containsChainAndCipher(identityKey, moonbeamChainId); | ||
|
||
const polkadotCipher = IdentityKey.getNetworkCipher(identityKey, polkadotNetworkId); | ||
const moonbeamCipher = IdentityKey.getNetworkCipher(identityKey, moonbeamNetworkId); | ||
const polkadotCipher = IdentityKey.getChainCipher(identityKey, polkadotChainId); | ||
const moonbeamCipher = IdentityKey.getChainCipher(identityKey, moonbeamChainId); | ||
|
||
identityKey = IdentityKey.updateCipher(identityKey, moonbeamNetworkId); | ||
const newMoonbeamCipher = IdentityKey.getNetworkCipher(identityKey, moonbeamNetworkId); | ||
identityKey = IdentityKey.updateCipher(identityKey, moonbeamChainId); | ||
const newMoonbeamCipher = IdentityKey.getChainCipher(identityKey, moonbeamChainId); | ||
|
||
// The moonbeam network cipher should be updated. | ||
// The moonbeam chain cipher should be updated. | ||
expect(moonbeamCipher).not.toBe(newMoonbeamCipher); | ||
|
||
// The polkadot cipher shouldn't be affected. | ||
expect(IdentityKey.getNetworkCipher(identityKey, polkadotNetworkId)).toBe(polkadotCipher); | ||
expect(IdentityKey.getChainCipher(identityKey, polkadotChainId)).toBe(polkadotCipher); | ||
|
||
// Cannot update a cipher of a network that does not exist. | ||
expect(() => IdentityKey.updateCipher(identityKey, 42)).toThrow("Cannot find networkId"); | ||
// Cannot update a cipher of a chain that does not exist. | ||
expect(() => IdentityKey.updateCipher(identityKey, 42)).toThrow("Cannot find chainId"); | ||
}); | ||
|
||
test("Encryption and decryption works", () => { | ||
let identityKey = ""; | ||
|
||
const polkadotNetworkId = 0; | ||
identityKey = IdentityKey.newCipher(identityKey, polkadotNetworkId); | ||
const polkadotChainId = 0; | ||
identityKey = IdentityKey.newCipher(identityKey, polkadotChainId); | ||
|
||
containsNetworkAndCipher(identityKey, polkadotNetworkId); | ||
containsChainAndCipher(identityKey, polkadotChainId); | ||
|
||
const polkadotAddress = "126X27SbhrV19mBFawys3ovkyBS87SGfYwtwa8J2FjHrtbmA"; | ||
const encryptedAddress = IdentityKey.encryptAddress(identityKey, polkadotNetworkId, polkadotAddress); | ||
const decryptedAddress = IdentityKey.decryptAddress(identityKey, polkadotNetworkId, encryptedAddress); | ||
const encryptedAddress = IdentityKey.encryptAddress(identityKey, polkadotChainId, polkadotAddress); | ||
const decryptedAddress = IdentityKey.decryptAddress(identityKey, polkadotChainId, encryptedAddress); | ||
|
||
expect(polkadotAddress).toBe(decryptedAddress); | ||
}); | ||
}); | ||
|
||
const containsNetworkAndCipher = (identityKey: string, networkId: number) => { | ||
const containsNetwork = new RegExp(`\\b${networkId}:`, "g"); | ||
expect(containsNetwork.test(identityKey)).toBe(true); | ||
const containsChainAndCipher = (identityKey: string, chainId: number) => { | ||
const containsChain = new RegExp(`\\b${chainId}:`, "g"); | ||
expect(containsChain.test(identityKey)).toBe(true); | ||
|
||
const networkCipher = IdentityKey.getNetworkCipher(identityKey, networkId); | ||
expect(cipherSize(networkCipher)).toBe(16); | ||
const chainCipher = IdentityKey.getChainCipher(identityKey, chainId); | ||
expect(cipherSize(chainCipher)).toBe(16); | ||
} | ||
|
||
const cipherSize = (cipher: string) => Buffer.from(cipher, "base64").length; |
Oops, something went wrong.