From 78a7309d18eb4f881fa41f1de7c86ea7cb24b0a7 Mon Sep 17 00:00:00 2001 From: steveluscher Date: Wed, 27 Jul 2022 10:15:17 -0700 Subject: [PATCH] feat: the `signMessages` API now takes an `address` param --- .../src/transact.ts | 3 +- .../src/types.ts | 1 + .../wallet-adapter-mobile/src/adapter.ts | 38 +++++++++++-------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/js/packages/mobile-wallet-adapter-protocol-web3js/src/transact.ts b/js/packages/mobile-wallet-adapter-protocol-web3js/src/transact.ts index e49ff63f7..823e5f710 100644 --- a/js/packages/mobile-wallet-adapter-protocol-web3js/src/transact.ts +++ b/js/packages/mobile-wallet-adapter-protocol-web3js/src/transact.ts @@ -1,6 +1,7 @@ import { Connection, PublicKey, Transaction, TransactionSignature } from '@solana/web3.js'; import { AuthorizeAPI, + Base64EncodedAddress, CloneAuthorizationAPI, DeauthorizeAPI, Finality, @@ -26,7 +27,7 @@ interface Web3SignTransactionsAPI { } interface Web3SignMessagesAPI { - signMessages(params: { payloads: Uint8Array[] }): Promise; + signMessages(params: { address: Base64EncodedAddress; payloads: Uint8Array[] }): Promise; } export interface Web3MobileWallet diff --git a/js/packages/mobile-wallet-adapter-protocol/src/types.ts b/js/packages/mobile-wallet-adapter-protocol/src/types.ts index 979b33394..52e060b73 100644 --- a/js/packages/mobile-wallet-adapter-protocol/src/types.ts +++ b/js/packages/mobile-wallet-adapter-protocol/src/types.ts @@ -63,6 +63,7 @@ export interface ReauthorizeAPI { } export interface SignMessagesAPI { signMessages(params: { + address: Base64EncodedAddress; payloads: Base64EncodedMessage[]; }): Promise>; } diff --git a/js/packages/wallet-adapter-mobile/src/adapter.ts b/js/packages/wallet-adapter-mobile/src/adapter.ts index 9bec52a62..992189cc1 100644 --- a/js/packages/wallet-adapter-mobile/src/adapter.ts +++ b/js/packages/wallet-adapter-mobile/src/adapter.ts @@ -1,5 +1,10 @@ import { Web3MobileWallet, transact } from '@solana-mobile/mobile-wallet-adapter-protocol-web3js'; -import { AppIdentity, AuthorizationResult, Base64EncodedAddress } from '@solana-mobile/mobile-wallet-adapter-protocol'; +import { + AppIdentity, + AuthorizationResult, + AuthToken, + Base64EncodedAddress, +} from '@solana-mobile/mobile-wallet-adapter-protocol'; import { BaseMessageSignerWalletAdapter, WalletConnectionError, @@ -171,13 +176,10 @@ export class SolanaMobileWalletAdapter extends BaseMessageSignerWalletAdapter { await this._authorizationResultCache.set(authorizationResult); } - private async performReauthorization( - wallet: Web3MobileWallet, - currentAuthorizationResult: AuthorizationResult, - ): Promise { + private async performReauthorization(wallet: Web3MobileWallet, authToken: AuthToken): Promise { try { const authorizationResult = await wallet.reauthorize({ - auth_token: currentAuthorizationResult.auth_token, + auth_token: authToken, }); this.handleAuthorizationResult(authorizationResult); // TODO: Evaluate whether there's any threat to not `awaiting` this expression } catch (e) { @@ -190,6 +192,7 @@ export class SolanaMobileWalletAdapter extends BaseMessageSignerWalletAdapter { this._authorizationResultCache.clear(); // TODO: Evaluate whether there's any threat to not `awaiting` this expression delete this._authorizationResult; delete this._publicKey; + delete this._selectedAddress; this.emit('disconnect'); } @@ -199,17 +202,19 @@ export class SolanaMobileWalletAdapter extends BaseMessageSignerWalletAdapter { return await transact(callback, config); } - private assertIsAuthorized(): AuthorizationResult { - const authorizationResult = this._authorizationResult; - if (!authorizationResult) throw new WalletNotConnectedError(); - return authorizationResult; + private assertIsAuthorized() { + if (!this._authorizationResult || !this._selectedAddress) throw new WalletNotConnectedError(); + return { + authToken: this._authorizationResult.auth_token, + selectedAddress: this._selectedAddress, + }; } private async performSignTransactions(transactions: Transaction[]): Promise { - const authorizationResult = this.assertIsAuthorized(); + const { authToken } = this.assertIsAuthorized(); try { return await this.transact(async (wallet) => { - await this.performReauthorization(wallet, authorizationResult); + await this.performReauthorization(wallet, authToken); const signedTransactions = await wallet.signTransactions({ transactions, }); @@ -226,10 +231,10 @@ export class SolanaMobileWalletAdapter extends BaseMessageSignerWalletAdapter { _options?: SendOptions, ): Promise { return await this.runWithGuard(async () => { - const authorizationResult = this.assertIsAuthorized(); + const { authToken } = this.assertIsAuthorized(); try { return await this.transact(async (wallet) => { - await this.performReauthorization(wallet, authorizationResult); + await this.performReauthorization(wallet, authToken); const signatures = await wallet.signAndSendTransactions({ connection, fee_payer: transaction.feePayer, @@ -259,11 +264,12 @@ export class SolanaMobileWalletAdapter extends BaseMessageSignerWalletAdapter { async signMessage(message: Uint8Array): Promise { return await this.runWithGuard(async () => { - const authorizationResult = this.assertIsAuthorized(); + const { authToken, selectedAddress } = this.assertIsAuthorized(); try { return await this.transact(async (wallet) => { - await this.performReauthorization(wallet, authorizationResult); + await this.performReauthorization(wallet, authToken); const [signedMessage] = await wallet.signMessages({ + address: selectedAddress, payloads: [message], }); const signature = signedMessage.slice(-SIGNATURE_LENGTH_IN_BYTES);