{"id":26562912,"url":"https://github.com/aelfproject/aelf-js-bridge","last_synced_at":"2025-03-22T15:29:14.836Z","repository":{"id":35187486,"uuid":"216754412","full_name":"AElfProject/aelf-js-bridge","owner":"AElfProject","description":"A library to interact with native apps","archived":false,"fork":false,"pushed_at":"2023-06-30T08:58:13.000Z","size":2420,"stargazers_count":4,"open_issues_count":18,"forks_count":7,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-24T23:41:05.413Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AElfProject.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-22T07:50:49.000Z","updated_at":"2024-06-19T00:19:54.143Z","dependencies_parsed_at":"2024-06-19T00:19:30.117Z","dependency_job_id":"c4c94880-02b0-4ca0-bc76-1b1d3131cee5","html_url":"https://github.com/AElfProject/aelf-js-bridge","commit_stats":null,"previous_names":["aelfproject/aelf-bridge"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AElfProject%2Faelf-js-bridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AElfProject%2Faelf-js-bridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AElfProject%2Faelf-js-bridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AElfProject%2Faelf-js-bridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AElfProject","download_url":"https://codeload.github.com/AElfProject/aelf-js-bridge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244977778,"owners_count":20541732,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-22T15:29:14.326Z","updated_at":"2025-03-22T15:29:14.829Z","avatar_url":"https://github.com/AElfProject.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# aelf-bridge\n\nEnglish | [中文](./README.zh-CN.md)\n\n\u003cp\u003e\n  \u003ca href=\"https://nodejs.org/download/\"\u003e\n    \u003cimg alt=\"Node version\" src=\"https://img.shields.io/node/v/aelf-bridge.svg\"\u003e\n  \u003c/a\u003e\n  \u003cimg alt=\"NPM\" src=\"https://img.shields.io/npm/l/aelf-bridge\"\u003e\n  \u003ca href=\"http://commitizen.github.io/cz-cli/\"\u003e\u003cimg alt=\"Commitizen friendly\" src=\"https://img.shields.io/badge/commitizen-friendly-brightgreen.svg\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/AElfProject/aelf-js-bridge/actions/workflows/publish.yml\"\u003e\n    \u003cimg alt=\"coverage\" src=\"https://github.com/AElfProject/aelf-js-bridge/actions/workflows/publish.yml/badge.svg\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n | Branch       | Tests          | Coverage       |\n|--------------|-----------------|----------------|\n| `master` | ![Tests](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/AElfProject/aelf-js-bridge/feature/badge-json/master-test-results.json) | ![Coverage](https://AElfProject.github.io/aelf-js-bridge/badges.svg) |\n\n\n\n\n## Descriptions\n\n## Table of Contents\n  - [Introduction](#introduction)\n  - [Installation](#installation)\n    - [Using version management tools](#using-version-management-tools)\n    - [Using the script tag](#using-the-script-tag)\n  - [Usage](#usage)\n    - [Introduction](#introduction)\n    - [Initialization](#initialization)\n      - [Options](#options)\n    - [Get wallet account information](#get-wallet-account-information)\n    - [Call contract method (read-only and send transaction)](#call-contract-method-read-only-and-send-transaction)\n    - [Calling the chain API](#calling-the-chain-api)\n    - [disconnect](#disconnect)\n\n## Introduction\n\nSince 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.\n\nThe wallet App described here includes a mobile (iOS/Android) native app, desktop app and more.\n\n## Installation\n\nAElf-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.\n\n### Using version management tools\n\n```bash\nnpm i aelf-bridge\n// or\nyarn add aelf-bridge\n```\n\n### Using the script tag\n\n```html\n\u003cscript src=\"https://unpkg.com/aelf-bridge@latest/dist/aelf-bridge.js\"\u003e\u003c/script\u003e\n```\n\n## Usage\n\n### Introduction\n\nThe 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.\n\nSince dApps are mostly web applications and web applications can communicate with clients in many ways, this SDK supports two of them:\n\n* 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.\n* 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.\n\nDevelopers can choose one of them depending on requirements, in the process of development, we provide two ways to support data mock and debugging:\n\n* [aelf-bridge-demo](https://github.com/AElfProject/aelf-bridge-demo): this demo uses `iframe` to overwrite `dapp.html`'s `postMessage` to simulate communication with mobile Apps.\n* [aelf-command dapp-server](https://github.com/AElfProject/aelf-command): `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.\n\n### Initialization\n\n```javascript\nimport AElfBridge from 'aelf-bridge';\n\n// Initialize the bridge instance, you can pass options during initialization to specify the behavior, see below for explanation\nconst bridgeInstance = new AElfBridge();\n// init with options\nconst bridgeInstance = new AElfBridge({\n  timeout: 5000 // ms\n});\n\n// After initializing the instance you need connect\nbridgeInstance.connect().then(isConnected =\u003e {\n  // isConnected True if the connection was successful.\n})\n```\n\n#### Options\n\nThe options can be passed as follows:\n\n```javascript\nconst defaultOptions = {\n  proxyType: String, // The default is `POST_MESSAGE`. Currently, we support the `POST_MESSAGE` and `SOCKET.IO` proxy types are provided. The `Websocket` mechanism will be provided in the future. Valid values ​​are available via `AElfBridge.getProxies()`.\n  channelType: String, // The default is `SIGN`, it is the serialization of the request and response, that is, Dapp exchanges the public and private keys with the client and the private key is used to verify the signature information, thereby verifying whether the information has been tampered with. Another method of symmetric encryption is provided. The parameter value is `ENCRYPT`, and the shared public key is used for symmetric encryption. The valid value of the parameter is obtained by `AElfBridge.getChannels()`.\n  timeout: Number, // Request timeout, defaults to 3000 milliseconds\n  appId: String, // The default is empty. Dapp does not specify if there is no special requirement. If you need to specify it, you need to randomly generate a 32-bit hex-coded id each time. A credential used to communicate with the client, specifying the Dapp ID. If it is not specified, the library will process it internally. The first run will generate a random 32-bit hex-encoded uuid. After the connection is successful, it will be stored in `localStorage`, then the value will be taken from `localStorage`. If not, then Generate a random id.\n  endpoint: String, // The default is empty. If the address of the node is empty, the client uses the internally saved primary link address by default, and can also specify to send a request to a specific node.\n  // Optional options in `POST_MESSAGE` communication mode\n  origin: String, // The default is `*`, the second parameter of the `postMessage` function, in most cases you do not need to specify\n  checkoutTimeout: Number, // The default is `200`, in milliseconds, it checks the client's injected `postMessage`. In most cases, you don't need to specify this\n  urlPrefix: String, // The default is `aelf://aelf.io?params=`, which is used to specify the protocol and prefix of the node. If the client does not have special requirements, it does not need to be changed.\n  // Optional options in `socket.io` communication mode.\n  socketUrl: String, // The address of the websocket connection, the default is `http://localhost:50845`\n  socketPath: String, // Path to the connection address, the default is empty\n  messageType: String // Pass the type of the socket.io message, the default is `bridge`\n}\n```\n\n### Get wallet account information\n\n`bridgeInstance.account()`\n\n```javascript\nbridgeInstance.account().then(res =\u003e {\n  console.log(res);\n})\nres = {\n  \"accounts\": [\n    {\n      \"name\": \"test\",\n      \"address\": \"XxajQQtYxnsgQp92oiSeENao9XkmqbEitDD8CJKfDctvAQmH6\"\n    }\n   ],\n  \"chains\": [\n    {\n      \"url\": \"http://13.231.179.27:8000\",\n      \"isMainChain\": true,\n      \"chainId\": \"AELF\"\n    },\n    {\n      \"url\": \"http://52.68.97.242:8000\",\n      \"isMainChain\": false,\n      \"chainId\": \"2112\"\n    },\n    {\n      \"url\": \"http://52.196.227.200:8000\",\n      \"isMainChain\": false,\n      \"chainId\": \"2113\"\n    }\n  ]\n}\n```\n\n### Calling the chain API\n\nAPI 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()`.\n\nDeveloper can use the methods of `bridgeInstance.chain` to call the api.\n\nExample:\n\n* Get block height\n\n```javascript\nbridgeInstance.chain.getBlockHeight().then(console.log).catch(console.log)\n```\n\n* Get chain status\n```javascript\nbridgeInstance.chain.getChainStatus().then(console.log).catch(console.log)\n```\n\n### Call contract method (read-only and send transaction)\n\n* Get contract method list\n* Send transaction\n* Contract read-only method\n\nExample:\n\n* Call the `Transfer` method of the `Token` contract to initiate a transfer transaction\n\n```javascript\nconst tokenAddress = 'mS8xMLs9SuWdNECkrfQPF8SuRXRuQzitpjzghi3en39C3SRvf'; // Get contract address by genesis contract method `GetContractAddressByName`\nbridgeInstance.chain.contractAt(tokenAddress).then(async contract =\u003e {\n    const tokenInfo = await contract.GetTokenInfo.call({symbol: 'ELF'});\n    const transactionId = await contract.Transfer({\n          amount: \"10000000000\",\n          to: \"fasatqawag\",\n          symbol: \"ELF\",\n          memo: \"transfer ELF\"\n    });\n    console.log(tokenInfo);\n    console.log(transactionId);\n})\n```\n\n### disconnect\n\nUsed to disconnect from the client and clearing the public key information, etc.\n\n`bridgeInstance.disconnect()`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faelfproject%2Faelf-js-bridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faelfproject%2Faelf-js-bridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faelfproject%2Faelf-js-bridge/lists"}