class UniversaTools::UWallet

Under construction. FS-based U wallet. This version misses support for multiple-resulting paying parcel operation, so it will be completely reqwritten. Please do not use.

Attributes

balance[R]

U left

test_balance[R]

TestU left

Public Class Methods

new(path, key: nil, keyring: nil, client: nil) click to toggle source

@param [String] path to the U ucontract @param [PrivateKey] key for the UContract @param [KeyRing] keyring to look for a key in @param [Universa::Client] client to connect to the Universa

# File lib/universa_tools/u_wallet.rb, line 40
def initialize(path, key: nil, keyring: nil, client: nil)
  key || keyring or raise ArgumentError, "key or keyring must be presented"
  key && keyring and raise ArgumentError, "only one of key or keyring must be presented"
  @client = client || Universa::Client.new
  restore_state path
  check_key(key, keyring)
  @mutex = Mutex.new
end

Public Instance Methods

busy?() click to toggle source
# File lib/universa_tools/u_wallet.rb, line 49
def busy?
  @mutex.locked?
end
register(contract) click to toggle source
# File lib/universa_tools/u_wallet.rb, line 53
def register contract
  @mutex.synchronize {
    contract.check or raise ArgumentError, "contract is not OK"
    units = contract.getProcessedCostU()
    puts "cost #{units}"
    parcel = Universa::Parcel.of(contract, @u, [@key])
    p parcel
    raise InsufficientFundsException if units > balance
    # @client.2
  }
end

Private Instance Methods

backup_name() click to toggle source
# File lib/universa_tools/u_wallet.rb, line 107
def backup_name
  @back_name ||= @current_name + "~"
end
check_key(key, keyring) click to toggle source
# File lib/universa_tools/u_wallet.rb, line 126
def check_key(key, keyring)
  address = @u.owner.getSimpleAddress
  @key = if key
           address.isMatchingKey(key.public_key) or raise ArgumentError, "key is wrong"
           key
         else
           keyring[address] or raise ArgumentError, "keyring does not contain required key"
         end
end
delete_backup() click to toggle source
# File lib/universa_tools/u_wallet.rb, line 103
def delete_backup
  File.exists?(backup_name) and FileUtils.rm(backup_name)
end
load_u(path) click to toggle source

Load and check U contract, check that it is a valid U contract, does not checks it state! @param [String] path to load contract from @return [Contract]

# File lib/universa_tools/u_wallet.rb, line 90
def load_u(path)
  u = open(path, 'rb') { |f| Contract.from_packed(f.read) }
  d = u.definition
  # {"issuerName"=>"Universa Reserve System", "name"=>"transaction units pack"}
  # J3uaVvHE7JqhvVb1c26RyDhfJw9eP2KR1KRhm2VdmYx7NwHpzdHTyEPjcmKpgkJAtzWLSPUw
  p u.issuer.getSimpleAddress.to_s
  if d['issuerName'] != 'Universa Reserve System' || d['name'] != 'transaction units pack' ||
      u.issuer.getSimpleAddress.to_s != 'J3uaVvHE7JqhvVb1c26RyDhfJw9eP2KR1KRhm2VdmYx7NwHpzdHTyEPjcmKpgkJAtzWLSPUw'
    error("not a U contract")
  end
  u
end
restore_state(path) click to toggle source
# File lib/universa_tools/u_wallet.rb, line 67
def restore_state(path)
  @current_name = File.expand_path path
  # current path may not exist if the, say commit or rollback operation were interrupted
  if File.exists?(@current_name)
    @u = load_u(@current_name)
    if @client.get_state(@u).approved?
      delete_backup()
      set_balance()
      return
    end
  end
  # main state is bad or missing - checking prev state
  @u = load_u(backup_name)
  if (@client.get_state(@u)).approved?
    rollback()
  else
    error("No valid U contracts found")
  end
end
rollback() click to toggle source

last become main main dissapears

# File lib/universa_tools/u_wallet.rb, line 118
def rollback
  File.exists?(@current_name) and FileUtils.rm(@current_name)
  # if it is interrupted, it will correctly restore on next run
  FileUtils.move backup_name, @current_name
  @u = load_u(@current_name)
  set_balance
end
set_balance() click to toggle source
# File lib/universa_tools/u_wallet.rb, line 111
def set_balance
  @balance = @u.state.transaction_units
  @test_balance = @u.state.test_transaction_units
end