1 | # tron-payments
|
2 |
|
3 | Library to assist in payment processing on tron. It first allows for generation
|
4 | of address according to the [BIP44 standard](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki).
|
5 |
|
6 | [Bitcore](https://bitcore.io/) is used for deterministic public and private keys.
|
7 | Please see the BIP32 standard for more information ([BIP32](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki)).
|
8 |
|
9 | Some work is inspired off of the prior work done by [tron-bip44](https://github.com/trapp/tron-bip44)
|
10 |
|
11 | Implements the [payments-common](https://github.com/go-faast/payments-common) interface.
|
12 |
|
13 | ## Getting Started
|
14 |
|
15 | ```bash
|
16 | npm install --save go-faast/tron-payments
|
17 | ```
|
18 |
|
19 | Create a new wallet (DON'T DO THIS ON PRODUCTION):
|
20 |
|
21 | ```js
|
22 | let { HdTronPayments } = require('@faast/tron-payments')
|
23 | let keys = HdTronPayments.generateNewKeys()
|
24 | console.log(keys.xpub)
|
25 | console.log(keys.xprv)
|
26 | ```
|
27 |
|
28 | Generate an tron deposit address from a public seed (xpub).
|
29 | This is useful if you are a hot wallet and don't store the private key. You will need
|
30 | to keep track of which path node you are on (increasing INT):
|
31 |
|
32 | ```js
|
33 | let tronPayments = new HdTronPayments({ hdKey: keys.xprv }) // xpub or xprv can be used
|
34 | // for path m/44'/195'/0'/0/1234
|
35 | let { address: depositAddress } = tronPayments.getPayport(1234)
|
36 | let privateKey = tronPayments.getPrivateKey(1234) // will throw Error if xpub was provided as hdKey
|
37 | ```
|
38 |
|
39 | or, if you'd rather not us bip44 and have existing private keys or addresses:
|
40 |
|
41 | ```js
|
42 | let { KeyPairTronPayments } = require('@faast/tron-payments')
|
43 | let tronPayments = new KeyPairTronPayments({ keyPairs: [privateKey0, address1, privateKey2] })
|
44 | let { address: depositAddress } = tronPayments.getPayport(1234) // address for privateKey2
|
45 | await tronpayments.getPrivateKey(1234) // will throw error because keyPair[1] is not a private key
|
46 | ```
|
47 |
|
48 | Validate an address:
|
49 |
|
50 | ```js
|
51 | if (tronPayments.isValidAddress(depositAddress)) {
|
52 | // do something
|
53 | }
|
54 | ```
|
55 |
|
56 | Get the public key from a private key:
|
57 |
|
58 | ```js
|
59 | let address = tronPayments.privateKeyToAddress(privateKey) // for path m/44'/195'/0/1234
|
60 | if(address === depositAddress){
|
61 | console.log('this library works')
|
62 | } else {
|
63 | console.log('better not use this library')
|
64 | }
|
65 | ```
|
66 |
|
67 | Get the derived xpub key from a root xprv:
|
68 |
|
69 | ```js
|
70 | let { xprvToXpub } = require('@faast/tron-payments')
|
71 | let xpub = xprvToXpub(xprv) // derives path m/44'/195'/0'
|
72 | ```
|
73 |
|
74 | Get the balance of an address:
|
75 |
|
76 | ```js
|
77 | let { confirmedBalance, unconfirmedBalance } = await tronPayments.getBalance(1234)
|
78 | ```
|
79 |
|
80 | Generate a sweep transaction for an address, then broadcast it:
|
81 |
|
82 | ```js
|
83 | let unsignedTx = await tronPayments.createSweepTransaction(1234, to)
|
84 | let signedTx = await tronPayments.signTransaction(unsignedTx)
|
85 | let { id: txHash } = await tronPayments.broadcastTransaction(signedtx)
|
86 | ```
|
87 |
|
88 | Generate a simple send transaction
|
89 |
|
90 | ```js
|
91 | let unsignedTx = await tronPayments.createTransaction(1234, to, '1.234')
|
92 | // You still need to sign and broadcast the transaction
|
93 | ```
|
94 |
|
95 | Get a transaction and check if it is confirmed:
|
96 |
|
97 | ```js
|
98 | let txInfo = await tronPayments.getTransactionInfo(txHash)
|
99 | if (txInfo.isConfirmed) {
|
100 | // txInfo.confirmations > 0
|
101 | }
|
102 | ```
|
103 |
|
104 | *See tests for more utilities*
|
105 |
|
106 | **Note:** It is suggested to generate your Private key offline with FAR more entropy than the default function, then use xprvToXpub.
|
107 | You have been warned!
|
108 |
|
109 | ## License
|
110 |
|
111 | MIT
|