All files / src TronLink.js

70% Statements 14/20
72.22% Branches 13/18
28.57% Functions 2/7
70% Lines 14/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                            1x                                                                                       28x 4x     24x   7x       6x   1x       15x   15x 9x     6x 6x     6x         3x        
import {Address} from "./utils/address"
 
/**
 * TronLink extension interaction functionality
 */
export default class TronLink {
 
    /**
     * Initiates TronLink support object.
     *
     * @param {Object} tronWeb tronWeb entity object
     *           (details: https://github.com/tronprotocol/tron-web)
     */
    constructor(tronWeb) {
        this.tronWeb = tronWeb
    }
 
    /**
     * Checks if TronLink browser extension is installed
     */
    isInstalled() {
        return !!this.tronWeb
    }
 
    /**
     * Checks if user is logged in to the TronLink plugin
     */
    isLoggedIn() {
        return this.tronWeb && this.tronWeb.ready
    }
 
    /**
     * Checks if user is logged in to the TronLink plugin.
     * Alias for isLoggedIn() method.
     */
    isUnlocked() {
        return this.isLoggedIn()
    }
 
    /**
     * Returns logged in user Tron address
     */
    getAccountAddress() {
        return this.tronWeb.defaultAddress.base58
    }
 
    NewContract(abi = [], address = false) {
        return new this.tronWeb.Contract(this.tronWeb, abi, address)
    }
 
    /**
     * Converts Tron address from one format to another.
     *
     * @param {String, Number} address Address to convert
     * @param {String} fromFormat From format string
     * @param {String} toFormat To format string
     */
    convertAddress(address, fromFormat, toFormat) {
        if (fromFormat == toFormat) {
            throw "From and To address formats are equal"
        }
 
        switch (toFormat) {
            case "hex":
                switch (fromFormat) {
                    case "base58":
                    case "tron":
                    case "trx":
                        return "0x" + this.tronWeb.address.toHex(address)
                }
                break
            case "base58":
            case "tron":
            case "trx":
                switch (fromFormat) {
                    case "hex":
                        if (!Address.isHexAddress(address)) {
                            throw "Invalid hex address"
                        }
 
                        Eif (address.startsWith("0x")) {
                            address = address.substr(2)
                        }
 
                        return this.tronWeb.address.fromHex(address)
                }
                break
        }
 
        throw "Invalid address formats"
    }
 
}