diff --git a/.changeset/twelve-rabbits-cough.md b/.changeset/twelve-rabbits-cough.md new file mode 100644 index 0000000000..64bd46dfc2 --- /dev/null +++ b/.changeset/twelve-rabbits-cough.md @@ -0,0 +1,5 @@ +--- +"@near-js/wallet-account": minor +--- + +Introduce methods to construct URLs to the wallet without instant redirect diff --git a/packages/wallet-account/src/wallet_account.ts b/packages/wallet-account/src/wallet_account.ts index 57f54a2e23..18d9115b72 100644 --- a/packages/wallet-account/src/wallet_account.ts +++ b/packages/wallet-account/src/wallet_account.ts @@ -167,7 +167,7 @@ export class WalletConnection { } /** - * Redirects current page to the wallet authentication page. + * Constructs string URL to the wallet authentication page. * @param options An optional options object * @param options.contractId The NEAR account where the contract is deployed * @param options.successUrl URL to redirect upon success. Default: current url @@ -176,11 +176,11 @@ export class WalletConnection { * @example * ```js * const wallet = new WalletConnection(near, 'my-app'); - * // redirects to the NEAR Wallet - * wallet.requestSignIn({ contractId: 'account-with-deploy-contract.near' }); + * // return string URL to the NEAR Wallet + * const url = await wallet.requestSignInUrl({ contractId: 'account-with-deploy-contract.near' }); * ``` */ - async requestSignIn({ contractId, methodNames, successUrl, failureUrl }: SignInOptions) { + async requestSignInUrl({contractId, methodNames, successUrl, failureUrl}: SignInOptions): Promise { const currentUrl = new URL(window.location.href); const newUrl = new URL(this._walletBaseUrl + LOGIN_WALLET_URL_SUFFIX); newUrl.searchParams.set('success_url', successUrl || currentUrl.href); @@ -202,17 +202,39 @@ export class WalletConnection { }); } - window.location.assign(newUrl.toString()); + return newUrl.toString(); } /** - * Requests the user to quickly sign for a transaction or batch of transactions by redirecting to the NEAR wallet. + * Redirects current page to the wallet authentication page. * @param options An optional options object - * @param options.transactions An array of transactions to be signed. - * @param options.meta Additional metadata to be included in the signing request. - * @param options.callbackUrl URL to redirect upon completion. Default: current URL. + * @param options.contractId The NEAR account where the contract is deployed + * @param options.successUrl URL to redirect upon success. Default: current url + * @param options.failureUrl URL to redirect upon failure. Default: current url + * + * @example + * ```js + * const wallet = new WalletConnection(near, 'my-app'); + * // redirects to the NEAR Wallet + * wallet.requestSignIn({ contractId: 'account-with-deploy-contract.near' }); + * ``` + */ + async requestSignIn(options: SignInOptions) { + const url = await this.requestSignInUrl(options); + + window.location.assign(url); + } + + /** + * Constructs string URL to the wallet to sign a transaction or batch of transactions. + * + * @param options A required options object + * @param options.transactions List of transactions to sign + * @param options.callbackUrl URL to redirect upon success. Default: current url + * @param options.meta Meta information the wallet will send back to the application. `meta` will be attached to the `callbackUrl` as a url search param + * */ - async requestSignTransactions({ transactions, meta, callbackUrl }: RequestSignTransactionsOptions): Promise { + requestSignTransactionsUrl({ transactions, meta, callbackUrl }: RequestSignTransactionsOptions): string { const currentUrl = new URL(window.location.href); const newUrl = new URL('sign', this._walletBaseUrl); @@ -223,7 +245,22 @@ export class WalletConnection { newUrl.searchParams.set('callbackUrl', callbackUrl || currentUrl.href); if (meta) newUrl.searchParams.set('meta', meta); - window.location.assign(newUrl.toString()); + return newUrl.toString(); + } + + /** + * Requests the user to quickly sign for a transaction or batch of transactions by redirecting to the wallet. + * + * @param options A required options object + * @param options.transactions List of transactions to sign + * @param options.callbackUrl URL to redirect upon success. Default: current url + * @param options.meta Meta information the wallet will send back to the application. `meta` will be attached to the `callbackUrl` as a url search param + * + */ + requestSignTransactions(options: RequestSignTransactionsOptions): void { + const url = this.requestSignTransactionsUrl(options); + + window.location.assign(url); } /** diff --git a/packages/wallet-account/test/wallet_account.test.js b/packages/wallet-account/test/wallet_account.test.js index a01a8b0c04..edce403c5c 100644 --- a/packages/wallet-account/test/wallet_account.test.js +++ b/packages/wallet-account/test/wallet_account.test.js @@ -109,6 +109,9 @@ describe('fails gracefully on the server side (without window)', () => { it('throws explicit error when calling other methods on the instance', () => { const serverWalletConnection = new WalletConnection(nearFake, ''); expect(() => serverWalletConnection.requestSignIn('signInContract', 'signInTitle', 'http://example.com/success', 'http://example.com/fail')).toThrow(/please ensure you are using WalletConnection on the browser/); + expect(() => serverWalletConnection.requestSignInUrl('signInContract', 'signInTitle', 'http://example.com/success', 'http://example.com/fail')).toThrow(/please ensure you are using WalletConnection on the browser/); + expect(() => serverWalletConnection.requestSignTransactions('signInContract', 'signInTitle', 'http://example.com/success', 'http://example.com/fail')).toThrow(/please ensure you are using WalletConnection on the browser/); + expect(() => serverWalletConnection.requestSignTransactionsUrl('signInContract', 'signInTitle', 'http://example.com/success', 'http://example.com/fail')).toThrow(/please ensure you are using WalletConnection on the browser/); }); it('can access other props on the instance', () => {