Requesting signatures and transactions
To use smart wallets, you could use the EIP1193 provider or use a backend. This guide teaches how to make different requests to the smart wallet with the backend and Unity (it's easier to do so in the context of gaming because no need to encode transactions with Unity).
Sign message#
To sign message with the smart wallet simply call the Openfort's signMessage
or signTypedData
method like so:
_10SignMessageRequest signMessageRequest = new SignMessageRequest("Hello World!");_10string signature = await openfort.SignMessage(signMessageRequest);
Transactions#
1. Create a transaction- Server side#
Create a request to your backend to create a transaction. In the body of the request:
- Include the
player
that signs the transaction. - Include the
policy
that interacts with the contract for gas. If non-existent the user will need to have gas tokens. - Include
optimistic
if you want the transactions to be confirmed faster.
Depending on the type of transaction you're creating you'll define interactions
. The interactions
field is an array of objects that contain the information of the contract to interact with, the function to call, and the arguments to pass to the function.
- the interactions field contains the
contract
that has previously been added to Openfort. - the
functionName
defines the function to call from within the contract. - If there exist more than one function with the same name, the
functionArgs
will be used to determine which function to call.
2. Sign the transaction with the embedded signer. - Client side#
Use the nextAction
returned by the backend to sign the transaction with the embedded signer.
The transaction will be automatically signed and broadcasted by using the sendSignatureTransactionIntentRequest
method.
_46private async void Mint() {_46 // your backend that creates a mint request_46 var webRequest = UnityWebRequest.Post("https://your-backend.com/api/mint", "");_46 webRequest.SetRequestHeader("Authorization", "Bearer " + AccessToken);_46 webRequest.SetRequestHeader("Content-Type", "application/json");_46 webRequest.SetRequestHeader("Accept", "application/json");_46 await SendWebRequestAsync(webRequest);_46_46 Debug.Log("Mint request sent");_46 if (webRequest.result != UnityWebRequest.Result.Success) {_46 Debug.Log("Mint Failed: " + webRequest.error);_46 return;_46 }_46_46 var responseText = webRequest.downloadHandler.text;_46 Debug.Log("Mint Response: " + responseText);_46 var responseJson = JsonConvert.DeserializeObject < RootObject > (responseText);_46 var id = responseJson.Data.Id;_46 if (responseJson.Data.NextAction == null) {_46 Debug.Log("No Next Action");_46 return;_46 }_46_46 var nextAction = responseJson.Data.NextAction.Payload.UserOpHash;_46_46 Debug.Log("Next Action: " + nextAction);_46 // This example assumes you have already checked that Openfort 'embeddedState' is _46 // `ready` and the user is `authenticated`_46 var intentResponse = await Openfort.SendSignatureTransactionIntentRequest(id, nextAction);_46 Debug.Log("Intent Response: " + intentResponse);_46}_46_46private Task SendWebRequestAsync(UnityWebRequest webRequest) {_46 TaskCompletionSource < bool > tcs = new TaskCompletionSource < bool > ();_46 webRequest.SendWebRequest().completed += _ => {_46 switch (webRequest.result) {_46 case UnityWebRequest.Result.Success:_46 tcs.SetResult(true);_46 break;_46 default:_46 tcs.SetException(new Exception(webRequest.error));_46 break;_46 }_46 };_46 return tcs.Task;_46}