Dev document

1 Download

2 Preparation

2.1 Interaction Flow

    1.Make sure the user get the Extension
    2.Connect the blockchain
    3.Initialize contract / Call the methods of the blockchain
    4.Call the methods of contract

2.2 Demo of Checking the Extension

1
let nightElfInstance = null;
2
class NightElfCheck {
3
constructor() {
4
const readyMessage = 'NightElf is ready';
5
let resovleTemp = null;
6
this.check = new Promise((resolve, reject) => {
7
if (window.NightElf) {
8
resolve(readyMessage);
9
}
10
setTimeout(() => {
11
reject({
12
error: 200001,
13
message: 'timeout / can not find NightElf / please install the extension'
14
});
15
}, 1000);
16
resovleTemp = resolve;
17
});
18
document.addEventListener('NightElf', result => {
19
console.log('test.js check the status of extension named nightElf: ', result);
20
resovleTemp(readyMessage);
21
});
22
}
23
static getInstance() {
24
if (!nightElfInstance) {
25
nightElfInstance = new NightElfCheck();
26
return nightElfInstance;
27
}
28
return nightElfInstance;
29
}
30
}
31
const nightElfCheck = NightElfCheck.getInstance();
32
nightElfCheck.check.then(message => {
33
// connectChain -> Login -> initContract -> call contract methods
34
});
Copied!

2.3 How Connect the blockchain

1
const aelf = new window.NightElf.AElf({
2
httpProvider: [
3
'http://127.0.0.1:8101',
4
],
5
appName: 'your own app name'
6
});
Copied!

2.4 How to call the API

Callback or promise are both ok.
1
// callback
2
aelf.chain.getChainStatus((error, result) => {
3
console.log('>>>>>>>>>>>>> getChainStatus >>>>>>>>>>>>>');
4
console.log(error, result);
5
});
6
7
// promise
8
aelf.chain.getChainStatus().then(result => {
9
console.log('>>>>>>>>>>>>> getChainStatus >>>>>>>>>>>>>');
10
console.log('promise then', result);
11
}).catch(error => {
12
console.log('promise catch', error);
13
});
Copied!

3 API Reference

Here you can find examples and in-depth information about NightELF's API.
You can see the demo code. click here
If you want to check token transfer demo. click here
The methods act the same as the methods of the aelf-sdk.js

3.1 LOGIN

LOGIN allows your application to request permission to interact with a user's NightELF and be provided with an account of the user's choosing.
1
aelf.login({
2
chainId: 'AELF',
3
payload: {
4
method: 'LOGIN',
5
contracts: [{
6
chainId: 'AELF',
7
contractAddress: '4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',
8
contractName: 'token',
9
description: 'token contract',
10
github: ''
11
}, {
12
chainId: 'AELF TEST',
13
contractAddress: '2Xg2HKh8vusnFMQsHCXW1q3vys5JxG5ZnjiGwNDLrrpb9Mb',
14
contractName: 'TEST contractName',
15
description: 'contract description',
16
github: ''
17
}]
18
}
19
}, (error, result) => {
20
console.log('login>>>>>>>>>>>>>>>>>>', result);
21
});
22
23
// result = {
24
// "error": 0,
25
// "errorMessage": "",
26
// "message": "",
27
// "detail": {
28
// "name": "your name",
29
// "address": "2RCLmZQ2291xDwSbDEJR6nLhFJcMkyfrVTq1i1YxWC4SdY49a6",
30
// "publicKey": {
31
// "x": "4958d5c48f003c771769f4a31413cd18053516615cbde502441af8452fb53441",
32
// "y": "a80cc48a7f3b0f2552fd030cacbe9012ba055a3d553b70003f2e637d55fa0f23"
33
// }
34
// },
35
// "sid": "350815427961739930163684",
36
// "from": "contentNightElf"
37
// }
38
39
// the data in the extension
40
// keychain = {
41
// keypairs: [{
42
// name: 'your keypairs name',
43
// address: 'your keypairs address',
44
// mnemonic: 'your keypairs mnemonic',
45
// privateKey: 'your keypairs privateKey',
46
// publicKey: {
47
// x: 'f79c25eb......',
48
// y: '7fa959ed......'
49
// }
50
// }],
51
// permissions: [{
52
// appName: 'hzzTest',
53
// address: 'your keyparis address',
54
// contracts: [{
55
// chainId: 'AELF',
56
// contractAddress: '4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',
57
// contractName: 'token',
58
// description: 'token contract',
59
// github: ''
60
// }],
61
// domain: 'Dapp domain'
62
// }]
63
// }
Copied!

3.2 GET_CHAIN_STATUS

1
aelf.chain.getChainStatus((error, result) => {
2
console.log('>>>>>>>>>>>>> connectChain >>>>>>>>>>>>>');
3
console.log(error, result);
4
});
Copied!

3.3 CALL_AELF_CHAIN

1
// this txid is an example.
2
const txid = 'c45edfcca86f4f528cd8e30634fa4ac53801aae05365cfefc3bfe9b652fe5768';
3
aelf.chain.getTxResult(txid, (err, result) => {
4
console.log('>>>>>>>>>>>>> getTxResult >>>>>>>>>>>>>');
5
console.log(err, result);
6
});
7
8
// result = {
9
// Status: "NotExisted"
10
// TransactionId: "c45edfcca86f4f528cd8e30634fa4ac53801aae05365cfefc3bfe9b652fe5768"
11
// ....
12
// }
Copied!

3.4 INIT_AELF_CONTRACT

1
// In aelf-sdk.js wallet is the realy wallet.
2
// But in extension sdk, we just need the address of the wallet.
3
const wallet = {
4
address: '2JqnxvDiMNzbSgme2oxpqUFpUYfMjTpNBGCLP2CsWjpbHdu'
5
};
6
// It is different from the wallet created by Aelf.wallet.getWalletByPrivateKey();
7
// There is only one value named address;
8
aelf.chain.contractAt(
9
'4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',
10
wallet,
11
(error, result) => {
12
console.log('>>>>>>>>>>>>> contractAt >>>>>>>>>>>>>');
13
console.log(error, result);
14
tokenContract = result;
15
}
16
);
17
18
// result = {
19
// Approve: ƒ (),
20
// Burn: ƒ (),
21
// ChargeTransactionFees: ƒ (),
22
// ClaimTransactionFees: ƒ (),
23
// ....
24
// }
Copied!

3.5 CALL_AELF_CONTRACT / CALL_AELF_CONTRACT_READONLY

1
// tokenContract from the pre step.
2
tokenContract.GetBalance.call(
3
{
4
symbol: 'AELF',
5
owner: '65dDNxzcd35jESiidFXN5JV8Z7pCwaFnepuYQToNefSgqk9'
6
},
7
(err, result) => {
8
console.log('>>>>>>>>>>>>>>>>>>>', result);
9
}
10
);
11
12
tokenContract.Approve(
13
{
14
symbol: 'AELF',
15
spender: '4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',
16
amount: '100'
17
},
18
(err, result) => {
19
console.log('>>>>>>>>>>>>>>>>>>>', result);
20
}
21
);
22
23
// If you use tokenContract.GetBalance.call this method is only applicable to queries that do not require extended authorization validation.(CALL_AELF_CONTRACT_READONLY)
24
// If you use tokenContract.Approve this requires extended authorization validation (CALL_AELF_CONTRACT)
25
26
// tokenContract.GetBalance.call(payload, (error, result) => {})
27
// result = {
28
// symbol: "AELF",
29
// owner: "65dDNxzcd35jESiidFXN5JV8Z7pCwaFnepuYQToNefSgqk9",
30
// balance: 0
31
// }
Copied!

3.6 CHECK_PERMISSION

CHECK_PERMISSION returns the contracts your can use with the address.
1
aelf.checkPermission({
2
type: 'address', // if you did not set type, it aways get by domain.
3
address: '4WBgSL2fSem9ABD4LLZBpwP8eEymVSS1AyTBCqXjt5cfxXK'
4
}, (error, result) => {
5
console.log('>>>>>>>>>>>>>', error, result);
6
});
7
8
// result = {
9
// ...,
10
// permissions:[
11
// {
12
// address: '...',
13
// appName: 'hzzTest',
14
// contracts: [{
15
// chainId: 'AELF',
16
// contractAddress: '4rkKQpsRFt1nU6weAHuJ6CfQDqo6dxruU3K3wNUFr6ZwZYc',
17
// contractName: 'token',
18
// description: 'token contract',
19
// github: ''
20
// },
21
// {
22
// chainId: 'AELF TEST',
23
// contractAddress: 'TEST contractAddress',
24
// contractName: 'TEST contractName',
25
// description: 'contract description',
26
// github: ''
27
// }],
28
// domian: 'Dapp domain'
29
// }
30
// ]
31
// }
Copied!

3.7 SET_CONTRACT_PERMISSION

SET_CONTRACT_PERMISSION applies to the users to allow the Dapp use the contract.
1
aelf.setContractPermission({
2
chainId: 'AELF',
3
payload: {
4
address: '2JqnxvDiMNzbSgme2oxpqUFpUYfMjTpNBGCLP2CsWjpbHdu',
5
contracts: [{
6
chainId: 'AELF',
7
contractAddress: 'TEST contractAddress',
8
contractName: 'AAAA',
9
description: 'contract description',
10
github: ''
11
}]
12
}
13
}, (error, result) => {
14
console.log('>>>>>>>>>>>>>', error, result);
15
});
Copied!

3.8 REMOVE_CONTRACT_PERMISSION

1
aelf.removeContractPermission({
2
chainId: 'AELF',
3
payload: {
4
contractAddress: '2Xg2HKh8vusnFMQsHCXW1q3vys5JxG5ZnjiGwNDLrrpb9Mb'
5
}
6
}, (error, result) => {
7
console.log('>>>>>>>>>>>>>', error, result);
8
});
Copied!

3.9 REMOVE_METHODS_WHITELIST

1
aelf.removeMethodsWhitelist({
2
chainId: 'AELF',
3
payload: {
4
contractAddress: '2Xg2HKh8vusnFMQsHCXW1q3vys5JxG5ZnjiGwNDLrrpb9Mb',
5
whitelist: ['Approve']
6
}
7
}, (error, result) => {
8
console.log('removeWhitelist>>>>>>>>>>>>>>>>>', result);
9
});
Copied!

3.10 GET_SIGNATURE

1
aelf.getSignature({
2
address: 'address',
3
hexToBeSign: 'hexToBeSign'
4
}).then(result => {
5
// ...
6
}).cathc(error => {
7
// ...
8
});
Copied!

4.For Extension Developers

4.1. Download the code

1
git clone https://github.com/AElfProject/aelf-web-extension.git
Copied!

4.2 Install dependent

1
npm install
Copied!

4.3 Run webpack

1
npm run none
2
npm run none:watch
3
npm run dev
4
npm run dev:watch
5
npm run pro
6
npm run pro:watch
Copied!

4.4 Add to the browser

1
open development mode, add the webpack output app/public.
Copied!

4.5 How to publish to chrome

zip app/public to public.zip, and follow the notes.

4.6 Can not use crx

You can not install the extension from an offline crx if Chrome >= 73.
Please import the .zip file with developer mode.

5 Formatted Data Example

1
NightElf = {
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 keupairs privateKey',
10
publicKey: {
11
x: 'you keupairs publicKey',
12
y: 'you keupairs 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
}
Copied!
Last modified 1yr ago