Skip to content

Commit

Permalink
Merge pull request #105 from 0xsequence/Windows_Secure_Key
Browse files Browse the repository at this point in the history
Windows secure key
  • Loading branch information
ZemindJan authored May 13, 2024
2 parents 16cec42 + 6046e65 commit 07814a6
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 27 deletions.
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
6 changes: 5 additions & 1 deletion Plugins/SequencePlugin/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Sequence Unreal SDK
===================

This plugin requires the modern xcode setting to be enabled to build on MacOS.
See [the following link](https://dev.epicgames.com/documentation/en-us/unreal-engine/using-modern-xcode-in-unreal-engine-5.3-and-newer) for more details.

===================

This SDK allows easy integration of Sequence Embedded Wallet from any Unreal Framework project.

## Manually Upgrading from previous versions
Expand Down Expand Up @@ -754,7 +759,6 @@ Refer to [these docs](https://developers.google.com/identity/one-tap/android/get

#### iOS
For iOS apps you also need to setup provisioning, [following these docs](https://dev.epicgames.com/documentation/en-us/unreal-engine/setting-up-ios-tvos-and-ipados-provisioning-profiles-and-signing-certificates-for-unreal-engine-projects?application_version=5.3)
New to 5.3 the ability to use Modernized XCode has been added. However we've experienced many issues with this setting enabled and recommend turning it off.

### Android
When setting up your project to build for Android you'll need to update the following settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Bitcoin-Cryptography-Library/cpp/Keccak256.hpp"
#include "Interfaces/IHttpResponse.h"
#include "Native/NativeOAuth.h"
#include "NativeEncryptors/WindowsEncryptor.h"
#include "Sequence/SequenceAPI.h"

UAuthenticator::UAuthenticator()
Expand Down Expand Up @@ -45,24 +46,26 @@ UAuthenticator::UAuthenticator()
}
else if constexpr (PLATFORM_WINDOWS)
{

this->Encryptor = NewObject<UWindowsEncryptor>();
}
else if constexpr (PLATFORM_IOS)
{

}
}

UAuthenticator * UAuthenticator::Make(UGenericNativeEncryptor * EncryptorIn)
{
UAuthenticator * Authenticator = NewObject<UAuthenticator>();
Authenticator->Init(EncryptorIn);
return Authenticator;
}

void UAuthenticator::Init(UGenericNativeEncryptor * EncryptorIn)
void UAuthenticator::SetCustomEncryptor(UGenericNativeEncryptor * EncryptorIn)
{
this->Encryptor = EncryptorIn;
if (this->Encryptor)
{
const FString EncryptorName = this->Encryptor->GetClass()->GetName();
UE_LOG(LogTemp,Display,TEXT("Setting custom encryptor to: %s"),*EncryptorName);
}
else
{
UE_LOG(LogTemp,Warning,TEXT("Received null instead of a pointer to an Encryptor Object using fallback encryptor"));
}
}

void UAuthenticator::ClearStoredCredentials() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ FString UAndroidEncryptor::Encrypt(const FString& StringIn)
jenv->DeleteLocalRef(gameActivityClass);
}
#endif
UE_LOG(LogTemp,Display,TEXT("Encrypted Result: %s"),*Result);
//UE_LOG(LogTemp,Display,TEXT("Encrypted Result: %s"),*Result);
return Result;
}

Expand All @@ -56,6 +56,6 @@ FString UAndroidEncryptor::Decrypt(const FString& StringIn)
jenv->DeleteLocalRef(gameActivityClass);
}
#endif
UE_LOG(LogTemp,Display,TEXT("Decrypted Result: %s"),*Result);
//UE_LOG(LogTemp,Display,TEXT("Decrypted Result: %s"),*Result);
return Result;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//Copyright 2024 Horizon Blockchain Games Inc. All rights reserved.

#include "NativeEncryptors/WindowsEncryptor.h"
#include "Misc/Base64.h"
#include "Internationalization/Regex.h"

#if PLATFORM_WINDOWS
#include "Windows/WindowsHWrapper.h"
#include "dpapi.h"
#endif

FString UWindowsEncryptor::Encrypt(const FString& StringIn)
{
FString Result = "";
UE_LOG(LogTemp,Display,TEXT("Preparing to encrypt on windows: %s"),*StringIn);
#if PLATFORM_WINDOWS
DATA_BLOB DataIn;
DATA_BLOB DataOut;
const FString ProcString = FBase64::Encode(StringIn);

//UE_LOG(LogTemp,Display,TEXT("Decrypted PreResult: %s"),*ProcString);
//UE_LOG(LogTemp,Display,TEXT("PreResult Length: %d"),ProcString.Len());

const int32 InSize = ProcString.Len();
uint8 * CharsIn = new uint8[InSize];
StringToBytes(ProcString,CharsIn,InSize);
BYTE *pbDataInput = CharsIn;
const DWORD cbDataInput = InSize;
UE_LOG(LogTemp,Display,TEXT("InByteCount: %d"),cbDataInput);

DataIn.pbData = pbDataInput;
DataIn.cbData = cbDataInput;
if (CryptProtectData(
&DataIn,
NULL,
NULL,
NULL,
NULL,
0,
&DataOut))
{
BytesToHex(DataOut.pbData,DataOut.cbData,Result);
LocalFree(DataOut.pbData);
}
else
{
UE_LOG(LogTemp,Display,TEXT("Encryption Failed on windows"));
}
delete[] CharsIn;
#endif
//UE_LOG(LogTemp,Display,TEXT("Encrypted Result: %s"),*Result);
return Result;
}

FString UWindowsEncryptor::Decrypt(const FString& StringIn)
{
FString Result = "";
UE_LOG(LogTemp,Display,TEXT("Preparing to decrypt on windows: %s"),*StringIn);
#if PLATFORM_WINDOWS
DATA_BLOB DataIn;
DATA_BLOB DataOut;

const int32 InSize = StringIn.Len() / 2;
uint8 * CharsIn = new uint8[InSize];

const FRegexPattern HexPattern(TEXT("^[a-fA-F0-9]+$"));
FRegexMatcher HexChecker(HexPattern,StringIn);

if (HexChecker.FindNext())
{
HexToBytes(StringIn,CharsIn);
}
else
{
delete[] CharsIn;
UE_LOG(LogTemp,Error,TEXT("Provided String is InValid and cannot be decoded!"));
return "";
}

BYTE *pbDataInput = CharsIn;
const DWORD cbDataInput = InSize;
DataIn.pbData = pbDataInput;
DataIn.cbData = cbDataInput;
LPWSTR pDescrOut = NULL;
if (CryptUnprotectData(
&DataIn,
&pDescrOut,
NULL,
NULL,
NULL,
0,
&DataOut))
{
const FString PreResult = BytesToString(DataOut.pbData,DataOut.cbData);

//UE_LOG(LogTemp,Display,TEXT("Decrypted PreResult: %s"),*PreResult);
//UE_LOG(LogTemp,Display,TEXT("PreResult Length: %d"),PreResult.Len());

if (FBase64::Decode(PreResult,Result))
{
UE_LOG(LogTemp,Display,TEXT("Successful B64 Decode"));
}
else
{
UE_LOG(LogTemp,Error,TEXT("UnSuccessful B64 Decode"));
}

LocalFree(DataOut.pbData);
LocalFree(pDescrOut);
}
else
{
const int32 ErrorCode = GetLastError();
UE_LOG(LogTemp,Display,TEXT("Decryption Failed on windows, Error code: %d"),ErrorCode);
}
delete[] CharsIn;
#endif
//UE_LOG(LogTemp,Display,TEXT("Decrypted Result: %s"),*Result);
return Result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ void ASequenceBackendManager::BeginPlay()

//SYNC FUNCTIONAL CALLS// [THESE ARE BLOCKING CALLS AND WILL RETURN DATA IMMEDIATELY]

void ASequenceBackendManager::SetupCustomEncryptor(UGenericNativeEncryptor * EncryptorIn)
{
if (this->Authenticator)
{
this->Authenticator->SetCustomEncryptor(EncryptorIn);
}
}

/*
Used to copy data to the systems clipboard!
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class SEQUENCEPLUGIN_API UAuthenticator : public UObject
private:
UAuthenticator();
public:
static UAuthenticator * Make(UGenericNativeEncryptor * EncryptorIn);
void SetCustomEncryptor(UGenericNativeEncryptor * EncryptorIn);

FString GetSigninURL(const ESocialSigninType& Type);

Expand All @@ -401,8 +401,6 @@ class SEQUENCEPLUGIN_API UAuthenticator : public UObject

void ClearStoredCredentials() const;
private:
void Init(UGenericNativeEncryptor * EncryptorIn);

bool CanHandleEmailLogin();

bool GetStoredCredentials(FCredentials_BE * Credentials) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
*
*/
UCLASS()
UCLASS(Blueprintable)
class SEQUENCEPLUGIN_API UAndroidEncryptor : public UGenericNativeEncryptor
{
GENERATED_BODY()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
*
*/
UCLASS()
UCLASS(Blueprintable)
class SEQUENCEPLUGIN_API UGenericNativeEncryptor : public UObject
{
GENERATED_BODY()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//Copyright 2024 Horizon Blockchain Games Inc. All rights reserved.

#pragma once

#include "CoreMinimal.h"
#include "NativeEncryptors/GenericNativeEncryptor.h"
#include "WindowsEncryptor.generated.h"

/**
*
*/
UCLASS(Blueprintable)
class SEQUENCEPLUGIN_API UWindowsEncryptor : public UGenericNativeEncryptor
{
GENERATED_BODY()

public:
virtual FString Encrypt(const FString& StringIn) override;

virtual FString Decrypt(const FString& StringIn) override;
};
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class SEQUENCEPLUGIN_API ASequenceBackendManager : public AActor

public:
//SYNC FUNCTIONAL CALLS// [THESE ARE BLOCKING CALLS AND WILL RETURN DATA IMMEDIATELY]

UFUNCTION(BlueprintCallable, CATEGORY="FUNCTION")
void SetupCustomEncryptor(UGenericNativeEncryptor * EncryptorIn);

/*
* Used to send data to clipboard for ease of use!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ public SequencePlugin(ReadOnlyTargetRules Target) : base(Target)
// ... add any modules that your module loads dynamically here ...
}
);


if (Target.Platform == UnrealTargetPlatform.Win64)
{
PublicSystemLibraries.AddRange(
new string[]
{
"Crypt32.lib"
}
);
}

//Add IOS Specific Dependencies
if (Target.Platform == UnrealTargetPlatform.IOS)
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Sequence Unreal SDK
===================

This plugin requires the modern xcode setting to be enabled to build on MacOS.
See [the following link](https://dev.epicgames.com/documentation/en-us/unreal-engine/using-modern-xcode-in-unreal-engine-5.3-and-newer) for more details.

===================

This SDK allows easy integration of Sequence Embedded Wallet from any Unreal Framework project.

## Manually Upgrading from previous versions
Expand Down Expand Up @@ -754,7 +759,6 @@ Refer to [these docs](https://developers.google.com/identity/one-tap/android/get

#### iOS
For iOS apps you also need to setup provisioning, [following these docs](https://dev.epicgames.com/documentation/en-us/unreal-engine/setting-up-ios-tvos-and-ipados-provisioning-profiles-and-signing-certificates-for-unreal-engine-projects?application_version=5.3)
New to 5.3 the ability to use Modernized XCode has been added. However we've experienced many issues with this setting enabled and recommend turning it off.

### Android
When setting up your project to build for Android you'll need to update the following settings:
Expand Down

0 comments on commit 07814a6

Please sign in to comment.