Skip to content
This repository has been archived by the owner on Jan 3, 2020. It is now read-only.

go-faast/tron-payments

Repository files navigation

tron-payments

Library to assist in payment processing on tron. It first allows for generation of address according to the BIP44 standard.

Bitcore is used for deterministic public and private keys. Please see the BIP32 standard for more information (BIP32).

Some work is inspired off of the prior work done by tron-bip44

Implements the payments-common interface.

Getting Started

npm install --save go-faast/tron-payments

Create a new wallet (DON'T DO THIS ON PRODUCTION):

let { HdTronPayments } = require('@faast/tron-payments')
let keys = HdTronPayments.generateNewKeys()
console.log(keys.xpub)
console.log(keys.xprv)

Generate an tron deposit address from a public seed (xpub). This is useful if you are a hot wallet and don't store the private key. You will need to keep track of which path node you are on (increasing INT):

let tronPayments = new HdTronPayments({ hdKey: keys.xprv }) // xpub or xprv can be used
// for path m/44'/195'/0'/0/1234
let { address: depositAddress } = tronPayments.getPayport(1234)
let privateKey = tronPayments.getPrivateKey(1234) // will throw Error if xpub was provided as hdKey

or, if you'd rather not us bip44 and have existing private keys or addresses:

let { KeyPairTronPayments } = require('@faast/tron-payments')
let tronPayments = new KeyPairTronPayments({ keyPairs: [privateKey0, address1, privateKey2] })
let { address: depositAddress } = tronPayments.getPayport(1234) // address for privateKey2
await tronpayments.getPrivateKey(1234) // will throw error because keyPair[1] is not a private key

Validate an address:

if (tronPayments.isValidAddress(depositAddress)) {
  // do something
}

Get the public key from a private key:

let address = tronPayments.privateKeyToAddress(privateKey) // for path m/44'/195'/0/1234
if(address === depositAddress){
  console.log('this library works')
} else {
  console.log('better not use this library')
}

Get the derived xpub key from a root xprv:

let { xprvToXpub } = require('@faast/tron-payments')
let xpub = xprvToXpub(xprv) // derives path m/44'/195'/0'

Get the balance of an address:

let { confirmedBalance, unconfirmedBalance } = await tronPayments.getBalance(1234)

Generate a sweep transaction for an address, then broadcast it:

let unsignedTx = await tronPayments.createSweepTransaction(1234, to)
let signedTx = await tronPayments.signTransaction(unsignedTx)
let { id: txHash } = await tronPayments.broadcastTransaction(signedtx)

Generate a simple send transaction

let unsignedTx = await tronPayments.createTransaction(1234, to, '1.234')
// You still need to sign and broadcast the transaction

Get a transaction and check if it is confirmed:

let txInfo = await tronPayments.getTransactionInfo(txHash)
if (txInfo.isConfirmed) {
  // txInfo.confirmations > 0
}

See tests for more utilities

Note: It is suggested to generate your Private key offline with FAR more entropy than the default function, then use xprvToXpub. You have been warned!

License

MIT