WalletService.java

  1. package com.syntifi.near.api.service;

  2. import com.syntifi.near.api.json.JsonHelper;
  3. import com.syntifi.near.api.model.key.Wallet;

  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileWriter;
  7. import java.io.IOException;

  8. /**
  9.  * Service methods to manipulate wallets
  10.  *
  11.  * @author Alexandre Carvalho
  12.  * @author Andre Bertolace
  13.  * @since 0.0.1
  14.  */
  15. public class WalletService {
  16.     /**
  17.      * Loads a wallet from filesystem
  18.      *
  19.      * @param file the file to read from
  20.      * @return a wallet with loaded content
  21.      * @throws IOException thrown if file not found or not a valid wallet json
  22.      */
  23.     public static Wallet loadWalletFromFile(File file) throws IOException {
  24.         return JsonHelper.OBJECT_MAPPER.readValue(
  25.                 JsonHelper.loadJsonFromFile(file),
  26.                 Wallet.class);
  27.     }

  28.     /**
  29.      * Writes a wallet to filesystem
  30.      *
  31.      * @param file   the file to write to
  32.      * @param wallet the wallet to persist
  33.      * @throws IOException thrown if file could not be written
  34.      */
  35.     public static void writeWalletToFile(File file, Wallet wallet) throws IOException {
  36.         try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
  37.             writer.write(JsonHelper.OBJECT_MAPPER.writeValueAsString(wallet));
  38.         }
  39.     }
  40. }