Class: Universa::Contract

Inherits:
RemoteAdapter show all
Defined in:
lib/universa/contract.rb

Overview

Universa contract adapter.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RemoteAdapter

#__getobj__, #__setobj__, #initialize, #inspect, invoke_static, remote_class, remote_class_name, remote_field, static_method, #to_s

Constructor Details

This class inherits a constructor from Universa::RemoteAdapter

Class Method Details

.create(issuer_key, expires_at: (Time.now + 90 * 24 * 60 * 60), use_short_address: false) ⇒ Contract

Create simple contract with preset critical parts:

  • expiration set to 90 days unless specified else

  • issuer role is set to the address of the issuer key_address, short ot long

  • creator role is set as link to issuer

  • owner role is set as link to issuer

  • change owner permission is set to link to owner

The while contract is then signed by the issuer key_address. Not that it will not seal it: caller almost always will add more data before it, then must call #seal().

Parameters:

  • issuer_key (PrivateKey)

    also will be used to sign it

  • expires_at (Time) (defaults to: (Time.now + 90 * 24 * 60 * 60))

    defaults to 90 days

  • use_short_address (Boolean) (defaults to: false)

    set to true to use short address of the issuer key_address in the role

Returns:

  • (Contract)

    simple contact, not sealed



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/universa/contract.rb', line 263

def self.create issuer_key, expires_at: (Time.now + 90 * 24 * 60 * 60), use_short_address: false
  contract = Contract.new
  contract.set_expires_at expires_at.utc
  contract.set_issuer_keys(use_short_address ? issuer_key.short_address : issuer_key.long_address)
  contract.register_role(contract.issuer.link_as("owner"))
  contract.register_role(contract.issuer.link_as("creator"))
  contract.add_permission ChangeOwnerPermission.new(contract.owner.link_as "@owner")
  contract.add_permission RevokePermission.new(contract.owner.link_as "@owner")
  contract.add_signer_key issuer_key
  contract
end

.from_packed(packed) ⇒ Object

Load from transaction pack



276
277
278
279
280
# File 'lib/universa/contract.rb', line 276

def self.from_packed packed
  packed.nil? and raise ArgumentError, "packed contract required"
  packed.force_encoding 'binary'
  self.invoke_static "fromPackedTransaction", packed
end

Instance Method Details

#==(other) ⇒ Object

Ruby-style contracts equality.



427
428
429
430
# File 'lib/universa/contract.rb', line 427

def == other
  return false if !other || !other.is_a?(Contract)
  hash_id == other.hash_id
end

#amountBigDecimal

Helper for many token-like contracts containing state.data.amount

Returns:

  • (BigDecimal)

    amount or nil



370
371
372
# File 'lib/universa/contract.rb', line 370

def amount
  v = state[:amount] and BigDecimal(v.to_s)
end

#amount=(value) ⇒ Object

Write helper for many token-like contracts containing state.data.amount. Saves value in state.data.anomount and properly encodes it so it will be preserved on packing.

Parameters:

  • value (Object)

    should be some representation of a number (also string)



378
379
380
# File 'lib/universa/contract.rb', line 378

def amount= (value)
  state[:amount] = value.to_s.force_encoding('utf-8')
end

#can_perform_role(name, *keys) ⇒ Object

Test that some set of keys could be used to perform some role.

Parameters:

  • name (String)

    of the role to check

  • keys (PublicKey)

    instances to check against



409
410
411
412
413
# File 'lib/universa/contract.rb', line 409

def can_perform_role(name, *keys)
  getRole(name.to_s).isAllowedForKeys(Set.new keys.map { |x|
    x.is_a?(PrivateKey) ? x.public_key : x
  })
end

#create_revocation(*keys) ⇒ Contract

Create a contract that revokes this one if register with the Universa network. BE CAREFUL! REVOCATION IS IRREVERSIBLE! period.

Parameters:

  • keys (PrivateKey)

    enough to allow this contract revocation

Returns:

  • (Contract)

    revocation contract. Register it with the Universa network to perform revocation.



420
421
422
423
424
# File 'lib/universa/contract.rb', line 420

def create_revocation(*keys)
  revoke = Service.umi.invoke_static 'ContractsService', 'createRevocation', *keys
  revoke.seal
  revoke
end

#creatorRole

Shortcut ofr get_creator

Returns:

  • (Role)

    universa role of the creator



296
297
298
# File 'lib/universa/contract.rb', line 296

def creator
  get_creator
end

#definitionObject

Returns definition data.

Returns:

  • definition data



349
350
351
# File 'lib/universa/contract.rb', line 349

def definition
  @definition ||= get_definition.get_data
end

#errors_stringString

Call it after check to get summaru of errors found.

Returns:

  • (String)

    possibly empty ”



401
402
403
# File 'lib/universa/contract.rb', line 401

def errors_string
  getErrors.map { |e| "(#{e.object || ''}): #{e.error}" }.join(', ').strip
end

#expires_atObject

shortcut for get_expires_at. Get the contract expiration time.



338
339
340
# File 'lib/universa/contract.rb', line 338

def expires_at
  get_expires_at
end

#expires_at=(time) ⇒ Object

set expires_at field

Parameters:

  • time (Time)

    when this contract will be expired, if yet APPROVED.



344
345
346
# File 'lib/universa/contract.rb', line 344

def expires_at=(time)
  set_expires_at time.utc
end

#hash_idHashId

shortcut for getHashId

Returns:

  • (HashId)

    of the contracr



323
324
325
# File 'lib/universa/contract.rb', line 323

def hash_id
  getId()
end

#issuerRole

Returns issuer role.

Returns:

  • (Role)

    issuer role



301
302
303
# File 'lib/universa/contract.rb', line 301

def issuer
  get_issuer
end

#keys_to_sign_withSet<PrivateKey>

returns keys that will be used to sign this contract on next #seal.

Returns:



290
291
292
# File 'lib/universa/contract.rb', line 290

def keys_to_sign_with
  get_keys_to_sign_with
end

#ok?Boolean

Shortcut for is_ok

Returns:

  • (Boolean)


317
318
319
# File 'lib/universa/contract.rb', line 317

def ok?
  is_ok
end

#originHashId

Returns of the origin contract.

Returns:

  • (HashId)

    of the origin contract



328
329
330
# File 'lib/universa/contract.rb', line 328

def origin
  getOrigin()
end

#ownerRole

Returns owner role.

Returns:

  • (Role)

    owner role



306
307
308
# File 'lib/universa/contract.rb', line 306

def owner
  get_owner
end

#owner=(key_address) ⇒ Object

Set owner to the key_address, usable only in the simplest case where owner is the single address.

Parameters:



312
313
314
# File 'lib/universa/contract.rb', line 312

def owner=(key_address)
  set_owner_key key_address
end

#packedObject

Get packed transaction containing the serialized signed contract and all its counterparts. Be sure to cal #seal somewhere before.

Returns:

  • binary string with packed transaction.



386
387
388
# File 'lib/universa/contract.rb', line 386

def packed
  get_packed_transaction
end

#parentHashId

Returns pf the parent contracr.

Returns:

  • (HashId)

    pf the parent contracr



333
334
335
# File 'lib/universa/contract.rb', line 333

def parent
  getParent()
end

#sealString

seal the contract

Returns:

  • (String)

    contract packed to the binary string



284
285
286
# File 'lib/universa/contract.rb', line 284

def seal
  super
end

#stateObject

Return state binder. Shortcut for Java API getStateData()



354
355
356
# File 'lib/universa/contract.rb', line 354

def state
  @state ||= getStateData()
end

#trace_errorsObject

trace found errors (call it afer check()): the Java version will not be able to trace to the process stdout, so we reqrite it here



392
393
394
395
396
# File 'lib/universa/contract.rb', line 392

def trace_errors
  getErrors.each { |e|
    puts "(#{e.object || ''}): #{e.error}"
  }
end

#transactionalBinder

Get transactional.data section creating it if need

Returns:



360
361
362
# File 'lib/universa/contract.rb', line 360

def transactional
  @transactional ||= getTransactionalData()
end