/**
   * Saves and outgoing transaction into the database
   *
   * @param hash
   * @param cryptoStatus
   * @param blockDepth
   * @param addressTo
   * @param addressFrom
   * @param value
   * @param op_Return
   * @param protocolStatus
   * @throws CantExecuteDatabaseOperationException
   */
  public void saveNewOutgoingTransaction(
      String hash,
      CryptoStatus cryptoStatus,
      int blockDepth,
      CryptoAddress addressTo,
      CryptoAddress addressFrom,
      long value,
      String op_Return,
      ProtocolStatus protocolStatus)
      throws CantExecuteDatabaseOperationException {
    DatabaseTable databaseTable =
        database.getTable(BitcoinCryptoNetworkDatabaseConstants.OUTGOING_TRANSACTIONS_TABLE_NAME);
    DatabaseTableRecord record = databaseTable.getEmptyRecord();

    /** generates the trx_id */
    UUID trxId = UUID.randomUUID();

    record.setUUIDValue(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_TRX_ID_COLUMN_NAME, trxId);
    record.setStringValue(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_HASH_COLUMN_NAME, hash);
    record.setStringValue(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_CRYPTO_STATUS_COLUMN_NAME,
        cryptoStatus.getCode());
    record.setIntegerValue(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_BLOCK_DEPTH_COLUMN_NAME,
        blockDepth);
    record.setStringValue(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_ADDRESS_TO_COLUMN_NAME,
        addressTo.getAddress());
    record.setStringValue(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_ADDRESS_FROM_COLUMN_NAME,
        addressFrom.getAddress());
    record.setDoubleValue(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_VALUE_COLUMN_NAME, value);
    record.setStringValue(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_OP_RETURN_COLUMN_NAME,
        op_Return);
    record.setStringValue(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_PROTOCOL_STATUS_COLUMN_NAME,
        protocolStatus.getCode());
    record.setStringValue(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_LAST_UPDATE_COLUMN_NAME,
        getCurrentDateTime());
    try {
      databaseTable.insertRecord(record);
    } catch (CantInsertRecordException e) {
      StringBuilder outputMessage =
          new StringBuilder(
              "There was an error inserting a new transaction in the Outgoing Transactions Table. Transaction record is:");
      outputMessage.append(System.lineSeparator());
      outputMessage.append(XMLParser.parseObject(record));

      throw new CantExecuteDatabaseOperationException(
          CantExecuteDatabaseOperationException.DEFAULT_MESSAGE,
          e,
          outputMessage.toString(),
          "database issue.");
    }
  }
  /**
   * Updates the Key detailed table for this account and key, with the passed addres
   *
   * @param hierarchyAccountId
   * @param ecKey
   * @param cryptoAddress
   * @param blockchainNetworkType
   */
  public void updateKeyDetailedStatsWithNewAddress(
      int hierarchyAccountId,
      ECKey ecKey,
      CryptoAddress cryptoAddress,
      BlockchainNetworkType blockchainNetworkType)
      throws CantExecuteDatabaseOperationException, UnexpectedResultReturnedFromDatabaseException {
    /** If we are not allowed to save detailed information then we will exit */
    if (!VaultKeyMaintenanceParameters.STORE_DETAILED_KEY_INFORMATION) return;

    DatabaseTable databaseTable =
        database.getTable(
            AssetsOverBitcoinCryptoVaultDatabaseConstants.KEY_MAINTENANCE_DETAIL_TABLE_NAME);
    databaseTable.addStringFilter(
        AssetsOverBitcoinCryptoVaultDatabaseConstants.KEY_MAINTENANCE_DETAIL_ACCOUNT_ID_COLUMN_NAME,
        String.valueOf(hierarchyAccountId),
        DatabaseFilterType.EQUAL);
    databaseTable.addStringFilter(
        AssetsOverBitcoinCryptoVaultDatabaseConstants.KEY_MAINTENANCE_DETAIL_PUBLIC_KEY_COLUMN_NAME,
        ecKey.getPublicKeyAsHex(),
        DatabaseFilterType.EQUAL);

    try {
      databaseTable.loadToMemory();
    } catch (CantLoadTableToMemoryException e) {
      throwLoadToMemoryException(e, databaseTable.getTableName());
    }

    if (databaseTable.getRecords().size() == 0) {
      StringBuilder output = new StringBuilder("The key " + ecKey.toString());
      output.append(System.lineSeparator());
      output.append("which generated the address " + cryptoAddress.getAddress());
      output.append(System.lineSeparator());
      output.append("is not a key derived from the vault.");

      throw new UnexpectedResultReturnedFromDatabaseException(
          null, output.toString(), "Vault derivation miss match");
    }

    DatabaseTableRecord record = databaseTable.getRecords().get(0);
    record.setStringValue(
        AssetsOverBitcoinCryptoVaultDatabaseConstants.KEY_MAINTENANCE_DETAIL_ADDRESS_COLUMN_NAME,
        cryptoAddress.getAddress());
    record.setStringValue(
        AssetsOverBitcoinCryptoVaultDatabaseConstants
            .KEY_MAINTENANCE_DETAIL_BLOCKCHAIN_NETWORK_TYPE_COLUMN_NAME,
        blockchainNetworkType.getCode());
    try {
      databaseTable.updateRecord(record);
    } catch (CantUpdateRecordException e) {
      throw new CantExecuteDatabaseOperationException(
          CantExecuteDatabaseOperationException.DEFAULT_MESSAGE,
          e,
          "error updating record",
          "database issue");
    }
  }
Пример #3
0
 public void persistDigitalAsset(
     DigitalAssetMetadata digitalAssetMetadata, ActorAssetUser actorAssetUser)
     throws CantPersistDigitalAssetException, CantCreateDigitalAssetFileException {
   System.out.println(
       "ASSET DISTRIBUTION Persist DAM: " + digitalAssetMetadata.getGenesisTransaction());
   setDigitalAssetLocalFilePath(digitalAssetMetadata);
   CryptoAddress cryptoAddress = actorAssetUser.getCryptoAddress();
   String actorAddress;
   if (cryptoAddress == null) {
     actorAddress = "UNDEFINED";
     System.out.println("ASSET DISTRIBUTION Harcoding Actor address because is null");
   } else {
     actorAddress = cryptoAddress.getAddress();
   }
   this.assetDistributionDao.persistDigitalAsset(
       digitalAssetMetadata.getGenesisTransaction(),
       this.digitalAssetFileStoragePath,
       digitalAssetMetadata.getDigitalAssetHash(),
       actorAssetUser.getActorPublicKey(),
       actorAddress);
   System.out.println("ASSET DISTRIBUTION registered in database");
   persistInLocalStorage(digitalAssetMetadata);
 }
Пример #4
0
 private void getWalletAddress(String contact_name) {
   try {
     // TODO parameters deliveredByActorId deliveredByActorType harcoded..
     CryptoAddress cryptoAddress =
         cryptoWallet.requestAddressToNewExtraUser(
             user_id,
             Actors.INTRA_USER,
             contact_name,
             Platforms.CRYPTO_CURRENCY_PLATFORM,
             VaultType.CRYPTO_CURRENCY_VAULT,
             "BITV",
             null,
             ReferenceWallet.BASIC_WALLET_BITCOIN_WALLET);
     user_address_wallet = cryptoAddress.getAddress();
   } catch (CantRequestCryptoAddressException e) {
     // referenceWalletSession.getErrorManager().reportUnexpectedUIException(UISource.ACTIVITY,
     // UnexpectedUIExceptionSeverity.CRASH, FermatException.wrapException(e));
     Toast.makeText(
             getActivity().getApplicationContext(),
             "Oooops! recovering from system error",
             Toast.LENGTH_SHORT)
         .show();
   }
 }
  private void loadRecordAsNew(
      DatabaseTableRecord databaseTableRecord,
      UUID trxId,
      String txHash,
      String walletPublicKey,
      CryptoAddress destinationAddress,
      long cryptoAmount,
      String op_Return,
      String notes,
      String deliveredByActorPublicKey,
      Actors deliveredByActorType,
      String deliveredToActorPublicKey,
      Actors deliveredToActorType,
      ReferenceWallet referenceWallet,
      boolean sameDevice,
      BlockchainNetworkType blockchainNetworkType) {

    databaseTableRecord.setUUIDValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_TRANSACTION_ID_COLUMN_NAME, trxId);
    //        if(requestId != null)
    //
    // databaseTableRecord.setUUIDValue(OutgoingIntraActorTransactionDatabaseConstants.OUTGOING_DRAFT_REQUEST_ID_COLUMN_NAME, requestId);

    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants
            .OUTGOING_DRAFT_WALLET_ID_TO_DEBIT_FROM_COLUMN_NAME,
        walletPublicKey);

    // TODO: This will be completed when the vault gives it to us
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_TRANSACTION_HASH_COLUMN_NAME,
        txHash);

    // TODO: This need to be completed in the future
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_ADDRESS_FROM_COLUMN_NAME,
        "MY_ADDRESS");
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_ADDRESS_TO_COLUMN_NAME,
        destinationAddress.getAddress());
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_CRYPTO_CURRENCY_COLUMN_NAME,
        destinationAddress.getCryptoCurrency().getCode());
    databaseTableRecord.setLongValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_CRYPTO_AMOUNT_COLUMN_NAME,
        cryptoAmount);
    if (op_Return != null)
      databaseTableRecord.setStringValue(
          OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_OP_RETURN_COLUMN_NAME,
          op_Return);
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_TRANSACTION_STATUS_COLUMN_NAME,
        com.bitdubai.fermat_ccp_plugin.layer.crypto_transaction.outgoing_draft.developer.bitdubai
            .version_1.enums.TransactionState.NEW.getCode());

    // TODO: This have to be changed for the tinestamp when the network recognize the transaction

    // eze te saco la division para obtener el timestamp bien
    databaseTableRecord.setLongValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_TIMESTAMP_COLUMN_NAME,
        System.currentTimeMillis());
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_DESCRIPTION_COLUMN_NAME, notes);
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_CRYPTO_STATUS_COLUMN_NAME,
        CryptoStatus.PENDING_SUBMIT.getCode());

    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_ACTOR_FROM_PUBLIC_KEY_COLUMN_NAME,
        deliveredByActorPublicKey);
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_ACTOR_FROM_TYPE_COLUMN_NAME,
        deliveredByActorType.getCode());
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_ACTOR_TO_PUBLIC_KEY_COLUMN_NAME,
        deliveredToActorPublicKey);
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_ACTOR_TO_TYPE_COLUMN_NAME,
        deliveredToActorType.getCode());
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_SAME_DEVICE_COLUMN_NAME,
        String.valueOf(sameDevice));
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_WALLET_REFERENCE_TYPE_COLUMN_NAME,
        referenceWallet.getCode());
    databaseTableRecord.setStringValue(
        OutgoingDraftTransactionDatabaseConstants.OUTGOING_DRAFT_RUNNING_NETWORK_TYPE,
        blockchainNetworkType.getCode());
  }
  /**
   * Gets the incoming transaction data and forms the CryptoTransaction object
   *
   * @param txHash
   * @return
   * @throws CantExecuteDatabaseOperationException
   */
  public List<CryptoTransaction> getIncomingCryptoTransaction(String txHash)
      throws CantExecuteDatabaseOperationException {
    DatabaseTable databaseTable =
        database.getTable(BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_TABLE_NAME);
    databaseTable.setStringFilter(
        BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_HASH_COLUMN_NAME,
        txHash,
        DatabaseFilterType.EQUAL);

    try {
      databaseTable.loadToMemory();
    } catch (CantLoadTableToMemoryException e) {
      throwLoadToMemoryException(e, databaseTable.getTableName());
    }

    List<CryptoTransaction> cryptoTransactions = new ArrayList<>();
    for (DatabaseTableRecord record : databaseTable.getRecords()) {

      /** Gets all the values */
      CryptoAddress addressFrom = new CryptoAddress();
      addressFrom.setAddress(
          record.getStringValue(
              BitcoinCryptoNetworkDatabaseConstants
                  .INCOMING_TRANSACTIONS_ADDRESS_FROM_COLUMN_NAME));
      addressFrom.setCryptoCurrency(CryptoCurrency.BITCOIN);

      CryptoAddress addressTo = new CryptoAddress();
      addressFrom.setAddress(
          record.getStringValue(
              BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_ADDRESS_TO_COLUMN_NAME));
      addressFrom.setCryptoCurrency(CryptoCurrency.BITCOIN);

      long amount =
          record.getLongValue(
              BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_VALUE_COLUMN_NAME);

      CryptoStatus cryptoStatus = null;
      try {
        cryptoStatus =
            CryptoStatus.getByCode(
                record.getStringValue(
                    BitcoinCryptoNetworkDatabaseConstants
                        .INCOMING_TRANSACTIONS_CRYPTO_STATUS_COLUMN_NAME));
      } catch (InvalidParameterException e) {
        e.printStackTrace();
      }

      String op_Return =
          record.getStringValue(
              BitcoinCryptoNetworkDatabaseConstants.INCOMING_TRANSACTIONS_OP_RETURN_COLUMN_NAME);

      /** Forms the CryptoTransaction object */
      CryptoTransaction cryptoTransaction = new CryptoTransaction();
      cryptoTransaction.setTransactionHash(txHash);
      cryptoTransaction.setAddressTo(addressTo);
      cryptoTransaction.setAddressFrom(addressFrom);
      cryptoTransaction.setCryptoAmount(amount);
      cryptoTransaction.setCryptoCurrency(CryptoCurrency.BITCOIN);
      cryptoTransaction.setCryptoStatus(cryptoStatus);
      cryptoTransaction.setOp_Return(op_Return);

      /** adds it to the list */
      cryptoTransactions.add(cryptoTransaction);
    }

    return cryptoTransactions;
  }