Add Privy type wallet
This commit is contained in:
85
src/Managing.Infrastructure.Web3/Services/PrivyService.cs
Normal file
85
src/Managing.Infrastructure.Web3/Services/PrivyService.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Managing.Infrastructure.Evm.Abstractions;
|
||||
using Managing.Infrastructure.Evm.Models;
|
||||
|
||||
public class PrivyService : IPrivyService
|
||||
{
|
||||
private readonly HttpClient _privyClient;
|
||||
private readonly string _appId;
|
||||
private readonly string _appSecret;
|
||||
|
||||
public PrivyService(IPrivySettings settings)
|
||||
{
|
||||
_privyClient = new HttpClient();
|
||||
_appId = settings.AppId;
|
||||
_appSecret = settings.AppSecret;
|
||||
|
||||
ConfigureHttpClient();
|
||||
}
|
||||
|
||||
private void ConfigureHttpClient()
|
||||
{
|
||||
_privyClient.BaseAddress = new Uri("https://api.privy.io/");
|
||||
var authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_appId}:{_appSecret}"));
|
||||
// _privyClient.DefaultRequestHeaders.Authorization =
|
||||
// new AuthenticationHeaderValue("Basic", $"{_appId}:{_appSecret}");
|
||||
_privyClient.DefaultRequestHeaders.Add("privy-app-id", _appId);
|
||||
// add custom header
|
||||
_privyClient.DefaultRequestHeaders.Add("Authorization", authToken);
|
||||
_privyClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
}
|
||||
|
||||
public async Task<PrivyWallet> CreateWalletAsync(string chainType = "ethereum")
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(new { chain_type = chainType });
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
var response = await _privyClient.PostAsJsonAsync("/v1/wallets", content);
|
||||
|
||||
var result = new PrivyWallet();
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
result = await response.Content.ReadFromJsonAsync<PrivyWallet>();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception(await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<HttpResponseMessage> SendTransactionAsync(string walletId, string recipientAddress, long value,
|
||||
string caip2 = "eip155:84532")
|
||||
{
|
||||
var requestBody = new
|
||||
{
|
||||
method = "eth_sendTransaction",
|
||||
caip2,
|
||||
@params = new
|
||||
{
|
||||
transaction = new
|
||||
{
|
||||
to = recipientAddress,
|
||||
value
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return await _privyClient.PostAsJsonAsync(
|
||||
$"/v1/wallets/{walletId}/rpc",
|
||||
requestBody
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user