From 57929ba2b55c5c2ee93b5b88289d7b87284f61b4 Mon Sep 17 00:00:00 2001 From: 10gic <2391796+10gic@users.noreply.github.com> Date: Wed, 18 Sep 2024 15:57:19 +0800 Subject: [PATCH 1/2] Fix kmp issue: memory leak found in Base58.decode in iOS --- codegen/lib/kotlin_helper.rb | 25 ++-- codegen/lib/templates/kotlin/ios_class.erb | 50 +++++-- codegen/lib/templates/kotlin/ios_enum.erb | 8 +- .../templates/kotlin/ios_parameter_access.erb | 26 ++++ .../kotlin/ios_parameter_release.erb | 22 +++ codegen/lib/templates/kotlin/ios_struct.erb | 10 +- .../TrustWalletCore/TWEthereumAbiFunction.h | 141 +++++++++--------- .../kotlin/com/trustwallet/core/AnySigner.kt | 28 +++- .../com/trustwallet/core/ByteArrayExt.kt | 7 +- .../kotlin/com/trustwallet/core/StringExt.kt | 7 +- src/interface/TWEthereumAbiFunction.cpp | 136 ++++++++--------- 11 files changed, 284 insertions(+), 176 deletions(-) create mode 100644 codegen/lib/templates/kotlin/ios_parameter_access.erb create mode 100644 codegen/lib/templates/kotlin/ios_parameter_release.erb diff --git a/codegen/lib/kotlin_helper.rb b/codegen/lib/kotlin_helper.rb index b37537b4fa4..404ac868acb 100644 --- a/codegen/lib/kotlin_helper.rb +++ b/codegen/lib/kotlin_helper.rb @@ -31,7 +31,13 @@ def self.js_parameters(params) def self.calling_parameters_ios(params) names = params.map do |param| name = fix_name(param.name) + if param.type.name == :data + "#{name}Data#{convert_calling_type_ios(param.type)}" + elsif param.type.name == :string + "#{name}String#{convert_calling_type_ios(param.type)}" + else "#{name}#{convert_calling_type_ios(param.type)}" + end end names.join(', ') end @@ -55,8 +61,6 @@ def self.fix_name(name) case name when '' "value" - when 'val' - "value" when 'return' '`return`' else @@ -65,19 +69,12 @@ def self.fix_name(name) end def self.convert_calling_type_ios(t) - case t.name - when :data - "#{if t.is_nullable then '?' else '' end}.toTwData()" - when :string - "#{if t.is_nullable then '?' else '' end}.toTwString()" + if t.is_enum + "#{if t.is_nullable then '?' else '' end}.nativeValue" + elsif t.is_class + "#{if t.is_nullable then '?' else '' end}.pointer" else - if t.is_enum - "#{if t.is_nullable then '?' else '' end}.nativeValue" - elsif t.is_class - "#{if t.is_nullable then '?' else '' end}.pointer" - else - '' - end + '' end end diff --git a/codegen/lib/templates/kotlin/ios_class.erb b/codegen/lib/templates/kotlin/ios_class.erb index 36bc94e8276..02369aeead4 100644 --- a/codegen/lib/templates/kotlin/ios_class.erb +++ b/codegen/lib/templates/kotlin/ios_class.erb @@ -2,6 +2,7 @@ import cnames.structs.TW<%= entity.name %> import kotlinx.cinterop.CPointer +import kotlinx.cinterop.toCValues <% constructors = entity.static_methods.select { |method| method.name.start_with?('Create') } -%> <% methods = entity.methods.select { |method| not method.name.start_with?('Delete') } -%> @@ -14,12 +15,9 @@ actual class <%= entity.name %> constructor( <% if constructor.return_type.is_nullable -%> @Throws(IllegalArgumentException::class) - actual constructor(<%= KotlinHelper.parameters(constructor.parameters) %>) : this( - TW<%= entity.name %><%= constructor.name %>(<%= KotlinHelper.calling_parameters_ios(constructor.parameters) %>) ?: throw IllegalArgumentException() -<% else -%> - actual constructor(<%= KotlinHelper.parameters(constructor.parameters) %>) : this( - TW<%= entity.name %><%= constructor.name %>(<%= KotlinHelper.calling_parameters_ios(constructor.parameters) %>)!! <% end -%> + actual constructor(<%= KotlinHelper.parameters(constructor.parameters) %>) : this( + wrapperTW<%= entity.name %><%= constructor.name %>(<%= KotlinHelper.arguments(constructor.parameters) %>) ) <% end -%> <%# Property declarations -%> @@ -31,12 +29,38 @@ actual class <%= entity.name %> constructor( <%# Method declarations -%> <% methods.each do |method| -%> - actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters.drop(1)) %>)<%= KotlinHelper.return_type(method.return_type) %> = - <%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(pointer#{', ' if not method.parameters.one?}#{KotlinHelper.calling_parameters_ios(method.parameters.drop(1))})") %> + actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters.drop(1)) %>)<%= KotlinHelper.return_type(method.return_type) %> { +<%= render('kotlin/ios_parameter_access.erb', { method: method }) -%> + val result = <%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(pointer#{', ' if not method.parameters.one?}#{KotlinHelper.calling_parameters_ios(method.parameters.drop(1))})") %> +<%= render('kotlin/ios_parameter_release.erb', { method: method }) -%> + return result + } <% end -%> -<% if entity.static_properties.any? || static_methods.any? -%> +<% if entity.static_properties.any? || static_methods.any? || constructors.any? -%> + + <%= if entity.static_properties.any? || static_methods.any? then "actual" else "private" end %> companion object { +<%# Constructor wrappers -%> +<% if constructors.any? -%> +<% constructors.each do |constructor| -%> - actual companion object { +<% if constructor.return_type.is_nullable -%> + @Throws(IllegalArgumentException::class) +<% end -%> + private fun wrapperTW<%= entity.name %><%= constructor.name %>(<%= KotlinHelper.parameters(constructor.parameters) %>): CPointer> { +<%= render('kotlin/ios_parameter_access.erb', { method: constructor }) -%> + val result = TW<%= entity.name %><%= constructor.name %>(<%= KotlinHelper.calling_parameters_ios(constructor.parameters) %>) +<%= render('kotlin/ios_parameter_release.erb', { method: constructor }) -%> +<% if constructor.return_type.is_nullable -%> + if (result == null) { + throw IllegalArgumentException() + } + return result +<% else -%> + return result!! +<% end -%> + } +<% end -%> +<% end -%> <%# Static property declarations -%> <% entity.static_properties.each do |property| -%> @@ -46,8 +70,12 @@ actual class <%= entity.name %> constructor( <%# Static method declarations -%> <% static_methods.each do |method| -%> - actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters) %>)<%= KotlinHelper.return_type(method.return_type) %> = - <%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(#{KotlinHelper.calling_parameters_ios(method.parameters)})") %> + actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters) %>)<%= KotlinHelper.return_type(method.return_type) %> { +<%= render('kotlin/ios_parameter_access.erb', { method: method }) -%> + val result = <%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(#{KotlinHelper.calling_parameters_ios(method.parameters)})") %> +<%= render('kotlin/ios_parameter_release.erb', { method: method }) -%> + return result + } <% end -%> } <% end -%> diff --git a/codegen/lib/templates/kotlin/ios_enum.erb b/codegen/lib/templates/kotlin/ios_enum.erb index 5f64c80a7fa..16d26ba89b4 100644 --- a/codegen/lib/templates/kotlin/ios_enum.erb +++ b/codegen/lib/templates/kotlin/ios_enum.erb @@ -41,8 +41,12 @@ actual enum class <%= entity.name %>( <%- entity.methods.each do |method| -%> <%- next if method.name.start_with?('Delete') -%> - actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters.drop(1)) %>)<%= KotlinHelper.return_type(method.return_type) %> = - TW<%= entity.name %><%= method.name %>(value<%= ', ' if not method.parameters.one? %><%= KotlinHelper.calling_parameters_ios(method.parameters.drop(1)) %>)<%= KotlinHelper.convert_calling_return_type_ios(method.return_type) %> + actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters.drop(1)) %>)<%= KotlinHelper.return_type(method.return_type) %> { +<%= render('kotlin/ios_parameter_access.erb', { method: method }) -%> + val result = TW<%= entity.name %><%= method.name %>(value<%= ', ' if not method.parameters.one? %><%= KotlinHelper.calling_parameters_ios(method.parameters.drop(1)) %>)<%= KotlinHelper.convert_calling_return_type_ios(method.return_type) %> +<%= render('kotlin/ios_parameter_release.erb', { method: method }) -%> + return result + } <%- end -%> <%# Value -%> <% if entity.cases.any? { |e| !e.value.nil? } -%> diff --git a/codegen/lib/templates/kotlin/ios_parameter_access.erb b/codegen/lib/templates/kotlin/ios_parameter_access.erb new file mode 100644 index 00000000000..fc209a58cfc --- /dev/null +++ b/codegen/lib/templates/kotlin/ios_parameter_access.erb @@ -0,0 +1,26 @@ +<% + method = locals[:method] + + method.parameters.each do |param| -%> +<% if param.type.name == :data -%> +<% if param.type.is_nullable -%> + val <%= param.name %>Data = if (<%= param.name %> == null) { + null + } else { + TWDataCreateWithBytes(<%= param.name %>.toUByteArray().toCValues(), <%= param.name %>.size.toULong()) + } +<% else -%> + val <%= param.name %>Data = TWDataCreateWithBytes(<%= param.name %>.toUByteArray().toCValues(), <%= param.name %>.size.toULong()) +<% end -%> +<% elsif param.type.name == :string -%> +<% if param.type.is_nullable -%> + val <%= param.name %>String = if (<%= param.name %> == null) { + null + } else { + TWStringCreateWithUTF8Bytes(<%= param.name %>) + } +<% else -%> + val <%= param.name %>String = TWStringCreateWithUTF8Bytes(<%= param.name %>) +<% end -%> +<% end -%> +<% end -%> diff --git a/codegen/lib/templates/kotlin/ios_parameter_release.erb b/codegen/lib/templates/kotlin/ios_parameter_release.erb new file mode 100644 index 00000000000..bf8bc9c4799 --- /dev/null +++ b/codegen/lib/templates/kotlin/ios_parameter_release.erb @@ -0,0 +1,22 @@ +<% + method = locals[:method] + + method.parameters.each do |param| -%> +<% if param.type.name == :data -%> +<% if param.type.is_nullable -%> + if (<%= param.name %>Data != null) { + TWDataDelete(<%= param.name %>Data) + } +<% else -%> + TWDataDelete(<%= param.name %>Data) +<% end -%> +<% elsif param.type.name == :string -%> +<% if param.type.is_nullable -%> + if (<%= param.name %>String != null) { + TWStringDelete(<%= param.name %>String) + } +<% else -%> + TWStringDelete(<%= param.name %>String) +<% end -%> +<% end -%> +<% end -%> diff --git a/codegen/lib/templates/kotlin/ios_struct.erb b/codegen/lib/templates/kotlin/ios_struct.erb index 6bf3da876ee..08fbcc34ef8 100644 --- a/codegen/lib/templates/kotlin/ios_struct.erb +++ b/codegen/lib/templates/kotlin/ios_struct.erb @@ -1,5 +1,7 @@ <%= render('kotlin/package.erb') %> +import kotlinx.cinterop.toCValues + actual object <%= entity.name %> { <%# Static property declarations -%> <% entity.static_properties.each do |property| -%> @@ -10,7 +12,11 @@ actual object <%= entity.name %> { <% entity.static_methods.each do |method| -%> <% next if method.name.start_with?('Create') -%> - actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters) %>)<%= KotlinHelper.return_type(method.return_type) %> = - <%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(#{KotlinHelper.calling_parameters_ios(method.parameters)})") %> + actual fun <%= KotlinHelper.format_name(method.name) %>(<%= KotlinHelper.parameters(method.parameters) %>)<%= KotlinHelper.return_type(method.return_type) %> { +<%= render('kotlin/ios_parameter_access.erb', { method: method }) -%> + val result = <%= KotlinHelper.convert_calling_return_type_ios(method.return_type, "TW#{entity.name}#{method.name}(#{KotlinHelper.calling_parameters_ios(method.parameters)})") %> +<%= render('kotlin/ios_parameter_release.erb', { method: method }) -%> + return result + } <% end -%> } diff --git a/include/TrustWalletCore/TWEthereumAbiFunction.h b/include/TrustWalletCore/TWEthereumAbiFunction.h index cb5fbb9407c..be1f7387e5c 100644 --- a/include/TrustWalletCore/TWEthereumAbiFunction.h +++ b/include/TrustWalletCore/TWEthereumAbiFunction.h @@ -40,179 +40,178 @@ TWString* _Nonnull TWEthereumAbiFunctionGetType(struct TWEthereumAbiFunction* _N /// Add a uint8 type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamUInt8(struct TWEthereumAbiFunction* _Nonnull fn, uint8_t val, bool isOutput); +int TWEthereumAbiFunctionAddParamUInt8(struct TWEthereumAbiFunction* _Nonnull fn, uint8_t value, bool isOutput); /// Add a uint16 type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamUInt16(struct TWEthereumAbiFunction* _Nonnull fn, uint16_t val, bool isOutput); +int TWEthereumAbiFunctionAddParamUInt16(struct TWEthereumAbiFunction* _Nonnull fn, uint16_t value, bool isOutput); /// Add a uint32 type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamUInt32(struct TWEthereumAbiFunction* _Nonnull fn, uint32_t val, bool isOutput); +int TWEthereumAbiFunctionAddParamUInt32(struct TWEthereumAbiFunction* _Nonnull fn, uint32_t value, bool isOutput); /// Add a uint64 type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamUInt64(struct TWEthereumAbiFunction* _Nonnull fn, uint64_t val, bool isOutput); +int TWEthereumAbiFunctionAddParamUInt64(struct TWEthereumAbiFunction* _Nonnull fn, uint64_t value, bool isOutput); /// Add a uint256 type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamUInt256(struct TWEthereumAbiFunction* _Nonnull fn, TWData* _Nonnull val, bool isOutput); +int TWEthereumAbiFunctionAddParamUInt256(struct TWEthereumAbiFunction* _Nonnull fn, TWData* _Nonnull value, bool isOutput); /// Add a uint(bits) type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamUIntN(struct TWEthereumAbiFunction* _Nonnull fn, int bits, TWData* _Nonnull val, bool isOutput); +int TWEthereumAbiFunctionAddParamUIntN(struct TWEthereumAbiFunction* _Nonnull fn, int bits, TWData* _Nonnull value, bool isOutput); /// Add a int8 type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamInt8(struct TWEthereumAbiFunction* _Nonnull fn, int8_t val, bool isOutput); +int TWEthereumAbiFunctionAddParamInt8(struct TWEthereumAbiFunction* _Nonnull fn, int8_t value, bool isOutput); /// Add a int16 type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamInt16(struct TWEthereumAbiFunction* _Nonnull fn, int16_t val, bool isOutput); +int TWEthereumAbiFunctionAddParamInt16(struct TWEthereumAbiFunction* _Nonnull fn, int16_t value, bool isOutput); /// Add a int32 type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamInt32(struct TWEthereumAbiFunction* _Nonnull fn, int32_t val, bool isOutput); +int TWEthereumAbiFunctionAddParamInt32(struct TWEthereumAbiFunction* _Nonnull fn, int32_t value, bool isOutput); /// Add a int64 type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamInt64(struct TWEthereumAbiFunction* _Nonnull fn, int64_t val, bool isOutput); +int TWEthereumAbiFunctionAddParamInt64(struct TWEthereumAbiFunction* _Nonnull fn, int64_t value, bool isOutput); /// Add a int256 type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified (stored in a block of data) +/// \param value for output parameters, value has to be specified (stored in a block of data) /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamInt256(struct TWEthereumAbiFunction* _Nonnull fn, TWData* _Nonnull val, bool isOutput); +int TWEthereumAbiFunctionAddParamInt256(struct TWEthereumAbiFunction* _Nonnull fn, TWData* _Nonnull value, bool isOutput); /// Add a int(bits) type parameter /// /// \param fn A Non-null eth abi function /// \param bits Number of bits of the integer parameter -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamIntN(struct TWEthereumAbiFunction* _Nonnull fn, int bits, TWData* _Nonnull val, bool isOutput); +int TWEthereumAbiFunctionAddParamIntN(struct TWEthereumAbiFunction* _Nonnull fn, int bits, TWData* _Nonnull value, bool isOutput); /// Add a bool type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamBool(struct TWEthereumAbiFunction* _Nonnull fn, bool val, bool isOutput); +int TWEthereumAbiFunctionAddParamBool(struct TWEthereumAbiFunction* _Nonnull fn, bool value, bool isOutput); /// Add a string type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamString(struct TWEthereumAbiFunction* _Nonnull fn, TWString* _Nonnull val, bool isOutput); +int TWEthereumAbiFunctionAddParamString(struct TWEthereumAbiFunction* _Nonnull fn, TWString* _Nonnull value, bool isOutput); /// Add an address type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamAddress(struct TWEthereumAbiFunction* _Nonnull fn, TWData* _Nonnull val, bool isOutput); +int TWEthereumAbiFunctionAddParamAddress(struct TWEthereumAbiFunction* _Nonnull fn, TWData* _Nonnull value, bool isOutput); /// Add a bytes type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamBytes(struct TWEthereumAbiFunction* _Nonnull fn, TWData* _Nonnull val, bool isOutput); +int TWEthereumAbiFunctionAddParamBytes(struct TWEthereumAbiFunction* _Nonnull fn, TWData* _Nonnull value, bool isOutput); /// Add a bytes[N] type parameter /// /// \param fn A Non-null eth abi function -/// \param size fixed size of the bytes array parameter (val). -/// \param val for output parameters, value has to be specified +/// \param size fixed size of the bytes array parameter (value). +/// \param value for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddParamBytesFix(struct TWEthereumAbiFunction* _Nonnull fn, size_t size, TWData* _Nonnull val, bool isOutput); +int TWEthereumAbiFunctionAddParamBytesFix(struct TWEthereumAbiFunction* _Nonnull fn, size_t size, TWData* _Nonnull value, bool isOutput); /// Add a type[] type parameter /// /// \param fn A Non-null eth abi function -/// \param val for output parameters, value has to be specified /// \param isOutput determines if the parameter is an input or output /// \return the index of the parameter (0-based). TW_EXPORT_METHOD @@ -282,173 +281,173 @@ TWData* _Nonnull TWEthereumAbiFunctionGetParamAddress(struct TWEthereumAbiFuncti /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamUInt8(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, uint8_t val); +int TWEthereumAbiFunctionAddInArrayParamUInt8(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, uint8_t value); /// Adding a uint16 type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamUInt16(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, uint16_t val); +int TWEthereumAbiFunctionAddInArrayParamUInt16(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, uint16_t value); /// Adding a uint32 type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamUInt32(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, uint32_t val); +int TWEthereumAbiFunctionAddInArrayParamUInt32(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, uint32_t value); /// Adding a uint64 type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamUInt64(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, uint64_t val); +int TWEthereumAbiFunctionAddInArrayParamUInt64(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, uint64_t value); /// Adding a uint256 type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter stored in a block of data +/// \param value the value of the parameter stored in a block of data /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamUInt256(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, TWData* _Nonnull val); +int TWEthereumAbiFunctionAddInArrayParamUInt256(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, TWData* _Nonnull value); /// Adding a uint[N] type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param bits Number of bits of the integer parameter /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter stored in a block of data +/// \param value the value of the parameter stored in a block of data /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamUIntN(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int bits, TWData* _Nonnull val); +int TWEthereumAbiFunctionAddInArrayParamUIntN(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int bits, TWData* _Nonnull value); /// Adding a int8 type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamInt8(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int8_t val); +int TWEthereumAbiFunctionAddInArrayParamInt8(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int8_t value); /// Adding a int16 type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamInt16(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int16_t val); +int TWEthereumAbiFunctionAddInArrayParamInt16(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int16_t value); /// Adding a int32 type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamInt32(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int32_t val); +int TWEthereumAbiFunctionAddInArrayParamInt32(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int32_t value); /// Adding a int64 type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamInt64(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int64_t val); +int TWEthereumAbiFunctionAddInArrayParamInt64(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int64_t value); /// Adding a int256 type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter stored in a block of data +/// \param value the value of the parameter stored in a block of data /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamInt256(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, TWData* _Nonnull val); +int TWEthereumAbiFunctionAddInArrayParamInt256(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, TWData* _Nonnull value); /// Adding a int[N] type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param bits Number of bits of the integer parameter /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter stored in a block of data +/// \param value the value of the parameter stored in a block of data /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamIntN(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int bits, TWData* _Nonnull val); +int TWEthereumAbiFunctionAddInArrayParamIntN(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, int bits, TWData* _Nonnull value); /// Adding a bool type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamBool(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, bool val); +int TWEthereumAbiFunctionAddInArrayParamBool(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, bool value); /// Adding a string type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamString(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, TWString* _Nonnull val); +int TWEthereumAbiFunctionAddInArrayParamString(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, TWString* _Nonnull value); /// Adding an address type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamAddress(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, TWData* _Nonnull val); +int TWEthereumAbiFunctionAddInArrayParamAddress(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, TWData* _Nonnull value); /// Adding a bytes type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param val the value of the parameter +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamBytes(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, TWData* _Nonnull val); +int TWEthereumAbiFunctionAddInArrayParamBytes(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, TWData* _Nonnull value); /// Adding a int64 type parameter of to the top-level input parameter array /// /// \param fn A Non-null eth abi function /// \param arrayIdx array index for the abi function (0-based). -/// \param size fixed size of the bytes array parameter (val). -/// \param val the value of the parameter +/// \param size fixed size of the bytes array parameter (value). +/// \param value the value of the parameter /// \return the index of the added parameter (0-based). TW_EXPORT_METHOD TW_METHOD_DISCARDABLE_RESULT -int TWEthereumAbiFunctionAddInArrayParamBytesFix(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, size_t size, TWData* _Nonnull val); +int TWEthereumAbiFunctionAddInArrayParamBytesFix(struct TWEthereumAbiFunction* _Nonnull fn, int arrayIdx, size_t size, TWData* _Nonnull value); TW_EXTERN_C_END diff --git a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/AnySigner.kt b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/AnySigner.kt index 6de0b110bea..d764e183856 100644 --- a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/AnySigner.kt +++ b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/AnySigner.kt @@ -4,17 +4,33 @@ package com.trustwallet.core +import kotlinx.cinterop.toCValues + actual object AnySigner { - actual fun sign(input: ByteArray, coin: CoinType): ByteArray = - TWAnySignerSign(input.toTwData(), coin.value)!!.readTwBytes()!! + actual fun sign(input: ByteArray, coin: CoinType): ByteArray { + val inputData = TWDataCreateWithBytes(input.toUByteArray().toCValues(), input.size.toULong()) + val result = TWAnySignerSign(input.toTwData(), coin.value)!!.readTwBytes()!! + TWDataDelete(inputData) + return result + } actual fun supportsJson(coin: CoinType): Boolean = TWAnySignerSupportsJSON(coin.value) - actual fun signJson(json: String, key: ByteArray, coin: CoinType): String = - TWAnySignerSignJSON(json.toTwString(), key.toTwData(), coin.value).fromTwString()!! + actual fun signJson(json: String, key: ByteArray, coin: CoinType): String { + val jsonString = TWStringCreateWithUTF8Bytes(json) + val keyData = TWDataCreateWithBytes(key.toUByteArray().toCValues(), key.size.toULong()) + val result = TWAnySignerSignJSON(jsonString, keyData, coin.value).fromTwString()!! + TWStringDelete(jsonString) + TWDataDelete(keyData) + return result + } - actual fun plan(input: ByteArray, coin: CoinType): ByteArray = - TWAnySignerPlan(input.toTwData(), coin.value)?.readTwBytes()!! + actual fun plan(input: ByteArray, coin: CoinType): ByteArray { + val inputData = TWDataCreateWithBytes(input.toUByteArray().toCValues(), input.size.toULong()) + val result = TWAnySignerPlan(input.toTwData(), coin.value)?.readTwBytes()!! + TWDataDelete(inputData) + return result + } } diff --git a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/ByteArrayExt.kt b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/ByteArrayExt.kt index ec5923272a1..67a7326c857 100644 --- a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/ByteArrayExt.kt +++ b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/ByteArrayExt.kt @@ -8,8 +8,13 @@ import kotlinx.cinterop.COpaquePointer import kotlinx.cinterop.readBytes import kotlinx.cinterop.toCValues +// Build ByteArray from TWData, and then delete TWData internal fun COpaquePointer?.readTwBytes(): ByteArray? = - TWDataBytes(this)?.readBytes(TWDataSize(this).toInt()) + this?.let { + val result = TWDataBytes(it)?.readBytes(TWDataSize(it).toInt()) + TWDataDelete(it) + result + } @OptIn(ExperimentalUnsignedTypes::class) internal fun ByteArray?.toTwData(): COpaquePointer? = diff --git a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/StringExt.kt b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/StringExt.kt index 2eb7fdd311e..101ebbfc7f5 100644 --- a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/StringExt.kt +++ b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/StringExt.kt @@ -11,5 +11,10 @@ import kotlinx.cinterop.toKString internal fun String?.toTwString(): COpaquePointer? = this?.let { TWStringCreateWithUTF8Bytes(it) } +// Build String from TWString, and then delete TWString internal fun CValuesRef<*>?.fromTwString(): String? = - this?.let { TWStringUTF8Bytes(it)?.toKString() } + this?.let { + val result = TWStringUTF8Bytes(it)?.toKString() + TWStringDelete(it) + result + } diff --git a/src/interface/TWEthereumAbiFunction.cpp b/src/interface/TWEthereumAbiFunction.cpp index db792629ee0..a57f6871702 100644 --- a/src/interface/TWEthereumAbiFunction.cpp +++ b/src/interface/TWEthereumAbiFunction.cpp @@ -32,89 +32,89 @@ TWString *_Nonnull TWEthereumAbiFunctionGetType(struct TWEthereumAbiFunction *_N ///// AddParam -int TWEthereumAbiFunctionAddParamUInt8(struct TWEthereumAbiFunction *_Nonnull func_in, uint8_t val, bool isOutput) { +int TWEthereumAbiFunctionAddParamUInt8(struct TWEthereumAbiFunction *_Nonnull func_in, uint8_t value, bool isOutput) { assert(func_in != nullptr); uint32_t bits = 8; - auto encodedValue = store(val); + auto encodedValue = store(value); return func_in->impl.addUintParam(bits, encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamUInt16(struct TWEthereumAbiFunction *_Nonnull func_in, uint16_t val, bool isOutput) { +int TWEthereumAbiFunctionAddParamUInt16(struct TWEthereumAbiFunction *_Nonnull func_in, uint16_t value, bool isOutput) { assert(func_in != nullptr); uint32_t bits = 16; - auto encodedValue = store(val); + auto encodedValue = store(value); return func_in->impl.addUintParam(bits, encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamUInt32(struct TWEthereumAbiFunction *_Nonnull func_in, uint32_t val, bool isOutput) { +int TWEthereumAbiFunctionAddParamUInt32(struct TWEthereumAbiFunction *_Nonnull func_in, uint32_t value, bool isOutput) { assert(func_in != nullptr); uint32_t bits = 32; - auto encodedValue = store(val); + auto encodedValue = store(value); return func_in->impl.addUintParam(bits, encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamUInt64(struct TWEthereumAbiFunction *_Nonnull func_in, uint64_t val, bool isOutput) { +int TWEthereumAbiFunctionAddParamUInt64(struct TWEthereumAbiFunction *_Nonnull func_in, uint64_t value, bool isOutput) { assert(func_in != nullptr); uint32_t bits = 64; - auto encodedValue = store(val); + auto encodedValue = store(value); return func_in->impl.addUintParam(bits, encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamUInt256(struct TWEthereumAbiFunction *_Nonnull func_in, TWData *_Nonnull val, bool isOutput) { +int TWEthereumAbiFunctionAddParamUInt256(struct TWEthereumAbiFunction *_Nonnull func_in, TWData *_Nonnull value, bool isOutput) { assert(func_in != nullptr); uint32_t bits = 256; - const Data& encodedValue = *(reinterpret_cast(val)); + const Data& encodedValue = *(reinterpret_cast(value)); return func_in->impl.addUintParam(bits, encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamUIntN(struct TWEthereumAbiFunction *_Nonnull func_in, int bits, TWData *_Nonnull val, bool isOutput) { +int TWEthereumAbiFunctionAddParamUIntN(struct TWEthereumAbiFunction *_Nonnull func_in, int bits, TWData *_Nonnull value, bool isOutput) { assert(func_in != nullptr); - const Data& encodedValue = *(reinterpret_cast(val)); + const Data& encodedValue = *(reinterpret_cast(value)); return func_in->impl.addUintParam(static_cast(bits), encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamInt8(struct TWEthereumAbiFunction *_Nonnull func_in, int8_t val, bool isOutput) { +int TWEthereumAbiFunctionAddParamInt8(struct TWEthereumAbiFunction *_Nonnull func_in, int8_t value, bool isOutput) { assert(func_in != nullptr); uint32_t bits = 8; - auto encodedValue = store(val); + auto encodedValue = store(value); return func_in->impl.addIntParam(bits, encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamInt16(struct TWEthereumAbiFunction *_Nonnull func_in, int16_t val, bool isOutput) { +int TWEthereumAbiFunctionAddParamInt16(struct TWEthereumAbiFunction *_Nonnull func_in, int16_t value, bool isOutput) { assert(func_in != nullptr); uint32_t bits = 16; - auto encodedValue = store(val); + auto encodedValue = store(value); return func_in->impl.addIntParam(bits, encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamInt32(struct TWEthereumAbiFunction *_Nonnull func_in, int32_t val, bool isOutput) { +int TWEthereumAbiFunctionAddParamInt32(struct TWEthereumAbiFunction *_Nonnull func_in, int32_t value, bool isOutput) { assert(func_in != nullptr); uint32_t bits = 32; - auto encodedValue = store(val); + auto encodedValue = store(value); return func_in->impl.addIntParam(bits, encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamInt64(struct TWEthereumAbiFunction *_Nonnull func_in, int64_t val, bool isOutput) { +int TWEthereumAbiFunctionAddParamInt64(struct TWEthereumAbiFunction *_Nonnull func_in, int64_t value, bool isOutput) { assert(func_in != nullptr); uint32_t bits = 64; - auto encodedValue = store(val); + auto encodedValue = store(value); return func_in->impl.addIntParam(bits, encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamInt256(struct TWEthereumAbiFunction *_Nonnull func_in, TWData *_Nonnull val, bool isOutput) { +int TWEthereumAbiFunctionAddParamInt256(struct TWEthereumAbiFunction *_Nonnull func_in, TWData *_Nonnull value, bool isOutput) { assert(func_in != nullptr); uint32_t bits = 256; - const Data& encodedValue = *(reinterpret_cast(val)); + const Data& encodedValue = *(reinterpret_cast(value)); return func_in->impl.addIntParam(bits, encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamIntN(struct TWEthereumAbiFunction *_Nonnull func_in, int bits, TWData *_Nonnull val, bool isOutput) { +int TWEthereumAbiFunctionAddParamIntN(struct TWEthereumAbiFunction *_Nonnull func_in, int bits, TWData *_Nonnull value, bool isOutput) { assert(func_in != nullptr); - const Data& encodedValue = *(reinterpret_cast(val)); + const Data& encodedValue = *(reinterpret_cast(value)); return func_in->impl.addIntParam(static_cast(bits), encodedValue, isOutput); } -int TWEthereumAbiFunctionAddParamBool(struct TWEthereumAbiFunction *_Nonnull func_in, bool val, bool isOutput) { +int TWEthereumAbiFunctionAddParamBool(struct TWEthereumAbiFunction *_Nonnull func_in, bool value, bool isOutput) { assert(func_in != nullptr); EthereumAbi::Proto::Param paramType; @@ -122,12 +122,12 @@ int TWEthereumAbiFunctionAddParamBool(struct TWEthereumAbiFunction *_Nonnull fun paramType.mutable_param()->mutable_boolean(); EthereumAbi::Proto::Token token; - token.set_boolean(val); + token.set_boolean(value); return func_in->impl.addParam(std::move(paramType), std::move(token), isOutput); } -int TWEthereumAbiFunctionAddParamString(struct TWEthereumAbiFunction *_Nonnull func_in, TWString *_Nonnull val, bool isOutput) { +int TWEthereumAbiFunctionAddParamString(struct TWEthereumAbiFunction *_Nonnull func_in, TWString *_Nonnull value, bool isOutput) { assert(func_in != nullptr); EthereumAbi::Proto::Param paramType; @@ -135,13 +135,13 @@ int TWEthereumAbiFunctionAddParamString(struct TWEthereumAbiFunction *_Nonnull f paramType.mutable_param()->mutable_string_param(); EthereumAbi::Proto::Token token; - auto* s = reinterpret_cast(val); + auto* s = reinterpret_cast(value); token.set_string_value(*s); return func_in->impl.addParam(std::move(paramType), std::move(token), isOutput); } -int TWEthereumAbiFunctionAddParamAddress(struct TWEthereumAbiFunction *_Nonnull func_in, TWData *_Nonnull val, bool isOutput) { +int TWEthereumAbiFunctionAddParamAddress(struct TWEthereumAbiFunction *_Nonnull func_in, TWData *_Nonnull value, bool isOutput) { assert(func_in != nullptr); EthereumAbi::Proto::Param paramType; @@ -149,7 +149,7 @@ int TWEthereumAbiFunctionAddParamAddress(struct TWEthereumAbiFunction *_Nonnull paramType.mutable_param()->mutable_address(); EthereumAbi::Proto::Token token; - const Data& addressData = *(reinterpret_cast(val)); + const Data& addressData = *(reinterpret_cast(value)); bool prefixed = true; auto addressStr = hex(addressData, prefixed); token.set_address(addressStr); @@ -157,7 +157,7 @@ int TWEthereumAbiFunctionAddParamAddress(struct TWEthereumAbiFunction *_Nonnull return func_in->impl.addParam(std::move(paramType), std::move(token), isOutput); } -int TWEthereumAbiFunctionAddParamBytes(struct TWEthereumAbiFunction *_Nonnull func_in, TWData *_Nonnull val, bool isOutput) { +int TWEthereumAbiFunctionAddParamBytes(struct TWEthereumAbiFunction *_Nonnull func_in, TWData *_Nonnull value, bool isOutput) { assert(func_in != nullptr); EthereumAbi::Proto::Param paramType; @@ -165,13 +165,13 @@ int TWEthereumAbiFunctionAddParamBytes(struct TWEthereumAbiFunction *_Nonnull fu paramType.mutable_param()->mutable_byte_array(); EthereumAbi::Proto::Token token; - const Data& bytesData = *(reinterpret_cast(val)); + const Data& bytesData = *(reinterpret_cast(value)); token.set_byte_array(bytesData.data(), bytesData.size()); return func_in->impl.addParam(std::move(paramType), std::move(token), isOutput); } -int TWEthereumAbiFunctionAddParamBytesFix(struct TWEthereumAbiFunction *_Nonnull func_in, size_t count, TWData *_Nonnull val, bool isOutput) { +int TWEthereumAbiFunctionAddParamBytesFix(struct TWEthereumAbiFunction *_Nonnull func_in, size_t count, TWData *_Nonnull value, bool isOutput) { assert(func_in != nullptr); EthereumAbi::Proto::Param paramType; @@ -179,7 +179,7 @@ int TWEthereumAbiFunctionAddParamBytesFix(struct TWEthereumAbiFunction *_Nonnull paramType.mutable_param()->mutable_byte_array_fix()->set_size(static_cast(count)); EthereumAbi::Proto::Token token; - Data bytesData = *(reinterpret_cast(val)); + Data bytesData = *(reinterpret_cast(value)); bytesData.resize(count); token.set_byte_array_fix(bytesData.data(), bytesData.size()); @@ -258,79 +258,79 @@ TWData *_Nonnull TWEthereumAbiFunctionGetParamAddress(struct TWEthereumAbiFuncti ///// AddInArrayParam -int TWEthereumAbiFunctionAddInArrayParamUInt8(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, uint8_t val) { +int TWEthereumAbiFunctionAddInArrayParamUInt8(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, uint8_t value) { assert(func_in != nullptr); - auto encodedVal = store(val); + auto encodedVal = store(value); return func_in->impl.addInArrayUintParam(arrayIdx, 8, encodedVal); } -int TWEthereumAbiFunctionAddInArrayParamUInt16(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, uint16_t val) { +int TWEthereumAbiFunctionAddInArrayParamUInt16(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, uint16_t value) { assert(func_in != nullptr); - auto encodedVal = store(val); + auto encodedVal = store(value); return func_in->impl.addInArrayUintParam(arrayIdx, 16, encodedVal); } -int TWEthereumAbiFunctionAddInArrayParamUInt32(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, uint32_t val) { +int TWEthereumAbiFunctionAddInArrayParamUInt32(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, uint32_t value) { assert(func_in != nullptr); - auto encodedVal = store(val); + auto encodedVal = store(value); return func_in->impl.addInArrayUintParam(arrayIdx, 32, encodedVal); } -int TWEthereumAbiFunctionAddInArrayParamUInt64(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, uint64_t val) { +int TWEthereumAbiFunctionAddInArrayParamUInt64(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, uint64_t value) { assert(func_in != nullptr); - auto encodedVal = store(val); + auto encodedVal = store(value); return func_in->impl.addInArrayUintParam(arrayIdx, 64, encodedVal); } -int TWEthereumAbiFunctionAddInArrayParamUInt256(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, TWData *_Nonnull val) { +int TWEthereumAbiFunctionAddInArrayParamUInt256(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, TWData *_Nonnull value) { assert(func_in != nullptr); - const Data& bytesData = *(reinterpret_cast(val)); + const Data& bytesData = *(reinterpret_cast(value)); return func_in->impl.addInArrayUintParam(arrayIdx, 256, bytesData); } -int TWEthereumAbiFunctionAddInArrayParamUIntN(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int bits, TWData *_Nonnull val) { +int TWEthereumAbiFunctionAddInArrayParamUIntN(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int bits, TWData *_Nonnull value) { assert(func_in != nullptr); - const Data& bytesData = *(reinterpret_cast(val)); + const Data& bytesData = *(reinterpret_cast(value)); return func_in->impl.addInArrayUintParam(arrayIdx, bits, bytesData); } -int TWEthereumAbiFunctionAddInArrayParamInt8(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int8_t val) { +int TWEthereumAbiFunctionAddInArrayParamInt8(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int8_t value) { assert(func_in != nullptr); - auto encodedVal = store(val); + auto encodedVal = store(value); return func_in->impl.addInArrayIntParam(arrayIdx, 8, encodedVal); } -int TWEthereumAbiFunctionAddInArrayParamInt16(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int16_t val) { +int TWEthereumAbiFunctionAddInArrayParamInt16(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int16_t value) { assert(func_in != nullptr); - auto encodedVal = store(val); + auto encodedVal = store(value); return func_in->impl.addInArrayIntParam(arrayIdx, 16, encodedVal); } -int TWEthereumAbiFunctionAddInArrayParamInt32(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int32_t val) { +int TWEthereumAbiFunctionAddInArrayParamInt32(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int32_t value) { assert(func_in != nullptr); - auto encodedVal = store(val); + auto encodedVal = store(value); return func_in->impl.addInArrayIntParam(arrayIdx, 32, encodedVal); } -int TWEthereumAbiFunctionAddInArrayParamInt64(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int64_t val) { +int TWEthereumAbiFunctionAddInArrayParamInt64(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int64_t value) { assert(func_in != nullptr); - auto encodedVal = store(val); + auto encodedVal = store(value); return func_in->impl.addInArrayIntParam(arrayIdx, 64, encodedVal); } -int TWEthereumAbiFunctionAddInArrayParamInt256(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, TWData *_Nonnull val) { +int TWEthereumAbiFunctionAddInArrayParamInt256(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, TWData *_Nonnull value) { assert(func_in != nullptr); - const Data& bytesData = *(reinterpret_cast(val)); + const Data& bytesData = *(reinterpret_cast(value)); return func_in->impl.addInArrayIntParam(arrayIdx, 256, bytesData); } -int TWEthereumAbiFunctionAddInArrayParamIntN(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int bits, TWData *_Nonnull val) { +int TWEthereumAbiFunctionAddInArrayParamIntN(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, int bits, TWData *_Nonnull value) { assert(func_in != nullptr); - const Data& bytesData = *(reinterpret_cast(val)); + const Data& bytesData = *(reinterpret_cast(value)); return func_in->impl.addInArrayIntParam(arrayIdx, bits, bytesData); } -int TWEthereumAbiFunctionAddInArrayParamBool(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, bool val) { +int TWEthereumAbiFunctionAddInArrayParamBool(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, bool value) { assert(func_in != nullptr); EthereumAbi::Proto::ParamType paramType; @@ -338,12 +338,12 @@ int TWEthereumAbiFunctionAddInArrayParamBool(struct TWEthereumAbiFunction *_Nonn paramType.mutable_boolean(); EthereumAbi::Proto::Token token; - token.set_boolean(val); + token.set_boolean(value); return func_in->impl.addInArrayParam(arrayIdx, std::move(paramType), std::move(token)); } -int TWEthereumAbiFunctionAddInArrayParamString(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, TWString *_Nonnull val) { +int TWEthereumAbiFunctionAddInArrayParamString(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, TWString *_Nonnull value) { assert(func_in != nullptr); EthereumAbi::Proto::ParamType paramType; @@ -351,12 +351,12 @@ int TWEthereumAbiFunctionAddInArrayParamString(struct TWEthereumAbiFunction *_No paramType.mutable_string_param(); EthereumAbi::Proto::Token token; - token.set_string_value(TWStringUTF8Bytes(val)); + token.set_string_value(TWStringUTF8Bytes(value)); return func_in->impl.addInArrayParam(arrayIdx, std::move(paramType), std::move(token)); } -int TWEthereumAbiFunctionAddInArrayParamAddress(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, TWData *_Nonnull val) { +int TWEthereumAbiFunctionAddInArrayParamAddress(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, TWData *_Nonnull value) { assert(func_in != nullptr); EthereumAbi::Proto::ParamType paramType; @@ -364,7 +364,7 @@ int TWEthereumAbiFunctionAddInArrayParamAddress(struct TWEthereumAbiFunction *_N paramType.mutable_address(); EthereumAbi::Proto::Token token; - const Data& addressData = *(reinterpret_cast(val)); + const Data& addressData = *(reinterpret_cast(value)); bool prefixed = true; auto addressStr = hex(addressData, prefixed); token.set_address(addressStr); @@ -372,7 +372,7 @@ int TWEthereumAbiFunctionAddInArrayParamAddress(struct TWEthereumAbiFunction *_N return func_in->impl.addInArrayParam(arrayIdx, std::move(paramType), std::move(token)); } -int TWEthereumAbiFunctionAddInArrayParamBytes(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, TWData *_Nonnull val) { +int TWEthereumAbiFunctionAddInArrayParamBytes(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, TWData *_Nonnull value) { assert(func_in != nullptr); EthereumAbi::Proto::ParamType paramType; @@ -380,13 +380,13 @@ int TWEthereumAbiFunctionAddInArrayParamBytes(struct TWEthereumAbiFunction *_Non paramType.mutable_byte_array(); EthereumAbi::Proto::Token token; - const Data& bytesData = *(reinterpret_cast(val)); + const Data& bytesData = *(reinterpret_cast(value)); token.set_byte_array(bytesData.data(), bytesData.size()); return func_in->impl.addInArrayParam(arrayIdx, std::move(paramType), std::move(token)); } -int TWEthereumAbiFunctionAddInArrayParamBytesFix(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, size_t count, TWData *_Nonnull val) { +int TWEthereumAbiFunctionAddInArrayParamBytesFix(struct TWEthereumAbiFunction *_Nonnull func_in, int arrayIdx, size_t count, TWData *_Nonnull value) { assert(func_in != nullptr); EthereumAbi::Proto::ParamType paramType; @@ -394,7 +394,7 @@ int TWEthereumAbiFunctionAddInArrayParamBytesFix(struct TWEthereumAbiFunction *_ paramType.mutable_byte_array_fix()->set_size(static_cast(count)); EthereumAbi::Proto::Token token; - Data bytesData = *(reinterpret_cast(val)); + Data bytesData = *(reinterpret_cast(value)); bytesData.resize(count); token.set_byte_array_fix(bytesData.data(), bytesData.size()); From 5523f93bdd81ddb4c81b6ae251dd14fdba5cd16f Mon Sep 17 00:00:00 2001 From: 10gic <2391796+10gic@users.noreply.github.com> Date: Wed, 18 Sep 2024 16:05:35 +0800 Subject: [PATCH 2/2] Remove unused functions --- .../src/iosMain/kotlin/com/trustwallet/core/AnySigner.kt | 4 ++-- .../src/iosMain/kotlin/com/trustwallet/core/ByteArrayExt.kt | 4 ---- .../src/iosMain/kotlin/com/trustwallet/core/StringExt.kt | 4 ---- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/AnySigner.kt b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/AnySigner.kt index d764e183856..480d260002f 100644 --- a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/AnySigner.kt +++ b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/AnySigner.kt @@ -10,7 +10,7 @@ actual object AnySigner { actual fun sign(input: ByteArray, coin: CoinType): ByteArray { val inputData = TWDataCreateWithBytes(input.toUByteArray().toCValues(), input.size.toULong()) - val result = TWAnySignerSign(input.toTwData(), coin.value)!!.readTwBytes()!! + val result = TWAnySignerSign(inputData, coin.value)!!.readTwBytes()!! TWDataDelete(inputData) return result } @@ -29,7 +29,7 @@ actual object AnySigner { actual fun plan(input: ByteArray, coin: CoinType): ByteArray { val inputData = TWDataCreateWithBytes(input.toUByteArray().toCValues(), input.size.toULong()) - val result = TWAnySignerPlan(input.toTwData(), coin.value)?.readTwBytes()!! + val result = TWAnySignerPlan(inputData, coin.value)?.readTwBytes()!! TWDataDelete(inputData) return result } diff --git a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/ByteArrayExt.kt b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/ByteArrayExt.kt index 67a7326c857..bc537de453d 100644 --- a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/ByteArrayExt.kt +++ b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/ByteArrayExt.kt @@ -15,7 +15,3 @@ internal fun COpaquePointer?.readTwBytes(): ByteArray? = TWDataDelete(it) result } - -@OptIn(ExperimentalUnsignedTypes::class) -internal fun ByteArray?.toTwData(): COpaquePointer? = - TWDataCreateWithBytes(this?.toUByteArray()?.toCValues(), this?.size?.toULong() ?: 0u) diff --git a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/StringExt.kt b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/StringExt.kt index 101ebbfc7f5..79cd8125d84 100644 --- a/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/StringExt.kt +++ b/kotlin/wallet-core-kotlin/src/iosMain/kotlin/com/trustwallet/core/StringExt.kt @@ -4,13 +4,9 @@ package com.trustwallet.core -import kotlinx.cinterop.COpaquePointer import kotlinx.cinterop.CValuesRef import kotlinx.cinterop.toKString -internal fun String?.toTwString(): COpaquePointer? = - this?.let { TWStringCreateWithUTF8Bytes(it) } - // Build String from TWString, and then delete TWString internal fun CValuesRef<*>?.fromTwString(): String? = this?.let {