class Universa::Contract
Universa
contract adapter.
Public Class Methods
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()
.
@param [PrivateKey] issuer_key also will be used to sign it @param [Time] expires_at
defaults to 90 days @param [Boolean] use_short_address set to true to use short address of the issuer key_address in the role @return [Contract] simple contact, not sealed
# 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.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
Load from transaction pack
# 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
Public Instance Methods
Ruby-style contracts equality.
# File lib/universa/contract.rb, line 418 def == other return false if !other || !other.is_a?(Contract) hash_id == other.hash_id end
Helper for many token-like contracts containing state.data.amount @return [BigDecimal] amount or nil
# File lib/universa/contract.rb, line 361 def amount v = state[:amount] and BigDecimal(v.to_s) end
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.
@param [Object] value should be some representation of a number (also string)
# File lib/universa/contract.rb, line 369 def amount= (value) state[:amount] = value.to_s.force_encoding('utf-8') end
Test that some set of keys could be used to perform some role.
@param [String] name of the role to check @param [PublicKey] keys instances to check against
# 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 a contract that revokes this one if register with the Universa
network. BE CAREFUL! REVOCATION IS IRREVERSIBLE! period.
@param [PrivateKey] keys enough to allow this contract revocation @return [Contract] revocation contract. Register it with the Universa
network to perform revocation.
# File lib/universa/contract.rb, line 411 def create_revocation(*keys) revoke = Service.umi.invoke_static 'ContractsService', 'createRevocation', *keys revoke.seal revoke end
Shortcut ofr get_creator @return [Role] universa role of the creator
# File lib/universa/contract.rb, line 287 def creator get_creator end
@return definition data
# File lib/universa/contract.rb, line 340 def definition @definition ||= get_definition.get_data end
Call it after check to get summaru of errors found.
@return [String] possibly empty ''
# File lib/universa/contract.rb, line 392 def errors_string getErrors.map { |e| "(#{e.object || ''}): #{e.error}" }.join(', ').strip end
shortcut for get_expires_at. Get the contract expiration time.
# File lib/universa/contract.rb, line 329 def expires_at get_expires_at end
set expires_at
field @param [Time] time when this contract will be expired, if yet APPROVED
.
# File lib/universa/contract.rb, line 335 def expires_at=(time) set_expires_at time end
shortcut for getHashId @return [HashId] of the contracr
# File lib/universa/contract.rb, line 314 def hash_id getId() end
@return [Role] issuer role
# File lib/universa/contract.rb, line 292 def issuer get_issuer end
returns keys that will be used to sign this contract on next {seal}. @return [Set<PrivateKey>] set of private keys
# File lib/universa/contract.rb, line 281 def keys_to_sign_with get_keys_to_sign_with end
Shortcut for is_ok
# File lib/universa/contract.rb, line 308 def ok? is_ok end
@return [HashId] of the origin contract
# File lib/universa/contract.rb, line 319 def origin getOrigin() end
@return [Role] owner role
# File lib/universa/contract.rb, line 297 def owner get_owner end
Set owner to the key_address, usable only in the simplest case where owner is the single address. @param [KeyAddress | PublicKey] key_address
# File lib/universa/contract.rb, line 303 def owner=(key_address) set_owner_key key_address end
Get packed transaction containing the serialized signed contract and all its counterparts. Be sure to cal {#seal} somewhere before.
@return binary string with packed transaction.
# File lib/universa/contract.rb, line 377 def packed get_packed_transaction end
@return [HashId] pf the parent contracr
# File lib/universa/contract.rb, line 324 def parent getParent() end
seal the contract @return [String] contract packed to the binary string
# File lib/universa/contract.rb, line 275 def seal super end
Return state
binder. Shortcut for Java API +getStateData()+
# File lib/universa/contract.rb, line 345 def state @state ||= getStateData() end
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
# File lib/universa/contract.rb, line 383 def trace_errors getErrors.each { |e| puts "(#{e.object || ''}): #{e.error}" } end
Get transactional.data
section creating it if need @return [Binder] instance
# File lib/universa/contract.rb, line 351 def transactional @transactional ||= getTransactionalData() end