@requestnetwork/utils 0.35.1-next.1686 → 0.35.1-next.1691

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -16,6 +16,9 @@ declare const _default: {
16
16
  duplicates: T_4[];
17
17
  };
18
18
  notNull: <T_5>(x: T_5 | null | undefined) => x is T_5;
19
+ setProviderFactory: (providerFactory?: ((network: string | undefined, defaultFactory: (network: string | undefined) => string | import("@ethersproject/abstract-provider").Provider) => string | import("@ethersproject/abstract-provider").Provider) | undefined) => void;
20
+ initPaymentDetectionApiKeys: (defaultProviderOptions?: Record<string, string | (() => string)> | undefined) => void;
21
+ getDefaultProvider: (network?: string | undefined) => import("@ethersproject/abstract-provider").Provider;
19
22
  SimpleLogger: typeof SimpleLogger;
20
23
  amount: {
21
24
  add: (amount: import("types/dist/request-logic-types").Amount, delta: import("types/dist/request-logic-types").Amount) => string;
@@ -42,6 +45,9 @@ declare const _default: {
42
45
  generate32BufferKey: () => Promise<string>;
43
46
  generate8randomBytes: () => Promise<string>;
44
47
  keccak256Hash: (data: string) => string;
48
+ /**
49
+ * Collection of general purpose utility function
50
+ */
45
51
  last20bytesOfNormalizedKeccak256Hash: (data: unknown) => string;
46
52
  normalize: (data: unknown) => string;
47
53
  normalizeKeccak256Hash: (data: unknown) => import("types/dist/multi-format/hash-types").IHash;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAOA,OAAO,YAAY,MAAM,iBAAiB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3C;;GAEG;AACH,wBAUE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAOA,OAAO,YAAY,MAAM,iBAAiB,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAG3C;;WAEG;;;;;;;;;;;;;;;;;;;;;;;;;;;AAFH;;GAEG;AACH,wBAWE"}
package/dist/index.js CHANGED
@@ -9,15 +9,16 @@ const retry_1 = require("./retry");
9
9
  const signature_1 = require("./signature");
10
10
  const simple_logger_1 = require("./simple-logger");
11
11
  const utils_1 = require("./utils");
12
+ const providers_1 = require("./providers");
12
13
  /**
13
14
  * Collection of general purpose utility function
14
15
  */
15
- exports.default = Object.assign({ SimpleLogger: simple_logger_1.default,
16
+ exports.default = Object.assign(Object.assign({ SimpleLogger: simple_logger_1.default,
16
17
  amount: amount_1.default,
17
18
  cachedThrottle: cached_throttle_1.default,
18
19
  crypto: crypto_1.default,
19
20
  encryption: encryption_1.default,
20
21
  identity: identity_1.default,
21
22
  retry: retry_1.default,
22
- signature: signature_1.default }, utils_1.default);
23
+ signature: signature_1.default }, providers_1.default), utils_1.default);
23
24
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,qCAA8B;AAC9B,uDAA+C;AAC/C,qCAA8B;AAC9B,6CAAsC;AACtC,yCAAkC;AAClC,mCAA4B;AAC5B,2CAAoC;AACpC,mDAA2C;AAC3C,mCAA4B;AAC5B;;GAEG;AACH,kCACE,YAAY,EAAZ,uBAAY;IACZ,MAAM,EAAN,gBAAM;IACN,cAAc,EAAd,yBAAc;IACd,MAAM,EAAN,gBAAM;IACN,UAAU,EAAV,oBAAU;IACV,QAAQ,EAAR,kBAAQ;IACR,KAAK,EAAL,eAAK;IACL,SAAS,EAAT,mBAAS,IACN,eAAK,EACR"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,qCAA8B;AAC9B,uDAA+C;AAC/C,qCAA8B;AAC9B,6CAAsC;AACtC,yCAAkC;AAClC,mCAA4B;AAC5B,2CAAoC;AACpC,mDAA2C;AAC3C,mCAA4B;AAC5B,2CAAoC;AACpC;;GAEG;AACH,gDACE,YAAY,EAAZ,uBAAY;IACZ,MAAM,EAAN,gBAAM;IACN,cAAc,EAAd,yBAAc;IACd,MAAM,EAAN,gBAAM;IACN,UAAU,EAAV,oBAAU;IACV,QAAQ,EAAR,kBAAQ;IACR,KAAK,EAAL,eAAK;IACL,SAAS,EAAT,mBAAS,IACN,mBAAS,GACT,eAAK,EACR"}
@@ -0,0 +1,14 @@
1
+ import { providers } from 'ethers';
2
+ declare type ProviderFactory = (network: string | undefined) => providers.Provider | string;
3
+ /**
4
+ * @param network the network to connect to
5
+ * @param defaultFactory the defaultFactory to use as fallback if needed
6
+ */
7
+ declare type CurrentProviderFactory = (network: string | undefined, defaultFactory: ProviderFactory) => providers.Provider | string;
8
+ declare const _default: {
9
+ setProviderFactory: (providerFactory?: CurrentProviderFactory | undefined) => void;
10
+ initPaymentDetectionApiKeys: (defaultProviderOptions?: Record<string, string | (() => string)> | undefined) => void;
11
+ getDefaultProvider: (network?: string | undefined) => providers.Provider;
12
+ };
13
+ export default _default;
14
+ //# sourceMappingURL=providers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.d.ts","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEnC,aAAK,eAAe,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,KAAK,SAAS,CAAC,QAAQ,GAAG,MAAM,CAAC;AAGpF;;;GAGG;AACH,aAAK,sBAAsB,GAAG,CAC5B,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,cAAc,EAAE,eAAe,KAC5B,SAAS,CAAC,QAAQ,GAAG,MAAM,CAAC;;;;;;AAuHjC,wBAIE"}
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const ethers_1 = require("ethers");
4
+ let warned = false;
5
+ /**
6
+ * Default API_KEYS configuration, can be overriden using initPaymentDetectionApiKeys
7
+ */
8
+ let providersApiKeys = {
9
+ // fallback to Ethers v4 default projectId
10
+ infura: () => process.env.RN_INFURA_KEY || '7d0d81d0919f4f05b9ab6634be01ee73',
11
+ };
12
+ /**
13
+ * @param defaultProviderOptions Default Provider Options as specified in https://docs.ethers.io/v5/api/providers/#providers-getDefaultProvider */
14
+ const initPaymentDetectionApiKeys = (defaultProviderOptions) => {
15
+ providersApiKeys = Object.assign(Object.assign({}, providersApiKeys), defaultProviderOptions);
16
+ };
17
+ /**
18
+ * Define default URLs for networks supported by Request payment detection but not by ethers' Infura Provider
19
+ */
20
+ const networkRpcs = {
21
+ private: ethers_1.providers.JsonRpcProvider.defaultUrl(),
22
+ matic: 'https://polygon-rpc.com/',
23
+ fantom: 'https://rpc.ftm.tools',
24
+ fuse: 'https://rpc.fuse.io',
25
+ bsctest: 'https://data-seed-prebsc-1-s1.binance.org:8545',
26
+ bsc: 'https://bsc-dataseed1.binance.org/',
27
+ xdai: 'https://rpc.xdaichain.com/',
28
+ 'arbitrum-rinkeby': 'https://rinkeby.arbitrum.io/rpc',
29
+ 'arbitrum-one': 'https://arb1.arbitrum.io/rpc',
30
+ avalanche: 'https://api.avax.network/ext/bc/C/rpc',
31
+ };
32
+ /**
33
+ * @see getDefaultProvider
34
+ * @param network
35
+ */
36
+ const defaultProviderFactory = (network) => {
37
+ if (!network) {
38
+ network = 'homestead';
39
+ }
40
+ // Returns environment variable override
41
+ const envVar = (process === null || process === void 0 ? void 0 : process.env) ? process.env[`RN_WEB3_RPC_URL_${network.toUpperCase()}`] : null;
42
+ if (envVar) {
43
+ return envVar;
44
+ }
45
+ // check default RPCs
46
+ if (networkRpcs[network]) {
47
+ return networkRpcs[network];
48
+ }
49
+ // use infura, if supported
50
+ try {
51
+ // try getting the URL for the given network. Will throw if not supported.
52
+ ethers_1.providers.InfuraProvider.getUrl(ethers_1.providers.getNetwork(network), {});
53
+ const apiKey = typeof providersApiKeys.infura === 'function'
54
+ ? providersApiKeys.infura()
55
+ : providersApiKeys.infura;
56
+ if (!apiKey && !warned) {
57
+ console.warn(`No API Key specified for Infura, using ethers default API key.
58
+ This is not recommended for Production environments.
59
+ To override Infura's default api key, use RN_INFURA_KEY environment variable, or call
60
+ initPaymentDetectionApiKeys({ infura: () => "myApiKey" });
61
+ `);
62
+ warned = true;
63
+ }
64
+ return new ethers_1.providers.InfuraProvider(network, apiKey);
65
+ }
66
+ catch (e) {
67
+ // suppress errors
68
+ }
69
+ if (!warned) {
70
+ console.warn(`No provider is specified for network ${network}, using ethers default provider.
71
+ This is not recommended for Production environments.
72
+ Use setProviderFactory to override the default provider`);
73
+ warned = true;
74
+ }
75
+ // use getDefaultProvider to keep the original behaviour
76
+ return ethers_1.providers.getDefaultProvider(network, providersApiKeys);
77
+ };
78
+ /**
79
+ * Defines the behaviour to obtain a Provider for a given Network.
80
+ * May be overriden using setProviderFactory
81
+ */
82
+ let currentProviderFactory = defaultProviderFactory;
83
+ /**
84
+ * Override the default providerFactory, which relies mainly on Infura.
85
+ * @param providerFactory if not specify, will reset to the default factory
86
+ */
87
+ const setProviderFactory = (providerFactory) => {
88
+ currentProviderFactory = providerFactory || defaultProviderFactory;
89
+ };
90
+ /**
91
+ * Returns a Web3 Provider for the given `network`.
92
+ *
93
+ * Configuration options:
94
+ * - Specify `RN_WEB3_RPC_URL_[NETWORK]` environment variable to override the default behaviour
95
+ * - Specify `RN_INFURA_KEY` to override the default Infura API KEY (recommended)
96
+ * - Use `initPaymentDetectionApiKeys` to override Infura API KEY when `RN_INFURA_KEY` is not usable
97
+ * - Use `setProviderFactory` for more complex configurations with multiple networks
98
+ *
99
+ * @param network the blockchain network. See https://chainid.network/chains.json `network` field for reference
100
+ */
101
+ const getDefaultProvider = (network) => {
102
+ const provider = currentProviderFactory(network, defaultProviderFactory);
103
+ if (typeof provider === 'string') {
104
+ return new ethers_1.providers.StaticJsonRpcProvider(provider);
105
+ }
106
+ return provider;
107
+ };
108
+ exports.default = {
109
+ setProviderFactory,
110
+ initPaymentDetectionApiKeys,
111
+ getDefaultProvider,
112
+ };
113
+ //# sourceMappingURL=providers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.js","sourceRoot":"","sources":["../src/providers.ts"],"names":[],"mappings":";;AAAA,mCAAmC;AAInC,IAAI,MAAM,GAAG,KAAK,CAAC;AAUnB;;GAEG;AACH,IAAI,gBAAgB,GAA4C;IAC9D,0CAA0C;IAC1C,MAAM,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,IAAI,kCAAkC;CAC9E,CAAC;AAEF;mJACmJ;AACnJ,MAAM,2BAA2B,GAAG,CAAC,sBAAgD,EAAQ,EAAE;IAC7F,gBAAgB,mCAAQ,gBAAgB,GAAK,sBAAsB,CAAE,CAAC;AACxE,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,GAA2B;IAC1C,OAAO,EAAE,kBAAS,CAAC,eAAe,CAAC,UAAU,EAAE;IAC/C,KAAK,EAAE,0BAA0B;IACjC,MAAM,EAAE,uBAAuB;IAC/B,IAAI,EAAE,qBAAqB;IAC3B,OAAO,EAAE,gDAAgD;IACzD,GAAG,EAAE,oCAAoC;IACzC,IAAI,EAAE,4BAA4B;IAClC,kBAAkB,EAAE,iCAAiC;IACrD,cAAc,EAAE,8BAA8B;IAC9C,SAAS,EAAE,uCAAuC;CACnD,CAAC;AAEF;;;GAGG;AACH,MAAM,sBAAsB,GAAoB,CAAC,OAA2B,EAAE,EAAE;IAC9E,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,GAAG,WAAW,CAAC;KACvB;IAED,wCAAwC;IACxC,MAAM,MAAM,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,GAAG,EAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;I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AsB,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC;IACzE,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;QAChC,OAAO,IAAI,kBAAS,CAAC,qBAAqB,CAAC,QAAQ,CAAC,CAAC;KACtD;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAEF,kBAAe;IACb,kBAAkB;IAClB,2BAA2B;IAC3B,kBAAkB;CACnB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@requestnetwork/utils",
3
- "version": "0.35.1-next.1686+b2792f0b",
3
+ "version": "0.35.1-next.1691+4a3c46af",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -40,7 +40,7 @@
40
40
  "test:watch": "yarn test --watch"
41
41
  },
42
42
  "dependencies": {
43
- "@requestnetwork/types": "0.35.1-next.1686+b2792f0b",
43
+ "@requestnetwork/types": "0.35.1-next.1691+4a3c46af",
44
44
  "eccrypto": "1.1.6",
45
45
  "ethers": "5.5.1",
46
46
  "secp256k1": "4.0.2",
@@ -57,5 +57,5 @@
57
57
  "ts-node": "9.0.0",
58
58
  "typescript": "4.4.4"
59
59
  },
60
- "gitHead": "b2792f0bee97ededbd0b400e7bcd0bd775315b1c"
60
+ "gitHead": "4a3c46af555c011a6bebf4d3fc556769f45df4bd"
61
61
  }