From 79e039eb44dd62a27534d7ef86f70d7d2cb7a02a Mon Sep 17 00:00:00 2001 From: Hank Stoever Date: Mon, 16 Dec 2019 18:49:51 -0800 Subject: [PATCH 1/4] support optional chaining --- .babelrc | 5 +++++ package.json | 1 + yarn.lock | 15 +++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 .babelrc diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..8761541 --- /dev/null +++ b/.babelrc @@ -0,0 +1,5 @@ +{ + "plugins": [ + "@babel/plugin-proposal-optional-chaining" + ] +} \ No newline at end of file diff --git a/package.json b/package.json index 1f5ebb0..30e2c8b 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ }, "prettier": "@blockstack/prettier-config", "devDependencies": { + "@babel/plugin-proposal-optional-chaining": "^7.7.5", "@blockstack/prettier-config": "^0.0.3", "@types/jest": "^24.0.23", "@typescript-eslint/eslint-plugin": "^2.10.0", diff --git a/yarn.lock b/yarn.lock index 9513bab..08f8fbb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -286,6 +286,14 @@ "@babel/helper-plugin-utils" "^7.0.0" "@babel/plugin-syntax-optional-catch-binding" "^7.7.4" +"@babel/plugin-proposal-optional-chaining@^7.7.5": + version "7.7.5" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-7.7.5.tgz#f0835f044cef85b31071a924010a2a390add11d4" + integrity sha512-sOwFqT8JSchtJeDD+CjmWCaiFoLxY4Ps7NjvwHC/U7l4e9i5pTRNt8nDMIFSOUL+ncFbYSwruHM8WknYItWdXw== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-optional-chaining" "^7.7.4" + "@babel/plugin-proposal-unicode-property-regex@^7.7.4": version "7.7.4" resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-unicode-property-regex/-/plugin-proposal-unicode-property-regex-7.7.4.tgz#7c239ccaf09470dbe1d453d50057460e84517ebb" @@ -329,6 +337,13 @@ dependencies: "@babel/helper-plugin-utils" "^7.0.0" +"@babel/plugin-syntax-optional-chaining@^7.7.4": + version "7.7.4" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-optional-chaining/-/plugin-syntax-optional-chaining-7.7.4.tgz#c91fdde6de85d2eb8906daea7b21944c3610c901" + integrity sha512-2MqYD5WjZSbJdUagnJvIdSfkb/ucOC9/1fRJxm7GAxY6YQLWlUvkfxoNbUPcPLHJyetKUDQ4+yyuUyAoc0HriA== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-top-level-await@^7.7.4": version "7.7.4" resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-top-level-await/-/plugin-syntax-top-level-await-7.7.4.tgz#bd7d8fa7b9fee793a36e4027fd6dd1aa32f946da" From b80b4015e392ff3d5b5ef298546167b257bd7708 Mon Sep 17 00:00:00 2001 From: Hank Stoever Date: Mon, 16 Dec 2019 18:52:13 -0800 Subject: [PATCH 2/4] better type API for `finished` --- src/auth.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/auth.ts b/src/auth.ts index 7963e33..7edc0d8 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -4,15 +4,15 @@ import { popupCenter } from './popup'; const dataVaultHost = 'https://vault.hankstoever.com'; interface FinishedData { - authResponse?: string; - userSession?: UserSession; + authResponse: string; + userSession: UserSession; } interface AuthOptions { // The URL you want the user to be redirected to after authentication. redirectTo: string; manifestPath: string; - finished?: (data?: FinishedData) => void; + finished?: (data: FinishedData) => void; vaultUrl?: string; sendToSignIn?: boolean; userSession?: UserSession; @@ -76,7 +76,7 @@ interface FinishedEventData { interface ListenerParams { popup: Window | null; authRequest: string; - finished?: (data?: FinishedData) => void; + finished?: (data: FinishedData) => void; dataVaultURL: URL; userSession: UserSession; } From 3bf4f05f4f3e0cce54c29a9823d859baf21b92ab Mon Sep 17 00:00:00 2001 From: Hank Stoever Date: Mon, 16 Dec 2019 18:52:53 -0800 Subject: [PATCH 3/4] better default params for popup --- src/auth.ts | 6 ------ src/index.ts | 1 + src/popup.ts | 17 +++++++++++++---- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/auth.ts b/src/auth.ts index 7edc0d8..9bad314 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -54,14 +54,8 @@ export const authenticate = ({ } ); - const height = 584; - const width = 440; const popup = popupCenter({ - url: `${dataVaultURL.origin}/actions.html?authRequest=${authRequest}`, - title: 'Continue with Data Vault', - w: width, - h: height }); setupListener({ popup, authRequest, finished, dataVaultURL, userSession }); diff --git a/src/index.ts b/src/index.ts index 269586e..4ddba1d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ export * from './auth'; +export * from './popup'; diff --git a/src/popup.ts b/src/popup.ts index d1d32a3..6287bd5 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -1,11 +1,20 @@ interface PopupOptions { url: string; - title: string; - w: number; - h: number; + title?: string; + w?: number; + h?: number; } -export const popupCenter = ({ url, title, w, h }: PopupOptions) => { +const defaultHeight = 584; +const defaultWidth = 440; +const defaultTitle = 'Continue with Data Vault'; + +export const popupCenter = ({ + url, + title = defaultTitle, + w = defaultWidth, + h = defaultHeight +}: PopupOptions) => { const dualScreenLeft = window.screenLeft || window.screenX; const dualScreenTop = window.screenTop || window.screenY; From 2681bb9bfccc82d65b11d5900303d86fa7c38bd0 Mon Sep 17 00:00:00 2001 From: Hank Stoever Date: Mon, 16 Dec 2019 18:53:23 -0800 Subject: [PATCH 4/4] use the new `BlockstackProvider` API provided by extension --- src/auth.ts | 9 ++++++--- src/index.ts | 1 + src/types.ts | 9 +++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/types.ts diff --git a/src/auth.ts b/src/auth.ts index 9bad314..e00aec9 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,7 +1,8 @@ import { UserSession, AppConfig } from 'blockstack'; +import './types'; import { popupCenter } from './popup'; -const dataVaultHost = 'https://vault.hankstoever.com'; +const defaultVaultURL = 'https://vault.hankstoever.com'; interface FinishedData { authResponse: string; @@ -22,7 +23,7 @@ interface AuthOptions { }; } -export const authenticate = ({ +export const authenticate = async ({ redirectTo, manifestPath, finished, @@ -31,7 +32,6 @@ export const authenticate = ({ userSession, appDetails }: AuthOptions) => { - const dataVaultURL = new URL(vaultUrl || dataVaultHost); if (!userSession) { const appConfig = new AppConfig( ['store_write', 'publish_data'], @@ -54,8 +54,11 @@ export const authenticate = ({ } ); + const extensionURL = await window.BlockstackProvider?.getURL(); + const dataVaultURL = new URL(extensionURL || vaultUrl || defaultVaultURL); const popup = popupCenter({ + url: `${dataVaultURL.origin}/actions.html?authRequest=${authRequest}` }); setupListener({ popup, authRequest, finished, dataVaultURL, userSession }); diff --git a/src/index.ts b/src/index.ts index 4ddba1d..a7948fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export * from './auth'; export * from './popup'; +export * from './types'; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..acd3bf7 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,9 @@ +export interface BlockstackProvider { + getURL: () => Promise; +} + +declare global { + interface Window { + BlockstackProvider?: BlockstackProvider; + } +}