class TelegramWallet < ActiveRecord::Base
  def get_icp_principal
    return self.icp_principal if self.icp_principal
    if self.icp_id.nil?
      if !self.telegram_id.nil?
        if `dfx identity use #{self.telegram_id}; echo $?`.to_i == 255
        `dfx identity new #{self.telegram_id} --storage-mode=plaintext`
          `dfx identity use #{self.telegram_id}`
          self.icp_principal = `dfx identity get-principal`.strip
          self.icp_id = `dfx ledger account-id`.strip
          `dfx identity use anonymous`
          self.save
        else
          # Identity already exist
          self.icp_id = `dfx ledger account-id`.strip
          self.icp_principal = `dfx identity get-principal`.strip
          `dfx identity use anonymous`
          self.save
        end
      end
    elsif
      `dfx identity use #{self.telegram_id}`
      self.icp_id = `dfx ledger account-id`.strip
      self.icp_principal = `dfx identity get-principal`.strip
      `dfx identity use anonymous`
      self.save
    end
    return self.icp_principal 
  end
  def get_icp_id
    return self.icp_id if self.icp_id
    if self.icp_principal.nil?
      if !self.telegram_id.nil?
        if `dfx identity use #{self.telegram_id}; echo $?`.to_i == 255
        `dfx identity new #{self.telegram_id} --storage-mode=plaintext`
          `dfx identity use #{self.telegram_id}`
          self.icp_principal = `dfx identity get-principal`.strip
          self.icp_id = `dfx ledger account-id`.strip
          `dfx identity use anonymous`
          self.save
        else
          # Identity already exist
          self.icp_id = `dfx ledger account-id`.strip
          self.icp_principal = `dfx identity get-principal`.strip
          `dfx identity use anonymous`
          self.save
        end
      end
    elsif
      `dfx identity use #{self.telegram_id}`
      self.icp_id = `dfx ledger account-id`.strip
      self.icp_principal = `dfx identity get-principal`.strip
      `dfx identity use anonymous`
      self.save
    end
    return self.icp_id
  end
end
