All files connection.js

73.21% Statements 41/56
68.75% Branches 11/16
81.82% Functions 9/11
70.83% Lines 34/48

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1201x     4x 4x 4x 4x                                                                 1x       4x   4x                   4x 4x             2x 2x         2x   2x             7x   7x 5x   2x 4x   4x 2x     4x 6x 2x 2x 2x 2x 4x 4x 4x   2x   2x     4x     2x                      
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)
    }
 
    listOutputs(publicKey, spent) {
        return this.conn.listOutputs(publicKey, spent)
    }
 
    getBlock(blockId) {
        return this.conn.getBlock(blockId)
    }
 
    listBlocks(transactionId) {
        return this.conn.listBlocks(transactionId)
            .then(blockIds => Promise.all(blockIds.map(blockId => this.conn.getBlock(blockId))))
    }
 
    listVotes(blockId) {
        return this.conn.listVotes(blockId)
    }
 
    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)
                Eif (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
            })
    }
}