Skip to content

Commit

Permalink
Merge pull request #32 from dojoengine/update-relay
Browse files Browse the repository at this point in the history
feat: new bindings from dojo.c
  • Loading branch information
Larkooo authored Apr 4, 2024
2 parents 79e365f + 4a2eb48 commit a2b3bc8
Show file tree
Hide file tree
Showing 56 changed files with 26,405 additions and 27,664 deletions.
Binary file modified Assets/Dojo/Plugins/Linux/libdojo_c.so
Binary file not shown.
5 changes: 3 additions & 2 deletions Assets/Dojo/Plugins/WebGL/starknet.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ mergeInto(LibraryManager.library, {
account.__destroy_into_raw();
dynCall_vi(cb, buffer);
},
AccountDeployBurner: async function (accountPtr, cb) {
AccountDeployBurner: async function (accountPtr, privateKey, cb) {
console.log(UTF8ToString(privateKey));
const account = wasm_bindgen.Account.__wrap(accountPtr);
const burner = await account.deployBurner();
const burner = await account.deployBurner(UTF8ToString(privateKey));

account.__destroy_into_raw();
dynCall_vi(cb, burner.__destroy_into_raw());
Expand Down
59 changes: 22 additions & 37 deletions Assets/Dojo/Plugins/WebGL/torii_c.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ mergeInto(LibraryManager.library, {
var buffer = _malloc(bufferSize);
stringToUTF8(entitiesString, buffer, bufferSize);

client.__destroy_into_raw();
dynCall_vi(cb, buffer);
});

client.__destroy_into_raw();
},
AddModelsToSync: function (clientPtr, models) {
var client = wasm_bindgen.Client.__wrap(clientPtr);
Expand Down Expand Up @@ -96,34 +97,37 @@ mergeInto(LibraryManager.library, {
var modelsString = UTF8ToString(models);
var modelsArray = JSON.parse(modelsString);

client.__destroy_into_raw();
client.onSyncModelChange(modelsArray, () => {
gameInstance.SendMessage(
UTF8ToString(callbackObjectName),
UTF8ToString(callbackMethodName)
);
});
},
// Subscribes to a topic
SubscribeTopic: async function (clientPtr, topic, cb) {
var client = wasm_bindgen.Client.__wrap(clientPtr);
const subscribed = await client.subscribeTopic(UTF8ToString(topic));

client.__destroy_into_raw();
dynCall_vi(cb, subscribed);
},
// Unsubscribes from a topic
UnsubscribeTopic: async function (clientPtr, topic, cb) {
var client = wasm_bindgen.Client.__wrap(clientPtr);
const unsubscribed = await client.unsubscribeTopic(UTF8ToString(topic));
// Encode typed data with the corresponding address and return the message hash
// typedData: JSON string
// address: string
EncodeTypedData: function (typedData, address) {
var encodedTypedData = wasm_bindgen.typedDataEncode(
UTF8ToString(typedData),
UTF8ToString(address)
);

client.__destroy_into_raw();
dynCall_vi(cb, unsubscribed);
// return buffer
var bufferSize = lengthBytesUTF8(encodedTypedData) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(encodedTypedData, buffer, bufferSize);

return buffer;
},
// Publishes a message to topic and returns the message id
PublishMessage: async function (clientPtr, topic, message, cb) {
// Publishes a message and returns its ID
// message: typed data JSON string
// signature: JSON string { r: string, s: string }
PublishMessage: async function (clientPtr, message, signature, cb) {
var client = wasm_bindgen.Client.__wrap(clientPtr);
const published = await client.publishMessage(UTF8ToString(topic), JSON.parse(UTF8ToString(message)));
const published = await client.publishMessage(UTF8ToString(message), JSON.parse(UTF8ToString(signature)));
console.log(published);
const publishedString = JSON.stringify(Array.from(published));
const bufferSize = lengthBytesUTF8(publishedString) + 1;
const buffer = _malloc(bufferSize);
Expand All @@ -132,23 +136,4 @@ mergeInto(LibraryManager.library, {
client.__destroy_into_raw();
dynCall_vi(cb, buffer);
},
OnMessage: async function (clientPtr, cb) {
var client = wasm_bindgen.Client.__wrap(clientPtr);
client.onMessage((propagationSource, source, messageId, topic, data) => {
const messageString = JSON.stringify({
propagationSource,
source,
messageId,
topic,
data: Array.from(data),
});

const bufferSize = lengthBytesUTF8(messageString) + 1;
const buffer = _malloc(bufferSize);
stringToUTF8(messageString, buffer, bufferSize);

client.__destroy_into_raw();
dynCall_vi(cb, buffer);
});
},
});
4 changes: 2 additions & 2 deletions Assets/Dojo/Plugins/Windows/libdojo_c.dll
Git LFS file not shown
4 changes: 2 additions & 2 deletions Assets/Dojo/Plugins/iOS/libdojo_c.a
Git LFS file not shown
Binary file modified Assets/Dojo/Plugins/macOS/libdojo_c.bundle
Binary file not shown.
2 changes: 1 addition & 1 deletion Assets/Dojo/Runtime/Config/WorldManagerLocalConfig.asset
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ MonoBehaviour:
toriiUrl: http://localhost:8080
rpcUrl: http://localhost:5050
relayUrl: /ip4/127.0.0.1/tcp/9090
relayWebrtcUrl: /ip4/127.0.0.1/udp/9091/webrtc-direct/certhash/uEiDx-luHsovHTHPuU5qzNHCCFkYYpFPirTn2p7ZIFP0Dig
relayWebrtcUrl: /ip4/127.0.0.1/udp/9091/webrtc-direct/certhash/uEiDD8KQcRhXJcyHxQ9MaxEFxVQY-0bsdTqJMCa4t7px4iA
limit: 100
worldAddress: 0x1bf3dfc0c2b66b3d4abb47e9c8e4c5552992dbc70bb2566b9f6d6ee9b707317
30 changes: 28 additions & 2 deletions Assets/Dojo/Runtime/ModelInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public ModelField(string name)
public abstract class ModelInstance : MonoBehaviour
{
public UnityEvent OnUpdated = new UnityEvent();
public Model Model { get; private set; }

// Initialize the model instance with the model
// Uses the ModelField attribute to map the model fields to the class fields
Expand All @@ -34,6 +35,8 @@ public abstract class ModelInstance : MonoBehaviour
// Called upon instantiation and model update
public virtual void Initialize(Model model)
{
Model = model;

var fields = GetType().GetFields();
foreach (var field in fields)
{
Expand All @@ -45,9 +48,9 @@ public virtual void Initialize(Model model)
}

var modelField = (ModelField)attribute[0];
var value = model.Members[modelField.Name];
var member = model.Members[modelField.Name];

HandleField(this, field, value);
HandleField(this, field, member);
}
}

Expand Down Expand Up @@ -96,6 +99,29 @@ private static void HandleField(object instance, System.Reflection.FieldInfo fie
}
}

public static Model ToModel<T>(T model) where T : ModelInstance
{
var members = new Dictionary<string, object>();
foreach (var field in model.GetType().GetFields())
{
var attribute = field.GetCustomAttributes(typeof(ModelField), false);
if (attribute.Length == 0)
{
continue;
}

var modelField = (ModelField)attribute[0];
members.Add(modelField.Name, field.GetValue(model));
}

return new Model(model.GetType().Name, members);
}

public Model ToModel()
{
return ToModel(this);
}

// Called when the model is updated
public virtual void OnUpdate(Model model)
{
Expand Down
65 changes: 35 additions & 30 deletions Assets/Dojo/Runtime/Starknet/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@ public class Account
private unsafe dojo.Account* account;
#endif

public FieldElement Address{ get; }
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.Hex()));
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.PrivateKey.Inner(),
var resultAccount = dojo.account_new(provider.client, privateKey.Inner.Inner,
CString.FromString(address.Hex()));
if (resultAccount.tag == dojo.ResultAccount_Tag.ErrAccount)
{
Expand All @@ -40,20 +42,23 @@ public unsafe Account(JsonRpcClient provider, SigningKey privateKey, FieldElemen

account = resultAccount._ok;
Address = address;
Signer = privateKey;
}
#endif


#if UNITY_WEBGL && !UNITY_EDITOR
public Account(IntPtr account) {
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)
private unsafe Account(dojo.Account* account, SigningKey signingKey)
{
this.account = account;
Address = new FieldElement(dojo.account_address(account));
Signer = signingKey;
}
#endif

Expand All @@ -65,25 +70,25 @@ private unsafe Account(dojo.Account* 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
// }
// 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
Expand Down Expand Up @@ -120,25 +125,25 @@ public async Task<FieldElement> ExecuteRaw(dojo.Call[] calls)
#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)
private unsafe Account DeployBurnerSync(JsonRpcClient provider, SigningKey signingKey)
{
var result = dojo.account_deploy_burner(provider.client, account);
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);
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)
public async Task<Account> DeployBurner(JsonRpcClient provider, SigningKey signingKey)
{
#if UNITY_WEBGL && !UNITY_EDITOR
return new Account(await StarknetInterop.AccountDeployBurnerAsync(await account.Task));
return new Account(await StarknetInterop.AccountDeployBurnerAsync(await account.Task, signingKey), signingKey);
#else
return await Task.Run(() => DeployBurnerSync(provider));
return await Task.Run(() => DeployBurnerSync(provider, signingKey));
#endif
}
}
Expand Down
21 changes: 9 additions & 12 deletions Assets/Dojo/Runtime/Starknet/BurnerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,33 @@ namespace Dojo.Starknet {
public class BurnerManager {
private JsonRpcClient provider;
private Account masterAccount;
private Account currentBurner;
private List<Account> burners = new();

public Account CurrentBurner => currentBurner;
public List<Account> Burners => burners;
public Account CurrentBurner { get; private set; }
public List<Account> Burners { get; } = new();

public BurnerManager(JsonRpcClient provider, Account masterAccount) {
this.provider = provider;
this.masterAccount = masterAccount;
}

public async Task<Account> DeployBurner()
public async Task<Account> DeployBurner(SigningKey signingKey)
{
var account = await masterAccount.DeployBurner(provider);
burners.Add(account);
currentBurner = account;
var account = await masterAccount.DeployBurner(provider, signingKey);
Burners.Add(account);
CurrentBurner = account;

return account;
}

// This will deploy a new burner if there is no current burner.
public async Task<Account> UseBurner()
{
return currentBurner ??= await DeployBurner();
return CurrentBurner ??= await DeployBurner(new SigningKey());
}

public Account UseBurner(FieldElement address) {
foreach (var burner in burners) {
foreach (var burner in Burners) {
if (burner.Address.Hex() == address.Hex()) {
currentBurner = burner;
CurrentBurner = burner;
return burner;
}
}
Expand Down
Loading

0 comments on commit a2b3bc8

Please sign in to comment.