deserializeTx method

Future<Transaction> deserializeTx (ScriptReader r)

Implementation

Future<Transaction> deserializeTx(ScriptReader r) async {
  var tx = await Transaction.deserialize(r);
  var code = Convert.bytesToHexStr(tx.payload.serialize());

  var contractIdx1 =
      code.indexOf('14' + '000000000000000000000000000000000000000');
  var contractIdx2 =
      code.indexOf('14' + '0000000000000000000000000000000000000002');

  if (contractIdx1 > 0 && substr(code, contractIdx1 + 41, 1) == '1') {
    tx.tokenType = 'ONT';
  } else if (contractIdx1 > 0 && substr(code, contractIdx1 + 41, 1) == '2') {
    tx.tokenType = 'ONG';
  } else {
    throw ArgumentError('Deformed transfer tx');
  }

  var contractIdx = max(contractIdx1, contractIdx2);
  var params = substr(code, 0, contractIdx);
  var paramsEnd = code.indexOf('6a7cc86c') + 8;
  String method;
  if (substr(params, paramsEnd, 4) == '51c1') {
    method = substr(params, paramsEnd + 6, params.length - paramsEnd - 6);
  } else {
    method = substr(params, paramsEnd + 2, params.length - paramsEnd - 2);
  }
  tx.method = Convert.hexStrToStr(method);

  var sb = ScriptReader(Buffer.fromBytes(Convert.hexStrToBytes(params)));
  if (tx.method == 'transfer') {
    sb.forward(5);
    tx.from = Address(sb.forward(20));
    sb.forward(4);
    tx.to = Address(sb.forward(20));
    sb.forward(3);
    var numTmp = sb.readUint8();
    if (Convert.bytesToHexStr(sb.branch(sb.ofst).forward(3)) == '6a7cc8') {
      tx.amount = BigInt.from(numTmp - 80);
    } else {
      tx.amount = Convert.bytesToBigInt(sb.forward(numTmp));
    }
  } else if (tx.method == 'transferFrom') {
    sb.advance(5);
    tx.from = Address(sb.forward(20));
    sb.advance(28);
    tx.to = Address(sb.forward(20));
    sb.advance(3);
    var numTmp = sb.readUint8();
    if (Convert.bytesToHexStr(sb.branch(sb.ofst).forward(3)) == '6a7cc8') {
      tx.amount = BigInt.from(numTmp - 80);
    } else {
      tx.amount = Convert.bytesToBigInt(sb.forward(numTmp));
    }
  } else {
    throw ArgumentError('Deformed transfer tx');
  }
  return tx;
}