# All files connection.js

97.96% Statements 48/49
93.75% Branches 15/16
100% Functions 9/9
97.62% Lines 41/42

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
    95
    96
    97
    98
    99
    100
    101
    102
    1031x

    16x
    16x
    16x
    16x

    2x

    1x

    1x

    2x

    6x

    6x

    5x
    5x

    1x

    3x
    3x

    2x

    2x

    1x

    13x

    13x
    10x

    3x
    4x

    4x
    2x

    4x
    6x
    2x
    2x
    2x
    2x
    4x
    4x
    4x

    2x

    2x

    4x

    2x

    1x

    1x

     import * as driver from 'bigchaindb-driver' // eslint-disable-line import/no-namespace

    export default class Connection {
        constructor(path, headers = {}) {
            this.path = path
            this.headers = Object.assign({}, headers)
            this.conn = new driver.Connection(path, headers)
        }

        getAssetId(tx) { // eslint-disable-line class-methods-use-this
            return tx.operation === 'CREATE' ? tx.id : tx.asset.id
        }

        getTransaction(transactionId) {
            return this.conn.getTransaction(transactionId)
        }

        listTransactions(assetId, operation) {
            return this.conn.listTransactions(assetId, operation)
        }

        searchAssets(text) {
            return this.conn.searchAssets(text)
        }

        createTransaction(publicKey, privateKey, payload, metadata) {
            try {
                // Create a transation
                const tx = driver.Transaction.makeCreateTransaction(
                    payload,
                    metadata,
                    [
                        driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(publicKey))
                    ],
                    publicKey
                )

                // sign/fulfill the transaction
                const txSigned = driver.Transaction.signTransaction(tx, privateKey)
                return this.conn.postTransactionCommit(txSigned).then(() => txSigned)
            } catch (error) {
                return Promise.reject(error)
            }
        }

        transferTransaction(tx, fromPublicKey, fromPrivateKey, toPublicKey, metadata) {
            try {
                const txTransfer = driver.Transaction.makeTransferTransaction(
                    [{ 'tx': tx, 'output_index': 0 }],
                    [driver.Transaction.makeOutput(driver.Transaction.makeEd25519Condition(toPublicKey))],
                    metadata,
                )
                const txTransferSigned = driver.Transaction.signTransaction(txTransfer, fromPrivateKey)
                // send it off to BigchainDB
                return this.conn.postTransactionCommit(txTransferSigned).then(() => txTransferSigned)
            } catch (error) {
                return Promise.reject(error)
            }
        }

        getSortedTransactions(assetId) {
            return this.conn.listTransactions(assetId)
                .then((txList) => {
                    if (txList.length <= 1) {
                        return txList
                    }
                    const inputTransactions = []
                    txList.forEach((tx) =>
                        tx.inputs.forEach(input => {
                            if (input.fulfills) {
                                inputTransactions.push(input.fulfills.transaction_id)
                            }
                        }))
                    const unspents = txList.filter((tx) => inputTransactions.indexOf(tx.id) === -1)
                    if (unspents.length) {
                        let tipTransaction = unspents[0]
                        let tipTransactionId = tipTransaction.inputs[0].fulfills.transaction_id
                        const sortedTxList = []
                        while (true) { // eslint-disable-line no-constant-condition
                            sortedTxList.push(tipTransaction)
                            try {
                                tipTransactionId = tipTransaction.inputs[0].fulfills.transaction_id
                            } catch (e) {
                                break
                            }
                            Iif (!tipTransactionId) {
                                break
                            }
                            tipTransaction = txList.filter((tx) => // eslint-disable-line no-loop-func, prefer-destructuring
                                tx.id === tipTransactionId)[0]
                        }
                        return sortedTxList.reverse()
                    } else {
                        console.error(
                            'something went wrong while sorting transactions',
                            txList, inputTransactions
                        )
                    }
                    return txList
                })
        }
    }