-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from thirdweb-dev/firekeeper/1271-712
1271, 712 and Session Keys
- Loading branch information
Showing
12 changed files
with
371 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,17 +29,16 @@ private static async Task Main(string[] args) | |
var readResult = await ThirdwebContract.ReadContract<string>(contract, "name"); | ||
Console.WriteLine($"Contract read result: {readResult}"); | ||
|
||
// Create accounts | ||
var privateKeyAccount = new PrivateKeyAccount(client, privateKey); | ||
var embeddedAccount = new EmbeddedAccount(client, "[email protected]"); | ||
var smartAccount = new SmartAccount(client, privateKeyAccount, "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", true, 421614); | ||
var embeddedAccount = new EmbeddedAccount(client, "firekeeper+7121271d@thirdweb.com"); | ||
var smartAccount = new SmartAccount(client, embeddedAccount, "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", true, 421614); | ||
|
||
var accounts = new List<IThirdwebAccount> { privateKeyAccount, embeddedAccount, smartAccount }; | ||
|
||
foreach (var account in accounts) | ||
{ | ||
await account.Connect(); | ||
} | ||
// Attempt to connect pk accounts | ||
await privateKeyAccount.Connect(); | ||
await embeddedAccount.Connect(); | ||
|
||
// Relog if embedded account not logged in | ||
if (!await embeddedAccount.IsConnected()) | ||
{ | ||
await embeddedAccount.SendOTP(); | ||
|
@@ -59,11 +58,34 @@ private static async Task Main(string[] args) | |
} | ||
} | ||
|
||
// Connect the smart account with embedded signer and grant a session key to pk account | ||
await smartAccount.Connect(); | ||
_ = await smartAccount.CreateSessionKey( | ||
signerAddress: await privateKeyAccount.GetAddress(), | ||
approvedTargets: new List<string>() { Constants.ADDRESS_ZERO }, | ||
nativeTokenLimitPerTransactionInWei: "0", | ||
permissionStartTimestamp: "0", | ||
permissionEndTimestamp: (Utils.GetUnixTimeStampNow() + 86400).ToString(), | ||
reqValidityStartTimestamp: "0", | ||
reqValidityEndTimestamp: Utils.GetUnixTimeStampIn10Years().ToString() | ||
); | ||
|
||
// Reconnect to same smart account with pk account as signer | ||
smartAccount = new SmartAccount(client, privateKeyAccount, "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", true, 421614, await smartAccount.GetAddress()); | ||
await smartAccount.Connect(); | ||
|
||
// Log addresses | ||
Console.WriteLine($"PrivateKey Account: {await privateKeyAccount.GetAddress()}"); | ||
Console.WriteLine($"Embedded Account: {await embeddedAccount.GetAddress()}"); | ||
Console.WriteLine($"Smart Account: {await smartAccount.GetAddress()}"); | ||
|
||
// Initialize wallet | ||
var thirdwebWallet = new ThirdwebWallet(); | ||
await thirdwebWallet.Initialize(accounts); | ||
await thirdwebWallet.Initialize(new List<IThirdwebAccount> { privateKeyAccount, embeddedAccount, smartAccount }); | ||
thirdwebWallet.SetActive(await smartAccount.GetAddress()); | ||
Console.WriteLine($"Active account: {await thirdwebWallet.GetAddress()}"); | ||
|
||
// Sign, triggering deploy as needed and 1271 verification | ||
var message = "Hello, Thirdweb!"; | ||
var signature = await thirdwebWallet.PersonalSign(message); | ||
Console.WriteLine($"Signed message: {signature}"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Numerics; | ||
using Nethereum.ABI.EIP712; | ||
|
||
namespace Thirdweb | ||
{ | ||
public static class EIP712 | ||
{ | ||
public async static Task<string> GenerateSignature_SmartAccount( | ||
string domainName, | ||
string version, | ||
BigInteger chainId, | ||
string verifyingContract, | ||
AccountAbstraction.SignerPermissionRequest signerPermissionRequest, | ||
IThirdwebAccount signer | ||
) | ||
{ | ||
var typedData = GetTypedDefinition_SmartAccount(domainName, version, chainId, verifyingContract); | ||
return await signer.SignTypedDataV4(signerPermissionRequest, typedData); | ||
} | ||
|
||
public async static Task<string> GenerateSignature_SmartAccount_AccountMessage( | ||
string domainName, | ||
string version, | ||
BigInteger chainId, | ||
string verifyingContract, | ||
byte[] message, | ||
IThirdwebAccount signer | ||
) | ||
{ | ||
var typedData = GetTypedDefinition_SmartAccount_AccountMessage(domainName, version, chainId, verifyingContract); | ||
var accountMessage = new AccountAbstraction.AccountMessage { Message = message }; | ||
return await signer.SignTypedDataV4(accountMessage, typedData); | ||
} | ||
|
||
public static TypedData<Domain> GetTypedDefinition_SmartAccount(string domainName, string version, BigInteger chainId, string verifyingContract) | ||
{ | ||
return new TypedData<Domain> | ||
{ | ||
Domain = new Domain | ||
{ | ||
Name = domainName, | ||
Version = version, | ||
ChainId = chainId, | ||
VerifyingContract = verifyingContract, | ||
}, | ||
Types = MemberDescriptionFactory.GetTypesMemberDescription(typeof(Domain), typeof(AccountAbstraction.SignerPermissionRequest)), | ||
PrimaryType = nameof(AccountAbstraction.SignerPermissionRequest), | ||
}; | ||
} | ||
|
||
public static TypedData<Domain> GetTypedDefinition_SmartAccount_AccountMessage(string domainName, string version, BigInteger chainId, string verifyingContract) | ||
{ | ||
return new TypedData<Domain> | ||
{ | ||
Domain = new Domain | ||
{ | ||
Name = domainName, | ||
Version = version, | ||
ChainId = chainId, | ||
VerifyingContract = verifyingContract, | ||
}, | ||
Types = MemberDescriptionFactory.GetTypesMemberDescription(typeof(Domain), typeof(AccountAbstraction.AccountMessage)), | ||
PrimaryType = nameof(AccountAbstraction.AccountMessage), | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters