Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce methods to construct URLs to the wallet without instant redirect #1260

Merged
merged 7 commits into from
Jan 23, 2024
Merged
5 changes: 5 additions & 0 deletions .changeset/twelve-rabbits-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@near-js/wallet-account": patch
denbite marked this conversation as resolved.
Show resolved Hide resolved
---

Introduce methods to construct URLs to the wallet without instant redirect
57 changes: 49 additions & 8 deletions packages/wallet-account/src/wallet_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<string> {
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);
Expand All @@ -202,13 +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.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<void> {
requestSignTransactionsUrl({ transactions, meta, callbackUrl }: RequestSignTransactionsOptions): string {
const currentUrl = new URL(window.location.href);
const newUrl = new URL('sign', this._walletBaseUrl);

Expand All @@ -219,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);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/wallet-account/test/wallet_account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down