Skip to content

Commit

Permalink
Problem: missing verify function for personal_sign (fix #354) (#355)
Browse files Browse the repository at this point in the history
Add verify signature for walletconnect 2.0
  • Loading branch information
leejw51crypto authored May 23, 2024
1 parent 4bb6b7f commit 8cd6814
Show file tree
Hide file tree
Showing 19 changed files with 263 additions and 150 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Changelog

## [Unreleased]

- Add verify signature for walletconnect 2.0

## [v0.0.19-alpha] - 2024-4-27
- Fix WC 2.0 for defiwallet
- Fix WC 2.0 for defiwallet
- Support UE 5.4


## [v0.0.18-alpha] - 2024-1-2
- Support metamask send-tx for wallet-connect 2.0
- Support Unreal Engine 5.3.0
- Add weakptr checking for WalletConnect 2.0
- Add weakptr checking for WalletConnect 2.0


## [v0.0.17-alpha] - 2023-8-10
Expand All @@ -22,7 +23,7 @@
## [v0.0.13-alpha] - 2023-6-15
- Support apple arm64
- Support Unreal Engine 5.2.0
- Update play-cpp-sdk headerfiles
- Update play-cpp-sdk headerfiles

## [v0.0.12-alpha] - 2023-5-04
- Support sending transactions using Metamask and Crypto.com Defi Wallet
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ UNAME := $(shell uname)
PWD = $(shell pwd)

# Set the play cpp sdk version
PLAYCPPSDK=v0.0.26-alpha
PLAYCPPSDK=v0.0.27-alpha
# Set the play-cpp-sdk cache path
PLAYCPPSDK_CACHE_DIR=./install/$(PLAYCPPSDK)
# Set the play-cpp-sdk target path
Expand Down
3 changes: 1 addition & 2 deletions Source/CronosPlayUnreal/Private/DynamicContractObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ void UDynamicContractObject::NewSigningEthContract(FString contractaddress,
}

char hdpath[100];
snprintf(hdpath, sizeof(hdpath), "m/44'/%d'/0'/0/%d", 60,
walletindex);
snprintf(hdpath, sizeof(hdpath), "m/44'/%d'/0'/0/%d", 60, walletindex);
rust::cxxbridge1::Box<PrivateKey> privatekey =
defiWallet->getCoreWallet()->get_key(hdpath);

Expand Down
48 changes: 48 additions & 0 deletions Source/CronosPlayUnreal/Private/PlayCppSdkActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using namespace std;
using namespace rust;
using namespace com::crypto::game_sdk;
void from_hex(const std::string &s, unsigned char *data, int len);

// Sets default values
APlayCppSdkActor::APlayCppSdkActor() {
Expand Down Expand Up @@ -573,6 +574,53 @@ void copyVecToTArray(const Vec<uint8_t> &src, TArray<uint8> &dst) {
assert(dst.Num() == src.size());
}

void APlayCppSdkActor::VerifyPersonal(FString user_message,
TArray<uint8> signature_bytes,
FString user_address, bool &success,
FString &output_message) {
if (NULL == _coreClient) {
success = false;
output_message = TEXT("Invalid Client");

return;
}

// Remove the "0x" prefix from user_address if it exists
if (user_address.StartsWith("0x")) {
user_address = user_address.RightChop(
2); // skip 2 bytes, get right side of the string
}

if (user_address.Len() != 40) {
success = false;
output_message = TEXT("Invalid address, address should be 20 bytes hex "
"string without 0x prefix");
return;
}

Vec<uint8_t> signature;
copyTArrayToVec(signature_bytes,
signature); // convert unreal array to c++ vector
std::array<uint8_t, 20> dstaddress; // address is fixed 20 bytes
from_hex(TCHAR_TO_UTF8(*user_address), dstaddress.data(), 20);

try {
bool ret = _coreClient->verify_personal_blocking(
TCHAR_TO_UTF8(*user_message), signature, dstaddress);
if (ret) {
success = true;
} else {
success = false;
output_message = TEXT("Signature verification failed");
}
} catch (const std::exception &e) {
success = false;
output_message =
FString::Printf(TEXT("PlayCppSdk VerifyPersonal Error: %s"),
UTF8_TO_TCHAR(e.what()));
}
}

void APlayCppSdkActor::SignPersonal(FString user_message,
FWalletconnectSignPersonalDelegate Out) {
::com::crypto::game_sdk::Walletconnect2Client *coreclient = GetClient();
Expand Down
15 changes: 15 additions & 0 deletions Source/CronosPlayUnreal/Public/PlayCppSdkActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,21 @@ class CRONOSPLAYUNREAL_API APlayCppSdkActor : public AActor {
void SignPersonal(FString user_message,
FWalletconnectSignPersonalDelegate Out);

/**
* verify general message
* @param user_message user message to verify
* @param signature signature byte arrays
* @param user_address user address
* @return success or not
* @return output_message error message
*/
UFUNCTION(BlueprintCallable,
meta = (DisplayName = "VerifyPersonal", Keywords = "PlayCppSdk"),
Category = "PlayCppSdk")
void VerifyPersonal(FString user_message, TArray<uint8> signature_bytes,
FString user_address, bool &success,
FString &output_message);

/**
* sign EIP155 tx
* @param info EIP 155 tx information
Expand Down
4 changes: 2 additions & 2 deletions Source/CronosPlayUnreal/Public/Utlis.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CRONOSPLAYUNREAL_API UUtlis : public UBlueprintFunctionLibrary {
UFUNCTION(BlueprintCallable,
meta = (DisplayName = "ToHex", Keywords = "PlayCppSdk"),
Category = "Utils")
static FString ToHex(const TArray<uint8>& address);
static FString ToHex(const TArray<uint8> &address);

/**
* Convert TArray<uint8> to std::array<uint8_t, 20>
Expand All @@ -32,5 +32,5 @@ class CRONOSPLAYUNREAL_API UUtlis : public UBlueprintFunctionLibrary {
* return all 0
*
*/
static std::array<std::uint8_t, 20> ToArray(const TArray<uint8>& address);
static std::array<std::uint8_t, 20> ToArray(const TArray<uint8> &address);
};
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class String final {
static String lossy(const char16_t *) noexcept;
static String lossy(const char16_t *, std::size_t) noexcept;

String &operator=(const String &) &noexcept;
String &operator=(String &&) &noexcept;
String &operator=(const String &) & noexcept;
String &operator=(String &&) & noexcept;

explicit operator std::string() const;

Expand Down Expand Up @@ -110,8 +110,8 @@ template <> struct copy_assignable_if<false> {
copy_assignable_if() noexcept = default;
copy_assignable_if(const copy_assignable_if &) noexcept = default;
copy_assignable_if &
operator=(const copy_assignable_if &) &noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) &noexcept = default;
operator=(const copy_assignable_if &) & noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) & noexcept = default;
};
} // namespace detail

Expand All @@ -124,8 +124,8 @@ class Slice final
Slice() noexcept;
Slice(T *, std::size_t count) noexcept;

Slice &operator=(const Slice<T> &) &noexcept = default;
Slice &operator=(Slice<T> &&) &noexcept = default;
Slice &operator=(const Slice<T> &) & noexcept = default;
Slice &operator=(Slice<T> &&) & noexcept = default;

T *data() const noexcept;
std::size_t size() const noexcept;
Expand Down Expand Up @@ -398,7 +398,7 @@ template <typename T> class Vec final {
Vec(Vec &&) noexcept;
~Vec() noexcept;

Vec &operator=(Vec &&) &noexcept;
Vec &operator=(Vec &&) & noexcept;
Vec &operator=(const Vec &) &;

std::size_t size() const noexcept;
Expand Down Expand Up @@ -464,7 +464,7 @@ template <typename T> Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {

template <typename T> Vec<T>::~Vec() noexcept { this->drop(); }

template <typename T> Vec<T> &Vec<T>::operator=(Vec &&other) &noexcept {
template <typename T> Vec<T> &Vec<T>::operator=(Vec &&other) & noexcept {
this->drop();
this->repr = other.repr;
new (&other) Vec();
Expand Down Expand Up @@ -605,7 +605,7 @@ class Error final : public std::exception {
~Error() noexcept override;

Error &operator=(const Error &) &;
Error &operator=(Error &&) &noexcept;
Error &operator=(Error &&) & noexcept;

const char *what() const noexcept override;

Expand Down Expand Up @@ -680,7 +680,9 @@ template <typename T> std::size_t align_of() { return layout::align_of<T>(); }
#ifndef CXXBRIDGE1_RELOCATABLE
#define CXXBRIDGE1_RELOCATABLE
namespace detail {
template <typename... Ts> struct make_void { using type = void; };
template <typename... Ts> struct make_void {
using type = void;
};

template <typename... Ts> using void_t = typename make_void<Ts...>::type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class String final {
static String lossy(const char16_t *) noexcept;
static String lossy(const char16_t *, std::size_t) noexcept;

String &operator=(const String &) &noexcept;
String &operator=(String &&) &noexcept;
String &operator=(const String &) & noexcept;
String &operator=(String &&) & noexcept;

explicit operator std::string() const;

Expand Down Expand Up @@ -110,8 +110,8 @@ template <> struct copy_assignable_if<false> {
copy_assignable_if() noexcept = default;
copy_assignable_if(const copy_assignable_if &) noexcept = default;
copy_assignable_if &
operator=(const copy_assignable_if &) &noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) &noexcept = default;
operator=(const copy_assignable_if &) & noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) & noexcept = default;
};
} // namespace detail

Expand All @@ -124,8 +124,8 @@ class Slice final
Slice() noexcept;
Slice(T *, std::size_t count) noexcept;

Slice &operator=(const Slice<T> &) &noexcept = default;
Slice &operator=(Slice<T> &&) &noexcept = default;
Slice &operator=(const Slice<T> &) & noexcept = default;
Slice &operator=(Slice<T> &&) & noexcept = default;

T *data() const noexcept;
std::size_t size() const noexcept;
Expand Down Expand Up @@ -398,7 +398,7 @@ template <typename T> class Vec final {
Vec(Vec &&) noexcept;
~Vec() noexcept;

Vec &operator=(Vec &&) &noexcept;
Vec &operator=(Vec &&) & noexcept;
Vec &operator=(const Vec &) &;

std::size_t size() const noexcept;
Expand Down Expand Up @@ -464,7 +464,7 @@ template <typename T> Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {

template <typename T> Vec<T>::~Vec() noexcept { this->drop(); }

template <typename T> Vec<T> &Vec<T>::operator=(Vec &&other) &noexcept {
template <typename T> Vec<T> &Vec<T>::operator=(Vec &&other) & noexcept {
this->drop();
this->repr = other.repr;
new (&other) Vec();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class String final {
static String lossy(const char16_t *) noexcept;
static String lossy(const char16_t *, std::size_t) noexcept;

String &operator=(const String &) &noexcept;
String &operator=(String &&) &noexcept;
String &operator=(const String &) & noexcept;
String &operator=(String &&) & noexcept;

explicit operator std::string() const;

Expand Down Expand Up @@ -108,7 +108,7 @@ class Str final {
Str(const char *);
Str(const char *, std::size_t);

Str &operator=(const Str &) &noexcept = default;
Str &operator=(const Str &) & noexcept = default;

explicit operator std::string() const;

Expand Down Expand Up @@ -154,8 +154,8 @@ template <> struct copy_assignable_if<false> {
copy_assignable_if() noexcept = default;
copy_assignable_if(const copy_assignable_if &) noexcept = default;
copy_assignable_if &
operator=(const copy_assignable_if &) &noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) &noexcept = default;
operator=(const copy_assignable_if &) & noexcept = delete;
copy_assignable_if &operator=(copy_assignable_if &&) & noexcept = default;
};
} // namespace detail

Expand All @@ -168,8 +168,8 @@ class Slice final
Slice() noexcept;
Slice(T *, std::size_t count) noexcept;

Slice &operator=(const Slice<T> &) &noexcept = default;
Slice &operator=(Slice<T> &&) &noexcept = default;
Slice &operator=(const Slice<T> &) & noexcept = default;
Slice &operator=(Slice<T> &&) & noexcept = default;

T *data() const noexcept;
std::size_t size() const noexcept;
Expand Down Expand Up @@ -439,7 +439,7 @@ template <typename T> class Box final {
explicit Box(const T &);
explicit Box(T &&);

Box &operator=(Box &&) &noexcept;
Box &operator=(Box &&) & noexcept;

const T *operator->() const noexcept;
const T &operator*() const noexcept;
Expand Down Expand Up @@ -507,7 +507,7 @@ template <typename T> Box<T>::~Box() noexcept {
}
}

template <typename T> Box<T> &Box<T>::operator=(Box &&other) &noexcept {
template <typename T> Box<T> &Box<T>::operator=(Box &&other) & noexcept {
if (this->ptr) {
this->drop();
}
Expand Down Expand Up @@ -577,7 +577,7 @@ template <typename T> class Vec final {
Vec(Vec &&) noexcept;
~Vec() noexcept;

Vec &operator=(Vec &&) &noexcept;
Vec &operator=(Vec &&) & noexcept;
Vec &operator=(const Vec &) &;

std::size_t size() const noexcept;
Expand Down Expand Up @@ -643,7 +643,7 @@ template <typename T> Vec<T>::Vec(Vec &&other) noexcept : repr(other.repr) {

template <typename T> Vec<T>::~Vec() noexcept { this->drop(); }

template <typename T> Vec<T> &Vec<T>::operator=(Vec &&other) &noexcept {
template <typename T> Vec<T> &Vec<T>::operator=(Vec &&other) & noexcept {
this->drop();
this->repr = other.repr;
new (&other) Vec();
Expand Down Expand Up @@ -784,7 +784,7 @@ class Error final : public std::exception {
~Error() noexcept override;

Error &operator=(const Error &) &;
Error &operator=(Error &&) &noexcept;
Error &operator=(Error &&) & noexcept;

const char *what() const noexcept override;

Expand Down Expand Up @@ -869,7 +869,9 @@ template <typename T> std::size_t align_of() { return layout::align_of<T>(); }
#ifndef CXXBRIDGE1_RELOCATABLE
#define CXXBRIDGE1_RELOCATABLE
namespace detail {
template <typename... Ts> struct make_void { using type = void; };
template <typename... Ts> struct make_void {
using type = void;
};

template <typename... Ts> using void_t = typename make_void<Ts...>::type;

Expand Down
Loading

0 comments on commit 8cd6814

Please sign in to comment.