Skip to content

Commit

Permalink
feat: sign with EKC
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Oct 6, 2021
1 parent 4db2bb7 commit ea2c3b3
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 15 deletions.
14 changes: 14 additions & 0 deletions docs/api/enums/errors_ErrorMessages.ERROR_MESSAGES.md
Expand Up @@ -12,10 +12,12 @@
- [CLAIMS\_NOT\_INITIALIZED](errors_ErrorMessages.ERROR_MESSAGES.md#claims_not_initialized)
- [CLAIM\_PUBLISHER\_NOT\_REQUESTER](errors_ErrorMessages.ERROR_MESSAGES.md#claim_publisher_not_requester)
- [DID\_DOCUMENT\_NOT\_INITIALIZED](errors_ErrorMessages.ERROR_MESSAGES.md#did_document_not_initialized)
- [EKC\_PROXY\_NOT\_PROVIDED](errors_ErrorMessages.ERROR_MESSAGES.md#ekc_proxy_not_provided)
- [ENS\_REGISTRY\_CONTRACT\_NOT\_INITIALIZED](errors_ErrorMessages.ERROR_MESSAGES.md#ens_registry_contract_not_initialized)
- [ENS\_REGISTRY\_NOT\_INITIALIZED](errors_ErrorMessages.ERROR_MESSAGES.md#ens_registry_not_initialized)
- [ENS\_RESOLVER\_NOT\_INITIALIZED](errors_ErrorMessages.ERROR_MESSAGES.md#ens_resolver_not_initialized)
- [ENS\_TYPE\_NOT\_SUPPORTED](errors_ErrorMessages.ERROR_MESSAGES.md#ens_type_not_supported)
- [ERROR\_IN\_AZURE\_PROVIDER](errors_ErrorMessages.ERROR_MESSAGES.md#error_in_azure_provider)
- [INSUFFICIENT\_BALANCE](errors_ErrorMessages.ERROR_MESSAGES.md#insufficient_balance)
- [JWT\_NOT\_INITIALIZED](errors_ErrorMessages.ERROR_MESSAGES.md#jwt_not_initialized)
- [METAMASK\_EXTENSION\_NOT\_AVAILABLE](errors_ErrorMessages.ERROR_MESSAGES.md#metamask_extension_not_available)
Expand Down Expand Up @@ -78,6 +80,12 @@ ___

___

### EKC\_PROXY\_NOT\_PROVIDED

**EKC\_PROXY\_NOT\_PROVIDED** = `"EKC proxy url is not provided"`

___

### ENS\_REGISTRY\_CONTRACT\_NOT\_INITIALIZED

**ENS\_REGISTRY\_CONTRACT\_NOT\_INITIALIZED** = `"ENS Registry contract not initialized"`
Expand All @@ -102,6 +110,12 @@ ___

___

### ERROR\_IN\_AZURE\_PROVIDER

**ERROR\_IN\_AZURE\_PROVIDER** = `"Error in Azure Provider"`

___

### INSUFFICIENT\_BALANCE

**INSUFFICIENT\_BALANCE** = `"Signer has insufficient balance"`
Expand Down
7 changes: 7 additions & 0 deletions docs/api/enums/types_WalletProvider.WalletProvider.md
Expand Up @@ -6,13 +6,20 @@

### Enumeration members

- [EKC](types_WalletProvider.WalletProvider.md#ekc)
- [EwKeyManager](types_WalletProvider.WalletProvider.md#ewkeymanager)
- [MetaMask](types_WalletProvider.WalletProvider.md#metamask)
- [PrivateKey](types_WalletProvider.WalletProvider.md#privatekey)
- [WalletConnect](types_WalletProvider.WalletProvider.md#walletconnect)

## Enumeration members

### EKC

**EKC** = `"EKC"`

___

### EwKeyManager

**EwKeyManager** = `"EwKeyManager"`
Expand Down
1 change: 1 addition & 0 deletions docs/api/modules/iam_iam_base.md
Expand Up @@ -27,6 +27,7 @@
| `infuraId?` | `string` | - |
| `ipfsUrl?` | `string` | - |
| `privateKey?` | `string` | - |
| `proxyUrl?` | `string` | - |
| `rpcUrl?` | `string` | only required in node env |

___
Expand Down
132 changes: 128 additions & 4 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -57,6 +57,7 @@
},
"dependencies": {
"@babel/runtime": "^7.12.5",
"@energyweb/ekc": "^0.6.0",
"@energyweb/iam-contracts": "^3.0.0",
"@ensdomains/ens": "^0.4.5",
"@ew-did-registry/claims": "0.5.2-alpha.101.0",
Expand Down
2 changes: 2 additions & 0 deletions src/errors/ErrorMessages.ts
Expand Up @@ -32,4 +32,6 @@ export enum ERROR_MESSAGES {
WITHDRAWAL_WAS_NOT_REQUESTED = "Stake withdrawal was not requested",
STAKE_WAS_NOT_PUT = "Stake was not put",
INSUFFICIENT_BALANCE = "Signer has insufficient balance",
ERROR_IN_AZURE_PROVIDER = "Error in Azure Provider",
EKC_PROXY_NOT_PROVIDED = "EKC proxy url is not provided",
}
19 changes: 17 additions & 2 deletions src/iam/iam-base.ts
@@ -1,4 +1,5 @@
import { providers, Signer, utils, Wallet, BigNumber, ethers } from "ethers";
import EKC from "@energyweb/ekc";
import {
DomainReader,
DomainTransactionFactory,
Expand Down Expand Up @@ -52,6 +53,7 @@ export type ConnectionOptions = {
bridgeUrl?: string;
privateKey?: string;
ewKeyManagerUrl?: string;
proxyUrl?: string;
};

export type EncodedCall = {
Expand Down Expand Up @@ -183,7 +185,7 @@ export class IAMBase {
initializeMetamask?: boolean;
walletProvider?: WalletProvider;
}): Promise<AccountInfo | undefined> {
const { privateKey, rpcUrl } = this._connectionOptions;
const { privateKey, rpcUrl, proxyUrl } = this._connectionOptions;

if (walletProvider === WalletProvider.PrivateKey) {
this.initWithPrivateKey(privateKey, rpcUrl);
Expand Down Expand Up @@ -244,6 +246,20 @@ export class IAMBase {
this._providerType = walletProvider;
return;
}
if (walletProvider === WalletProvider.EKC) {
if (!proxyUrl) {
throw new Error(ERROR_MESSAGES.EKC_PROXY_NOT_PROVIDED);
}
try {
// proxyURL should be
await EKC.init({ proxyUrl });
// await EKC.login({ mode: "popup" });
} catch (error) {
throw new Error(ERROR_MESSAGES.ERROR_IN_AZURE_PROVIDER);
}
// this._signer = EKC.getSigner() as Signer;
// this._provider = this._signer.provider;
}
throw new Error(ERROR_MESSAGES.WALLET_TYPE_NOT_PROVIDED);
}

Expand Down Expand Up @@ -377,7 +393,6 @@ export class IAMBase {
* @description Closes the connection between application and the signer's wallet
*/
async closeConnection() {
await this._walletConnectService.closeConnection();
this.clearSession();
this._did = undefined;
this._address = undefined;
Expand Down
1 change: 1 addition & 0 deletions src/types/WalletProvider.ts
Expand Up @@ -3,4 +3,5 @@ export enum WalletProvider {
MetaMask = "MetaMask",
EwKeyManager = "EwKeyManager",
PrivateKey = "PrivateKey",
EKC = "EKC",
}
4 changes: 2 additions & 2 deletions test/iam.test.ts
Expand Up @@ -18,12 +18,12 @@ import {
import { labelhash } from "../src/utils/ENS_hash";
import { orgTests } from "./organization.testSuite";
import { appsTests } from "./application.testSuite";
import { initializeConnectionTests } from "./initializeConnection.testSuite";
import { claimsTests } from "./claimsTests/claims.testSuite";
import { setCacheClientOptions, setChainConfig } from "../src/iam/chainConfig";
import { utilsTests } from "./utils/utils.testSuite";
import { assetsTests } from "./assets.testsuite";
import { stakingTests } from "./staking";
import { signerTests } from "./signer/signerTests";

const { namehash } = utils;

Expand Down Expand Up @@ -107,7 +107,7 @@ describe("IAM tests", () => {

describe("Organization tests", orgTests);
describe("Application tests", appsTests);
describe("InitializeConnection tests", initializeConnectionTests);
describe("InitializeConnection tests", signerTests);
describe("Claim tests", claimsTests);
describe("Utils tests", utilsTests);
describe("Assets tests", assetsTests);
Expand Down
19 changes: 19 additions & 0 deletions test/signer/EKCSignerTests.ts
@@ -0,0 +1,19 @@
import { IAM } from "../../src/iam";
import { rootOwner } from "../iam.test";
import { rpcUrl } from "../setup_contracts";

export function ekcSignerTests() {
test("init IAM with connected DID registry", async () => {
const iam = new IAM({
privateKey: rootOwner.privateKey,
rpcUrl,
});

await iam.initializeConnection({
initCacheServer: false,
createDocument: true,
reinitializeMetamask: false,
proxyUrl: "http://localhost:5000/api/v1",
});
});
}
@@ -1,13 +1,13 @@
import { IAM } from "../src/iam";
import { ERROR_MESSAGES } from "../src/errors";
import { rpcUrl } from "./setup_contracts";
import { WalletProvider } from "../src/types/WalletProvider";
import { IAM } from "../../src/iam";
import { ERROR_MESSAGES } from "../../src/errors";
import { rpcUrl } from "../setup_contracts";
import { WalletProvider } from "../../src/types/WalletProvider";

const iam_withoutKey = new IAM({ rpcUrl });

export const initializeConnectionTests = () => {
test("initializeConnection requires privateKey", async () => {
await expect(iam_withoutKey.initializeConnection()).rejects.toThrow(ERROR_MESSAGES.PRIVATE_KEY_NOT_PROVIDED);
export const noSignerTests = () => {
test("initializeConnection requires privateKey or walletProvider enum", async () => {
await expect(iam_withoutKey.initializeConnection()).rejects.toThrow(ERROR_MESSAGES.WALLET_TYPE_NOT_PROVIDED);
});

test("initializeConnection requires walletProvider to be known value", async () => {
Expand Down
7 changes: 7 additions & 0 deletions test/signer/signerTests.ts
@@ -0,0 +1,7 @@
import { ekcSignerTests } from "./EKCSignerTests";
import { noSignerTests } from "./noSignerTests";

export function signerTests() {
describe("No signer tests", noSignerTests);
describe("EKC signer tests", ekcSignerTests);
}

0 comments on commit ea2c3b3

Please sign in to comment.