Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Content/Example.umap
Binary file not shown.
Binary file modified Plugins/Web3AuthSDK/Content/AuthInterface.uasset
Binary file not shown.
206 changes: 123 additions & 83 deletions Plugins/Web3AuthSDK/Source/Web3AuthSDK/Private/Web3Auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
// Need to keep a pointer to self later.
// How this works is:
// Android:
// 1) Just before opening BrowserView, assign thiz to the current instance. Code then moves from C++ to Java.
// 1) Just before opening BrowserView, assign this to the current instance. Code then moves from C++ to Java.
// 2) When returning from BrowserView, onDeepLink is called. Code returns to C++ from Java
// 3) In the implementation of onDeepLink, thiz is used to call the c++ method (setResultUrl) on this instance.
// 3) In the implementation of onDeepLink, this is used to call the c++ method (setResultUrl) on this instance.
// IOS:
// 1) Just before opening WebAuthenticate, assign thiz to the current instance. Code then moves from C++ to ObjC.
// 1) Just before opening WebAuthenticate, assign this to the current instance. Code then moves from C++ to ObjC.
// 2) When returning from WebAuthenticate, callBackFromWebAuthenticateIOS is called. Code returns to C++ from ObjC.
// 3) In the implementation of callBackFromWebAuthenticateIOS, thiz is used to call the c++ method (setResultUrl) on this instance.
// 3) In the implementation of callBackFromWebAuthenticateIOS, this is used to call the c++ method (setResultUrl) on this instance.

UWeb3Auth* thiz_instance = nullptr;

Expand Down Expand Up @@ -75,9 +75,10 @@ void UWeb3Auth::processRequest(FString path, FLoginParams* loginParams = nullptr


TSharedPtr<FJsonObject> initParams = MakeShareable(new FJsonObject);
UE_LOG(LogTemp, Warning, TEXT("clientId: %s"), *web3AuthOptions.clientId);
initParams->SetStringField("clientId", web3AuthOptions.clientId);

switch (web3AuthOptions.network) {
switch (web3AuthOptions.web3AuthNetwork) {
case FNetwork::MAINNET:
initParams->SetStringField("network", "mainnet");
break;
Expand Down Expand Up @@ -124,7 +125,7 @@ void UWeb3Auth::processRequest(FString path, FLoginParams* loginParams = nullptr
}
#endif

switch (web3AuthOptions.buildEnv) {
switch (web3AuthOptions.authBuildEnv) {
case FBuildEnv::PRODUCTION:
initParams->SetStringField("buildEnv", "production");
break;
Expand Down Expand Up @@ -164,22 +165,29 @@ void UWeb3Auth::processRequest(FString path, FLoginParams* loginParams = nullptr
FJsonObjectConverter::UStructToJsonObjectString(FChainConfig::StaticStruct(), &web3AuthOptions.chainConfig, chainConfigOutput);
initParams->SetStringField("chainConfig", chainConfigOutput);

if (!web3AuthOptions.loginConfig.IsEmpty()) {
FString output;

TSharedPtr<FJsonObject> loginConfigMap = MakeShareable(new FJsonObject);

for (auto item : web3AuthOptions.loginConfig) {
TSharedPtr<FJsonObject> loginConfigObject = MakeShareable(new FJsonObject);
FJsonObjectConverter::UStructToJsonObject(FLoginConfigItem::StaticStruct(), &item.Value, loginConfigObject.ToSharedRef(), 0, 0);
if (!web3AuthOptions.authConnectionConfig.IsEmpty())
{
TArray<TSharedPtr<FJsonValue>> jsonArray;

loginConfigMap->SetObjectField(item.Key, loginConfigObject);
for (const FAuthConnectionConfig& Config : web3AuthOptions.authConnectionConfig)
{
TSharedPtr<FJsonObject> JsonObject = MakeShared<FJsonObject>();
if (FJsonObjectConverter::UStructToJsonObject(FAuthConnectionConfig::StaticStruct(), &Config, JsonObject.ToSharedRef(), 0, 0))
{
jsonArray.Add(MakeShared<FJsonValueObject>(JsonObject));
}
}

TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&output);
FJsonSerializer::Serialize(loginConfigMap.ToSharedRef(), Writer);
FString Output;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&Output);
FJsonSerializer::Serialize(jsonArray, Writer);

initParams->SetStringField("loginConfig", output);
initParams->SetStringField("authConnectionConfig", Output);
}
else
{
FString emptyArrayJson = TEXT("[]");
initParams->SetStringField("authConnectionConfig", emptyArrayJson);
}

paramMap->SetObjectField("options", initParams.ToSharedRef());
Expand Down Expand Up @@ -236,35 +244,35 @@ void UWeb3Auth::processRequest(FString path, FLoginParams* loginParams = nullptr
TSharedRef< TJsonWriter<> > jsonWriter = TJsonWriterFactory<>::Create(&json);
FJsonSerializer::Serialize(paramMap.ToSharedRef(), jsonWriter);

if (web3AuthOptions.buildEnv == FBuildEnv::STAGING) {
web3AuthOptions.sdkUrl = "https://staging-auth.web3auth.io/v9";
if (web3AuthOptions.authBuildEnv == FBuildEnv::STAGING) {
web3AuthOptions.sdkUrl = "https://staging-auth.web3auth.io/v10";
}
else if(web3AuthOptions.buildEnv == FBuildEnv::TESTING) {
else if(web3AuthOptions.authBuildEnv == FBuildEnv::TESTING) {
web3AuthOptions.sdkUrl = "https://develop-auth.web3auth.io";
} else {
web3AuthOptions.sdkUrl = "https://auth.web3auth.io/v9";
web3AuthOptions.sdkUrl = "https://auth.web3auth.io/v10";
}

if (web3AuthOptions.buildEnv == FBuildEnv::STAGING) {
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v3";
} else if (web3AuthOptions.buildEnv == FBuildEnv::TESTING) {
if (web3AuthOptions.authBuildEnv == FBuildEnv::STAGING) {
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v5";
} else if (web3AuthOptions.authBuildEnv == FBuildEnv::TESTING) {
web3AuthOptions.walletSdkUrl = "https://develop-wallet.web3auth.io";
} else {
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v3";
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v5";
}

createSession(json, 600, false, "*");
}

void UWeb3Auth::launchWalletServices(FChainConfig chainConfig) {
void UWeb3Auth::showWalletUI(const TArray<FChainConfig>& chainConfig, const FString& chainId) {
this->sessionId = keyStoreUtils->GetSessionId();
if (!this->sessionId.IsEmpty()) {
TSharedPtr <FJsonObject> paramMap = MakeShareable(new FJsonObject);

TSharedPtr <FJsonObject> initParams = MakeShareable(new FJsonObject);
initParams->SetStringField("clientId", web3AuthOptions.clientId);

switch (web3AuthOptions.network) {
switch (web3AuthOptions.web3AuthNetwork) {
case FNetwork::MAINNET:
initParams->SetStringField("network", "mainnet");
break;
Expand Down Expand Up @@ -310,7 +318,7 @@ void UWeb3Auth::launchWalletServices(FChainConfig chainConfig) {
}
#endif

switch (web3AuthOptions.buildEnv) {
switch (web3AuthOptions.authBuildEnv) {
case FBuildEnv::PRODUCTION:
initParams->SetStringField("buildEnv", "production");
break;
Expand Down Expand Up @@ -347,29 +355,47 @@ void UWeb3Auth::launchWalletServices(FChainConfig chainConfig) {
initParams->SetStringField("originData", jsonString);
}

FString chainConfigOutput;
FJsonObjectConverter::UStructToJsonObjectString(FChainConfig::StaticStruct(), &chainConfig, chainConfigOutput);
initParams->SetStringField("chainConfig", chainConfigOutput);
TArray<TSharedPtr<FJsonValue>> JsonArray;

if (!web3AuthOptions.loginConfig.IsEmpty()) {
FString output;
for (const FChainConfig& Config : chainConfig)
{
TSharedPtr<FJsonObject> JsonObject = MakeShared<FJsonObject>();
if (FJsonObjectConverter::UStructToJsonObject(FChainConfig::StaticStruct(), &Config, JsonObject.ToSharedRef(), 0, 0))
{
JsonArray.Add(MakeShared<FJsonValueObject>(JsonObject));
}
}
FString chainConfigOutput;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&chainConfigOutput);
FJsonSerializer::Serialize(JsonArray, Writer);
initParams->SetStringField("chains", chainConfigOutput);

TSharedPtr <FJsonObject> loginConfigMap = MakeShareable(new FJsonObject);
initParams->SetStringField("chainId", chainId);

for (auto item: web3AuthOptions.loginConfig) {
TSharedPtr <FJsonObject> loginConfigObject = MakeShareable(new FJsonObject);
FJsonObjectConverter::UStructToJsonObject(FLoginConfigItem::StaticStruct(),
&item.Value,
loginConfigObject.ToSharedRef(), 0, 0);
if (!web3AuthOptions.authConnectionConfig.IsEmpty())
{
TArray<TSharedPtr<FJsonValue>> JsonArray;

loginConfigMap->SetObjectField(item.Key, loginConfigObject);
}
for (const FAuthConnectionConfig& Config : web3AuthOptions.authConnectionConfig)
{
TSharedPtr<FJsonObject> JsonObject = MakeShared<FJsonObject>();
if (FJsonObjectConverter::UStructToJsonObject(FAuthConnectionConfig::StaticStruct(), &Config, JsonObject.ToSharedRef(), 0, 0))
{
JsonArray.Add(MakeShared<FJsonValueObject>(JsonObject));
}
}

TSharedRef <TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&output);
FJsonSerializer::Serialize(loginConfigMap.ToSharedRef(), Writer);
FString Output;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&Output);
FJsonSerializer::Serialize(JsonArray, Writer);

initParams->SetStringField("loginConfig", output);
}
initParams->SetStringField("authConnectionConfig", Output);
}
else
{
FString emptyArrayJson = TEXT("[]");
initParams->SetStringField("authConnectionConfig", emptyArrayJson);
}

paramMap->SetObjectField("options", initParams.ToSharedRef());
paramMap->SetStringField("actionType", "login");
Expand All @@ -378,12 +404,12 @@ void UWeb3Auth::launchWalletServices(FChainConfig chainConfig) {
TSharedRef <TJsonWriter<>> jsonWriter = TJsonWriterFactory<>::Create(&json);
FJsonSerializer::Serialize(paramMap.ToSharedRef(), jsonWriter);

if (web3AuthOptions.buildEnv == FBuildEnv::STAGING) {
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v3";
} else if (web3AuthOptions.buildEnv == FBuildEnv::TESTING) {
if (web3AuthOptions.authBuildEnv == FBuildEnv::STAGING) {
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v5";
} else if (web3AuthOptions.authBuildEnv == FBuildEnv::TESTING) {
web3AuthOptions.walletSdkUrl = "https://develop-wallet.web3auth.io";
} else {
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v3";
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v5";
}

createSession(json, 86400, true, "*");
Expand Down Expand Up @@ -421,7 +447,7 @@ void UWeb3Auth::request(FChainConfig chainConfig, FString method, TArray<FString
TSharedPtr <FJsonObject> initParams = MakeShareable(new FJsonObject);
initParams->SetStringField("clientId", web3AuthOptions.clientId);

switch (web3AuthOptions.network) {
switch (web3AuthOptions.web3AuthNetwork) {
case FNetwork::MAINNET:
initParams->SetStringField("network", "mainnet");
break;
Expand Down Expand Up @@ -467,7 +493,7 @@ void UWeb3Auth::request(FChainConfig chainConfig, FString method, TArray<FString
}
#endif

switch (web3AuthOptions.buildEnv) {
switch (web3AuthOptions.authBuildEnv) {
case FBuildEnv::PRODUCTION:
initParams->SetStringField("buildEnv", "production");
break;
Expand All @@ -491,25 +517,30 @@ void UWeb3Auth::request(FChainConfig chainConfig, FString method, TArray<FString
FJsonObjectConverter::UStructToJsonObjectString(FChainConfig::StaticStruct(), &chainConfig, chainConfigOutput);
initParams->SetStringField("chainConfig", chainConfigOutput);

if (!web3AuthOptions.loginConfig.IsEmpty()) {
FString output;

TSharedPtr <FJsonObject> loginConfigMap = MakeShareable(new FJsonObject);

for (auto item: web3AuthOptions.loginConfig) {
TSharedPtr <FJsonObject> loginConfigObject = MakeShareable(new FJsonObject);
FJsonObjectConverter::UStructToJsonObject(FLoginConfigItem::StaticStruct(),
&item.Value,
loginConfigObject.ToSharedRef(), 0, 0);
if (!web3AuthOptions.authConnectionConfig.IsEmpty())
{
TArray<TSharedPtr<FJsonValue>> JsonArray;

loginConfigMap->SetObjectField(item.Key, loginConfigObject);
}
for (const FAuthConnectionConfig& Config : web3AuthOptions.authConnectionConfig)
{
TSharedPtr<FJsonObject> JsonObject = MakeShared<FJsonObject>();
if (FJsonObjectConverter::UStructToJsonObject(FAuthConnectionConfig::StaticStruct(), &Config, JsonObject.ToSharedRef(), 0, 0))
{
JsonArray.Add(MakeShared<FJsonValueObject>(JsonObject));
}
}

TSharedRef <TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&output);
FJsonSerializer::Serialize(loginConfigMap.ToSharedRef(), Writer);
FString Output;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&Output);
FJsonSerializer::Serialize(JsonArray, Writer);

initParams->SetStringField("loginConfig", output);
}
initParams->SetStringField("authConnectionConfig", Output);
}
else
{
FString emptyArrayJson = TEXT("[]");
initParams->SetStringField("authConnectionConfig", emptyArrayJson);
}

paramMap->SetObjectField("options", initParams.ToSharedRef());
//paramMap->SetStringField("actionType", "login");
Expand All @@ -518,12 +549,12 @@ void UWeb3Auth::request(FChainConfig chainConfig, FString method, TArray<FString
TSharedRef <TJsonWriter<>> jsonWriter = TJsonWriterFactory<>::Create(&json);
FJsonSerializer::Serialize(paramMap.ToSharedRef(), jsonWriter);

if (web3AuthOptions.buildEnv == FBuildEnv::STAGING) {
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v3";
} else if (web3AuthOptions.buildEnv == FBuildEnv::TESTING) {
if (web3AuthOptions.authBuildEnv == FBuildEnv::STAGING) {
web3AuthOptions.walletSdkUrl = "https://staging-wallet.web3auth.io/v5";
} else if (web3AuthOptions.authBuildEnv == FBuildEnv::TESTING) {
web3AuthOptions.walletSdkUrl = "https://develop-wallet.web3auth.io";
} else {
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v3";
web3AuthOptions.walletSdkUrl = "https://wallet.web3auth.io/v5";
}

//createSession(json, 86400, true);
Expand Down Expand Up @@ -707,12 +738,21 @@ FString UWeb3Auth::startLocalWebServer() {

if (httpRouter.IsValid()) {

auto x = httpRouter->BindRoute(FHttpPath(TEXT("/auth")), EHttpServerRequestVerbs::VERB_GET,
[this](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete) { return requestAuthCallback(Request, OnComplete); });


auto y = httpRouter->BindRoute(FHttpPath(TEXT("/complete")), EHttpServerRequestVerbs::VERB_GET,
[this](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete) { return requestCompleteCallback(Request, OnComplete); });
auto x = httpRouter->BindRoute(
FHttpPath(TEXT("/auth")),
EHttpServerRequestVerbs::VERB_GET,
FHttpRequestHandler::CreateLambda(
[this](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete) {
return requestAuthCallback(Request, OnComplete);
}));

auto y = httpRouter->BindRoute(
FHttpPath(TEXT("/complete")),
EHttpServerRequestVerbs::VERB_GET,
FHttpRequestHandler::CreateLambda(
[this](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete) {
return requestCompleteCallback(Request, OnComplete);
}));

httpRoutes.Add(TPairInitializer<TSharedPtr<IHttpRouter>, FHttpRouteHandle>(httpRouter, x));
httpRoutes.Add(TPairInitializer<TSharedPtr<IHttpRouter>, FHttpRouteHandle>(httpRouter, y));
Expand Down Expand Up @@ -811,15 +851,15 @@ void UWeb3Auth::callBackFromWebAuthenticateIOS(NSString* sResult) {
#endif


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

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

FString UWeb3Auth::getEd25519PrivKey() {
FString UWeb3Auth::getEd25519PrivateKey() {
if (web3AuthResponse.coreKitEd25519PrivKey.IsEmpty() || web3AuthResponse.ed25519PrivKey.IsEmpty()) {
return "";
}
Expand All @@ -844,8 +884,8 @@ void UWeb3Auth::authorizeSession() {
FString pubKey = crypto->generatePublicKey(this->sessionId);
FString session = this->sessionId;
FString origin = this->redirecturl;
//UE_LOG(LogTemp, Warning, TEXT("In authorizeSession Session-ID: %s"), *session);
//UE_LOG(LogTemp, Warning, TEXT("In authorizeSession Origin: %s"), *origin);
//UE_LOG(LogTemp, Warning, TEXT ("In authorizeSession Session-ID: %s"), *session);
//UE_LOG(LogTemp, Warning, TEXT ("In authorizeSession Origin: %s"), *origin);
if(origin.IsEmpty()) {
origin = keyStoreUtils->GetRedirectUrl();
}
Expand Down Expand Up @@ -987,7 +1027,7 @@ void UWeb3Auth::fetchProjectConfig()
{
FString network;

switch (web3AuthOptions.network)
switch (web3AuthOptions.web3AuthNetwork)
{
case FNetwork::MAINNET:
network = "mainnet";
Expand Down
Loading