1 | # AMON libraries
|
2 |
|
3 | [![CircleCI](https://circleci.com/gh/amontech/amon-lib/tree/master.svg?style=svg&circle-token=35a5a437b160dcd5edeb20b19b5b75fcebd7082d)](https://circleci.com/gh/amontech/amon-lib/tree/master)
|
4 |
|
5 | This library is a set of common utilities used in various part of AMON projects.
|
6 |
|
7 | ## Documentation:
|
8 |
|
9 | [API Documentation](https://amontech.github.io/amon-lib/)
|
10 |
|
11 | ## Install
|
12 |
|
13 | Install via npm or yarn
|
14 | ```bash
|
15 | npm i -S amon-lib
|
16 | yarn add amon-lib
|
17 | ```
|
18 |
|
19 | Import in your project
|
20 | ```javascript
|
21 | const AmonLib = require('amon-lib');
|
22 | import AmonLib from 'amon-lib';
|
23 | ```
|
24 |
|
25 | ## Examples
|
26 |
|
27 | First you need to get an instance of AmonLib. This instance is useful to use the lib on different networks.
|
28 |
|
29 | ```javascript
|
30 | const amonLib = new AmonLib({ network: 'mainnet' });
|
31 | ```
|
32 |
|
33 | `network` can be either `mainnet` or `testnet`
|
34 |
|
35 | ### Coins
|
36 |
|
37 | Supported coins: `BTC`, `ETH`, `AMN`
|
38 | - Validate address
|
39 | ```javascript
|
40 | const validAddress = amonLib.coins('BTC').validAddress('1FJ2PMM75HRh63TmoYLe6Wd9apxNK3aem9');
|
41 | // validAddress = true
|
42 | ```
|
43 |
|
44 | - Transaction block explorer url
|
45 | ```javascript
|
46 | const txExplorerUrl = amonLib.coins('BTC').txExplorerUrl('ad44d7ff0a7a1e433d00fbe9db1a8cf4cd509c3bb928c3963f2e4575fc4c5861');
|
47 | // txExplorerUrl = 'https://live.blockcypher.com/btc/tx/ad44d7ff0a7a1e433d00fbe9db1a8cf4cd509c3bb928c3963f2e4575fc4c5861'
|
48 | ```
|
49 |
|
50 | - Address block explorer url
|
51 | ```javascript
|
52 | const addressExplorerUrl = amonLib.coins('BTC').addressExplorerUrl('1FJ2PMM75HRh63TmoYLe6Wd9apxNK3aem9');
|
53 | // addressExplorerUrl = 'https://live.blockcypher.com/btc/address/1FJ2PMM75HRh63TmoYLe6Wd9apxNK3aem9'
|
54 | ```
|
55 |
|
56 | ### URI
|
57 |
|
58 | #### Parse URI
|
59 |
|
60 | ```js
|
61 | const data = amonLib.URI.parse('bitcoin:mkzgubTA5Ahi6BPSkE6MN9pEafRutznkMe?amount=0.12');
|
62 | const { address, coinCode, amount } = data;
|
63 | // address = mkzgubTA5Ahi6BPSkE6MN9pEafRutznkMe
|
64 | // coinCode = BTC
|
65 | // amount = 0.12
|
66 | ```
|
67 |
|
68 | #### Generate URI
|
69 |
|
70 | ```js
|
71 | const data = {
|
72 | coinCode: 'BTC',
|
73 | address: 'mkzgubTA5Ahi6BPSkE6MN9pEafRutznkMe',
|
74 | amount: '0.12',
|
75 | };
|
76 |
|
77 | const str = amonLib.URI.stringify(data);
|
78 | // str = bitcoin:mkzgubTA5Ahi6BPSkE6MN9pEafRutznkMe?amount=0.12
|
79 | ```
|
80 |
|
81 |
|
82 | ### Hash
|
83 |
|
84 | #### SHA-256
|
85 |
|
86 | ```js
|
87 | const password = 'secretsanta';
|
88 | const hash = AmonLib.crypto.sha.hash(password); // => 0a4f185e2483d5ea4e370c6b4ee31c51840f212a7c25de997509a8953d5fcb86
|
89 | ```
|
90 |
|
91 | #### bcrypt
|
92 |
|
93 | ```js
|
94 | const password = 'secretsanta';
|
95 | const hash = AmonLib.crypto.bcrypt.hash(password); // => 0a4f185e2483d5ea4e370c6b4ee31c51840f212a7c25de997509a8953d5fcb86
|
96 | const valid = AmonLib.crypto.bcrypt.verifyHash(password, hash); // => true
|
97 | ```
|