aelf-bridge
English | 中文
Branch | Tests | Coverage |
---|
feature/badge | | |
Descriptions
Table of Contents
Introduction
Since dApps are not allowed to store any wallet information, the wallet application stores AElf wallet information and can communicate directly with the AElf chain. In order to protect wallet information and provide dApps with the ability to interact with the chain, aelf-bridge can be used for interacting with the wallet.
The wallet App described here includes a mobile (iOS/Android) native app, desktop app and more.
Installation
AElf-bridge is part of the AElf ecosystem. Since dApps are mostly web applications, we provide a JavaScript
SDK available using Npm
or yarn
as a version management tool or directly with the script tag.
Using version management tools
npm i aelf-bridge
// or
yarn add aelf-bridge
Using the script tag
<script src="https://unpkg.com/aelf-bridge@latest/dist/aelf-bridge.js"></script>
Usage
Introduction
The communications between a dApp and the chain need to go through some wallet software. This wallet software could be any client that has implemented the AElf bridge protocol. at the time of writing (2020.06), AElf mobile wallet App and aelf-command
have implemented this protocol.
Since dApps are mostly web applications and web applications can communicate with clients in many ways, this SDK supports two of them:
- postMessage: a dApp will run in a container (
iframe
or mobile Apps' webview
), and the container needs to overwrite window.postMessage
method in the dApp, so the dApp and the container can communicate with each other by overwritten postMessage
. AElf mobile wallet App has implemented this. - WebSocket(Socket.io): use traditional B/S architecture, communicate by
WebSocket
. SDK uses Socket.io
to support WebSocket
communication, and this requires servers need to support Socket.io
too. aelf-command
has implemented this way.
Developers can choose one of them depending on requirements, in the process of development, we provide two ways to support data mock and debugging:
- aelf-bridge-demo: this demo uses
iframe
to overwrite dapp.html
's postMessage
to simulate communication with mobile Apps. - aelf-command dapp-server:
aelf-command
provides a simple socket.io
server to support the communication method socket.io
in aelf-bridge
, developers can change the communication way to SOCKET.IO
, and give the URI given by running aelf-command dapp-server
as an option when initializing aelf-bridge
instance. Therefore, developers can inspect the communications in the Network tab of browser.
Initialization
import AElfBridge from 'aelf-bridge';
const bridgeInstance = new AElfBridge();
const bridgeInstance = new AElfBridge({
timeout: 5000
});
bridgeInstance.connect().then(isConnected => {
})
Options
The options can be passed as follows:
const defaultOptions = {
proxyType: String,
channelType: String,
timeout: Number,
appId: String,
endpoint: String,
origin: String,
checkoutTimeout: Number,
urlPrefix: String,
socketUrl: String,
socketPath: String,
messageType: String
}
Get wallet account information
bridgeInstance.account()
bridgeInstance.account().then(res => {
console.log(res);
})
res = {
"accounts": [
{
"name": "test",
"address": "XxajQQtYxnsgQp92oiSeENao9XkmqbEitDD8CJKfDctvAQmH6"
}
],
"chains": [
{
"url": "http://13.231.179.27:8000",
"isMainChain": true,
"chainId": "AELF"
},
{
"url": "http://52.68.97.242:8000",
"isMainChain": false,
"chainId": "2112"
},
{
"url": "http://52.196.227.200:8000",
"isMainChain": false,
"chainId": "2113"
}
]
}
Calling the chain API
API for interacting with the node. The API available methods can be viewed by {chain address}/swagger/index.html
, to get the currently supported APIs you can call AElfBridge.getChainApis()
.
Developer can use the methods of bridgeInstance.chain
to call the api.
Example:
bridgeInstance.chain.getBlockHeight().then(console.log).catch(console.log)
bridgeInstance.chain.getChainStatus().then(console.log).catch(console.log)
Call contract method (read-only and send transaction)
- Get contract method list
- Send transaction
- Contract read-only method
Example:
- Call the
Transfer
method of the Token
contract to initiate a transfer transaction
const tokenAddress = 'mS8xMLs9SuWdNECkrfQPF8SuRXRuQzitpjzghi3en39C3SRvf';
bridgeInstance.chain.contractAt(tokenAddress).then(async contract => {
const tokenInfo = await contract.GetTokenInfo.call({symbol: 'ELF'});
const transactionId = await contract.Transfer({
amount: "10000000000",
to: "fasatqawag",
symbol: "ELF",
memo: "transfer ELF"
});
console.log(tokenInfo);
console.log(transactionId);
})
disconnect
Used to disconnect from the client and clearing the public key information, etc.
bridgeInstance.disconnect()