Skip to content

Commit

Permalink
Refactor provider factories to support custom options
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-d committed Sep 15, 2023
1 parent e57a63d commit e5ae554
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 930 deletions.
695 changes: 21 additions & 674 deletions LICENSE

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ Event listeners can be set for login/logout events.
```typescript
import {WalletConnectProviderFactory} from "@elrond-giants/erdjs-auth";
const provider = new WalletConnectProviderFactory("devnet").createProvider();
const provider = new WalletConnectProviderFactory({
chainId: "D",
projectId: "yourProjectId"
}).createProvider();
provider.on(
"login",
Expand Down
356 changes: 146 additions & 210 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elrond-giants/erdjs-auth",
"version": "0.8.0",
"version": "0.9.0",
"description": "Easy to use MultiversX typescript Auth Providers, with a common interface.",
"main": "dist/index.js",
"types": "dist/index.d.js",
Expand All @@ -14,14 +14,14 @@
"MultiversX"
],
"author": "ElrondGiants",
"license": "GPL-3.0-or-later",
"license": "MIT",
"dependencies": {
"@multiversx/sdk-core": "^12.6.0",
"@multiversx/sdk-core": "^12.8.0",
"@multiversx/sdk-extension-provider": "^3.0.0",
"@multiversx/sdk-hw-provider": "^3.0.3",
"@multiversx/sdk-hw-provider": "^6.4.0",
"@multiversx/sdk-wallet": "^4.2.0",
"@multiversx/sdk-wallet-connect-provider": "^4.0.2",
"@multiversx/sdk-web-wallet-provider": "^3.0.0",
"@multiversx/sdk-web-wallet-provider": "^3.1.0",
"tslib": "^2.4.0"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions src/AuthProviders/LedgerProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { HWProvider } from '@multiversx/sdk-hw-provider';

import {
AuthProviderType, EventHandler,
EventType,
Expand Down Expand Up @@ -69,7 +68,7 @@ export class LedgerProvider implements IAuthProvider {
token,
});
this.address = address;
this.signature = signature.hex();
this.signature = signature.toString("hex");
} else {
this.address = await this.provider.login({ addressIndex: this.addressIndex });
}
Expand Down
5 changes: 1 addition & 4 deletions src/Factories/PemProviderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { IAuthProvider, IAuthProviderFactory } from '../types';
import EventsBus from "../EventBus";

export class PemProviderFactory implements IAuthProviderFactory {
private readonly walletPemKey: string;
constructor(walletPemKey: string) {
this.walletPemKey = walletPemKey;
}
constructor(private readonly walletPemKey: string) {}
createProvider(): IAuthProvider {
return new PemProvider(this.walletPemKey, new EventsBus());
}
Expand Down
34 changes: 19 additions & 15 deletions src/Factories/WalletConnectProviderFactory.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import {WalletConnectV2Provider} from "@multiversx/sdk-wallet-connect-provider";
import {WalletConnectProvider} from '../AuthProviders';
import {IAuthProvider, IAuthProviderFactory, INetworkConfig, NetworkEnv} from '../types';
import {network} from "../network";
import {
IAuthProvider,
IAuthProviderFactory,
WalletConnectProviderOptions
} from '../types';
import EventsBus from "../EventBus";
import {getNetworkOptions} from "../utils/network";

export class WalletConnectProviderFactory implements IAuthProviderFactory {
private networkOptions: INetworkConfig;
private readonly projectId: string;

constructor(env: NetworkEnv, projectId: string) {
this.networkOptions = network[env];
this.projectId = projectId;
export class WalletConnectProviderFactory implements IAuthProviderFactory {
protected options: Required<WalletConnectProviderOptions>;

constructor({chainId, projectId, relayAddress}: WalletConnectProviderOptions) {
if (!relayAddress) {
const networkOptions = getNetworkOptions(chainId);
relayAddress = networkOptions.relayAddress
}
this.options = {chainId, projectId, relayAddress};
}

createProvider(): IAuthProvider {
let walletConnectProvider: WalletConnectProvider;
const eventBus = new EventsBus();

const provider = new WalletConnectV2Provider(
Expand All @@ -24,14 +30,12 @@ export class WalletConnectProviderFactory implements IAuthProviderFactory {
onClientEvent: (event) => eventBus.emit(event.name, event.data),
},

this.networkOptions.chainId,
this.networkOptions.relayAddress,
this.projectId
this.options.chainId,
this.options.relayAddress,
this.options.projectId
);

return new WalletConnectProvider(provider, eventBus);

walletConnectProvider = new WalletConnectProvider(provider, eventBus);

return walletConnectProvider;
}
}
41 changes: 24 additions & 17 deletions src/Factories/WebProviderFactory.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { WalletProvider } from '@multiversx/sdk-web-wallet-provider/out';
import { WebProvider } from '../AuthProviders';
import {IAuthProvider, IAuthProviderFactory, IWebConnectionOptions, NetworkEnv} from '../types';
import {network} from "../network";
import {
IAuthProvider,
IAuthProviderFactory,
WebProviderOptions
} from '../types';
import EventsBus from "../EventBus";
import {getNetworkOptions} from "../utils/network";

export class WebProviderFactory implements IAuthProviderFactory {
private readonly networkAddress: string;
private connectionOptions: IWebConnectionOptions = {};
private address: string | null = null;

constructor(env: NetworkEnv) {
const networkOptions = network[env];
this.networkAddress = networkOptions.walletAddress;
}

setConnectionOptions(value: IWebConnectionOptions) {
this.connectionOptions = value;

return this;
export class WebProviderFactory implements IAuthProviderFactory {
private address: string | null = null;
protected options: Required<WebProviderOptions>;

constructor({chainId, walletAddress, networkOptions}: WebProviderOptions) {
if (!walletAddress) {
const options = getNetworkOptions(chainId);
walletAddress = options.walletAddress;
}
this.options = {chainId, walletAddress, networkOptions: networkOptions ?? {}};
}

setAddress(value: string | null) {
Expand All @@ -27,8 +29,13 @@ export class WebProviderFactory implements IAuthProviderFactory {
}

createProvider(): IAuthProvider {
const provider = new WalletProvider(this.networkAddress);

return new WebProvider(provider, this.connectionOptions, new EventsBus(), this.address);
const provider = new WalletProvider(this.options.walletAddress);

return new WebProvider(
provider,
this.options.networkOptions || {},
new EventsBus(),
this.address
);
}
}
2 changes: 1 addition & 1 deletion src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const network: {
},
"devnet": {
bridgeAddress: "https://bridge.walletconnect.org",
walletAddress: "https://devnet-wallet.multiversx.com/dapp/init",
walletAddress: "https://devnet2-wallet.multiversx.com/dapp/init",
chainId: "D",
relayAddress: "wss://relay.walletconnect.com"
},
Expand Down
22 changes: 21 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,24 @@ export type AuthToken = {
address: string;
body: string;
signature: string;
} & DecodedLoginTokenType;
} & DecodedLoginTokenType;

export type NetworkOptions = {
chainId: string;
walletAddress?: string;
bridgeAddress?: string;
relayAddress?: string;
webConnectionOptions?: IWebConnectionOptions;
};

export type WebProviderOptions = {
chainId: string;
walletAddress?: string,
networkOptions?: IWebConnectionOptions
};

export type WalletConnectProviderOptions = {
chainId: string;
projectId: string;
relayAddress?: string;
}
12 changes: 12 additions & 0 deletions src/utils/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {INetworkConfig} from "../types";
import {network} from "../network";

export const getNetworkOptions = (chainId: string): INetworkConfig => {
const networkOptions = Object.values(network)
.find((network) => network.chainId === chainId);
if (!networkOptions) {
throw new Error(`Unknown chainId: ${chainId}`);
}

return networkOptions;
}

0 comments on commit e5ae554

Please sign in to comment.