# All files / src/tx/applyTx checkSpendCond.v2.js

0% Statements 0/198
0% Branches 0/96
0% Functions 0/18
0% Lines 0/198

    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
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545

     /**
     * Copyright (c) 2018-present, Leap DAO (leapdao.org)
     *
     * This source code is licensed under the Mozilla Public License Version 2.0
     * found in the LICENSE file in the root directory of this source tree.
     */

    const { Type, Output } = require('leap-core');
    const Transaction = require('ethereumjs-tx');
    const VM = require('ethereumjs-vm');
    const utils = require('ethereumjs-util');
    const {
      BigInt,
      multiply,
      add,
      subtract,
      lessThan,
      greaterThan,
    } = require('jsbi-utils');
    const isEqual = require('lodash/isEqual');
    const getColors = require('../../api/methods/getColors');
    const {
      NFT_COLOR_BASE,
      NST_COLOR_BASE,
    } = require('../../api/methods/constants');
    const {
      ERC20_BYTECODE,
      ERC721_BYTECODE,
      ERC1948_BYTECODE,
    } = require('./ercBytecode');
    const { isNFT, isNST } = require('./../../utils');

    const { Account } = VM.deps;

    const REACTOR_ADDR = Buffer.from(
      '0000000000000000000000000000000000000001',
      'hex'
    );

    const ERC20_MINT_FUNCSIG = Buffer.from(
      '40c10f19000000000000000000000000',
      'hex'
    );

    const ERC721_MINT_FUNCSIG = Buffer.from(
      '40c10f19000000000000000000000000',
      'hex'
    );

    const ERC1948_MINT_FUNCSIG = Buffer.from(
      '1e458bee000000000000000000000000',
      'hex'
    );

    // increaseAllowance(address spender, uint256 addedValue)
    const ERC20_INCREASE_ALLOWANCE_FUNCSIG = Buffer.from(
      '39509351000000000000000000000000',
      'hex'
    );

    // approve(address to, uint256 tokenId)
    const ERC721_APPROVE_FUNCSIG = Buffer.from(
      '095ea7b3000000000000000000000000',
      'hex'
    );

    const ERC20_ERC721_TRANSFER_EVENT = Buffer.from(
      'ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
      'hex'
    );

    const ERC1948_DATA_UPDATED_EVENT = Buffer.from(
      '8ec06c2117d45dcb6bcb6ecf8918414a7ff1cb1ed07da8175e2cf638d0f4777f',
      'hex'
    );

    // 6 mil as gas limit
    const GAS_LIMIT = BigInt(6000000);
    const GAS_LIMIT_HEX = `0x${GAS_LIMIT.toString(16)}`;

    // fixed value until we get support for it in the transaction format
    const FIXED_GAS_PRICE = BigInt(142);

    function setAccount(account, address, stateManager) {
      return new Promise((resolve, reject) => {
        stateManager.putAccount(address, account, err => {
          if (err) {
            return reject(err);
          }
          return resolve();
        });
      });
    }

    function setAccountCode(code, address, stateManager) {
      return new Promise((resolve, reject) => {
        stateManager.putContractCode(address, code, err => {
          if (err) {
            return reject(err);
          }
          return resolve();
        });
      });
    }

    // runs a transaction through the vm
    function runTx(vm, raw, from) {
      // create a new transaction out of the js object
      const tx = new Transaction(raw);

      Object.defineProperty(tx, 'from', {
        // instead of tx.sign(Buffer.from(secretKey, 'hex'))
        // eslint-disable-next-line object-shorthand
        get() {
          return from || REACTOR_ADDR;
        },
      });

      return new Promise((resolve, reject) => {
        // run the tx \o/
        vm.runTx({ tx }, (err, results) => {
          if (err) {
            return reject(err);
          }
          if (results.vm.exceptionError) {
            return reject(results.vm.exceptionError);
          }
          return resolve(results);
        });
      });
    }

    const addColors = (map, colors, colorBase = 0) => {
      colors.forEach((color, i) => {
        map[i + colorBase] = Buffer.from(color.replace('0x', ''), 'hex');
      });
    };

    module.exports = async (state, tx, bridgeState, nodeConfig = {}) => {
      if (tx.type !== Type.SPEND_COND) {
        throw new Error('Spending Condition tx expected');
      }
      if (nodeConfig.network && nodeConfig.network.noSpendingConditions) {
        throw new Error('Spending Conditions are not supported on this network');
      }

      // colorMap for a color to address mapping
      const colorMap = {};

      addColors(colorMap, await getColors(bridgeState, false), 0);
      addColors(colorMap, await getColors(bridgeState, true), NFT_COLOR_BASE);
      addColors(
        colorMap,
        await getColors(bridgeState, false, true),
        NST_COLOR_BASE
      );

      const toMint = [];
      const LEAPTokenColor = 0;
      const txInputLen = tx.inputs.length;
      // signature for replay protection
      const sigHashBuf = tx.sigHashBuf();

      let spendingInput;
      let spendingInputUnspent;
      let spendingAddrBuf;
      let spendingAddress;

      // this is a bag of N(F/S)Ts to remember and update owners
      const nftBag = {};
      // this is a bag of ERC20s to help transform inputs to outputs
      const tokenBag = {};

      for (let i = 0; i < txInputLen; i += 1) {
        const input = tx.inputs[i];
        const unspent = state.unspent[input.prevout.hex()];

        if (!unspent) {
          throw new Error(`unspent: ${input.prevout.hex()} does not exists`);
        }

        const tokenValueBuf = utils.setLengthLeft(
          utils.toBuffer(`0x${BigInt(unspent.value).toString(16)}`),
          32
        );
        const contractAddr = colorMap[unspent.color];
        const contractAddrStr = `0x${contractAddr.toString('hex')}`;
        if (isNFT(unspent.color) || isNST(unspent.color)) {
          const tokenId = `0x${tokenValueBuf.toString('hex')}`;
          nftBag[contractAddrStr] = !nftBag[contractAddrStr]
            ? {}
            : nftBag[contractAddrStr];
          nftBag[contractAddrStr][tokenId] = unspent.address;
        } else if (i > 0) {
          tokenBag[contractAddrStr] = !tokenBag[contractAddrStr]
            ? {}
            : tokenBag[contractAddrStr];
          if (!tokenBag[contractAddrStr][unspent.address]) {
            tokenBag[contractAddrStr][unspent.address] = BigInt(unspent.value);
          } else {
            tokenBag[contractAddrStr][unspent.address] = add(
              tokenBag[contractAddrStr][unspent.address],
              BigInt(unspent.value)
            );
          }
        }

        if (!contractAddr) {
          // just to make sure
          throw new Error(`No contract for color: ${unspent.color}`);
        }

        if (input.script) {
          if (!input.msgData) {
            throw new Error('You need to supply both the script and message data');
          }

          // For now we require LEAP token (color = 0) for paying gas.
          // In the future we may want a new transaction type for
          // proposing other tokens to be eligible for paying gas to a specifiec ratio to
          // the LEAP token.
          if (unspent.color !== LEAPTokenColor) {
            throw new Error('Only color 0 is supported to pay for gas right now.');
          }

          // TODO:
          // we only allow one spending condition in an transaction, do we want to throw if we find more?
          spendingInput = input;
          spendingInputUnspent = unspent;
          spendingAddrBuf = utils.ripemd160(spendingInput.script);
          spendingAddress = `0x${spendingAddrBuf.toString('hex')}`;

          // continue, input of spending condition is just for gas and will not be minted
          // but any leftover after subtracting gas is returned to the owner as the last output.

          // eslint-disable-next-line no-continue
          continue;
        }

        // XXX: owner
        let addrBuf = Buffer.from(unspent.address.replace('0x', ''), 'hex');
        const spendingIsOwner = addrBuf.equals(spendingAddrBuf);

        let callData;
        let bytecode;
        let allowance;

        if (!spendingIsOwner) {
          if (unspent.address !== input.signer) {
            throw new Error(
              `output owner ${unspent.address} unequal input signer: ${
                input.signer
              }`
            );
          }
          allowance = {};
        } else {
          addrBuf = sigHashBuf;
        }

        if (isNST(unspent.color)) {
          callData = Buffer.concat([
            ERC1948_MINT_FUNCSIG,
            addrBuf,
            tokenValueBuf,
            utils.toBuffer(unspent.data),
          ]);
          bytecode = ERC1948_BYTECODE;

          if (allowance) {
            allowance = {
              from: addrBuf,
              callData: Buffer.concat([
                ERC721_APPROVE_FUNCSIG,
                sigHashBuf,
                tokenValueBuf,
              ]),
            };
          }
        } else if (isNFT(unspent.color)) {
          callData = Buffer.concat([ERC721_MINT_FUNCSIG, addrBuf, tokenValueBuf]);
          bytecode = ERC721_BYTECODE;

          if (allowance) {
            allowance = {
              from: addrBuf,
              callData: Buffer.concat([
                ERC721_APPROVE_FUNCSIG,
                sigHashBuf,
                tokenValueBuf,
              ]),
            };
          }
        } else {
          callData = Buffer.concat([ERC20_MINT_FUNCSIG, addrBuf, tokenValueBuf]);
          bytecode = ERC20_BYTECODE;

          if (allowance) {
            allowance = {
              from: addrBuf,
              callData: Buffer.concat([
                ERC20_INCREASE_ALLOWANCE_FUNCSIG,
                sigHashBuf,
                tokenValueBuf,
              ]),
            };
          }
        }

        toMint.push({
          contractAddr,
          callData,
          bytecode,
          color: unspent.color,
          allowance,
        });
      }
      // creating a new VM instance
      const vm = new VM({ hardfork: 'petersburg' });

      // deploy spending condition
      await setAccountCode(spendingInput.script, sigHashBuf, vm.stateManager);

      // creating the reactor account with some wei for minting
      const reactorAccount = new Account();

      reactorAccount.balance = '0xf00000000000000001';
      await setAccount(reactorAccount, REACTOR_ADDR, vm.stateManager);

      // for deploying colors and mint tokens
      let nonceCounter = 0;

      // keep track of deployed contracts
      const deployed = {};
      const nonces = {};

      // now deploy the contracts and mint all tokens
      while (toMint.length) {
        const obj = toMint.pop();
        const addrHex = obj.contractAddr.toString('hex');

        if (deployed[`0x${addrHex}`] === undefined) {
          deployed[`0x${addrHex}`] = obj.color;
          // eslint-disable-next-line no-await-in-loop
          await setAccountCode(obj.bytecode, obj.contractAddr, vm.stateManager);
        }

        // eslint-disable-next-line no-await-in-loop
        await runTx(vm, {
          nonce: nonceCounter,
          gasLimit: GAS_LIMIT_HEX,
          to: obj.contractAddr,
          data: obj.callData,
        });
        nonceCounter += 1;

        // for approval / allowance
        if (obj.allowance) {
          const owner = obj.allowance.from.toString('hex');
          // eslint-disable-next-line no-bitwise
          const nonce = nonces[owner] | 0;

          if (nonce === 0) {
            const acc = new Account();
            acc.balance = '0xf00000000000000001';
            // eslint-disable-next-line no-await-in-loop
            await setAccount(acc, obj.allowance.from, vm.stateManager);
          }

          // eslint-disable-next-line no-await-in-loop
          await runTx(
            vm,
            {
              nonce,
              gasLimit: GAS_LIMIT_HEX,
              to: obj.contractAddr,
              data: obj.allowance.callData,
            },
            obj.allowance.from
          );

          // update nonce
          nonces[owner] = nonce + 1;
        }
      }

      // need to commit to trie, needs a checkpoint first 🤪
      await new Promise(resolve => {
        vm.stateManager.checkpoint(() => {
          vm.stateManager.commit(() => {
            resolve();
          });
        });
      });

      const evmResult = await runTx(vm, {
        nonce: nonceCounter,
        gasLimit: GAS_LIMIT_HEX, // TODO: set gas Limit to (inputs - outputs) / gasPrice
        to: sigHashBuf,
        // NOPE: the plasma address is replaced with sighash, to prevent replay attacks
        data: spendingInput.msgData,
      });

      const logOuts = [];

      // iterate through all events
      evmResult.vm.logs.forEach(log => {
        const originAddr = `0x${log[0].toString('hex')}`;
        const topics = log[1];
        const data = log[2];
        const originColor = deployed[originAddr];

        if (originColor === undefined) {
          return;
        }

        if (isNST(originColor) && topics[0].equals(ERC1948_DATA_UPDATED_EVENT)) {
          const nstTokenId = `0x${topics[1].toString('hex')}`;
          // const nstFromData = data.slice(0, 32);
          const nstToData = `0x${data.slice(32, 64).toString('hex')}`;

          const tokenOwner = nftBag[originAddr][nstTokenId];
          logOuts.push(
            new Output(BigInt(nstTokenId), tokenOwner, originColor, nstToData)
          );
          return;
        }

        if (topics[0].equals(ERC20_ERC721_TRANSFER_EVENT)) {
          let fromAddr = topics[1].slice(12, 32);
          let toAddr = topics[2].slice(12, 32);

          // replace injected sigHash with plasma address
          if (toAddr.equals(sigHashBuf)) {
            toAddr = spendingAddress;
          } else {
            toAddr = `0x${toAddr.toString('hex')}`;
          }
          if (fromAddr.equals(sigHashBuf)) {
            fromAddr = spendingAddress;
          } else {
            fromAddr = `0x${fromAddr.toString('hex')}`;
          }

          // todo: support transfer of ERC1948
          if (!isNFT(originColor) && data.length === 0) {
            // this hack assumes that an ERC1949 is minted
            // and that Transfer Event is emmited before UpdateData Event
            // so in only puts the new owner into the nftBag
            nftBag[originAddr][`0x${topics[3].toString('hex')}`] = toAddr;
            return;
          }
          // ? ERC721(tokenId) : ERC20(transferAmount)
          const transferAmount = isNFT(originColor)
            ? BigInt(`0x${topics[3].toString('hex')}`)
            : BigInt(`0x${data.toString('hex')}`, 16);

          if (isNFT(originColor) || isNST(originColor)) {
            logOuts.push(new Output(transferAmount, toAddr, originColor));
          } else {
            tokenBag[originAddr][fromAddr] = subtract(
              tokenBag[originAddr][fromAddr],
              transferAmount
            );
            if (!tokenBag[originAddr][toAddr]) {
              tokenBag[originAddr][toAddr] = BigInt(0);
            }
            tokenBag[originAddr][toAddr] = add(
              tokenBag[originAddr][toAddr],
              transferAmount
            );
          }
        }
      });
      for (const originAddr in tokenBag) {
        if (Object.prototype.hasOwnProperty.call(tokenBag, originAddr)) {
          for (const owner in tokenBag[originAddr]) {
            if (Object.prototype.hasOwnProperty.call(tokenBag[originAddr], owner)) {
              if (greaterThan(tokenBag[originAddr][owner], BigInt(0))) {
                logOuts.push(
                  new Output(
                    tokenBag[originAddr][owner],
                    owner,
                    deployed[originAddr]
                  )
                );
              }
            }
          }
        }
      }

      const gasUsed = BigInt(evmResult.gasUsed);
      // XXX: Fixed gasPrice for now. We include it again in the tx format as the next breaking change.
      const gasPrice = FIXED_GAS_PRICE;

      const minGasPrice = BigInt(
        bridgeState.minGasPrices[bridgeState.minGasPrices.length - 1]
      );

      if (lessThan(gasPrice, minGasPrice)) {
        return Promise.reject(
          new Error(
            `tx gasPrice ${gasPrice.toString()} less than minGasPrice: ${minGasPrice.toString()}`
          )
        );
      }

      const gasChange = subtract(
        BigInt(spendingInputUnspent.value),
        multiply(gasPrice, gasUsed)
      );

      if (lessThan(gasChange, BigInt(0))) {
        throw new Error(
          'Not enough input for spending condition to cover gas costs'
        );
      }

      // Now return the leftovers
      logOuts.push(
        new Output(
          gasChange,
          spendingInputUnspent.address,
          spendingInputUnspent.color
        )
      );

      // TODO: compact logOuts
      if (!isEqual(tx.outputs, logOuts)) {
        const txOuts = tx.outputs
          .map(output => JSON.stringify(output.toJSON()))
          .join(',');
        const logs = logOuts
          .map(output => JSON.stringify(output.toJSON()))
          .join(',');
        const err = new Error(
          `outputs do not match computation results. \n outputs ${txOuts} \n calculated: ${logs}`
        );
        err.logOuts = logOuts;
        return Promise.reject(err);
      }
      return Promise.resolve(logOuts);
    };