Aptos
在 Bitget Wallet App 及安装 Chrome Extension 的 Chrome 浏览器中运行 DApp 时,可获得全局对象 window.bitkeep.aptos
并进行后续的 API 调用。
const provider = window.bitkeep.aptos;
const provider = window.bitkeep.aptos;
注入对象的属性和方法
connect
- () => Promise<{ publicKey: string; address: string; }>:连接方法,返回公钥与地址account
- () => Promise:获取账户信息isConnected
- () => Promise:获取连接状态network
- () => Promise:获取当前网络onAccountChange
- Function:账户变动的回调onNetworkChange
- Function:网络变化的回调onDisconnect
- Function:断开连接的回调signTransaction
- (transaction, options) => Promise:钱包签名方法,传入交易信息与附加信息signMessage
- (msg) => Promise:签署消息disconnect
- () => Promise:断开连接
链接钱包
返回值
result
- objectpublicKey
- stringaddress
- string
try {
const response = await provider.connect();
console.log(response); // { publicKey: string, address: string }
const account = await provider.account();
console.log(account); // { publicKey: string, address: string }
} catch (error) {
// { code: 4001, message: "User rejected the request."}
}
try {
const response = await provider.connect();
console.log(response); // { publicKey: string, address: string }
const account = await provider.account();
console.log(account); // { publicKey: string, address: string }
} catch (error) {
// { code: 4001, message: "User rejected the request."}
}
获取钱包地址
const account = await provider.account();
const account = await provider.account();
发送交易
当 DApp 成功链接 Bitget Wallet 后,可以发送交易到 Aptos 区块链。
Bitget WalletAPI 以 2 种方式处理交易。
- 签署交易并提交给 Aptos 区块链。向 DApp 返回一个待定交易。
- 签署一个交易,但不提交给 Aptos 区块链。将已签署的交易返回,并由 DApp 提交交易。
请看下面的例子来了解这两个选项。
签署和提交
下面的代码示例显示了如何使用 signAndSubmitTransaction()
API 来签署交易并将其发送到 Aptos 区块链上。
// Example Transaction, following an [EntryFunctionPayload](https://github.com/aptos-labs/aptos-core/blob/main/ecosystem/typescript/sdk/src/generated/models/EntryFunctionPayload.ts#L8-L21)
const transaction = {
arguments: [address, "717"],
function: "0x1::coin::transfer",
type: "entry_function_payload",
type_arguments: ["0x1::aptos_coin::TestCoin"],
};
/** 自定义 gas fee
* default {
"gas_unit_price":"100",
"max_gas_amount":"10000"
}
*/
const options = {
gas_unit_price: 100,
max_gas_amount: 10000,
};
try {
const pendingTransaction = await provider.signAndSubmitTransaction(
transaction
);
// const pendingTransaction = await provider.signAndSubmitTransaction(transaction, options);
// In most cases a dApp will want to wait for the transaction, in these cases you can use the typescript sdk
const client = new AptosClient("https://testnet.aptoslabs.com");
client.waitForTransaction(pendingTransaction.hash);
} catch (error) {
// see "Errors"
}
// Example Transaction, following an [EntryFunctionPayload](https://github.com/aptos-labs/aptos-core/blob/main/ecosystem/typescript/sdk/src/generated/models/EntryFunctionPayload.ts#L8-L21)
const transaction = {
arguments: [address, "717"],
function: "0x1::coin::transfer",
type: "entry_function_payload",
type_arguments: ["0x1::aptos_coin::TestCoin"],
};
/** 自定义 gas fee
* default {
"gas_unit_price":"100",
"max_gas_amount":"10000"
}
*/
const options = {
gas_unit_price: 100,
max_gas_amount: 10000,
};
try {
const pendingTransaction = await provider.signAndSubmitTransaction(
transaction
);
// const pendingTransaction = await provider.signAndSubmitTransaction(transaction, options);
// In most cases a dApp will want to wait for the transaction, in these cases you can use the typescript sdk
const client = new AptosClient("https://testnet.aptoslabs.com");
client.waitForTransaction(pendingTransaction.hash);
} catch (error) {
// see "Errors"
}
只签署
重要:不建议使用,对用户来说并非绝对安全。钱包也会发出额外警告。
下面的代码例子显示了如何使用 signTransaction()
API 来只签署交易,而不提交给 Aptos 区块链。
// Example Transaction
const transaction = {
arguments: [address, "717"],
function: "0x1::coin::transfer",
type: "entry_function_payload",
type_arguments: ["0x1::aptos_coin::TestCoin"],
};
/** 自定义 gas fee
* default {
"gas_unit_price":"100",
"max_gas_amount":"10000"
}
*/
const options = {
gas_unit_price: 100,
max_gas_amount: 10000,
};
try {
const signTransaction = await provider.signTransaction(transaction);
// const signTransaction = await provider.signTransaction(transaction, options)
} catch (error) {
// see "Errors"
}
// Example Transaction
const transaction = {
arguments: [address, "717"],
function: "0x1::coin::transfer",
type: "entry_function_payload",
type_arguments: ["0x1::aptos_coin::TestCoin"],
};
/** 自定义 gas fee
* default {
"gas_unit_price":"100",
"max_gas_amount":"10000"
}
*/
const options = {
gas_unit_price: 100,
max_gas_amount: 10000,
};
try {
const signTransaction = await provider.signTransaction(transaction);
// const signTransaction = await provider.signTransaction(transaction, options)
} catch (error) {
// see "Errors"
}
签署消息
DApp 通过使用 Bitget WalletAPI 要求用户签署信息:wallet.signMessage(payload: SignMessagePayload)
。
signMessage(payload: SignMessagePayload)
向用户提示要签名的 payload.message- 返回
Promise<SignMessageResponse>
。
类型:
export interface SignMessagePayload {
address?: boolean; // Should we include the address of the account in the message
application?: boolean; // Should we include the domain of the dapp
chainId?: boolean; // Should we include the current chain id the wallet is connected to
message: string; // The message to be signed and displayed to the user
nonce: string; // A nonce the dapp should generate
}
export interface SignMessageResponse {
address: string;
application: string;
chainId: number;
fullMessage: string; // The message that was generated to sign
message: string; // The message passed in by the user
nonce: string;
prefix: string; // Should always be APTOS
signature: string; // The signed full message
}
export interface SignMessagePayload {
address?: boolean; // Should we include the address of the account in the message
application?: boolean; // Should we include the domain of the dapp
chainId?: boolean; // Should we include the current chain id the wallet is connected to
message: string; // The message to be signed and displayed to the user
nonce: string; // A nonce the dapp should generate
}
export interface SignMessageResponse {
address: string;
application: string;
chainId: number;
fullMessage: string; // The message that was generated to sign
message: string; // The message passed in by the user
nonce: string;
prefix: string; // Should always be APTOS
signature: string; // The signed full message
}
签名消息
signMessage({nonce: 1234034, message: "Welcome to dapp!" })
将生成要签名的 fullMessage 并作为签名返回:
APTOS
nonce: 1234034
message: Welcome to dapp!
APTOS
nonce: 1234034
message: Welcome to dapp!
验证
签署信息是为了验证私有资源的所有权。
import nacl from 'tweetnacl';
const message = "hello";
const nonce = "random_string"
try {
const response = await provider.signMessage({
message,
nonce,
});
const { publicKey } = await provider.account();
// Remove the 0x prefix
const key = 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';
const message = "hello";
const nonce = "random_string"
try {
const response = await provider.signMessage({
message,
nonce,
});
const { publicKey } = await provider.account();
// Remove the 0x prefix
const key = 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);
}
事件监听
网络支持
We support network: Mainnet
| Devnet
正式 Mainnet
已经上线, Testnet
已经修改成 Mainnet
Bitget Wallet 提供的默认网络:
// default networks in the wallet
enum Network {
Mainnet = 'Mainnet'
Devnet = 'Devnet'
}
// default networks in the wallet
enum Network {
Mainnet = 'Mainnet'
Devnet = 'Devnet'
}
onNetworkChange() 和 network()
DApp 需要确保用户链接的是目标网络,因此需要获取当前网络、切换网络以及网络切换监听。
// Current network
let network = await provider.network();
// event listener for network changing
provider.onNetworkChange((newNetwork) => {
network = newNetwork; // { networkName: 'Mainnet' }
});
// Current network
let network = await provider.network();
// event listener for network changing
provider.onNetworkChange((newNetwork) => {
network = newNetwork; // { networkName: 'Mainnet' }
});
onAccountChange()
用户与 DApp 互动时可能会改变账户,可通过 onAccountChange 监听变更结果。
// get current account
let currentAccount = await provider.account();
// event listener for disconnecting
provider.onAccountChange((newAccount) => {
// If the new account has already connected to your app then the newAccount will be returned
if (newAccount) {
currentAccount = newAccount;
} else {
// Otherwise you will need to ask to connect to the new account
currentAccount = provider.connect();
}
});
// get current account
let currentAccount = await provider.account();
// event listener for disconnecting
provider.onAccountChange((newAccount) => {
// If the new account has already connected to your app then the newAccount will be returned
if (newAccount) {
currentAccount = newAccount;
} else {
// Otherwise you will need to ask to connect to the new account
currentAccount = provider.connect();
}
});