UNPKG

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