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 #{self.telegram_id} identity whoami; echo $?`.to_i == 255
        `dfx identity new #{self.telegram_id} --storage-mode=plaintext`
          self.icp_principal = `dfx --identity #{self.telegram_id} identity get-principal`.strip
          self.icp_id = `dfx --identity #{self.telegram_id} ledger account-id`.strip
          self.save
        else
          # Identity already exist
          self.icp_id = `dfx --identity #{self.telegram_id} ledger account-id`.strip
          self.icp_principal = `dfx --identity #{self.telegram_id} identity get-principal`.strip
          self.save
        end
      end
    elsif
      self.icp_id = `dfx --identity #{self.telegram_id} ledger account-id`.strip
      self.icp_principal = `dfx --identity #{self.telegram_id} identity get-principal`.strip
      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 #{self.telegram_id} identity whoami; echo $?`.to_i == 255
        `dfx identity new #{self.telegram_id} --storage-mode=plaintext`
          self.icp_principal = `dfx --identity #{self.telegram_id} identity get-principal`.strip
          self.icp_id = `dfx --identity #{self.telegram_id} ledger account-id`.strip
          self.save
        else
          # Identity already exist
          self.icp_id = `dfx --identity #{self.telegram_id} ledger account-id`.strip
          self.icp_principal = `dfx --identity #{self.telegram_id} identity get-principal`.strip
          self.save
        end
      end
    elsif
      self.icp_id = `dfx --identity #{self.telegram_id} ledger account-id`.strip
      self.icp_principal = `dfx --identity #{self.telegram_id} identity get-principal`.strip
      self.save
    end
    return self.icp_id
  end
end
