Skip to content

Commit

Permalink
Merge pull request #1260 from denbite/wallet_sign_tx_url
Browse files Browse the repository at this point in the history
Introduce methods to construct URLs to the wallet without instant redirect
  • Loading branch information
vikinatora authored Jan 23, 2024
2 parents ab4f8d3 + b78a324 commit 8a94f69
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
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": minor
---

Introduce methods to construct URLs to the wallet without instant redirect
59 changes: 48 additions & 11 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,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<void> {
requestSignTransactionsUrl({ transactions, meta, callbackUrl }: RequestSignTransactionsOptions): string {
const currentUrl = new URL(window.location.href);
const newUrl = new URL('sign', this._walletBaseUrl);

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

/**
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

0 comments on commit 8a94f69

Please sign in to comment.