# All files ormobject.js

100% Statements 43/43
89.29% Branches 25/28
100% Functions 11/11
100% Lines 38/38

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
    1151x

    1x

    40x
    20x
    20x
    20x
    20x
    20x
    12x
    12x
    14x

    2x
    2x
    2x
    1x

    1x

    2x
    2x
    5x

    5x

    6x
    6x
    1x

    6x

    6x

    5x

    2x
    2x
    1x

    2x

    1x

    1x

    2x
    2x
    1x

    2x

    1x

    1x

     import uuid from 'uuid/v4'

    // The likelihood to generate a vanity address that is 11 times "Burn" is extremely low:
    // - https://en.bitcoin.it/wiki/Vanitygen#Use_of_vanitygen_to_try_to_attack_addresses
    const BURN_ADDRESS = 'BurnBurnBurnBurnBurnBurnBurnBurnBurnBurnBurn'

    export default class OrmObject {
        constructor(modelName, modelSchema, connection, appId = 'global', transactionList = []) {
            this._name = modelName
            this._schema = modelSchema
            this._connection = connection
            this._appId = appId
            if (transactionList.length) {
                this.transactionHistory = transactionList
                this.id = transactionList[0].asset.data.id
                this.data = Object.assign({}, ...transactionList.map(tx => (tx.metadata)))
            }
        }

        retrieve(input = undefined) {
            let query
            if (input !== undefined && input !== '') {
                query = `"${input}"`
            } else {
                query = `"id:${this._appId}:${this._name}:"`
            }
            return this._connection.searchAssets(query)
                .then(assets =>
                    Promise.all(assets.map(asset =>
                        this._connection.getSortedTransactions(asset.id)
                            .then(txList =>
                                new OrmObject(
                                    this._name,
                                    this._schema,
                                    this._connection,
                                    this._appId,
                                    txList
                                )))))
        }

        create(inputs, id = uuid()) {
            if (inputs === undefined) {
                console.error('inputs missing')
            }
            const assetPayload = {
                'schema': this._schema,
                'id': `id:${this._appId}:${this._name}:${id}`
            }
            return this._connection
                .createTransaction(
                    inputs.keypair.publicKey,
                    inputs.keypair.privateKey,
                    assetPayload,
                    inputs.data
                )
                .then(tx => Promise.resolve(this._connection.getSortedTransactions(tx.id).then((txList) =>
                    new OrmObject(
                        this._name,
                        this._schema,
                        this._connection,
                        this._appId,
                        txList
                    ))))
        }

        append(inputs) {
            if (inputs === undefined) {
                console.error('inputs missing')
            }
            return this._connection
                .transferTransaction(
                    this.transactionHistory[this.transactionHistory.length - 1],
                    inputs.keypair.publicKey,
                    inputs.keypair.privateKey,
                    inputs.toPublicKey,
                    inputs.data
                )
                .then(() =>
                    Promise.resolve(this._connection.getSortedTransactions(this.transactionHistory[0].id)
                        .then((txList) =>
                            new OrmObject(
                                this._name,
                                this._schema,
                                this._connection,
                                this._appId,
                                txList
                            ))))
        }

        burn(inputs) {
            if (inputs === undefined) {
                console.error('inputs missing')
            }

            return this._connection
                .transferTransaction(
                    this.transactionHistory[this.transactionHistory.length - 1],
                    inputs.keypair.publicKey,
                    inputs.keypair.privateKey,
                    BURN_ADDRESS,
                    { status: 'BURNED' }
                )
                .then(() =>
                    Promise.resolve(this._connection.getSortedTransactions(this.transactionHistory[0].id)
                        .then((txList) =>
                            new OrmObject(
                                this._name,
                                this._schema,
                                this._connection,
                                this._appId,
                                txList
                            ))))
        }
    }