From ea2c3b3c001d1abf7e88d386f74ef329fa023c57 Mon Sep 17 00:00:00 2001 From: Dmitry Fesenko Date: Wed, 6 Oct 2021 10:25:39 +0300 Subject: [PATCH] feat: sign with EKC --- .../errors_ErrorMessages.ERROR_MESSAGES.md | 14 ++ .../types_WalletProvider.WalletProvider.md | 7 + docs/api/modules/iam_iam_base.md | 1 + package-lock.json | 132 +++++++++++++++++- package.json | 1 + src/errors/ErrorMessages.ts | 2 + src/iam/iam-base.ts | 19 ++- src/types/WalletProvider.ts | 1 + test/iam.test.ts | 4 +- test/signer/EKCSignerTests.ts | 19 +++ .../noSignerTests.ts} | 14 +- test/signer/signerTests.ts | 7 + 12 files changed, 206 insertions(+), 15 deletions(-) create mode 100644 test/signer/EKCSignerTests.ts rename test/{initializeConnection.testSuite.ts => signer/noSignerTests.ts} (58%) create mode 100644 test/signer/signerTests.ts diff --git a/docs/api/enums/errors_ErrorMessages.ERROR_MESSAGES.md b/docs/api/enums/errors_ErrorMessages.ERROR_MESSAGES.md index 85047f76..8b0a4fe2 100644 --- a/docs/api/enums/errors_ErrorMessages.ERROR_MESSAGES.md +++ b/docs/api/enums/errors_ErrorMessages.ERROR_MESSAGES.md @@ -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) @@ -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"` @@ -102,6 +110,12 @@ ___ ___ +### ERROR\_IN\_AZURE\_PROVIDER + +• **ERROR\_IN\_AZURE\_PROVIDER** = `"Error in Azure Provider"` + +___ + ### INSUFFICIENT\_BALANCE • **INSUFFICIENT\_BALANCE** = `"Signer has insufficient balance"` diff --git a/docs/api/enums/types_WalletProvider.WalletProvider.md b/docs/api/enums/types_WalletProvider.WalletProvider.md index ed3f669c..868d9651 100644 --- a/docs/api/enums/types_WalletProvider.WalletProvider.md +++ b/docs/api/enums/types_WalletProvider.WalletProvider.md @@ -6,6 +6,7 @@ ### 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) @@ -13,6 +14,12 @@ ## Enumeration members +### EKC + +• **EKC** = `"EKC"` + +___ + ### EwKeyManager • **EwKeyManager** = `"EwKeyManager"` diff --git a/docs/api/modules/iam_iam_base.md b/docs/api/modules/iam_iam_base.md index 800ff672..5168c28e 100644 --- a/docs/api/modules/iam_iam_base.md +++ b/docs/api/modules/iam_iam_base.md @@ -27,6 +27,7 @@ | `infuraId?` | `string` | - | | `ipfsUrl?` | `string` | - | | `privateKey?` | `string` | - | +| `proxyUrl?` | `string` | - | | `rpcUrl?` | `string` | only required in node env | ___ diff --git a/package-lock.json b/package-lock.json index 3bd401db..221da61b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,22 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@azure/msal-browser": { + "version": "2.17.0", + "resolved": "https://registry.npmjs.org/@azure/msal-browser/-/msal-browser-2.17.0.tgz", + "integrity": "sha512-NDK0NfsiRkjUU4V4jTt++aUPVg3JnRF4zV3B6WEOXDMH3OCbSJyqdO1WhdUWgMNQZz6Dk9bO/c6Bf4vUEgg+WA==", + "requires": { + "@azure/msal-common": "^5.0.0" + } + }, + "@azure/msal-common": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/@azure/msal-common/-/msal-common-5.0.1.tgz", + "integrity": "sha512-CmPR3XM9+CGUu7V/+bAwDxyN6XqWJJhVLmv7utT3sbgay4l5roVXsD1t4wURTs8PwzxmmnJOrhvvGhoDxUW69g==", + "requires": { + "debug": "^4.1.1" + } + }, "@babel/code-frame": { "version": "7.14.5", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.14.5.tgz", @@ -1112,6 +1128,114 @@ "minimist": "^1.2.0" } }, + "@energyweb/ekc": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/@energyweb/ekc/-/ekc-0.6.0.tgz", + "integrity": "sha512-Vrrk/6HMe4nfUvtwov+5+rBpQA0RGXwzkqASTN/mPubm4QwbguAf13XQLWKmDKzFu+kVoN6B2uVfE5JotxUelg==", + "requires": { + "@azure/msal-browser": "~2.17.0", + "@ethersproject/providers": "~5.4.5", + "ethers": "~5.4.7" + }, + "dependencies": { + "@ethersproject/abi": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/@ethersproject/abi/-/abi-5.4.1.tgz", + "integrity": "sha512-9mhbjUk76BiSluiiW4BaYyI58KSbDMMQpCLdsAR+RsT2GyATiNYxVv+pGWRrekmsIdY3I+hOqsYQSTkc8L/mcg==", + "requires": { + "@ethersproject/address": "^5.4.0", + "@ethersproject/bignumber": "^5.4.0", + "@ethersproject/bytes": "^5.4.0", + "@ethersproject/constants": "^5.4.0", + "@ethersproject/hash": "^5.4.0", + "@ethersproject/keccak256": "^5.4.0", + "@ethersproject/logger": "^5.4.0", + "@ethersproject/properties": "^5.4.0", + "@ethersproject/strings": "^5.4.0" + } + }, + "@ethersproject/providers": { + "version": "5.4.5", + "resolved": "https://registry.npmjs.org/@ethersproject/providers/-/providers-5.4.5.tgz", + "integrity": "sha512-1GkrvkiAw3Fj28cwi1Sqm8ED1RtERtpdXmRfwIBGmqBSN5MoeRUHuwHPppMtbPayPgpFcvD7/Gdc9doO5fGYgw==", + "requires": { + "@ethersproject/abstract-provider": "^5.4.0", + "@ethersproject/abstract-signer": "^5.4.0", + "@ethersproject/address": "^5.4.0", + "@ethersproject/basex": "^5.4.0", + "@ethersproject/bignumber": "^5.4.0", + "@ethersproject/bytes": "^5.4.0", + "@ethersproject/constants": "^5.4.0", + "@ethersproject/hash": "^5.4.0", + "@ethersproject/logger": "^5.4.0", + "@ethersproject/networks": "^5.4.0", + "@ethersproject/properties": "^5.4.0", + "@ethersproject/random": "^5.4.0", + "@ethersproject/rlp": "^5.4.0", + "@ethersproject/sha2": "^5.4.0", + "@ethersproject/strings": "^5.4.0", + "@ethersproject/transactions": "^5.4.0", + "@ethersproject/web": "^5.4.0", + "bech32": "1.1.4", + "ws": "7.4.6" + } + }, + "ethers": { + "version": "5.4.7", + "resolved": "https://registry.npmjs.org/ethers/-/ethers-5.4.7.tgz", + "integrity": "sha512-iZc5p2nqfWK1sj8RabwsPM28cr37Bpq7ehTQ5rWExBr2Y09Sn1lDKZOED26n+TsZMye7Y6mIgQ/1cwpSD8XZew==", + "requires": { + "@ethersproject/abi": "5.4.1", + "@ethersproject/abstract-provider": "5.4.1", + "@ethersproject/abstract-signer": "5.4.1", + "@ethersproject/address": "5.4.0", + "@ethersproject/base64": "5.4.0", + "@ethersproject/basex": "5.4.0", + "@ethersproject/bignumber": "5.4.2", + "@ethersproject/bytes": "5.4.0", + "@ethersproject/constants": "5.4.0", + "@ethersproject/contracts": "5.4.1", + "@ethersproject/hash": "5.4.0", + "@ethersproject/hdnode": "5.4.0", + "@ethersproject/json-wallets": "5.4.0", + "@ethersproject/keccak256": "5.4.0", + "@ethersproject/logger": "5.4.1", + "@ethersproject/networks": "5.4.2", + "@ethersproject/pbkdf2": "5.4.0", + "@ethersproject/properties": "5.4.1", + "@ethersproject/providers": "5.4.5", + "@ethersproject/random": "5.4.0", + "@ethersproject/rlp": "5.4.0", + "@ethersproject/sha2": "5.4.0", + "@ethersproject/signing-key": "5.4.0", + "@ethersproject/solidity": "5.4.0", + "@ethersproject/strings": "5.4.0", + "@ethersproject/transactions": "5.4.0", + "@ethersproject/units": "5.4.0", + "@ethersproject/wallet": "5.4.0", + "@ethersproject/web": "5.4.0", + "@ethersproject/wordlists": "5.4.0" + }, + "dependencies": { + "@ethersproject/bignumber": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/@ethersproject/bignumber/-/bignumber-5.4.2.tgz", + "integrity": "sha512-oIBDhsKy5bs7j36JlaTzFgNPaZjiNDOXsdSgSpXRucUl+UA6L/1YLlFeI3cPAoodcenzF4nxNPV13pcy7XbWjA==", + "requires": { + "@ethersproject/bytes": "^5.4.0", + "@ethersproject/logger": "^5.4.0", + "bn.js": "^4.11.9" + } + } + } + }, + "ws": { + "version": "7.4.6", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.6.tgz", + "integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==" + } + } + }, "@energyweb/iam-contracts": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@energyweb/iam-contracts/-/iam-contracts-3.0.0.tgz", @@ -8243,7 +8367,7 @@ } }, "ethereumjs-abi": { - "version": "git+https://github.com/ethereumjs/ethereumjs-abi.git#1a27c59c15ab1e95ee8e5c4ed6ad814c49cc439e", + "version": "git+https://github.com/ethereumjs/ethereumjs-abi.git#ee3994657fa7a427238e6ba92a84d0b529bbcde0", "from": "git+https://github.com/ethereumjs/ethereumjs-abi.git", "requires": { "bn.js": "^4.11.8", @@ -16278,7 +16402,7 @@ }, "abbrev": { "version": "1.1.1", - "resolved": "", + "resolved": false, "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==", "dev": true }, @@ -16341,7 +16465,7 @@ }, "ansicolors": { "version": "0.3.2", - "resolved": "", + "resolved": false, "integrity": "sha1-ZlWX3oap/+Oqm/vmyuXG6kJrSXk=", "dev": true }, @@ -18179,7 +18303,7 @@ }, "text-table": { "version": "0.2.0", - "resolved": "", + "resolved": false, "integrity": "sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=", "dev": true }, diff --git a/package.json b/package.json index f954c746..88ee9b52 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/errors/ErrorMessages.ts b/src/errors/ErrorMessages.ts index 20e0b7a5..83bec731 100644 --- a/src/errors/ErrorMessages.ts +++ b/src/errors/ErrorMessages.ts @@ -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", } diff --git a/src/iam/iam-base.ts b/src/iam/iam-base.ts index c29b9e32..863185a9 100644 --- a/src/iam/iam-base.ts +++ b/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, @@ -52,6 +53,7 @@ export type ConnectionOptions = { bridgeUrl?: string; privateKey?: string; ewKeyManagerUrl?: string; + proxyUrl?: string; }; export type EncodedCall = { @@ -183,7 +185,7 @@ export class IAMBase { initializeMetamask?: boolean; walletProvider?: WalletProvider; }): Promise { - const { privateKey, rpcUrl } = this._connectionOptions; + const { privateKey, rpcUrl, proxyUrl } = this._connectionOptions; if (walletProvider === WalletProvider.PrivateKey) { this.initWithPrivateKey(privateKey, rpcUrl); @@ -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); } @@ -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; diff --git a/src/types/WalletProvider.ts b/src/types/WalletProvider.ts index 09383a8a..9bd258c5 100644 --- a/src/types/WalletProvider.ts +++ b/src/types/WalletProvider.ts @@ -3,4 +3,5 @@ export enum WalletProvider { MetaMask = "MetaMask", EwKeyManager = "EwKeyManager", PrivateKey = "PrivateKey", + EKC = "EKC", } diff --git a/test/iam.test.ts b/test/iam.test.ts index 75997f79..bbd9ff81 100644 --- a/test/iam.test.ts +++ b/test/iam.test.ts @@ -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; @@ -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); diff --git a/test/signer/EKCSignerTests.ts b/test/signer/EKCSignerTests.ts new file mode 100644 index 00000000..3e955e2f --- /dev/null +++ b/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", + }); + }); +} diff --git a/test/initializeConnection.testSuite.ts b/test/signer/noSignerTests.ts similarity index 58% rename from test/initializeConnection.testSuite.ts rename to test/signer/noSignerTests.ts index 8713d97f..7c014d50 100644 --- a/test/initializeConnection.testSuite.ts +++ b/test/signer/noSignerTests.ts @@ -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 () => { diff --git a/test/signer/signerTests.ts b/test/signer/signerTests.ts new file mode 100644 index 00000000..d283726b --- /dev/null +++ b/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); +}