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

Refactor the account info output type to be compatible with AIP-62 standard #441

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

0xmaayan
Copy link
Collaborator

To support the different Public Key types and for developers to easily be able to pass the account info down to SDK functions, changing the Account Info return type to be compatible with AIP-62

An updated version with this change here: https://aptos-labs.github.io/aptos-wallet-adapter/nextjs-example-testing/

Will be released with a major version update

OLD

export type AccountInfo = {
  address: string;
  publicKey: string | string[];
  minKeysRequired?: number;
  ansName?: string | null;
};

NEW

export type AccountInfoOutput = {
  address: AccountAddress;
  publicKey: PublicKey | PublicKey[]; // PublicKey[] for backward compatibility
  minKeysRequired?: number;
  ansName?: string | null;
};

This way, one can do

  const signWithSDK = async () => {
    if (!account) return;

    // make sure it is not an array of public keys
    if (Array.isArray(account.publicKey)) return;

    const transaction = await aptosClient(network).transaction.build.simple({
      sender: account.address,
      data: {
        function: "0x1::coin::transfer",
        typeArguments: [APTOS_COIN],
        functionArguments: [account.address.toString(), 1], // 1 is in Octas
      },
    });
    const response = await aptosClient(network).transaction.simulate.simple({
      signerPublicKey: account.publicKey,
      transaction,
    });
  };

@0xmaayan 0xmaayan changed the title Refactor the account info output type to be compatible with API-62 standard Refactor the account info output type to be compatible with AIP-62 standard Oct 30, 2024
packages/wallet-adapter-core/src/WalletCore.ts Outdated Show resolved Hide resolved
@@ -74,7 +79,7 @@ export declare interface WalletCoreEvents {
readyStateChange(wallet: Wallet): void;
standardWalletsAdded(wallets: Wallet | AptosStandardSupportedWallet): void;
networkChange(network: NetworkInfo | null): void;
accountChange(account: AccountInfo | null): void;
accountChange(account: AccountInfo | StandardAccountInfoInput | null): void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it might be better to just share the AccountInfoOutput instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tricky as the adapter still supports legacy and new standard types, so the account the adapter gets back from the wallet can be of both types. Changing it gets us into a type-rabbit-hole, so we simply change the actual input type the adapter returns.

In the near future, we should probably support only AIP-62 wallets

Copy link
Contributor

@hardsetting hardsetting Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use AccountInfo from @aptos-labs/wallet-standard here, that uses the PublicKey type.

If I understand correctly, this is an event that can be listened to externally by the dapp.
Since we're bumping the major, we might as well clean up the interface and have the "right" type here.

In case of legacy plugins, we should be able to adapt the return types to AccountInfo so that the dapp sees one consistent return type.

EDIT: here's a good spot where we could normalize

Copy link
Contributor

@hardsetting hardsetting left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this change!
As per my comments, I think we should try using AccountInfo from the standard :)

@@ -74,7 +79,7 @@ export declare interface WalletCoreEvents {
readyStateChange(wallet: Wallet): void;
standardWalletsAdded(wallets: Wallet | AptosStandardSupportedWallet): void;
networkChange(network: NetworkInfo | null): void;
accountChange(account: AccountInfo | null): void;
accountChange(account: AccountInfo | StandardAccountInfoInput | null): void;
Copy link
Contributor

@hardsetting hardsetting Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use AccountInfo from @aptos-labs/wallet-standard here, that uses the PublicKey type.

If I understand correctly, this is an event that can be listened to externally by the dapp.
Since we're bumping the major, we might as well clean up the interface and have the "right" type here.

In case of legacy plugins, we should be able to adapt the return types to AccountInfo so that the dapp sees one consistent return type.

EDIT: here's a good spot where we could normalize

@@ -131,7 +131,7 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
private _wallet: Wallet | null = null;

// Current connected account
private _account: AccountInfo | null = null;
private _account: AccountInfo | StandardAccountInfoInput | null = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, let's just use AccountInfo from the standard. We can convert it to the right type before setting its value

@@ -465,7 +465,7 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
* @param account An account
*/
private ensureAccountExists(
account: AccountInfo | null
account: AccountInfo | StandardAccountInfoInput | null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Comment on lines +548 to 550
setAccount(account: AccountInfo | StandardAccountInfo | null): void {
this._account = account;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a good spot where we support both AccountInfos (the old internal type, and the standard type) as inputs, and normalize it to the standard one.

@@ -687,9 +642,30 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
* @return account info
* @throws WalletAccountError
*/
get account(): AccountInfo | null {
get account(): AccountInfoOutput | null {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just return AccountInfo from the wallet standard package

Comment on lines +657 to +662
: // for backward compatibility, if the account public key is of type `string[]` convert each string to Ed25519PublicKey
Array.isArray(this._account.publicKey) &&
this._account.publicKey.every(
(item) => typeof item === "string"
)
? this._account.publicKey.map((key) => new Ed25519PublicKey(key))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should convert string[] and minKeysRequired to MultiEd25519PublicKey that extends PublicKey

@@ -147,7 +150,7 @@ function WalletSelection() {
}

interface WalletConnectionProps {
account: AccountInfo | null;
account: AccountInfoOutput | null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use AccountInfo from the standard

@@ -44,9 +45,9 @@ export function isInstallRequired(wallet: AnyAptosWallet) {
}

/** Truncates the provided wallet address at the middle with an ellipsis. */
export function truncateAddress(address: string | undefined) {
export function truncateAddress(address: AccountAddress | undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit: we could use AccountAddressInput, but nbd

@hardsetting
Copy link
Contributor

hardsetting commented Oct 31, 2024

By quickly looking at the code, I think we could do this:

  • define a normalizeAccountInfo that normalizes any AccountInfo to the standard's one. Here you would put the code that converts string to Ed25519PublicKey and (string[], minKeysRequired) to MultiEd25519PublicKey
  • when calling connectWallet here, we normalize the account info before calling setAccount
  • when we receive an "accountChange" event here, we normalize the account info before setAccount and emit
  • setAccount should only take the standard's AccountInfo as input

basically.. let's normalize as soon as we have a chance to make our life easier :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants