Skip to content

Commit

Permalink
Coinbase Login & Fix OAuth Linking
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Sep 21, 2024
1 parent 45b66ba commit 7fa71e6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
21 changes: 11 additions & 10 deletions Thirdweb.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,7 @@
// );
// }

// var ecosystemWallet = await EcosystemWallet.Create(
// client: client,
// ecosystemId: "ecosystem.the-bonfire",
// ecosystemPartnerId: "20842d97-be35-4ecc-b51e-9f3ba0843a60",
// email: "[email protected]"
// );
// var ecosystemWallet = await EcosystemWallet.Create(client: client, ecosystemId: "ecosystem.the-bonfire", email: "[email protected]");

// if (!await ecosystemWallet.IsConnected())
// {
Expand All @@ -117,9 +112,15 @@
// );
// Console.WriteLine($"Ecosystem Wallet typed sign: {ecosystemTypedSignature}");

// var siweSigner = await PrivateKeyWallet.Generate(client: client);
// var ecosystemWalletOther = await EcosystemWallet.Create(client: client, ecosystemId: "ecosystem.the-bonfire", authProvider: AuthProvider.Siwe, siweSigner: siweSigner);
// var linkedAccounts = await ecosystemWallet.LinkAccount(walletToLink: ecosystemWalletOther, chainId: 421614);
// var ecosystemWalletOther = await EcosystemWallet.Create(client: client, ecosystemId: "ecosystem.the-bonfire", authProvider: AuthProvider.Telegram);
// var linkedAccounts = await ecosystemWallet.LinkAccount(
// walletToLink: ecosystemWalletOther,
// browserOpenAction: (url) =>
// {
// var psi = new ProcessStartInfo { FileName = url, UseShellExecute = true };
// _ = Process.Start(psi);
// }
// );
// Console.WriteLine($"Linked accounts: {JsonConvert.SerializeObject(linkedAccounts, Formatting.Indented)}");

// var ecosystemSmartWallet = await SmartWallet.Create(ecosystemWallet, 421614);
Expand Down Expand Up @@ -322,7 +323,7 @@

#region InAppWallet - OAuth

// var inAppWalletOAuth = await InAppWallet.Create(client: client, authProvider: AuthProvider.X);
// var inAppWalletOAuth = await InAppWallet.Create(client: client, authProvider: AuthProvider.Coinbase);
// if (!await inAppWalletOAuth.IsConnected())
// {
// _ = await inAppWalletOAuth.LoginWithOauth(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public partial class EcosystemWallet : PrivateKeyWallet
private readonly string _email;
private readonly string _phoneNumber;
private readonly string _authProvider;
private readonly string _ecosystemId;
private readonly string _ecosystemPartnerId;

private string _address;

Expand All @@ -26,9 +28,21 @@ public partial class EcosystemWallet : PrivateKeyWallet
private const string EMBEDDED_WALLET_PATH_V1 = $"{EMBEDDED_WALLET_BASE_PATH}/v1";
private const string ENCLAVE_PATH = $"{EMBEDDED_WALLET_PATH_V1}/enclave-wallet";

private EcosystemWallet(ThirdwebClient client, EmbeddedWallet embeddedWallet, IThirdwebHttpClient httpClient, string email, string phoneNumber, string authProvider, IThirdwebWallet siweSigner)
private EcosystemWallet(
string ecosystemId,
string ecosystemPartnerId,
ThirdwebClient client,
EmbeddedWallet embeddedWallet,
IThirdwebHttpClient httpClient,
string email,
string phoneNumber,
string authProvider,
IThirdwebWallet siweSigner
)
: base(client, null)
{
this._ecosystemId = ecosystemId;
this._ecosystemPartnerId = ecosystemPartnerId;
this._embeddedWallet = embeddedWallet;
this._httpClient = httpClient;
this._email = email;
Expand Down Expand Up @@ -79,6 +93,7 @@ public static async Task<EcosystemWallet> Create(
AuthProvider.Line => "Line",
AuthProvider.Guest => "Guest",
AuthProvider.X => "X",
AuthProvider.Coinbase => "Coinbase",
AuthProvider.Default => string.IsNullOrEmpty(email) ? "Phone" : "Email",
_ => throw new ArgumentException("Invalid AuthProvider"),
};
Expand Down Expand Up @@ -113,12 +128,12 @@ public static async Task<EcosystemWallet> Create(
try
{
var userAddress = await ResumeEnclaveSession(enclaveHttpClient, embeddedWallet, email, phoneNumber, authproviderStr).ConfigureAwait(false);
return new EcosystemWallet(client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner) { _address = userAddress };
return new EcosystemWallet(ecosystemId, ecosystemPartnerId, client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner) { _address = userAddress };
}
catch
{
enclaveHttpClient.RemoveHeader("Authorization");
return new EcosystemWallet(client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner) { _address = null };
return new EcosystemWallet(ecosystemId, ecosystemPartnerId, client, embeddedWallet, enclaveHttpClient, email, phoneNumber, authproviderStr, siweSigner) { _address = null };
}
}

Expand Down Expand Up @@ -329,6 +344,7 @@ public async Task<List<LinkedAccount>> LinkAccount(
case "Telegram":
case "Line":
case "X":
case "Coinbase":
serverRes = await walletToLink.PreAuth_OAuth(isMobile ?? false, browserOpenAction, mobileRedirectScheme, browser).ConfigureAwait(false);
break;
default:
Expand Down Expand Up @@ -450,6 +466,11 @@ public async Task<string> LoginWithOtp(string otp)
var redirectUrl = isMobile ? mobileRedirectScheme : "http://localhost:8789/";
var loginUrl = await this._embeddedWallet.FetchHeadlessOauthLoginLinkAsync(this._authProvider, platform).ConfigureAwait(false);
loginUrl = platform == "web" ? loginUrl : $"{loginUrl}?platform={platform}&redirectUrl={redirectUrl}&developerClientId={this.Client.ClientId}&authOption={this._authProvider}";
loginUrl = $"{loginUrl}&ecosystemId={this._ecosystemId}";
if (!string.IsNullOrEmpty(this._ecosystemPartnerId))
{
loginUrl = $"{loginUrl}&ecosystemPartnerId={this._ecosystemPartnerId}";
}

browser ??= new InAppWalletBrowser();
var browserResult = await browser.Login(this.Client, loginUrl, redirectUrl, browserOpenAction, cancellationToken).ConfigureAwait(false);
Expand Down
5 changes: 4 additions & 1 deletion Thirdweb/Thirdweb.Wallets/InAppWallet/InAppWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public enum AuthProvider
Siwe,
Line,
Guest,
X
X,
Coinbase
}

public struct LinkedAccount
Expand Down Expand Up @@ -105,6 +106,7 @@ public static async Task<InAppWallet> Create(
Thirdweb.AuthProvider.Line => "Line",
Thirdweb.AuthProvider.Guest => "Guest",
Thirdweb.AuthProvider.X => "X",
Thirdweb.AuthProvider.Coinbase => "Coinbase",
Thirdweb.AuthProvider.Default => string.IsNullOrEmpty(email) ? "Phone" : "Email",
_ => throw new ArgumentException("Invalid AuthProvider"),
};
Expand Down Expand Up @@ -230,6 +232,7 @@ public async Task<List<LinkedAccount>> LinkAccount(
case "Telegram":
case "Line":
case "X":
case "Coinbase":
serverRes = await walletToLink.PreAuth_OAuth(isMobile ?? false, browserOpenAction, mobileRedirectScheme, browser).ConfigureAwait(false);
break;
default:
Expand Down

0 comments on commit 7fa71e6

Please sign in to comment.