-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAccount.cs
More file actions
157 lines (139 loc) · 5.04 KB
/
Account.cs
File metadata and controls
157 lines (139 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Linq;
using System.Threading.Tasks;
using bottlenoselabs.C2CS.Runtime;
using dojo_bindings;
using Debug = UnityEngine.Debug;
namespace Dojo.Starknet
{
public class Account
{
#if UNITY_WEBGL && !UNITY_EDITOR
private TaskCompletionSource<IntPtr> account = new();
#else
private unsafe dojo.Account* account;
#endif
public FieldElement Address { get; }
public SigningKey Signer { get; }
#if UNITY_WEBGL && !UNITY_EDITOR
private async void createAccount(JsonRpcClient provider, SigningKey privateKey, FieldElement address)
{
account.SetResult(await StarknetInterop.NewAccountAsync(provider.client, privateKey, address));
}
public Account(JsonRpcClient provider, SigningKey privateKey, FieldElement address)
{
createAccount(provider, privateKey, address);
Address = address;
Signer = privateKey;
}
#else
public unsafe Account(JsonRpcClient provider, SigningKey privateKey, FieldElement address)
{
var resultAccount = dojo.account_new(provider.client, privateKey.Inner.Inner,
CString.FromString(address.Hex()));
if (resultAccount.tag == dojo.ResultAccount_Tag.ErrAccount)
{
throw new Exception(resultAccount.err.message);
}
account = resultAccount._ok;
Address = address;
Signer = privateKey;
}
#endif
#if UNITY_WEBGL && !UNITY_EDITOR
public Account(IntPtr account, SigningKey signingKey) {
this.account.SetResult(account);
Address = new FieldElement(StarknetInterop.AccountAddress(account));
Signer = signingKey;
}
#else
private unsafe Account(dojo.Account* account, SigningKey signingKey)
{
this.account = account;
Address = new FieldElement(dojo.account_address(account));
Signer = signingKey;
}
#endif
unsafe ~Account()
{
#if UNITY_WEBGL && !UNITY_EDITOR
#else
dojo.account_free(account);
#endif
}
// public async unsafe Task<FieldElement> ChainId()
// {
// #if UNITY_WEBGL && !UNITY_EDITOR
// var chainId = StarknetInterop.AccountChainId(await account.Task);
// #else
// var chainId = dojo.account_chain_id(account);
// #endif
// return new FieldElement(chainId);
// }
// public unsafe void SetBlockId(dojo.BlockId blockId)
// {
// // #if UNITY_WEBGL && !UNITY_EDITOR
// StarknetInterop.account(account, blockId.Hex());
// #else
// dojo.account_set_block_id(account, blockId);
// #endif
// }
#if UNITY_WEBGL && !UNITY_EDITOR
// webgl js interop starknet bindings
public async Task<FieldElement> ExecuteRaw(dojo.Call[] calls)
{
try
{
var res = await StarknetInterop.AccountExecuteRawAsync(await account.Task, calls);
return res;
}
catch (Exception e)
{
Debug.LogError($"Error in ExecuteRaw: {e.Message}");
throw;
}
}
#else
private unsafe FieldElement ExecuteRawSync(dojo.Call[] calls)
{
dojo.Call* callsPtr;
fixed (dojo.Call* ptr = &calls[0])
{
callsPtr = ptr;
}
var result = dojo.account_execute_raw(account, callsPtr, (nuint)calls.Length);
if (result.tag == dojo.ResultFieldElement_Tag.ErrFieldElement)
{
throw new Exception(result.err.message);
}
return new FieldElement(result.ok);
}
public async Task<FieldElement> ExecuteRaw(dojo.Call[] calls)
{
return await Task.Run(() => ExecuteRawSync(calls));
}
#endif
#if !UNITY_WEBGL || UNITY_EDITOR
// This will synchroneously wait for the burner to be deployed.
// Implemented for C bindings that arent async.
private unsafe Account DeployBurnerSync(JsonRpcClient provider, SigningKey signingKey)
{
var result = dojo.account_deploy_burner(provider.client, account, signingKey.Inner.Inner);
if (result.tag == dojo.ResultAccount_Tag.ErrAccount)
{
throw new Exception(result.err.message);
}
return new Account(result._ok, signingKey);
}
#endif
// Deploy a burner and return the account once it is deployed.
public async Task<Account> DeployBurner(JsonRpcClient provider, SigningKey signingKey)
{
#if UNITY_WEBGL && !UNITY_EDITOR
return new Account(await StarknetInterop.AccountDeployBurnerAsync(await account.Task, signingKey), signingKey);
#else
return await Task.Run(() => DeployBurnerSync(provider, signingKey));
#endif
}
}
}