Aptos
OpenBlock DappSDK inject window.openblock.aptos into website。
Integration DappSDK
NPM
yarn add @openblockhq/dappsdk
CDN
// load dappsdk at document start to speed up the initialization process
// It is recommended to directly reference the following CDN link to facilitate instant SDK update. Developers are not recommended to download this JS file to their own server for use
<div>
<button id="signAndSubmitTransactionButton">Transaction</button>
</div>
<script type="text/javascript" charset="UTF-8" src="https://obstatic.243096.com/download/dapp/sdk/index.js">
</script>
<script type="text/javascript">
const provider = window.openblock.aptos;
const signAndSubmitTransactionButton = document.getElementById('signAndSubmitTransactionButton');
signAndSubmitTransactionButton.onclick = async () => {
const payload = {
type: 'entry_function_payload',
function: '0x1::managed_coin::register',
arguments: [],
type_arguments: [
'0xe4497a32bf4a9fd5601b27661aa0b933a923191bf403bd08669ab2468d43b379::move_coin::MoveCoin',
],
};
const result = await provider.signAndSubmitTransaction(payload);
console.log(result);
};
</script>
sdkLoaded
const sdkLoaded = window.openblock.sdkLoaded;
version
const version = window.openblock.version;
OpenBlock RPC API Methods
connect
response:
{
publicKey: string,// prefix 0x
address: string,
}
const provider = window.openblock.aptos;
const account = await provider.connect();
console.log(account);
account
response:
{
publicKey: string,// prefix 0x
address: string,
}
const provider = window.openblock.aptos;
const account = await provider.account();
console.log(account);
network
response:
{
api: string,// aptos node_url
name: string, // only support mainnet
chainId: number, // 1
}
const provider = window.openblock.aptos;
const network = await provider.network();
console.log(network);
disconnect
response: <boolean>
const provider = window.openblock.aptos;
const result = await provider.disconnect();
console.log(result);
isConnected
response: <boolean>
const provider = window.openblock.aptos;
const result = await provider.isConnected();
console.log(result);
signAndSubmitTransaction
request : Types.EntryFunctionPayload
response:
{
hash: string, //prefix 0x
}
const provider = window.openblock.aptos;
const payload = {
type: 'entry_function_payload',
function: '0x1::managed_coin::register',
arguments: [],
type_arguments: [
'0xe4497a32bf4a9fd5601b27661aa0b933a923191bf403bd08669ab2468d43b379::move_coin::MoveCoin',
],
};
const result = await provider.signAndSubmitTransaction(payload);
console.log(result);
signMessage
request : 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
}
response: 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 //prefix 0x
}
const provider = window.openblock.aptos;
const signMessagePayload = {
address: true,
application: true,
chainId: true,
message: 'hello world!',
nonce: 888,
};
const signMessageResponse = await provider.signMessage(signMessagePayload);
console.log(result);
onAccountChange
const handleAccountChange = async (account) => {
if (account === undefined) {
return;
}
/* account :
{ publicKey: string,// prefix 0x
address: string,
}
*/
};
await provider.onAccountChange(handleAccountChange);
onNetworkChange
const handleNetworkChange = (network) => {
/* network :
{
api: string,// aptos node_url
name: string, // only support mainnet
chainId: number, // 1
}
*/
};
await provider.onNetworkChange(handleNetworkChange);