aelf-bridge
English | 中文
Table of Contents
Introduction
In order to provide dApps with the ability to interact with the chain and to protect wallet information, aelf-bridge can be used for interacting with the wallet. The wallet application stores AElf wallet information and can communicate directly with the AElf chain.
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 the dApp and the chain need to go through the keeper of wallet info, this keeper could be any clients which has implemented the AElf bridge protocol, for now(2019.12), AElf mobile wallet App has implemented this protocol.
Since dApps are mostly web applications, and web applications can communicate with clients in many ways, this SDK chooses 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 dApp, so dap and container can communicate with each other by overwritten postMessage
. - 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.
Developers can choose one of them depend on requirements, in the process of development, we provide two ways to support data mock and debug:
- aelf-bridge-demo: This demo used
iframe
to overwrite dapp.html
's postMessage
to simulate communication with mobile App; - 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 = {
"code": 0,
"msg": "success",
"errors": [],
"data": {
"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"
}
]
}
}
Call contract method (read-only and send transaction)
- Send transaction
bridgeInstance.invoke(params)
- Contract read-only method
bridgeInstance.invokeRead(params)
The two parameters are similar:
params
:
argument = {
name: String,
value: Boolean | String | Object | '...'
}
params = {
endpoint: String,
contractAddress: String,
contractMethod: String,
arguments: argument[]
}
Example:
- Call the
Transfer
method of the Token
contract to initiate a transfer transaction
bridgeInstance.invoke({
contractAddress: 'mS8xMLs9SuWdNECkrfQPF8SuRXRuQzitpjzghi3en39C3SRvf',
contractMethod: 'Transfer',
arguments: [
{
name: "transfer",
value: {
amount: "10000000000",
to: "fasatqawag",
symbol: "ELF",
memo: "transfer ELF"
}
}
]
}).then(console.log);
- Call the
GetNativeTokenInfo
method of the Token
contract to get the native token information:
bridge.invokeRead({
contractAddress: 'mS8xMLs9SuWdNECkrfQPF8SuRXRuQzitpjzghi3en39C3SRvf',
contractMethod: 'GetNativeTokenInfo',
arguments: []
}).then(setResult).catch(setResult);
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()
.
bridgeInstance.api(params)
The params
parameters are as follows:
argument = {
name: String,
value: Boolean | String | Object | '...'
}
params = {
endpoint: String,
apiPath: String,
arguments: argument[]
}
Example:
bridgeInstance.api({
apiPath: '/api/blockChain/blockHeight',
arguments: []
}).then(console.log).catch(console.log)
disconnect
Used to disconnect from the client and clearing the public key information, etc.
bridgeInstance.disconnect()