From af3e6cbecd8fca7b1b7912491b2788aae4bf5dfd Mon Sep 17 00:00:00 2001 From: krisbitney Date: Thu, 2 Mar 2023 14:28:23 +0530 Subject: [PATCH 1/2] added polywrap uri authority inference for common URI formats to Uri --- packages/js/core/README.md | 2 +- packages/js/core/src/__tests__/Uri.spec.ts | 29 +++- packages/js/core/src/types/Uri.ts | 63 ++++++-- packages/js/core/src/types/WrapError.ts | 12 +- packages/js/core/src/utils/RegExpGroups.ts | 5 + packages/js/core/src/utils/index.ts | 1 + .../js/plugin/src/utils/getErrorSource.ts | 8 +- yarn.lock | 136 ++++-------------- 8 files changed, 117 insertions(+), 139 deletions(-) create mode 100644 packages/js/core/src/utils/RegExpGroups.ts diff --git a/packages/js/core/README.md b/packages/js/core/README.md index f09614fc4b..8aeee84228 100644 --- a/packages/js/core/README.md +++ b/packages/js/core/README.md @@ -359,7 +359,7 @@ export interface UriConfig { /** * A Polywrap URI. Some examples of valid URIs are: * wrap://ipfs/QmHASH - * wrap://ens/sub.dimain.eth + * wrap://ens/sub.domain.eth * wrap://fs/directory/file.txt * wrap://uns/domain.crypto * diff --git a/packages/js/core/src/__tests__/Uri.spec.ts b/packages/js/core/src/__tests__/Uri.spec.ts index f38bfa7f1e..e6fe6a3abc 100644 --- a/packages/js/core/src/__tests__/Uri.spec.ts +++ b/packages/js/core/src/__tests__/Uri.spec.ts @@ -15,7 +15,7 @@ describe("Uri", () => { }); it("Fails if an authority is not present", () => { - expect(() => new Uri("wrap://path")).toThrowError(/URI is malformed,/); + expect(() => new Uri("wrap://path")).toThrowError(/URI authority is missing,/); }); it("Fails if a path is not present", () => { @@ -50,4 +50,31 @@ describe("Uri", () => { path: "uri", }); }); + + it("Infers common URI authorities", () => { + let uri = new Uri("https://domain.com/path/to/thing"); + expect(uri.uri).toEqual("wrap://https/https://domain.com/path/to/thing"); + expect(uri.authority).toEqual("https"); + expect(uri.path).toEqual("https://domain.com/path/to/thing"); + + uri = new Uri("http://domain.com/path/to/thing"); + expect(uri.uri).toEqual("wrap://http/http://domain.com/path/to/thing"); + expect(uri.authority).toEqual("http"); + expect(uri.path).toEqual("http://domain.com/path/to/thing"); + + uri = new Uri("ipfs://QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw"); + expect(uri.uri).toEqual("wrap://ipfs/QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw"); + expect(uri.authority).toEqual("ipfs"); + expect(uri.path).toEqual("QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw"); + + uri = new Uri("ens://domain.eth"); + expect(uri.uri).toEqual("wrap://ens/domain.eth"); + expect(uri.authority).toEqual("ens"); + expect(uri.path).toEqual("domain.eth"); + + uri = new Uri("ens://domain.eth:pkg@1.0.0"); + expect(uri.uri).toEqual("wrap://ens/domain.eth:pkg@1.0.0"); + expect(uri.authority).toEqual("ens"); + expect(uri.path).toEqual("domain.eth:pkg@1.0.0"); + }); }); diff --git a/packages/js/core/src/types/Uri.ts b/packages/js/core/src/types/Uri.ts index 755e32a39e..08336c99e1 100644 --- a/packages/js/core/src/types/Uri.ts +++ b/packages/js/core/src/types/Uri.ts @@ -1,3 +1,5 @@ +import { RegExpGroups } from "../utils/RegExpGroups"; + import { Result, ResultErr, ResultOk } from "@polywrap/result"; // $start: UriConfig @@ -18,7 +20,7 @@ export interface UriConfig { /** * A Polywrap URI. Some examples of valid URIs are: * wrap://ipfs/QmHASH - * wrap://ens/sub.dimain.eth + * wrap://ens/sub.domain.eth * wrap://fs/directory/file.txt * wrap://uns/domain.crypto * @@ -135,17 +137,10 @@ export class Uri { } // Extract the authoriy & path - const result = processed.match(/wrap:\/\/([a-z][a-z0-9-_]+)\/(.*)/); - let uriParts: string[]; - - // Remove all empty strings - if (result) { - uriParts = result.filter((str) => !!str); - } else { - uriParts = []; - } + const re = /^wrap:\/\/((?[a-z][a-z0-9-_]+)\/)?(?.*)$/; + const result: RegExpGroups<"authority" | "path"> = re.exec(processed); - if (uriParts.length !== 3) { + if (!result || !result.groups || !result.groups.path) { return ResultErr( Error( `URI is malformed, here are some examples of valid URIs:\n` + @@ -157,10 +152,30 @@ export class Uri { ); } + let { authority, path } = result.groups; + + if (!authority) { + const inferred = Uri.inferAuthority(path); + if (!inferred) { + return ResultErr( + Error( + `URI authority is missing, here are some examples of valid URIs:\n` + + `wrap://ipfs/QmHASH\n` + + `wrap://ens/domain.eth\n` + + `ens/domain.eth\n\n` + + `Invalid URI Received: ${uri}` + ) + ); + } + authority = inferred.authority; + path = inferred.path; + processed = `wrap://${authority}/${path}`; + } + return ResultOk({ uri: processed, - authority: uriParts[1], - path: uriParts[2], + authority, + path, }); } @@ -194,4 +209,26 @@ export class Uri { public toJSON(): string /* $ */ { return this._config.uri; } + + private static inferAuthority(path: string): UriConfig | undefined { + let authority: string | undefined; + + if (path.startsWith("https://")) { + authority = "https"; + } else if (path.startsWith("http://")) { + authority = "http"; + } else if (path.startsWith("ipfs://")) { + authority = "ipfs"; + path = path.substring(7); + } else if (path.startsWith("ens://")) { + authority = "ens"; + path = path.substring(6); + } + + if (!authority) { + return undefined; + } + + return { authority, path, uri: `wrap://${authority}/${path}` }; + } } diff --git a/packages/js/core/src/types/WrapError.ts b/packages/js/core/src/types/WrapError.ts index f2bae3bea8..5dd42fa7a5 100644 --- a/packages/js/core/src/types/WrapError.ts +++ b/packages/js/core/src/types/WrapError.ts @@ -1,4 +1,5 @@ import { CleanResolutionStep } from "../algorithms"; +import { RegExpGroups } from "../utils/RegExpGroups"; export type ErrorSource = Readonly<{ file?: string; @@ -52,12 +53,6 @@ export interface WrapErrorOptions { innerError?: WrapError; } -type RegExpGroups = - | (RegExpExecArray & { - groups?: { [name in T]: string | undefined } | { [key: string]: string }; - }) - | null; - export class WrapError extends Error { readonly name: string = "WrapError"; readonly code: WrapErrorCode; @@ -170,7 +165,7 @@ export class WrapError extends Error { | "resolutionStack" | "cause" > = WrapError.re.exec(error); - if (!result) { + if (!result || !result.groups) { return undefined; } const { @@ -184,8 +179,7 @@ export class WrapError extends Error { col, resolutionStack: resolutionStackStr, cause, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - } = result.groups!; + } = result.groups; const code = parseInt(codeStr as string); diff --git a/packages/js/core/src/utils/RegExpGroups.ts b/packages/js/core/src/utils/RegExpGroups.ts new file mode 100644 index 0000000000..35e7fa063f --- /dev/null +++ b/packages/js/core/src/utils/RegExpGroups.ts @@ -0,0 +1,5 @@ +export type RegExpGroups = + | (RegExpExecArray & { + groups?: { [name in T]: string | undefined } | { [key: string]: string }; + }) + | null; diff --git a/packages/js/core/src/utils/index.ts b/packages/js/core/src/utils/index.ts index f9908b4d74..6645b883e9 100644 --- a/packages/js/core/src/utils/index.ts +++ b/packages/js/core/src/utils/index.ts @@ -1,3 +1,4 @@ export * from "./combinePaths"; export * from "./getEnvFromUriHistory"; export * from "./is-buffer"; +export * from "./RegExpGroups"; diff --git a/packages/js/plugin/src/utils/getErrorSource.ts b/packages/js/plugin/src/utils/getErrorSource.ts index 8b737f432a..f87e497e00 100644 --- a/packages/js/plugin/src/utils/getErrorSource.ts +++ b/packages/js/plugin/src/utils/getErrorSource.ts @@ -1,10 +1,4 @@ -import { ErrorSource } from "@polywrap/core-js"; - -type RegExpGroups = - | (RegExpExecArray & { - groups?: { [name in T]: string | undefined } | { [key: string]: string }; - }) - | null; +import { ErrorSource, RegExpGroups } from "@polywrap/core-js"; const re = /\((?.*):(?\d+):(?\d+)\)$/; diff --git a/yarn.lock b/yarn.lock index aa442fe692..19accc23dc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -433,7 +433,7 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" -"@ethersproject/abi@5.7.0", "@ethersproject/abi@^5.0.0", "@ethersproject/abi@^5.7.0": +"@ethersproject/abi@5.7.0", "@ethersproject/abi@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/abi/-/abi-5.7.0.tgz#b3f3e045bbbeed1af3947335c247ad625a44e449" integrity sha512-351ktp42TiRcYB3H1OP8yajPeAQstMW/yCFokj/AthP9bLHzQFPlOrxOcwYEDkUAICmOHljvN4K39OMTMUa9RA== @@ -448,7 +448,7 @@ "@ethersproject/properties" "^5.7.0" "@ethersproject/strings" "^5.7.0" -"@ethersproject/abstract-provider@5.7.0", "@ethersproject/abstract-provider@^5.0.0", "@ethersproject/abstract-provider@^5.6.1", "@ethersproject/abstract-provider@^5.7.0": +"@ethersproject/abstract-provider@5.7.0", "@ethersproject/abstract-provider@^5.6.1", "@ethersproject/abstract-provider@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/abstract-provider/-/abstract-provider-5.7.0.tgz#b0a8550f88b6bf9d51f90e4795d48294630cb9ef" integrity sha512-R41c9UkchKCpAqStMYUpdunjo3pkEvZC3FAwZn5S5MGbXoMQOHIdHItezTETxAO5bevtMApSyEhn9+CHcDsWBw== @@ -461,7 +461,7 @@ "@ethersproject/transactions" "^5.7.0" "@ethersproject/web" "^5.7.0" -"@ethersproject/abstract-signer@5.7.0", "@ethersproject/abstract-signer@^5.0.0", "@ethersproject/abstract-signer@^5.6.2", "@ethersproject/abstract-signer@^5.7.0": +"@ethersproject/abstract-signer@5.7.0", "@ethersproject/abstract-signer@^5.6.2", "@ethersproject/abstract-signer@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/abstract-signer/-/abstract-signer-5.7.0.tgz#13f4f32117868452191a4649723cb086d2b596b2" integrity sha512-a16V8bq1/Cz+TGCkE2OPMTOUDLS3grCpdjoJCYNnVBbdYEMSgKrU0+B90s8b6H+ByYTBZN7a3g76jdIJi7UfKQ== @@ -472,18 +472,7 @@ "@ethersproject/logger" "^5.7.0" "@ethersproject/properties" "^5.7.0" -"@ethersproject/address@5.0.7": - version "5.0.7" - resolved "https://registry.yarnpkg.com/@ethersproject/address/-/address-5.0.7.tgz#ee7fd7d3b3a400dec6035c7b3f0b7e4652207308" - integrity sha512-+63DiYG+2og6rFNvQmLlLw8i5LtyT65n+jtHd06Ic81rLHc+JUKRpeZFhBa+gqh9f+P8V0xtKR5NI/EHXOfgSw== - dependencies: - "@ethersproject/bignumber" "^5.0.10" - "@ethersproject/bytes" "^5.0.4" - "@ethersproject/keccak256" "^5.0.3" - "@ethersproject/logger" "^5.0.5" - "@ethersproject/rlp" "^5.0.3" - -"@ethersproject/address@5.7.0", "@ethersproject/address@^5.0.0", "@ethersproject/address@^5.6.1", "@ethersproject/address@^5.7.0": +"@ethersproject/address@5.7.0", "@ethersproject/address@^5.6.1", "@ethersproject/address@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/address/-/address-5.7.0.tgz#19b56c4d74a3b0a46bfdbb6cfcc0a153fc697f37" integrity sha512-9wYhYt7aghVGo758POM5nqcOMaE168Q6aRLJZwUmiqSrAungkG74gSSeKEIR7ukixesdRZGPgVqme6vmxs1fkA== @@ -494,21 +483,13 @@ "@ethersproject/logger" "^5.7.0" "@ethersproject/rlp" "^5.7.0" -"@ethersproject/base64@5.7.0", "@ethersproject/base64@^5.0.0", "@ethersproject/base64@^5.6.1", "@ethersproject/base64@^5.7.0": +"@ethersproject/base64@5.7.0", "@ethersproject/base64@^5.6.1", "@ethersproject/base64@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/base64/-/base64-5.7.0.tgz#ac4ee92aa36c1628173e221d0d01f53692059e1c" integrity sha512-Dr8tcHt2mEbsZr/mwTPIQAf3Ai0Bks/7gTw9dSqk1mQvhW3XvRlmDJr/4n+wg1JmCl16NZue17CDh8xb/vZ0sQ== dependencies: "@ethersproject/bytes" "^5.7.0" -"@ethersproject/basex@5.0.7": - version "5.0.7" - resolved "https://registry.yarnpkg.com/@ethersproject/basex/-/basex-5.0.7.tgz#2f7026b12c9dee6cdc7b7bf1805461836e635495" - integrity sha512-OsXnRsujGmYD9LYyJlX+cVe5KfwgLUbUJrJMWdzRWogrygXd5HvGd7ygX1AYjlu1z8W/+t2FoQnczDR/H2iBjA== - dependencies: - "@ethersproject/bytes" "^5.0.9" - "@ethersproject/properties" "^5.0.7" - "@ethersproject/basex@5.7.0", "@ethersproject/basex@^5.6.1", "@ethersproject/basex@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/basex/-/basex-5.7.0.tgz#97034dc7e8938a8ca943ab20f8a5e492ece4020b" @@ -517,7 +498,7 @@ "@ethersproject/bytes" "^5.7.0" "@ethersproject/properties" "^5.7.0" -"@ethersproject/bignumber@5.7.0", "@ethersproject/bignumber@^5.0.0", "@ethersproject/bignumber@^5.0.10", "@ethersproject/bignumber@^5.6.2", "@ethersproject/bignumber@^5.7.0": +"@ethersproject/bignumber@5.7.0", "@ethersproject/bignumber@^5.6.2", "@ethersproject/bignumber@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/bignumber/-/bignumber-5.7.0.tgz#e2f03837f268ba655ffba03a57853e18a18dc9c2" integrity sha512-n1CAdIHRWjSucQO3MC1zPSVgV/6dy/fjL9pMrPP9peL+QxEg9wOsVqwD4+818B6LUEtaXzVHQiuivzRoxPxUGw== @@ -526,21 +507,21 @@ "@ethersproject/logger" "^5.7.0" bn.js "^5.2.1" -"@ethersproject/bytes@5.7.0", "@ethersproject/bytes@^5.0.0", "@ethersproject/bytes@^5.0.4", "@ethersproject/bytes@^5.0.9", "@ethersproject/bytes@^5.6.1", "@ethersproject/bytes@^5.7.0": +"@ethersproject/bytes@5.7.0", "@ethersproject/bytes@^5.6.1", "@ethersproject/bytes@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/bytes/-/bytes-5.7.0.tgz#a00f6ea8d7e7534d6d87f47188af1148d71f155d" integrity sha512-nsbxwgFXWh9NyYWo+U8atvmMsSdKJprTcICAkvbBffT75qDocbuggBU0SJiVK2MuTrp0q+xvLkTnGMPK1+uA9A== dependencies: "@ethersproject/logger" "^5.7.0" -"@ethersproject/constants@5.7.0", "@ethersproject/constants@^5.0.0", "@ethersproject/constants@^5.6.1", "@ethersproject/constants@^5.7.0": +"@ethersproject/constants@5.7.0", "@ethersproject/constants@^5.6.1", "@ethersproject/constants@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/constants/-/constants-5.7.0.tgz#df80a9705a7e08984161f09014ea012d1c75295e" integrity sha512-DHI+y5dBNvkpYUMiRQyxRBYBefZkJfo70VUkUAsRjcPs47muV9evftfZ0PJVCXYbAiCgght0DtcF9srFQmIgWA== dependencies: "@ethersproject/bignumber" "^5.7.0" -"@ethersproject/contracts@5.7.0", "@ethersproject/contracts@^5.0.0": +"@ethersproject/contracts@5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/contracts/-/contracts-5.7.0.tgz#c305e775abd07e48aa590e1a877ed5c316f8bd1e" integrity sha512-5GJbzEU3X+d33CdfPhcyS+z8MzsTrBGk/sc+G+59+tPa9yFkl6HQ9D6L0QMgNTA9q8dT0XKxxkyp883XsQvbbg== @@ -556,7 +537,7 @@ "@ethersproject/properties" "^5.7.0" "@ethersproject/transactions" "^5.7.0" -"@ethersproject/hash@5.7.0", "@ethersproject/hash@^5.0.0", "@ethersproject/hash@^5.6.1", "@ethersproject/hash@^5.7.0": +"@ethersproject/hash@5.7.0", "@ethersproject/hash@^5.6.1", "@ethersproject/hash@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/hash/-/hash-5.7.0.tgz#eb7aca84a588508369562e16e514b539ba5240a7" integrity sha512-qX5WrQfnah1EFnO5zJv1v46a8HW0+E5xuBBDTwMFZLuVTx0tbU2kkx15NqdjxecrLGatQN9FGQKpb1FKdHCt+g== @@ -571,7 +552,7 @@ "@ethersproject/properties" "^5.7.0" "@ethersproject/strings" "^5.7.0" -"@ethersproject/hdnode@5.7.0", "@ethersproject/hdnode@^5.0.0", "@ethersproject/hdnode@^5.6.2", "@ethersproject/hdnode@^5.7.0": +"@ethersproject/hdnode@5.7.0", "@ethersproject/hdnode@^5.6.2", "@ethersproject/hdnode@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/hdnode/-/hdnode-5.7.0.tgz#e627ddc6b466bc77aebf1a6b9e47405ca5aef9cf" integrity sha512-OmyYo9EENBPPf4ERhR7oj6uAtUAhYGqOnIS+jE5pTXvdKBS99ikzq1E7Iv0ZQZ5V36Lqx1qZLeak0Ra16qpeOg== @@ -589,7 +570,7 @@ "@ethersproject/transactions" "^5.7.0" "@ethersproject/wordlists" "^5.7.0" -"@ethersproject/json-wallets@5.7.0", "@ethersproject/json-wallets@^5.0.0", "@ethersproject/json-wallets@^5.6.1", "@ethersproject/json-wallets@^5.7.0": +"@ethersproject/json-wallets@5.7.0", "@ethersproject/json-wallets@^5.6.1", "@ethersproject/json-wallets@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/json-wallets/-/json-wallets-5.7.0.tgz#5e3355287b548c32b368d91014919ebebddd5360" integrity sha512-8oee5Xgu6+RKgJTkvEMl2wDgSPSAQ9MB/3JYjFV9jlKvcYHUXZC+cQp0njgmxdHkYWn8s6/IqIZYm0YWCjO/0g== @@ -608,7 +589,7 @@ aes-js "3.0.0" scrypt-js "3.0.1" -"@ethersproject/keccak256@5.7.0", "@ethersproject/keccak256@^5.0.0", "@ethersproject/keccak256@^5.0.3", "@ethersproject/keccak256@^5.6.1", "@ethersproject/keccak256@^5.7.0": +"@ethersproject/keccak256@5.7.0", "@ethersproject/keccak256@^5.6.1", "@ethersproject/keccak256@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/keccak256/-/keccak256-5.7.0.tgz#3186350c6e1cd6aba7940384ec7d6d9db01f335a" integrity sha512-2UcPboeL/iW+pSg6vZ6ydF8tCnv3Iu/8tUmLLzWWGzxWKFFqOBQFLo6uLUv6BDrLgCDfN28RJ/wtByx+jZ4KBg== @@ -616,7 +597,7 @@ "@ethersproject/bytes" "^5.7.0" js-sha3 "0.8.0" -"@ethersproject/logger@5.7.0", "@ethersproject/logger@^5.0.0", "@ethersproject/logger@^5.0.5", "@ethersproject/logger@^5.6.0", "@ethersproject/logger@^5.7.0": +"@ethersproject/logger@5.7.0", "@ethersproject/logger@^5.6.0", "@ethersproject/logger@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/logger/-/logger-5.7.0.tgz#6ce9ae168e74fecf287be17062b590852c311892" integrity sha512-0odtFdXu/XHtjQXJYA3u9G0G8btm0ND5Cu8M7i5vhEcE8/HmF4Lbdqanwyv4uQTr2tx6b7fQRmgLrsnpQlmnig== @@ -628,14 +609,14 @@ dependencies: "@ethersproject/logger" "^5.7.0" -"@ethersproject/networks@^5.0.0", "@ethersproject/networks@^5.6.3", "@ethersproject/networks@^5.7.0": +"@ethersproject/networks@^5.6.3", "@ethersproject/networks@^5.7.0": version "5.7.1" resolved "https://registry.yarnpkg.com/@ethersproject/networks/-/networks-5.7.1.tgz#118e1a981d757d45ccea6bb58d9fd3d9db14ead6" integrity sha512-n/MufjFYv3yFcUyfhnXotyDlNdFb7onmkSy8aQERi2PjNcnWQ66xXxa3XlS8nCcA8aJKJjIIMNJTC7tu80GwpQ== dependencies: "@ethersproject/logger" "^5.7.0" -"@ethersproject/pbkdf2@5.7.0", "@ethersproject/pbkdf2@^5.0.0", "@ethersproject/pbkdf2@^5.7.0": +"@ethersproject/pbkdf2@5.7.0", "@ethersproject/pbkdf2@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/pbkdf2/-/pbkdf2-5.7.0.tgz#d2267d0a1f6e123f3771007338c47cccd83d3102" integrity sha512-oR/dBRZR6GTyaofd86DehG72hY6NpAjhabkhxgr3X2FpJtJuodEl2auADWBZfhDHgVCbu3/H/Ocq2uC6dpNjjw== @@ -643,7 +624,7 @@ "@ethersproject/bytes" "^5.7.0" "@ethersproject/sha2" "^5.7.0" -"@ethersproject/properties@5.7.0", "@ethersproject/properties@^5.0.0", "@ethersproject/properties@^5.0.7", "@ethersproject/properties@^5.6.0", "@ethersproject/properties@^5.7.0": +"@ethersproject/properties@5.7.0", "@ethersproject/properties@^5.6.0", "@ethersproject/properties@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/properties/-/properties-5.7.0.tgz#a6e12cb0439b878aaf470f1902a176033067ed30" integrity sha512-J87jy8suntrAkIZtecpxEPxY//szqr1mlBaYlQ0r4RCaiD2hjheqF9s1LVE8vVuJCXisjIP+JgtK/Do54ej4Sw== @@ -702,33 +683,7 @@ bech32 "1.1.4" ws "7.4.6" -"@ethersproject/providers@^5.0.0": - version "5.7.2" - resolved "https://registry.yarnpkg.com/@ethersproject/providers/-/providers-5.7.2.tgz#f8b1a4f275d7ce58cf0a2eec222269a08beb18cb" - integrity sha512-g34EWZ1WWAVgr4aptGlVBF8mhl3VWjv+8hoAnzStu8Ah22VHBsuGzP17eb6xDVRzw895G4W7vvx60lFFur/1Rg== - dependencies: - "@ethersproject/abstract-provider" "^5.7.0" - "@ethersproject/abstract-signer" "^5.7.0" - "@ethersproject/address" "^5.7.0" - "@ethersproject/base64" "^5.7.0" - "@ethersproject/basex" "^5.7.0" - "@ethersproject/bignumber" "^5.7.0" - "@ethersproject/bytes" "^5.7.0" - "@ethersproject/constants" "^5.7.0" - "@ethersproject/hash" "^5.7.0" - "@ethersproject/logger" "^5.7.0" - "@ethersproject/networks" "^5.7.0" - "@ethersproject/properties" "^5.7.0" - "@ethersproject/random" "^5.7.0" - "@ethersproject/rlp" "^5.7.0" - "@ethersproject/sha2" "^5.7.0" - "@ethersproject/strings" "^5.7.0" - "@ethersproject/transactions" "^5.7.0" - "@ethersproject/web" "^5.7.0" - bech32 "1.1.4" - ws "7.4.6" - -"@ethersproject/random@5.7.0", "@ethersproject/random@^5.0.0", "@ethersproject/random@^5.6.1", "@ethersproject/random@^5.7.0": +"@ethersproject/random@5.7.0", "@ethersproject/random@^5.6.1", "@ethersproject/random@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/random/-/random-5.7.0.tgz#af19dcbc2484aae078bb03656ec05df66253280c" integrity sha512-19WjScqRA8IIeWclFme75VMXSBvi4e6InrUNuaR4s5pTF2qNhcGdCUwdxUVGtDDqC00sDLCO93jPQoDUH4HVmQ== @@ -736,7 +691,7 @@ "@ethersproject/bytes" "^5.7.0" "@ethersproject/logger" "^5.7.0" -"@ethersproject/rlp@5.7.0", "@ethersproject/rlp@^5.0.0", "@ethersproject/rlp@^5.0.3", "@ethersproject/rlp@^5.6.1", "@ethersproject/rlp@^5.7.0": +"@ethersproject/rlp@5.7.0", "@ethersproject/rlp@^5.6.1", "@ethersproject/rlp@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/rlp/-/rlp-5.7.0.tgz#de39e4d5918b9d74d46de93af80b7685a9c21304" integrity sha512-rBxzX2vK8mVF7b0Tol44t5Tb8gomOHkj5guL+HhzQ1yBh/ydjGnpw6at+X6Iw0Kp3OzzzkcKp8N9r0W4kYSs9w== @@ -744,7 +699,7 @@ "@ethersproject/bytes" "^5.7.0" "@ethersproject/logger" "^5.7.0" -"@ethersproject/sha2@5.7.0", "@ethersproject/sha2@^5.0.0", "@ethersproject/sha2@^5.6.1", "@ethersproject/sha2@^5.7.0": +"@ethersproject/sha2@5.7.0", "@ethersproject/sha2@^5.6.1", "@ethersproject/sha2@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/sha2/-/sha2-5.7.0.tgz#9a5f7a7824ef784f7f7680984e593a800480c9fb" integrity sha512-gKlH42riwb3KYp0reLsFTokByAKoJdgFCwI+CCiX/k+Jm2mbNs6oOaCjYQSlI1+XBVejwH2KrmCbMAT/GnRDQw== @@ -753,7 +708,7 @@ "@ethersproject/logger" "^5.7.0" hash.js "1.1.7" -"@ethersproject/signing-key@5.7.0", "@ethersproject/signing-key@^5.0.0", "@ethersproject/signing-key@^5.6.2", "@ethersproject/signing-key@^5.7.0": +"@ethersproject/signing-key@5.7.0", "@ethersproject/signing-key@^5.6.2", "@ethersproject/signing-key@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/signing-key/-/signing-key-5.7.0.tgz#06b2df39411b00bc57c7c09b01d1e41cf1b16ab3" integrity sha512-MZdy2nL3wO0u7gkB4nA/pEf8lu1TlFswPNmy8AiYkfKTdO6eXBJyUdmHO/ehm/htHw9K/qF8ujnTyUAD+Ry54Q== @@ -765,7 +720,7 @@ elliptic "6.5.4" hash.js "1.1.7" -"@ethersproject/solidity@5.7.0", "@ethersproject/solidity@^5.0.0": +"@ethersproject/solidity@5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/solidity/-/solidity-5.7.0.tgz#5e9c911d8a2acce2a5ebb48a5e2e0af20b631cb8" integrity sha512-HmabMd2Dt/raavyaGukF4XxizWKhKQ24DoLtdNbBmNKUOPqwjsKQSdV9GQtj9CBEea9DlzETlVER1gYeXXBGaA== @@ -777,7 +732,7 @@ "@ethersproject/sha2" "^5.7.0" "@ethersproject/strings" "^5.7.0" -"@ethersproject/strings@5.7.0", "@ethersproject/strings@^5.0.0", "@ethersproject/strings@^5.6.1", "@ethersproject/strings@^5.7.0": +"@ethersproject/strings@5.7.0", "@ethersproject/strings@^5.6.1", "@ethersproject/strings@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/strings/-/strings-5.7.0.tgz#54c9d2a7c57ae8f1205c88a9d3a56471e14d5ed2" integrity sha512-/9nu+lj0YswRNSH0NXYqrh8775XNyEdUQAuf3f+SmOrnVewcJ5SBNAjF7lpgehKi4abvNNXyf+HX86czCdJ8Mg== @@ -786,7 +741,7 @@ "@ethersproject/constants" "^5.7.0" "@ethersproject/logger" "^5.7.0" -"@ethersproject/transactions@5.7.0", "@ethersproject/transactions@^5.0.0", "@ethersproject/transactions@^5.6.2", "@ethersproject/transactions@^5.7.0": +"@ethersproject/transactions@5.7.0", "@ethersproject/transactions@^5.6.2", "@ethersproject/transactions@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/transactions/-/transactions-5.7.0.tgz#91318fc24063e057885a6af13fdb703e1f993d3b" integrity sha512-kmcNicCp1lp8qanMTC3RIikGgoJ80ztTyvtsFvCYpSCfkjhD0jZ2LOrnbcuxuToLIUYYf+4XwD1rP+B/erDIhQ== @@ -801,7 +756,7 @@ "@ethersproject/rlp" "^5.7.0" "@ethersproject/signing-key" "^5.7.0" -"@ethersproject/units@5.7.0", "@ethersproject/units@^5.0.0": +"@ethersproject/units@5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/units/-/units-5.7.0.tgz#637b563d7e14f42deeee39245275d477aae1d8b1" integrity sha512-pD3xLMy3SJu9kG5xDGI7+xhTEmGXlEqXU4OfNapmfnxLVY4EMSSRp7j1k7eezutBPH7RBN/7QPnwR7hzNlEFeg== @@ -831,7 +786,7 @@ "@ethersproject/transactions" "^5.6.2" "@ethersproject/wordlists" "^5.6.1" -"@ethersproject/wallet@5.7.0", "@ethersproject/wallet@^5.0.0": +"@ethersproject/wallet@5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/wallet/-/wallet-5.7.0.tgz#4e5d0790d96fe21d61d38fb40324e6c7ef350b2d" integrity sha512-MhmXlJXEJFBFVKrDLB4ZdDzxcBxQ3rLyCkhNqVu3CDYvR97E+8r01UgrI+TI99Le+aYm/in/0vp86guJuM7FCA== @@ -863,7 +818,7 @@ "@ethersproject/properties" "^5.7.0" "@ethersproject/strings" "^5.7.0" -"@ethersproject/web@^5.0.0", "@ethersproject/web@^5.6.1", "@ethersproject/web@^5.7.0": +"@ethersproject/web@^5.6.1", "@ethersproject/web@^5.7.0": version "5.7.1" resolved "https://registry.yarnpkg.com/@ethersproject/web/-/web-5.7.1.tgz#de1f285b373149bee5928f4eb7bcb87ee5fbb4ae" integrity sha512-Gueu8lSvyjBWL4cYsWsjh6MtMwM0+H4HvqFPZfB6dV8ctbP9zFAO73VG1cMWae0FLPCtz0peKPpZY8/ugJJX2w== @@ -874,7 +829,7 @@ "@ethersproject/properties" "^5.7.0" "@ethersproject/strings" "^5.7.0" -"@ethersproject/wordlists@5.7.0", "@ethersproject/wordlists@^5.0.0", "@ethersproject/wordlists@^5.6.1", "@ethersproject/wordlists@^5.7.0": +"@ethersproject/wordlists@5.7.0", "@ethersproject/wordlists@^5.6.1", "@ethersproject/wordlists@^5.7.0": version "5.7.0" resolved "https://registry.yarnpkg.com/@ethersproject/wordlists/-/wordlists-5.7.0.tgz#8fb2c07185d68c3e09eb3bfd6e779ba2774627f5" integrity sha512-S2TFNJNfHWVHNE6cNDjbVlZ6MgE17MIxMbMg2zv3wn+3XSJGosL1m9ZVv3GXCf/2ymSsQ+hRI5IzoMJTG6aoVA== @@ -4839,41 +4794,6 @@ esutils@^2.0.2: resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64" integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g== -ethers@5.0.7: - version "5.0.7" - resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.0.7.tgz#41c3d774e0a57bfde12b0198885789fb41a14976" - integrity sha512-1Zu9s+z4BgsDAZcGIYACJdWBB6mVtCCmUonj68Njul7STcSdgwOyj0sCAxCUr2Nsmsamckr4E12q3ecvZPGAUw== - dependencies: - "@ethersproject/abi" "^5.0.0" - "@ethersproject/abstract-provider" "^5.0.0" - "@ethersproject/abstract-signer" "^5.0.0" - "@ethersproject/address" "^5.0.0" - "@ethersproject/base64" "^5.0.0" - "@ethersproject/bignumber" "^5.0.0" - "@ethersproject/bytes" "^5.0.0" - "@ethersproject/constants" "^5.0.0" - "@ethersproject/contracts" "^5.0.0" - "@ethersproject/hash" "^5.0.0" - "@ethersproject/hdnode" "^5.0.0" - "@ethersproject/json-wallets" "^5.0.0" - "@ethersproject/keccak256" "^5.0.0" - "@ethersproject/logger" "^5.0.0" - "@ethersproject/networks" "^5.0.0" - "@ethersproject/pbkdf2" "^5.0.0" - "@ethersproject/properties" "^5.0.0" - "@ethersproject/providers" "^5.0.0" - "@ethersproject/random" "^5.0.0" - "@ethersproject/rlp" "^5.0.0" - "@ethersproject/sha2" "^5.0.0" - "@ethersproject/signing-key" "^5.0.0" - "@ethersproject/solidity" "^5.0.0" - "@ethersproject/strings" "^5.0.0" - "@ethersproject/transactions" "^5.0.0" - "@ethersproject/units" "^5.0.0" - "@ethersproject/wallet" "^5.0.0" - "@ethersproject/web" "^5.0.0" - "@ethersproject/wordlists" "^5.0.0" - ethers@5.7.0: version "5.7.0" resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.7.0.tgz#0055da174b9e076b242b8282638bc94e04b39835" From 42938069dc222619903a789d5667587bc2f750df Mon Sep 17 00:00:00 2001 From: krisbitney Date: Fri, 3 Mar 2023 14:09:28 +0530 Subject: [PATCH 2/2] generalized URI authority inference --- packages/js/core/src/__tests__/Uri.spec.ts | 8 ++++---- packages/js/core/src/types/Uri.ts | 21 ++++++--------------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/packages/js/core/src/__tests__/Uri.spec.ts b/packages/js/core/src/__tests__/Uri.spec.ts index e6fe6a3abc..778fdde148 100644 --- a/packages/js/core/src/__tests__/Uri.spec.ts +++ b/packages/js/core/src/__tests__/Uri.spec.ts @@ -53,14 +53,14 @@ describe("Uri", () => { it("Infers common URI authorities", () => { let uri = new Uri("https://domain.com/path/to/thing"); - expect(uri.uri).toEqual("wrap://https/https://domain.com/path/to/thing"); + expect(uri.uri).toEqual("wrap://https/domain.com/path/to/thing"); expect(uri.authority).toEqual("https"); - expect(uri.path).toEqual("https://domain.com/path/to/thing"); + expect(uri.path).toEqual("domain.com/path/to/thing"); uri = new Uri("http://domain.com/path/to/thing"); - expect(uri.uri).toEqual("wrap://http/http://domain.com/path/to/thing"); + expect(uri.uri).toEqual("wrap://http/domain.com/path/to/thing"); expect(uri.authority).toEqual("http"); - expect(uri.path).toEqual("http://domain.com/path/to/thing"); + expect(uri.path).toEqual("domain.com/path/to/thing"); uri = new Uri("ipfs://QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw"); expect(uri.uri).toEqual("wrap://ipfs/QmaM318ABUXDhc5eZGGbmDxkb2ZgnbLxigm5TyZcCsh1Kw"); diff --git a/packages/js/core/src/types/Uri.ts b/packages/js/core/src/types/Uri.ts index 08336c99e1..4af5dbe35d 100644 --- a/packages/js/core/src/types/Uri.ts +++ b/packages/js/core/src/types/Uri.ts @@ -210,24 +210,15 @@ export class Uri { return this._config.uri; } - private static inferAuthority(path: string): UriConfig | undefined { - let authority: string | undefined; - - if (path.startsWith("https://")) { - authority = "https"; - } else if (path.startsWith("http://")) { - authority = "http"; - } else if (path.startsWith("ipfs://")) { - authority = "ipfs"; - path = path.substring(7); - } else if (path.startsWith("ens://")) { - authority = "ens"; - path = path.substring(6); - } + private static inferAuthority(_path: string): UriConfig | undefined { + const re = /^(?[a-z][a-z0-9-_]+):\/\/(?.*)$/; + const result: RegExpGroups<"authority" | "path"> = re.exec(_path); - if (!authority) { + if (!result || !result.groups) { return undefined; } + const authority = result.groups.authority as string; + const path = result.groups.path as string; return { authority, path, uri: `wrap://${authority}/${path}` }; }