-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathController.cs
More file actions
201 lines (174 loc) · 6.16 KB
/
Controller.cs
File metadata and controls
201 lines (174 loc) · 6.16 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Threading.Tasks;
using bottlenoselabs.C2CS.Runtime;
using dojo_bindings;
using UnityEngine;
using Dojo.Starknet;
using System.Linq;
namespace Dojo.Controller
{
[Serializable]
public struct Policy
{
public FieldElement target;
public string method;
public string description;
public Policy(FieldElement target, string method, string description)
{
this.target = target;
this.method = method;
this.description = description;
}
public dojo.Policy ToNative()
{
return new dojo.Policy { target = target.Inner, method = method, description = description };
}
}
public class Controller
{
unsafe private dojo.ControllerAccount* controller;
#if UNITY_WEBGL && !UNITY_EDITOR
public FieldElement Address => new FieldElement(ControllerInterop.Address());
#else
unsafe public FieldElement Address => new FieldElement(dojo.controller_address(controller));
#endif
private static dojo.FnPtr_ControllerAccountPtr_Void onConnectCallback;
private static TaskCompletionSource<Controller> connectionTask;
unsafe private Controller(dojo.ControllerAccount* controller)
{
this.controller = controller;
}
private Controller() {}
#if UNITY_WEBGL && !UNITY_EDITOR
public static async Task<Controller> Connect(string rpcUrl, Policy[] policies, FieldElement chainId = null)
{
if (await ControllerInterop.Connect(rpcUrl, policies, chainId))
{
return new Controller();
}
Debug.LogWarning("Failed to connect to controller");
return null;
}
#else
unsafe private static Controller GetAccount(Policy[] policies, FieldElement chainId)
{
var nativePolicies = policies.Select(p => p.ToNative()).ToArray();
dojo.Policy* policiesPtr = null;
if (nativePolicies.Length > 0)
{
fixed (dojo.Policy* ptr = &nativePolicies[0])
{
policiesPtr = ptr;
}
}
var result = dojo.controller_account(policiesPtr, (UIntPtr)policies.Length, chainId.Inner);
if (result.tag == dojo.ResultControllerAccount_Tag.ErrControllerAccount)
{
Debug.LogWarning(result.err.message);
return null;
}
return new Controller(result._ok);
}
unsafe public static Task<Controller> Connect(string rpcUrl, Policy[] policies, FieldElement chainId = null)
{
if (chainId != null) {
var account = GetAccount(policies, chainId);
if (account != null) {
return Task.FromResult(account);
}
}
connectionTask = new TaskCompletionSource<Controller>();
var nativePolicies = policies.Select(p => p.ToNative()).ToArray();
CString crpcUrl = CString.FromString(rpcUrl);
dojo.Policy* policiesPtr = null;
if (nativePolicies.Length > 0)
{
fixed (dojo.Policy* ptr = &nativePolicies[0])
{
policiesPtr = ptr;
}
}
onConnectCallback = new dojo.FnPtr_ControllerAccountPtr_Void((controllerPtr) =>
{
var controller = new Controller(controllerPtr);
connectionTask.TrySetResult(controller);
});
dojo.controller_connect(crpcUrl, policiesPtr, (UIntPtr)policies.Length, onConnectCallback);
return connectionTask.Task;
}
#endif
#if UNITY_WEBGL && !UNITY_EDITOR
#else
unsafe public static bool Clear(Policy[] policies, FieldElement chainId)
{
var nativePolicies = policies.Select(p => p.ToNative()).ToArray();
dojo.Policy* policiesPtr = null;
if (nativePolicies.Length > 0)
{
fixed (dojo.Policy* ptr = &nativePolicies[0])
{
policiesPtr = ptr;
}
}
var result = dojo.controller_clear(policiesPtr, (UIntPtr)policies.Length, chainId.Inner);
if (result.tag == dojo.Resultbool_Tag.Errbool)
{
throw new Exception(result.err.message);
}
return result.ok;
}
#endif
#if UNITY_WEBGL && !UNITY_EDITOR
public async Task<FieldElement> Execute(Call[] calls)
{
var hashOrError = await ControllerInterop.Execute(calls);
if (hashOrError.StartsWith("0x")) {
return new FieldElement(hashOrError);
}
throw new Exception(hashOrError);
}
#else
unsafe private FieldElement ExecuteSync(Call[] calls)
{
var nativeCalls = calls.Select(c => c.ToNative()).ToArray();
dojo.Call* callsPtr;
fixed (dojo.Call* ptr = &nativeCalls[0])
{
callsPtr = ptr;
}
var result = dojo.controller_execute_from_outside(controller, callsPtr, (UIntPtr)calls.Length);
if (result.tag == dojo.ResultFieldElement_Tag.ErrFieldElement)
{
throw new Exception(result.err.message);
}
return new FieldElement(result.ok);
}
public async Task<FieldElement> Execute(Call[] calls)
{
return await Task.Run(() => ExecuteSync(calls));
}
#endif
#if UNITY_WEBGL && !UNITY_EDITOR
public Task<string> Username()
{
return ControllerInterop.Username();
}
#else
unsafe public Task<string> Username()
{
return Task.Run(() => CString.ToString(dojo.controller_username(controller)));
}
#endif
#if UNITY_WEBGL && !UNITY_EDITOR
public Task<FieldElement> ChainId()
{
return ControllerInterop.ChainId();
}
#else
unsafe public Task<FieldElement> ChainId()
{
return Task.Run(() => new FieldElement(dojo.controller_chain_id(controller)));
}
#endif
}
}