lib/universa/contract.rb in universa-0.1.6 vs lib/universa/contract.rb in universa-0.1.7

- old
+ new

@@ -3,10 +3,19 @@ # Adapter for Universa ChangeOwnerPermission class ChangeOwnerPermission < RemoteAdapter remote_class "com.icodici.universa.contract.permissions.ChangeOwnerPermission" end + # Adapter for Universa RevokePermission + class RevokePermission < RemoteAdapter + remote_class "com.icodici.universa.contract.permissions.RevokePermission" + end + + class SplitJoinPermission < RemoteAdapter + remote_class "com.icodici.universa.contract.permissions.SplitJoinPermission" + end + # Adapter for Universa Role class Role < RemoteAdapter remote_class "com.icodici.universa.contract.roles.Role" end @@ -14,24 +23,67 @@ class Binder < RemoteAdapter remote_class "net.sergeych.tools.Binder" # Set object for a key # - # @param [Object] key, key.to_s will be used (so use Symbols or Strings freely) + # @param [Object] key key.to_s will be used (so use Symbols or Strings freely) # @param [Object] value - def []=(key,value) + def []=(key, value) set(key.to_s, value) end # Get object by key. - # @param [Object] key, key.to_s will be used (so use Symbols or Strings freely) + # @param [Object] key key.to_s will be used (so use Symbols or Strings freely) # @return [Object] or nil def [](key) get(key.to_s) end + + def self.of hash + invoke_static "of", hash.transform_keys(&:to_s) + end end + # Adapter for Universa +HashId+ class, helps to avoid confusion when using different + # representations of the ID. + class HashId < RemoteAdapter + remote_class "com.icodici.universa.HashId" + + # Construct from binary representation, not to confuse with binary one. + # + # @param [String] digest_bytes binary string of some +hash_id.bytes+ + # @return [HashId] instance with instance.bytes == digest.bytes + def self.from_digest(digest_bytes) + digest_bytes.force_encoding 'binary' + invoke_static 'with_digest', digest_bytes + end + + # Construct from string representation of the ID, not to confuse with binary one. + # + # @param [String] string_id id string representation, like from +hash_id_instance.to_s+. See {#to_s}. + def self.from_string(string_id) + string_id.force_encoding 'utf-8' + invoke_static 'with_digest', string_id + end + + # Get binary representation. It is shorter than string representation but contain non-printable characters and + # can cause problems if treated like a string. Use {#to_s} to get string representation instead. + # + # @return [String] binary string + def bytes + get_digest + end + + # Get string representation. It is, actually, base64 encoded string representation. Longer, but could easily + # be transferred with text protocols. + # + # @return [String] string representation + def to_s + Base64.encode64(get_digest).gsub(/\s/, '') + end + end + # Universa contract adapter. class Contract < RemoteAdapter remote_class "com.icodici.universa.contract.Contract" # Create simple contract with preset critical parts: @@ -54,16 +106,18 @@ 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 def self.from_packed packed + packed.force_encoding 'binary' self.invoke_static "fromPackedTransaction", packed end # seal the contract # @return [String] contract packed to the binary string @@ -106,15 +160,31 @@ # shortcut for get_expires_at def expires_at get_expires_at end + def expires_at= time + set_expires_at time + end + # @return definition data def definition - get_definition.get_data + @definition ||= get_definition.get_data end + def state + @state ||= get_state_data + end + + def amount + v = state[:amount] and BigDecimal(v.to_s) + 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. def packed get_packed_transaction end # trace found errors (call it afer check()): the Java version will not be able to trace to the @@ -122,9 +192,14 @@ def trace_errors getErrors.each {|e| puts "(#{e.object || ''}): #{e.error}" } end + + # def create_revocation *keys + # Service.umi.invoke_static 'ContractService', 'createRevocation', *keyss + # sel + # end end end \ No newline at end of file