Skip to content

Commit

Permalink
feat: the signMessages API now takes an address param
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher committed Jul 28, 2022
1 parent 93edf47 commit 78a7309
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Connection, PublicKey, Transaction, TransactionSignature } from '@solana/web3.js';
import {
AuthorizeAPI,
Base64EncodedAddress,
CloneAuthorizationAPI,
DeauthorizeAPI,
Finality,
Expand All @@ -26,7 +27,7 @@ interface Web3SignTransactionsAPI {
}

interface Web3SignMessagesAPI {
signMessages(params: { payloads: Uint8Array[] }): Promise<Uint8Array[]>;
signMessages(params: { address: Base64EncodedAddress; payloads: Uint8Array[] }): Promise<Uint8Array[]>;
}

export interface Web3MobileWallet
Expand Down
1 change: 1 addition & 0 deletions js/packages/mobile-wallet-adapter-protocol/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface ReauthorizeAPI {
}
export interface SignMessagesAPI {
signMessages(params: {
address: Base64EncodedAddress;
payloads: Base64EncodedMessage[];
}): Promise<Readonly<{ signed_payloads: Base64EncodedSignedMessage[] }>>;
}
Expand Down
38 changes: 22 additions & 16 deletions js/packages/wallet-adapter-mobile/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -171,13 +176,10 @@ export class SolanaMobileWalletAdapter extends BaseMessageSignerWalletAdapter {
await this._authorizationResultCache.set(authorizationResult);
}

private async performReauthorization(
wallet: Web3MobileWallet,
currentAuthorizationResult: AuthorizationResult,
): Promise<void> {
private async performReauthorization(wallet: Web3MobileWallet, authToken: AuthToken): Promise<void> {
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) {
Expand All @@ -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');
}

Expand All @@ -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<Transaction[]> {
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,
});
Expand All @@ -226,10 +231,10 @@ export class SolanaMobileWalletAdapter extends BaseMessageSignerWalletAdapter {
_options?: SendOptions,
): Promise<TransactionSignature> {
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,
Expand Down Expand Up @@ -259,11 +264,12 @@ export class SolanaMobileWalletAdapter extends BaseMessageSignerWalletAdapter {

async signMessage(message: Uint8Array): Promise<Uint8Array> {
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);
Expand Down

0 comments on commit 78a7309

Please sign in to comment.