Keyless Accounts
The Aptos .NET SDK provides an implementation of Keyless accounts to derive accounts from social provider logins. In this guide, we will provide snippets of creating accounts.
Creating KeylessAccounts
Section titled “Creating KeylessAccounts”KeylessAccounts are created to sign transactions and interact with the blockchain using social provider logins. To create a Keyless account, you will need to follow a few steps to obtain the necessary components of a Keyless account.
-
Create a Ephemeral Key Pair
The first step to creating a Keyless account is to create an ephemeral key pair. This is an ephemeral key used to sign transactions. It’s important to store this key pair in a secure location in the application as it will be used to sign transactions.
var ephemeralKeyPair = EphemeralKeyPair.Generate(); -
Obtaining an OpenID Connect (OIDC) Identity Token
To obtain an
id_token
(OIDC Identity Token), you will need to authenticate with a social provider. At the end of the authorization flow, the user should be redirected to your application with anid_token
. You will need to store thisid_token
in a secure location in the application. It’s important that theid_token
has a nonce field that matches thenonce
field inside theEphemeralKeyPair
.Example:
var nonce = ephemeralKeyPair.Nonce;var authorizationUrl = "https://accounts.google.com/o/oauth2/v2/auth&nonce=" + nonce; -
Deriving a Keyless Account
Once the user has the following components, they should be able to derive a Keyless account.
id_token
: Obtained from the authorization flow.EphemeralKeyPair
: Created in the previous steps.
It’s important that the
nonce
field inside theEphemeralKeyPair
matches thenonce
field inside theid_token
to ensure that the user can sign transactions.var client = new AptosClient(Networks.Mainnet);var keylessAccount = await client.Keyless.DeriveAccount(idToken, ephemeralKeyPair); -
Sign and Submit transactions
After deriving a Keyless account, you can sign and submit transactions using the
AptosClient
.// 1. Build the transactionvar transaction = await client.Transaction.Build(sender: keylessAccount,data: new GenerateEntryFunctionPayloadData(function: "0x1::aptos_account::transfer_coins",typeArguments: ["0x1::aptos_coin::AptosCoin"],functionArguments: [account.Address, "100000"]));// 2. Sign and submit the transactionvar submittedTransaction = await client.Transaction.SignAndSubmitTransaction(keylessAccount, transaction);// 3. (Optional) Wait for the transaction to be committedvar committedTransaction = await client.Transaction.WaitForTransaction(submittedTransaction.Hash);