Class: Universa::Contract
- Inherits:
-
RemoteAdapter
- Object
- Delegator
- RemoteAdapter
- Universa::Contract
- Defined in:
- lib/universa/contract.rb
Overview
Universa contract adapter.
Class Method Summary collapse
-
.create(issuer_key, expires_at: (Time.now + 90 * 24 * 60 * 60), use_short_address: false) ⇒ Contract
Create simple contract with preset critical parts:.
-
.from_packed(packed) ⇒ Object
Load from transaction pack.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Ruby-style contracts equality.
-
#amount ⇒ BigDecimal
Helper for many token-like contracts containing state.data.amount.
-
#amount=(value) ⇒ Object
Write helper for many token-like contracts containing state.data.amount.
-
#can_perform_role(name, *keys) ⇒ Object
Test that some set of keys could be used to perform some role.
-
#create_revocation(*keys) ⇒ Contract
Create a contract that revokes this one if register with the Universa network.
-
#creator ⇒ Role
Shortcut ofr get_creator.
-
#definition ⇒ Object
Definition data.
-
#errors_string ⇒ String
Call it after check to get summaru of errors found.
-
#expires_at ⇒ Object
shortcut for get_expires_at.
-
#expires_at=(time) ⇒ Object
set
expires_at
field. -
#hash_id ⇒ HashId
shortcut for getHashId.
-
#issuer ⇒ Role
Issuer role.
-
#keys_to_sign_with ⇒ Set<PrivateKey>
returns keys that will be used to sign this contract on next #seal.
-
#ok? ⇒ Boolean
Shortcut for is_ok.
-
#origin ⇒ HashId
Of the origin contract.
-
#owner ⇒ Role
Owner role.
-
#owner=(key_address) ⇒ Object
Set owner to the key_address, usable only in the simplest case where owner is the single address.
-
#packed ⇒ Object
Get packed transaction containing the serialized signed contract and all its counterparts.
-
#parent ⇒ HashId
Pf the parent contracr.
-
#seal ⇒ String
seal the contract.
-
#state ⇒ Object
Return
state
binder. -
#trace_errors ⇒ Object
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.
-
#transactional ⇒ Binder
Get
transactional.data
section creating it if need.
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().
254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/universa/contract.rb', line 254 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 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. ChangeOwnerPermission.new(contract.owner.link_as "@owner") contract. RevokePermission.new(contract.owner.link_as "@owner") contract.add_signer_key issuer_key contract end |
.from_packed(packed) ⇒ Object
Load from transaction pack
267 268 269 270 271 |
# File 'lib/universa/contract.rb', line 267 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.
418 419 420 421 |
# File 'lib/universa/contract.rb', line 418 def == other return false if !other || !other.is_a?(Contract) hash_id == other.hash_id end |
#amount ⇒ BigDecimal
Helper for many token-like contracts containing state.data.amount
361 362 363 |
# File 'lib/universa/contract.rb', line 361 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.
369 370 371 |
# File 'lib/universa/contract.rb', line 369 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.
400 401 402 403 404 |
# File 'lib/universa/contract.rb', line 400 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.
411 412 413 414 415 |
# File 'lib/universa/contract.rb', line 411 def create_revocation(*keys) revoke = Service.umi.invoke_static 'ContractsService', 'createRevocation', *keys revoke.seal revoke end |
#creator ⇒ Role
Shortcut ofr get_creator
287 288 289 |
# File 'lib/universa/contract.rb', line 287 def creator get_creator end |
#definition ⇒ Object
Returns definition data.
340 341 342 |
# File 'lib/universa/contract.rb', line 340 def definition @definition ||= get_definition.get_data end |
#errors_string ⇒ String
Call it after check to get summaru of errors found.
392 393 394 |
# File 'lib/universa/contract.rb', line 392 def errors_string getErrors.map { |e| "(#{e.object || ''}): #{e.error}" }.join(', ').strip end |
#expires_at ⇒ Object
shortcut for get_expires_at. Get the contract expiration time.
329 330 331 |
# File 'lib/universa/contract.rb', line 329 def expires_at get_expires_at end |
#expires_at=(time) ⇒ Object
set expires_at
field
335 336 337 |
# File 'lib/universa/contract.rb', line 335 def expires_at=(time) set_expires_at time end |
#hash_id ⇒ HashId
shortcut for getHashId
314 315 316 |
# File 'lib/universa/contract.rb', line 314 def hash_id getId() end |
#issuer ⇒ Role
Returns issuer role.
292 293 294 |
# File 'lib/universa/contract.rb', line 292 def issuer get_issuer end |
#keys_to_sign_with ⇒ Set<PrivateKey>
returns keys that will be used to sign this contract on next #seal.
281 282 283 |
# File 'lib/universa/contract.rb', line 281 def keys_to_sign_with get_keys_to_sign_with end |
#ok? ⇒ Boolean
Shortcut for is_ok
308 309 310 |
# File 'lib/universa/contract.rb', line 308 def ok? is_ok end |
#origin ⇒ HashId
Returns of the origin contract.
319 320 321 |
# File 'lib/universa/contract.rb', line 319 def origin getOrigin() end |
#owner ⇒ Role
Returns owner role.
297 298 299 |
# File 'lib/universa/contract.rb', line 297 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.
303 304 305 |
# File 'lib/universa/contract.rb', line 303 def owner=(key_address) set_owner_key key_address end |
#packed ⇒ Object
Get packed transaction containing the serialized signed contract and all its counterparts. Be sure to cal #seal somewhere before.
377 378 379 |
# File 'lib/universa/contract.rb', line 377 def packed get_packed_transaction end |
#parent ⇒ HashId
Returns pf the parent contracr.
324 325 326 |
# File 'lib/universa/contract.rb', line 324 def parent getParent() end |
#seal ⇒ String
seal the contract
275 276 277 |
# File 'lib/universa/contract.rb', line 275 def seal super end |
#state ⇒ Object
Return state
binder. Shortcut for Java API getStateData()
345 346 347 |
# File 'lib/universa/contract.rb', line 345 def state @state ||= getStateData() end |
#trace_errors ⇒ Object
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
383 384 385 386 387 |
# File 'lib/universa/contract.rb', line 383 def trace_errors getErrors.each { |e| puts "(#{e.object || ''}): #{e.error}" } end |
#transactional ⇒ Binder
Get transactional.data
section creating it if need
351 352 353 |
# File 'lib/universa/contract.rb', line 351 def transactional @transactional ||= getTransactionalData() end |