# All files ormobject.js

92.31% Statements 36/39
72.22% Branches 13/18
100% Functions 11/11
91.67% Lines 33/36

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

    1x

    22x
    11x
    11x
    11x
    11x
    11x
    7x
    7x
    9x

    1x
    1x
    1x
    1x
    1x

    1x

    4x
    4x

    4x
    4x

    4x

    4x

    1x
    1x

    1x

    1x

    1x

    1x
    1x

    1x

    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 = '', 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[`${this._appId}-${this._name}`].id
                this.data = Object.assign({}, ...transactionList.map(tx => (tx.metadata)))
            }
        }

        retrieve(input) {
            const query = input || `"${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) {
            Iif (inputs === undefined) {
                console.error('inputs missing')
            }
            const assetPayload = {}
            assetPayload[`${this._appId}-${this._name}`] = {
                'schema': this._schema,
                'id': `id:${this._appId}:${uuid()}`
            }
            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) {
            Iif (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) {
            Iif (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
                            ))))
        }
    }