Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Update types, a provider validation method, and accept 2 more constru…
Browse files Browse the repository at this point in the history
…ctor arguments
  • Loading branch information
eggplantzzz committed Nov 4, 2021
1 parent 72b2760 commit fb18c0b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/hdwallet-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"ganache-core": "2.13.0",
"mocha": "8.1.2",
"ts-node": "^9.0.0",
"typescript": "^4.1.4"
"typescript": "^4.1.4",
"web3": "1.5.3"
},
"keywords": [
"etheruem",
Expand Down
3 changes: 2 additions & 1 deletion packages/hdwallet-provider/src/constructor/Constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
MnemonicPhrase,
PrivateKey,
Provider,
ProviderUrl,
ProviderOrUrl,
AddressIndex,
NumberOfAddresses,
Expand Down Expand Up @@ -41,7 +42,7 @@ export type InputSigningAuthority =
export interface CommonOptions {
providerOrUrl?: ProviderOrUrl;
provider?: Provider;
url?: string;
url?: ProviderUrl;
addressIndex?: AddressIndex;
numberOfAddresses?: NumberOfAddresses;
shareNonce?: ShareNonce;
Expand Down
9 changes: 8 additions & 1 deletion packages/hdwallet-provider/src/constructor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ export interface Mnemonic {
phrase: MnemonicPhrase;
password?: MnemonicPassword;
}
import type { Provider as LegacyProvider } from "web3/providers";
type Eip1193Provider = {
request: (options: {
method: string;
params?: unknown[] | object;
}) => Promise<any>;
};
export type PrivateKey = string;
export type Provider = any;
export type Provider = LegacyProvider | Eip1193Provider;
export type ProviderUrl = string;
export type ProviderOrUrl = Provider | ProviderUrl;
export type AddressIndex = number;
Expand Down
42 changes: 29 additions & 13 deletions packages/hdwallet-provider/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class HDWalletProvider {

constructor(...args: ConstructorArguments) {
const {
providerOrUrl, // required
provider,
url,
providerOrUrl,
addressIndex = 0,
numberOfAddresses = 10,
shareNonce = true,
Expand All @@ -78,11 +80,21 @@ class HDWalletProvider {
pollingInterval
});

if (!HDWalletProvider.isValidProvider(providerOrUrl)) {
let providerToUse;
if (HDWalletProvider.isValidProvider(provider)) {
providerToUse = provider;
} else if (HDWalletProvider.isValidProvider(url)) {
providerToUse = url;
} else {
providerToUse = providerOrUrl;
}

if (!HDWalletProvider.isValidProvider(providerToUse)) {
throw new Error(
[
`Malformed provider URL: '${providerOrUrl}'`,
"Please specify a correct URL, using the http, https, ws, or wss protocol.",
`No provider or an invalid provider was specified: '${providerToUse}'`,
"Please specify a valid provider or URL, using the http, https, " +
"ws, or wss protocol.",
""
].join("\n")
);
Expand Down Expand Up @@ -219,8 +231,8 @@ class HDWalletProvider {
: this.engine.addProvider(singletonNonceSubProvider);

this.engine.addProvider(new FiltersSubprovider());
if (typeof providerOrUrl === "string") {
const url = providerOrUrl;
if (typeof providerToUse === "string") {
const url = providerToUse;

const providerProtocol = (
Url.parse(url).protocol || "http:"
Expand All @@ -235,8 +247,7 @@ class HDWalletProvider {
this.engine.addProvider(new RpcProvider({ rpcUrl: url }));
}
} else {
const provider = providerOrUrl;
this.engine.addProvider(new ProviderSubprovider(provider));
this.engine.addProvider(new ProviderSubprovider(providerToUse));
}

// Required by the provider engine.
Expand Down Expand Up @@ -356,15 +367,20 @@ class HDWalletProvider {
return this.addresses;
}

public static isValidProvider(provider: string | any): boolean {
const validProtocols = ["http:", "https:", "ws:", "wss:"];

public static isValidProvider(provider: any): boolean {
if (!provider) return false;
if (typeof provider === "string") {
const validProtocols = ["http:", "https:", "ws:", "wss:"];
const url = Url.parse(provider.toLowerCase());
return !!(validProtocols.includes(url.protocol || "") && url.slashes);
} else if ("request" in provider) {
// provider is an 1193 provider
return true;
} else if ("send" in provider) {
// provider is a "legacy" provider
return true;
}

return true;
return false;
}
}

Expand Down

0 comments on commit fb18c0b

Please sign in to comment.