|
1 |
| -# ain-js |
2 |
| -AI Network Blockchain SDK for javascript (or typescript). |
| 1 | +# AIN Blockchain SDK |
3 | 2 |
|
4 |
| -## API Documentation |
5 |
| -API documentation is available at https://ainblockchain.github.io/ain-js/. |
6 |
| - |
7 |
| -## Quick start |
8 |
| -Start with the quick start section in the [online docs](https://docs.ainetwork.ai/ain-blockchain/developer-guide/getting-started). |
| 3 | +[](https://npmjs.org/package/@ainblockchain/ain-js) |
| 4 | + |
| 5 | + |
9 | 6 |
|
| 7 | +A simple library for JavaScript and TypeScript to interact with AI Network via [JSON RPC API](https://github.com/ainblockchain/ain-blockchain/blob/master/JSON_RPC_API.md). |
10 | 8 |
|
11 | 9 | ## Installation
|
| 10 | + |
| 11 | +```sh |
| 12 | +$ npm install @ainblockchain/ain-js |
12 | 13 | ```
|
13 |
| -yarn add @ainblockchain/ain-js |
14 |
| -``` |
15 | 14 |
|
16 |
| -## Examples |
17 |
| -### A Simple Example |
| 15 | +## Usage |
| 16 | + |
| 17 | +The full API of this library can be found in [API document](https://ainblockchain.github.io/ain-js), along with [code examples](https://github.com/ainblockchain/quickstart). The following code shows how to create a wallet account using the wallet API. |
| 18 | + |
| 19 | +### Create Wallet |
| 20 | + |
| 21 | +```js |
| 22 | +const Ain = require('@ainblockchain/ain-js').default; |
| 23 | + |
| 24 | +const ain = new Ain('https://testnet-api.ainetwork.ai', 'wss://testnet-event.ainetwork.ai', 0); |
| 25 | + |
| 26 | +function main() { |
| 27 | + const accounts = ain.wallet.create(1); |
| 28 | + |
| 29 | + console.log(accounts[0]); |
| 30 | +} |
| 31 | + |
| 32 | +main(); |
| 33 | + |
| 34 | +// output example: |
| 35 | +// { |
| 36 | +// address: '0xb2585543Cfcfb79CF73a1a14b2DfBC411913940F', |
| 37 | +// private_key: '...', |
| 38 | +// public_key: '...' |
| 39 | +// } |
18 | 40 | ```
|
19 |
| -const Ain = require('./lib/ain').default; |
20 |
| -const ain = new Ain('http://localhost:8081/', 'ws://localhost:5100/'); |
21 |
| -// or const ain = new Ain('https://testnet-api.ainetwork.ai/', 'wss://testnet-event.ainetwork.ai/'); |
22 |
| -
|
23 |
| -ain.wallet.create(1); |
24 |
| -
|
25 |
| -console.log(ain.wallet.accounts); |
26 |
| -/* |
27 |
| -{ |
28 |
| - '0xb2585543Cfcfb79CF73a1a14b2DfBC411913940F': { |
29 |
| - address: '0xb2585543Cfcfb79CF73a1a14b2DfBC411913940F', |
30 |
| - private_key: 'd910c1835eaa89f15452aa3f0bd95f61fb9a04464150e37d617a40ed0071558c', |
31 |
| - public_key: '008bcc621aed85140b97d71b3aa5a88e56fbdc0d5d17b2297ec2d3da2edf3b0594676981ebf16ec3490ddb8f3ba4d4aaf77d5055256f1c044474a7aa22704f60' |
32 |
| - } |
| 41 | + |
| 42 | +### Read and Write Data |
| 43 | + |
| 44 | +```js |
| 45 | +const Ain = require('@ainblockchain/ain-js').default; |
| 46 | + |
| 47 | +const ain = new Ain('https://testnet-api.ainetwork.ai', 'wss://testnet-event.ainetwork.ai', 0); |
| 48 | + |
| 49 | +async function main() { |
| 50 | + const address = ain.wallet.addAndSetDefaultAccount('YOUR_PRIVATE_KEY'); |
| 51 | + |
| 52 | + // write value to db |
| 53 | + const result = await ain.db.ref('YOUR_DATA_PATH').setValue({ |
| 54 | + value: 'hello', |
| 55 | + gas_price: 500, |
| 56 | + timestamp: Date.now(), |
| 57 | + nonce: -1, |
| 58 | + }); |
| 59 | + |
| 60 | + // read value from db |
| 61 | + const data = await ain.db.ref('YOUR_DATA_PATH').getValue(); |
| 62 | + console.log(data); |
33 | 63 | }
|
34 |
| -*/ |
35 | 64 |
|
36 |
| -const accounts = ain.db.ref('/accounts').getValue().then(result => { |
37 |
| - console.log(result); |
38 |
| -}); |
| 65 | +main(); |
39 | 66 | ```
|
40 | 67 |
|
41 |
| -### More Use Cases |
42 |
| -#### [ainize-js](https://github.com/ainize-team/ainize-js) |
43 |
| -- [AinModule](https://github.com/ainize-team/ainize-js/blob/main/src/ain.ts) |
| 68 | +### Rules and Owners |
| 69 | + |
| 70 | +[Rule configs](https://docs.ainetwork.ai/ain-blockchain/ai-network-design/blockchain-database/rules-and-owners/rule-configs) validate transactions and control write permissions, while [owner configs](https://docs.ainetwork.ai/ain-blockchain/ai-network-design/blockchain-database/rules-and-owners/owner-configs) manage write access to both rules and themselves. |
| 71 | + |
| 72 | +The following code shows how to configure a rule to allow write access for all users: |
| 73 | + |
| 74 | +```js |
| 75 | +const Ain = require('@ainblockchain/ain-js').default; |
| 76 | + |
| 77 | +const ain = new Ain('https://testnet-api.ainetwork.ai', 'wss://testnet-event.ainetwork.ai', 0); |
44 | 78 |
|
45 |
| -#### [ainft-js](https://github.com/ainize-team/ainize-js) |
46 |
| -- [AinftJs](https://github.com/ainft-team/ainft-js/blob/main/src/ainft.ts) |
| 79 | +async function main() { |
| 80 | + const address = ain.wallet.addAndSetDefaultAccount('YOUR_PRIVATE_KEY'); |
47 | 81 |
|
48 |
| -## Test How-To |
49 |
| -For testing, you need a blockchain node cluster running locally. |
50 |
| -1. Clone AIN Blockchain and install |
| 82 | + // set the rule to allow write access for all users |
| 83 | + const result = await ain.db.ref(appPath).setRule({ |
| 84 | + value: { |
| 85 | + '.rule': { |
| 86 | + write: true, |
| 87 | + }, |
| 88 | + }, |
| 89 | + gas_price: 500, |
| 90 | + timestamp: Date.now(), |
| 91 | + nonce: -1, |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +main(); |
51 | 96 | ```
|
52 |
| -git clone [email protected]:ainblockchain/ain-blockchain.git |
53 |
| -cd ain-blockchain |
54 |
| -yarn install |
| 97 | + |
| 98 | +### Function Call |
| 99 | + |
| 100 | +```js |
| 101 | +const Ain = require('@ainblockchain/ain-js').default; |
| 102 | + |
| 103 | +const ain = new Ain('https://testnet-api.ainetwork.ai', 'wss://testnet-event.ainetwork.ai', 0); |
| 104 | + |
| 105 | +async function main() { |
| 106 | + const address = ain.wallet.addAndSetDefaultAccount('YOUR_PRIVATE_KEY'); |
| 107 | + |
| 108 | + // trigger a function when a value is written to the data path |
| 109 | + const result = await ain.db.ref('YOUR_DATA_PATH').setFunction({ |
| 110 | + value: { |
| 111 | + '.function': { |
| 112 | + YOUR_FUNCTION_ID: { |
| 113 | + function_type: 'REST', |
| 114 | + function_url: 'YOUR_FUNCTION_URL', |
| 115 | + function_id: 'YOUR_FUNCTION_ID', |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + gas_price: 500, |
| 120 | + timestamp: Date.now(), |
| 121 | + nonce: -1, |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +main(); |
55 | 126 | ```
|
56 | 127 |
|
57 |
| -2. Start blockchain locally |
| 128 | +## Documentation |
| 129 | + |
| 130 | +Browse the documentation online: |
| 131 | + |
| 132 | +- [Quick Start](https://docs.ainetwork.ai/ain-blockchain/developer-guide/getting-started) |
| 133 | +- [Full API Documentation](https://ainblockchain.github.io/ain-js) |
| 134 | +- [Developer Guide](https://docs.ainetwork.ai/ain-blockchain/developer-guide) |
| 135 | + |
| 136 | +## Testing |
| 137 | + |
| 138 | +To run tests, a local blockchain node must be running. |
| 139 | + |
| 140 | +1. Clone and install the AIN Blockchain: |
| 141 | + |
| 142 | +```sh |
| 143 | +$ git clone https://github.com/ainblockchain/ain-blockchain.git |
| 144 | +$ cd ain-blockchain |
| 145 | +$ npm install |
58 | 146 | ```
|
59 |
| -cd ain-blockchain |
60 |
| -bash start_local_blockchain.sh |
| 147 | + |
| 148 | +2. Start the local blockchain: |
| 149 | + |
| 150 | +```sh |
| 151 | +$ bash start_local_blockchain.sh |
61 | 152 | ```
|
62 |
| -* Note that the node 2 of the blockchain needs to be started with ENABLE_EVENT_HANDLER=true env variable for the event manager test cases. |
63 | 153 |
|
64 |
| -3. Run tests |
| 154 | +- For event manager test cases, ensure Node 2 is started with the `ENABLE_EVENT_HANDLER` environment variable set to `true`. |
| 155 | + |
| 156 | +3. Run the tests: |
| 157 | + |
| 158 | +```sh |
| 159 | +$ npm run test |
65 | 160 | ```
|
66 |
| -yarn run test |
67 |
| -yarn run test_snapshot # update test snapshot files |
| 161 | + |
| 162 | +- To update test snapshot files: |
| 163 | + |
| 164 | +```sh |
| 165 | +$ npm run test_snapshot |
68 | 166 | ```
|
69 | 167 |
|
70 |
| -## LICENSE |
| 168 | +## License |
71 | 169 |
|
72 |
| -MPL-2.0 |
| 170 | +MPL-2.0 License. |
0 commit comments