lib/universa/contract.rb in universa-0.1.8 vs lib/universa/contract.rb in universa-0.1.9
- old
+ new
@@ -17,34 +17,11 @@
# Adapter for Universa Role
class Role < RemoteAdapter
remote_class "com.icodici.universa.contract.roles.Role"
end
- # Adapter for Universa Binder class. Provides some ruby-style helpers
- 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] 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)
- # @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"
@@ -145,10 +122,14 @@
# @return [Role] owner role
def owner
get_owner
end
+ def owner= key
+ set_owner_key key
+ end
+
# Shortcut for is_ok
def ok?
is_ok
end
@@ -170,17 +151,37 @@
def definition
@definition ||= get_definition.get_data
end
def state
- @state ||= get_state_data
+ @state ||= getStateData()
end
+ # Get +transactional.data+ section creating it if need
+ # @return [Binder] instance
+ def transactional
+ @transactional ||= getTransactionalData()
+ end
+
+ # def transactional?
+ # !!getTransactional()
+ # end
+
+ # Helper for many token-like contracts containing state.data.amount
+ # @return [BigDecimal] amount or nil
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)
+ def amount= (value)
+ state[:amount] = value.to_s.force_encoding('utf-8')
+ 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
@@ -193,13 +194,32 @@
getErrors.each {|e|
puts "(#{e.object || ''}): #{e.error}"
}
end
- # def create_revocation *keys
- # Service.umi.invoke_static 'ContractService', 'createRevocation', *keyss
- # sel
- # end
+ # Call it after check to get summaru of errors found.
+ #
+ # @return [String] possibly empty ''
+ def errors_string
+ getErrors.map {|e| "(#{e.object || ''}): #{e.error}"}.join(', ').strip
+ end
+
+ 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.
+ def create_revocation(*keys)
+ revoke = Service.umi.invoke_static 'ContractsService', 'createRevocation', *keys
+ revoke.seal
+ revoke
+ end
end
end
\ No newline at end of file