Browser Extension
aelf-web-extension#
Introduction#
aelf Web Extension provides an interface for DApp developers to interact with the aelf blockchain. This guide outlines the usage and implementation details for both users and developers.
For Users#
Release Version#
Please wait for the official release.
Development Version#
dev version For those using QQ Browser and similar, you can add the extension manually.
Notice#
For DApp Developers#
Interaction Flow#
How to Use#
To access the complete data structure, click here. For an extension demo, refer to the provided examples.
Data Format#
1{2"histories": [],3"keychain": {4"keypairs": [5{6"name": "your keypairs name",7"address": "your keypairs address",8"mnemonic": "your keypairs mnemonic",9"privateKey": "your keypairs privateKey",10"publicKey": {11"x": "your keypairs publicKey",12"y": "your keypairs publicKey"13}14}15],16"permissions": [17{18"chainId": "AELF",19"contractAddress": "contract address",20"contractName": "contract name",21"description": "contract description",22"github": "contract github",23"whitelist": {24"Approve": {25"parameter1": "a",26"parameter2": "b",27"parameter3": "c"28}29}30}31]32}33}
Demo of Checking the Extension#
1let nightElfInstance = null;23class NightElfCheck {4constructor() {5const readyMessage = 'NightElf is ready';6let resolveTemp = null;7this.check = new Promise((resolve, reject) => {8if (window.NightElf) {9resolve(readyMessage);10}11setTimeout(() => {12reject({13error: 200001,14message: 'timeout / cannot find NightElf / please install the extension'15});16}, 1000);17resolveTemp = resolve;18});19document.addEventListener('NightElf', result => {20console.log('Checking the status of extension named NightElf: ', result);21resolveTemp(readyMessage);22});23}2425static getInstance() {26if (!nightElfInstance) {27nightElfInstance = new NightElfCheck();28return nightElfInstance;29}30return nightElfInstance;31}32}3334const nightElfCheck = NightElfCheck.getInstance();35nightElfCheck.check.then(message => {36// connectChain -> Login -> initContract -> call contract methods37});
GET_CHAIN_STATUS#
You can see the demo ./devDemos/test.html. [demo.js just a draft]
Token Transfer#
If you want to check Token Transfer Demo. You can Click Here
The methods calls act the same as the methods call of the aelf-sdk.js
Note: ... stands for omitted data.
1const aelf = new window.NightElf.AElf({2httpProvider: ['http://192.168.197.56:8101/chain'],3appName: 'Test'4});56aelf.chain.getChainStatus((error, result) => {7console.log('Chain Status:', error, result);8});
Expected Result :#
1result = {2ChainId: "AELF"3GenesisContractAddress: "61W3AF3Voud7cLY2mejzRuZ4WEN8mrDMioA9kZv3H8taKxF"4}
CALL_AELF_CHAIN#
Example of retrieving a transaction result:
1const txid = 'c45edfcca86f4f528cd8e30634fa4ac53801aae05365cfefc3bfe9b652fe5768';2aelf.chain.getTxResult(txid, (err, result) => {3console.log('Transaction Result:', err, result);4});
Expected Result :#
1result = {2Status: "NotExisted"3TransactionId: "ff5bcd126f9b7f22bbfd0816324390776f10ccb3fe0690efc84c5fcf6bdd3fc6"4}
LOGIN#
Example login call:
1aelf.login({2appName: 'hzzTest',3chainId: 'AELF',4payload: {5method: 'LOGIN',6contracts: [7{8chainId: 'AELF',9contractAddress: '4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',10contractName: 'token',11description: 'token contract',12github: ''13},14{15chainId: 'AELF TEST',16contractAddress: '2Xg2HKh8vusnFMQsHCXW1q3vys5JxG5ZnjiGwNDLrrpb9Mb',17contractName: 'TEST contractName',18description: 'contract description',19github: ''20}21]22}23}, (error, result) => {24console.log('Login Result:', result);25});2627// keychain = {28// keypairs: [{29// name: 'your keypairs name',30// address: 'your keypairs address',31// mnemonic: 'your keypairs mnemonic',32// privateKey: 'your keypairs privateKey',33// publicKey: {34// x: 'f79c25eb......',35// y: '7fa959ed......'36// }37// }],38// permissions: [{39// appName: 'hzzTest',40// address: 'your keyparis address',41// contracts: [{42// chainId: 'AELF',43// contractAddress: '4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',44// contractName: 'token',45// description: 'token contract',46// github: ''47// }],48// domain: 'Dapp domain'49// }]50// }
INIT_AELF_CONTRACT#
Example of initializing a contract:
1// In aelf-sdk.js wallet is the realy wallet.2// But in extension sdk, we just need the address of the wallet.3const tokenContract;4const wallet = {5address: '2JqnxvDiMNzbSgme2oxpqUFpUYfMjTpNBGCLP2CsWjpbHdu'6};7// It is different from the wallet created by Aelf.wallet.getWalletByPrivateKey();8// There is only one value named address;9aelf.chain.contractAtAsync(10'4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',11wallet,12(error, result) => {13console.log('>>>>>>>>>>>>> contractAtAsync >>>>>>>>>>>>>');14console.log(error, result);15tokenContract = result;16}17);
Expected Result :#
1result = {2Approve: ƒ (),3Burn: ƒ (),4ChargeTransactionFees: ƒ (),5ClaimTransactionFees: ƒ (),6....7}
CALL_AELF_CONTRACT / CALL_AELF_CONTRACT_READONLY#
Example contract method calls:
1tokenContract.GetBalance.call(2{ symbol: 'AELF', owner: '65dDNxzcd35jESiidFXN5JV8Z7pCwaFnepuYQToNefSgqk9' },3(err, result) => {4console.log('Get Balance Result:', result);5}6);78tokenContract.Approve(9{ symbol: 'AELF', spender: '4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc', amount: '100' },10(err, result) => {11console.log('Approve Result:', result);12}13);1415// If you use tokenContract.GetBalance.call this method is only applicable to queries that do not require extended authorization validation.(CALL_AELF_CONTRACT_READONLY)16// If you use tokenContract.Approve this requires extended authorization validation (CALL_AELF_CONTRACT)1718// tokenContract.GetBalance.call(payload, (error, result) => {})19// result = {20// symbol: "AELF",21// owner: "65dDNxzcd35jESiidFXN5JV8Z7pCwaFnepuYQToNefSgqk9",22// balance: 023// }
CHECK_PERMISSION#
Example permission check:
1aelf.checkPermission({2appName: 'hzzTest',3type: 'address',4address: '4WBgSL2fSem9ABD4LLZBpwP8eEymVSS1AyTBCqXjt5cfxXK'5}, (error, result) => {6console.log('Check Permission Result:', result);7});
Expected Result :#
1result = {2...,3permissions:[4{5address: '...',6appName: 'hzzTest',7contracts: [{8chainId: 'AELF',9contractAddress: '4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',10contractName: 'token',11description: 'token contract',12github: ''13},14{15chainId: 'AELF TEST',16contractAddress: 'TEST contractAddress',17contractName: 'TEST contractName',18description: 'contract description',19github: ''20}],21domian: 'Dapp domain'22}23]24}
SET_CONTRACT_PERMISSION#
Example of removing methods whitelist:
1aelf.removeContractPermission({2appName: 'hzzTest',3chainId: 'AELF',4payload: {5contractAddress: '2Xg2HKh8vusnFMQsHCXW1q3vys5JxG5ZnjiGwNDLrrpb9Mb'6}7}, (error, result) => {8console.log('removeContractPermission>>>>>>>>>>>>>>>>>>>', result);9});
Expected Result#
1keychain = {2keypairs: {...},3permissions: [{4appName: 'hzzTest',5address: 'your keyparis address',6contracts: [{7chainId: 'AELF',8contractAddress: '4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',9contractName: 'token',10description: 'token contract',11github: ''12}],13domain: 'Dapp domain'14}]15}
REMOVE_METHODS_WHITELIST#
Example of removing contract permission:
1aelf.removeContractPermission({2appName: 'hzzTest',3chainId: 'AELF',4payload: {5contractAddress: '2Xg2HKh8vusnFMQsHCXW1q3vys5JxG5ZnjiGwNDLrrpb9Mb'6}7}, (error, result) => {8console.log('Remove Contract Permission Result:', result);9});
Expected Result#
1keychain = {2keypairs: {...},3permissions: [{4appName: 'hzzTest',5address: 'your keyparis address',6contracts: [{7chainId: 'AELF',8contractAddress: '4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',9contractName: 'token',10description: 'token contract',11github: '',12whitelist: {}13}],14domain: 'Dapp domain'15}]16}
For Extension Developers#
1git clone https://github.com/hzz780/aelf-web-extension.git
1npm install
1webpack -w
1open development mode, add the webpack output app/public.
Project Information#
We use ECDH` to use public key to encryt data and private key to decrypt data.
Edited on: 15 July 2024 07:08:01 GMT+0