Skip to content

Aptos-AIP-62

AIP-62 标准是 Aptos 官方的新标准。是一组与链无关的接口和约定,旨在改进应用程序与注入钱包的交互方式。它使用 Wallet Standard 方式嗅探已安装的钱包从而自动显示可连接钱包列表。AIP-62 还升级了 DApp 与钱包之间交互的数据接口,如已封装好的 RawTransaction 等。

TIP

在不久的将来,随着钱包采用新标准,钱包适配器将弃用旧标准并仅保留 AIP-62 钱包标准支持。

在 Bitget Wallet App 及安装 Chrome Extension 的 Chrome 浏览器中运行 DApp 时,App 可通过包裹AptosWalletAdapterProvider实现钱包注入。

js
import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react";

<AptosWalletAdapterProvider autoConnect>
  <App />
</AptosWalletAdapterProvider>;
import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react";

<AptosWalletAdapterProvider autoConnect>
  <App />
</AptosWalletAdapterProvider>;

获取钱包列表

DApp 可直接使用 hooks 获取已通过 registerWallet 注册的钱包列表:

js
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { wallets } = useWallet(); // [{name: 'Bitget Wallet', icon: '...'}]
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { wallets } = useWallet(); // [{name: 'Bitget Wallet', icon: '...'}]

连接钱包

给 connect 方法传入 钱包名来连接指定钱包:

参数

  • name - string:钱包名
js
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { connect } = useWallet(); // [{name: 'Bitget Wallet', icon: '...'}]

await connect("Bitget Wallet");
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { connect } = useWallet(); // [{name: 'Bitget Wallet', icon: '...'}]

await connect("Bitget Wallet");

获取钱包地址

js
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { account } = useWallet(); // {address: '...', publicKey: '...'}
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { account } = useWallet(); // {address: '...', publicKey: '...'}

获取网络

js
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { network } = useWallet();
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { network } = useWallet();

消息签名

参数

  • options
    • message - string
    • nonce - string
js
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { signMessage } = useWallet();

const res = await signMessage({
  message: "The message to be signed and displayed to the user",
  nonce: "1",
});
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { signMessage } = useWallet();

const res = await signMessage({
  message: "The message to be signed and displayed to the user",
  nonce: "1",
});

签名交易

js
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { signTransaction } = useWallet();

const transaction = {
  arguments: ["100000", "330679"],
  function:
    "0xc7efb4076dbe143cbcd98cfaaa929ecfc8f299203dfff63b95ccb6bfe19850fa::router::swap_exact_input",
  type: "entry_function_payload",
  type_arguments: [
    "0x1::aptos_coin::AptosCoin",
    "0x159df6b7689437016108a019fd5bef736bac692b6d4a1f10c941f6fbb9a74ca6::oft::CakeOFT",
  ],
};
try {
  const res = await signTransaction(transaction);
} catch (error) {
  // see "Errors"
}
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { signTransaction } = useWallet();

const transaction = {
  arguments: ["100000", "330679"],
  function:
    "0xc7efb4076dbe143cbcd98cfaaa929ecfc8f299203dfff63b95ccb6bfe19850fa::router::swap_exact_input",
  type: "entry_function_payload",
  type_arguments: [
    "0x1::aptos_coin::AptosCoin",
    "0x159df6b7689437016108a019fd5bef736bac692b6d4a1f10c941f6fbb9a74ca6::oft::CakeOFT",
  ],
};
try {
  const res = await signTransaction(transaction);
} catch (error) {
  // see "Errors"
}

签署和提交

js
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { signAndSubmitTransaction } = useWallet();

try {
  const pendingTransaction = await signAndSubmitTransaction({
    data: transaction,
  });
} catch (error) {
  // see "Errors"
}
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { signAndSubmitTransaction } = useWallet();

try {
  const pendingTransaction = await signAndSubmitTransaction({
    data: transaction,
  });
} catch (error) {
  // see "Errors"
}

验证

签署信息是为了验证私有资源的所有权。

js
import nacl from 'tweetnacl';
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { signMessage, account } = useWallet();

const message = "hello";
const nonce = "random_string"

try {
  const response = await signMessage({
    message,
    nonce,
  });
  // Remove the 0x prefix
  const key = account.publicKey!.slice(2, 66);
  const verified = nacl.sign.detached.verify(Buffer.from(response.fullMessage),
                                             Buffer.from(response.signature, 'hex'),
                                             Buffer.from(key, 'hex'));
  console.log(verified);
} catch (error) {
  console.error(error);
}
import nacl from 'tweetnacl';
import { useWallet } from "@aptos-labs/wallet-adapter-react";

const { signMessage, account } = useWallet();

const message = "hello";
const nonce = "random_string"

try {
  const response = await signMessage({
    message,
    nonce,
  });
  // Remove the 0x prefix
  const key = account.publicKey!.slice(2, 66);
  const verified = nacl.sign.detached.verify(Buffer.from(response.fullMessage),
                                             Buffer.from(response.signature, 'hex'),
                                             Buffer.from(key, 'hex'));
  console.log(verified);
} catch (error) {
  console.error(error);
}