Skip to content

Commit

Permalink
update unrealeditor version
Browse files Browse the repository at this point in the history
  • Loading branch information
grvgoel81 authored and Gaurav Goel committed Nov 15, 2023
1 parent 41b8222 commit 8138397
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 8 deletions.
30 changes: 30 additions & 0 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/ECCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,36 @@ FString UECCrypto::generateECDSASignature(const FString& privateKeyHex, const FS
return signature_hex;
}

FString generateECPrivateKey()
{
EC_KEY *ecKey = EC_KEY_new_by_curve_name(NID_secp128r1);
if (!ecKey) {
std::cerr << "Error creating EC_KEY structure." << std::endl;
return "";
}

if (!EC_KEY_generate_key(ecKey)) {
std::cerr << "Error generating EC key." << std::endl;
EC_KEY_free(ecKey);
return "";
}

const BIGNUM *privateKey = EC_KEY_get0_private_key(ecKey);
FString privateKeyHex = BN_bn2hex(privateKey);

EC_KEY_free(ecKey);

return privateKeyHex;
}

std::vector<byte> generateRandomBytes()
{
AutoSeededRandomPool rng;
std::vector<byte> bytes(16);
rng.GenerateBlock(bytes.data(), bytes.size());
return bytes;
}

UECCrypto::~UECCrypto()
{
}
Expand Down
67 changes: 63 additions & 4 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3Auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
FOnLogin AWeb3Auth::loginEvent;
FOnLogout AWeb3Auth::logoutEvent;

FWeb3AuthResponse AWeb3Auth::web3AuthResponse;

UKeyStoreUtils* AWeb3Auth::keyStoreUtils;
UECCrypto* AWeb3Auth::crypto;

Expand Down Expand Up @@ -70,7 +72,15 @@ void AWeb3Auth::request(FString path, FLoginParams* loginParams = NULL, TShared
case FNetwork::CYAN:
initParams->SetStringField("network", "cyan");
break;

case FNetwork::AQUA:
initParams->SetStringField("network", "aqua");
break;
case FNetwork::SAPPHIRE_DEVNET:
initParams->SetStringField("network", "sapphire_devnet");
break;
case FNetwork::SAPPHIRE_MAINNET:
initParams->SetStringField("network", "sapphire_mainnet");
break;
}

if (web3AuthOptions.redirectUrl != "")
Expand All @@ -81,6 +91,18 @@ void AWeb3Auth::request(FString path, FLoginParams* loginParams = NULL, TShared
initParams->SetStringField("redirectUrl", redirectUrl);
#endif

switch (web3AuthOptions.buildEnv) {
case FBuildEnv::PRODUCTION:
initParams->SetStringField("buildEnv", "production");
break;
case FBuildEnv::TESTING:
initParams->SetStringField("buildEnv", "testing");
break;
case FBuildEnv::STAGING:
initParams->SetStringField("buildEnv", "staging");
break;
2}

if (web3AuthOptions.whiteLabel.name != "") {
FString output;
this->GetJsonStringFromStruct(web3AuthOptions.whiteLabel, output);
Expand All @@ -106,7 +128,8 @@ void AWeb3Auth::request(FString path, FLoginParams* loginParams = NULL, TShared
initParams->SetStringField("loginConfig", output);
}

paramMap->SetObjectField("init", initParams.ToSharedRef());
paramMap->SetObjectField("options", initParams.ToSharedRef());
paramMap->SetObjectField("actionType", "login");


TSharedPtr<FJsonObject> params = MakeShareable(new FJsonObject);
Expand All @@ -132,7 +155,16 @@ void AWeb3Auth::request(FString path, FLoginParams* loginParams = NULL, TShared
const FString jsonOutput = json;
FString base64 = FBase64::Encode(jsonOutput);

FString url = web3AuthOptions.sdkUrl + "/" + path + "#" + base64;
if (web3AuthOptions.buildEnv == FBuildEnv::STAGING) {
web3AuthOptions.sdkUrl = "https://staging-auth.web3auth.io/v5";
}
else if(web3AuthOptions.buildEnv == FBuildEnv::TESTING) {
web3AuthOptions.sdkUrl = "https://develop-auth.web3auth.io";
} else {
web3AuthOptions.sdkUrl = "https://auth.web3auth.io/v5";
}

FString url = web3AuthOptions.sdkUrl + "/" + path + "#" + "b64Params=" + base64;

#if PLATFORM_ANDROID
if (JNIEnv* Env = FAndroidApplication::GetJavaEnv(true)) {
Expand All @@ -152,7 +184,7 @@ void AWeb3Auth::request(FString path, FLoginParams* loginParams = NULL, TShared

void AWeb3Auth::processLogin(FLoginParams loginParams) {
UE_LOG(LogTemp, Warning, TEXT("login called"));
this->request("login", &loginParams);
this->request("start", &loginParams);
}

/*void AWeb3Auth::logout(FJsonObject params) {
Expand Down Expand Up @@ -340,6 +372,33 @@ void AWeb3Auth::callBackFromWebAuthenticateIOS(NSString* sResult) {
}
#endif

FString AWeb3Auth::getPrivKey() {
if (web3AuthResponse.coreKitKey.IsEmpty() || web3AuthResponse.privKey.IsEmpty()) {
return "";
}

return web3AuthOptions.useCoreKitKey ? web3AuthResponse.coreKitKey : web3AuthResponse.privKey;
}

FString AWeb3Auth::getEd25519PrivKey() {
if (web3AuthResponse.coreKitEd25519PrivKey.IsEmpty() || web3AuthResponse.ed25519PrivKey.IsEmpty()) {
return "";
}

return web3AuthOptions.useCoreKitKey ? web3AuthResponse.coreKitEd25519PrivKey : web3AuthResponse.ed25519PrivKey;
}

FUserInfo AWeb3Auth::getUserInfo() {
if (web3AuthResponse.userInfo.IsEmpty()) {
FString error = Web3AuthError::getError(ErrorCode::NOUSERFOUND);
UE_LOG(LogTemp, Fatal, TEXT("%s"), *error);

return FUserInfo();
}

return web3AuthResponse.userInfo;
}

void AWeb3Auth::BeginPlay() {
Super::BeginPlay();
}
Expand Down
27 changes: 27 additions & 0 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3AuthApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ void UWeb3AuthApi::Logout(const FLogoutApiRequest logoutApiRequest, const TFunct
request->ProcessRequest();
}

void UWeb3AuthApi::CreateSession(const FLogoutApiRequest logoutApiRequest, const TFunction<void(FString)> callback)
{
TSharedRef<IHttpRequest> request = FHttpModule::Get().CreateRequest();
request->SetVerb(TEXT("POST"));
request->SetURL(TEXT("https://broadcast-server.tor.us/store/set"));

FString FormString = "key=" + logoutApiRequest.key + "&data=" + FGenericPlatformHttp::UrlEncode(logoutApiRequest.data) + "&signature=" + logoutApiRequest.signature + "&timeout=" + FString::FromInt(logoutApiRequest.timeout);

request->SetHeader(TEXT("Content-Type"), TEXT("application/x-www-form-urlencoded"));
request->SetContentAsString(FormString);

request->OnProcessRequestComplete().BindLambda([callback](FHttpRequestPtr request, FHttpResponsePtr response, bool success) {
FString response_string = response->GetContentAsString();
UE_LOG(LogTemp, Log, TEXT("Response: %s "), *response_string);
UE_LOG(LogTemp, Log, TEXT("Status code: %d "), response->GetResponseCode());

if (success && response->GetResponseCode() == EHttpResponseCodes::Created) {
callback(response_string);
}
else {
UE_LOG(LogTemp, Error, TEXT("Request failed"));
}
});

request->ProcessRequest();
}

UWeb3AuthApi::UWeb3AuthApi()
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "Web3AuthError.h"
1 change: 1 addition & 0 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/ECCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ class WEB3AUTHSDK_API UECCrypto : public UObject

FString generatePublicKey(const FString& privateKeyHex);
FString generateECDSASignature(const FString& privateKeyHex, const FString& data);
FString generateECPrivateKey()
};
59 changes: 58 additions & 1 deletion Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/Web3Auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,22 @@ enum class FMFALevel : uint8
UENUM(BlueprintType)
enum class FNetwork : uint8
{
MAINNET = 0, TESTNET = 1, CYAN = 2
MAINNET = 0, TESTNET = 1, CYAN = 2, AQUA = 3, SAPPHIRE_DEVNET = 4, SAPPHIRE_MAINNET = 5
};

UENUM(BlueprintType)
enum class FChainNamespace : uint8
{
EIP155,
SOLANA
};

UENUM(BlueprintType)
enum class FBuildEnv : uint8
{
PRODUCTION,
STAGING,
TESTING
};


Expand Down Expand Up @@ -420,6 +435,20 @@ struct FUserInfo

FUserInfo() {};

bool IsEmpty() const {
return email.IsEmpty()
&& name.IsEmpty()
&& profileImage.IsEmpty()
&& aggregateVerifier.IsEmpty()
&& verifier.IsEmpty()
&& verifierId.IsEmpty()
&& typeOfLogin.IsEmpty()
&& dappShare.IsEmpty()
&& idToken.IsEmpty()
&& oAuthIdToken.IsEmpty()
&& oAuthAccessToken.IsEmpty();
}

};


Expand Down Expand Up @@ -478,12 +507,21 @@ struct FWeb3AuthOptions
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FNetwork network;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FBuildEnv buildEnv;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FWhiteLabelData whiteLabel;

UPROPERTY(BlueprintReadWrite)
TMap<FString, FLoginConfigItem> loginConfig;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FChainNamespace chainNamespace;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool useCoreKitKey;


FWeb3AuthOptions() {};

Expand All @@ -492,8 +530,11 @@ struct FWeb3AuthOptions
redirectUrl = other.redirectUrl;
sdkUrl = other.sdkUrl;
network = other.network;
buildEnv = other.buildEnv;
whiteLabel = other.whiteLabel;
loginConfig = other.loginConfig;
chainNamespace = other.chainNamespace;
useCoreKitKey = other.useCoreKitKey;
}

};
Expand All @@ -519,6 +560,12 @@ struct FWeb3AuthResponse
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FUserInfo userInfo;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString coreKitKey;

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString coreKitEd25519PrivKey;

FWeb3AuthResponse() {};

};
Expand Down Expand Up @@ -553,6 +600,7 @@ class WEB3AUTHSDK_API AWeb3Auth : public AActor
GENERATED_BODY()

FWeb3AuthOptions web3AuthOptions;
static FWeb3AuthResponse web3AuthResponse;

TSharedPtr<IHttpRouter> httpRouter;
TArray<TPair<TSharedPtr<IHttpRouter>, FHttpRouteHandle>> httpRoutes;
Expand Down Expand Up @@ -608,6 +656,15 @@ class WEB3AUTHSDK_API AWeb3Auth : public AActor
return output;
}

UFUNCTION(BlueprintCallable)
FString getPrivKey();

UFUNCTION(BlueprintCallable)
FString getEd25519PrivKey();

UFUNCTION(BlueprintCallable)
FUserInfo getUserInfo();

#if PLATFORM_IOS
static void callBackFromWebAuthenticateIOS(NSString* sResult);
#endif
Expand Down
3 changes: 3 additions & 0 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/Web3AuthApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class WEB3AUTHSDK_API UWeb3AuthApi : public UObject
// Logout the user session
void Logout(const FLogoutApiRequest logoutApiRequest, const TFunction<void(FString)> callback);

// Create the user session
void CreateSession(const FLogoutApiRequest logoutApiRequest, const TFunction<void(FString)> callback);

private:
// Private constructor to enforce singleton pattern
UWeb3AuthApi();
Expand Down
43 changes: 43 additions & 0 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Public/Web3AuthError.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include "CoreMinimal.h"
#include <string>

enum ErrorCode
{
NOUSERFOUND,
ENCODING_ERROR,
DECODING_ERROR,
SOMETHING_WENT_WRONG,
RUNTIME_ERROR,
APP_CANCELLED
};

class Web3AuthError
{
public:
static FString getError(ErrorCode code) {
switch (code)
{
case ErrorCode::NOUSERFOUND:
return "No user found, please login again!";
break;
case ErrorCode::ENCODING_ERROR:
return "Encoding Error";
break;
case ErrorCode::DECODING_ERROR:
return "Decoding Error";
break;
case ErrorCode::SOMETHING_WENT_WRONG:
return "Something went wrong!";
break;
case ErrorCode::RUNTIME_ERROR:
return "Runtime Error";
break;
case ErrorCode::APP_CANCELLED:
return "App Cancelled";
break;
}
return "";
}
};
11 changes: 8 additions & 3 deletions Plugins/Web3AuthSDK/Web3AuthSDK.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "1.0",
"VersionName": "2.0",
"FriendlyName": "Web3AuthSDK",
"Description": "",
"Category": "Other",
Expand All @@ -19,7 +19,12 @@
"Name": "Web3AuthSDK",
"Type": "Runtime",
"LoadingPhase": "Default",
"WhitelistPlatforms": [ "Android", "IOS", "Win64", "Mac" ]
"PlatformAllowList": [
"Android",
"IOS",
"Win64",
"Mac"
]
}
]
}
}

0 comments on commit 8138397

Please sign in to comment.