Skip to content

Commit

Permalink
feat(walletconnect): add wallet connect support to UnityEditor (#143)
Browse files Browse the repository at this point in the history
Add a new `WalletConnectAccount` and `WalletConnectAccountAsset` to manage WalletConnect accounts in the editor.

BREAKING CHANGE: `AlgorandWalletConnectSession` flow has been changed to reflect new `JsonRpcClient`. 
- `StartConnection` renamed to `Connect`
- `Disconnect` renamed to `DisconnectWallet`
- `WaitForConnectionApproval` renamed to `WaitForWalletApproval`
- `SavedSession` renamed to `SessionData`
  • Loading branch information
jasonboukheir committed Jun 5, 2022
1 parent 2912d05 commit 60c13af
Show file tree
Hide file tree
Showing 94 changed files with 1,324 additions and 429 deletions.
2 changes: 1 addition & 1 deletion Editor/CareBoo.AlgoSdk.Editor/AddressDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace AlgoSdk.Editor
{
[CustomPropertyDrawer(typeof(Address))]
public class AddressDrawer : BytesTextDrawer<Address>
public class AddressDrawer : FixedBytesTextDrawer<Address>
{
protected override Address GetByteArray(string s)
{
Expand Down
6 changes: 3 additions & 3 deletions Editor/CareBoo.AlgoSdk.Editor/Base32Drawer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Collections.Generic;
using UnityEditor;
using System.Linq;
using Unity.Collections;
using UnityEditor;

namespace AlgoSdk.Editor
{
[CustomPropertyDrawer(typeof(TransactionId))]
public class Base32Drawer : BytesTextDrawer
public class Base32Drawer : FixedBytesTextDrawer
{
protected override List<byte> GetBytes(string s)
{
Expand All @@ -15,7 +15,7 @@ protected override List<byte> GetBytes(string s)

protected override string GetString(List<byte> bytes)
{
var t = new NativeText(Base32Encoding.ToString(bytes.ToArray()), Allocator.Temp);
var t = new NativeText(Base32Encoding.ToString(bytes.ToArray()), Allocator.Persistent);
try
{
Base32Encoding.TrimPadding(ref t);
Expand Down
2 changes: 1 addition & 1 deletion Editor/CareBoo.AlgoSdk.Editor/Base64Drawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AlgoSdk.Editor
[CustomPropertyDrawer(typeof(Sig))]
[CustomPropertyDrawer(typeof(TealBytes))]
[CustomPropertyDrawer(typeof(VrfPubKey))]
public class Base64Drawer : BytesTextDrawer
public class Base64Drawer : FixedBytesTextDrawer
{
protected override List<byte> GetBytes(string s)
{
Expand Down
24 changes: 18 additions & 6 deletions Editor/CareBoo.AlgoSdk.Editor/BytesTextDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace AlgoSdk.Editor
{
public abstract class BytesTextDrawer<T> : BytesTextDrawer
where T : struct, IByteArray
public abstract class BytesTextDrawer<TResult> : BytesTextDrawer
where TResult : struct, IByteArray
{
protected override string GetString(List<byte> bytes)
{
T t = default;
TResult t = default;
for (var i = 0; i < bytes.Count; i++)
t[i] = bytes[i];
return GetString(t);
Expand All @@ -26,16 +26,22 @@ protected override List<byte> GetBytes(string s)
return bytes;
}

protected abstract string GetString(T bytes);
protected abstract T GetByteArray(string s);
protected abstract string GetString(TResult bytes);
protected abstract TResult GetByteArray(string s);
}

public abstract class FixedBytesTextDrawer<TResult> : BytesTextDrawer<TResult>
where TResult : struct, IByteArray
{
protected override SerializedBytes GetSerializedBytes(SerializedProperty property) => new SerializedFixedBytes(property);
}

public abstract class BytesTextDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
position = EditorGUI.PrefixLabel(position, label);
var byteProperties = new SerializedBytes(property);
var byteProperties = GetSerializedBytes(property);
var text = GetString(byteProperties.GetBytes());
text = EditorGUI.DelayedTextField(position, text);
try
Expand All @@ -50,5 +56,11 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten

protected abstract string GetString(List<byte> bytes);
protected abstract List<byte> GetBytes(string s);
protected abstract SerializedBytes GetSerializedBytes(SerializedProperty property);
}

public abstract class FixedBytesTextDrawer : BytesTextDrawer
{
protected override SerializedBytes GetSerializedBytes(SerializedProperty property) => new SerializedFixedBytes(property);
}
}
4 changes: 2 additions & 2 deletions Editor/CareBoo.AlgoSdk.Editor/FixedStringDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
{
position = EditorGUI.PrefixLabel(position, label);
var lengthProperty = property.FindPropertyRelative("utf8LengthInBytes");
var byteProperties = new SerializedBytes(property);
var byteProperties = new SerializedFixedBytes(property);
var bytes = byteProperties.GetBytes();
var length = math.min(lengthProperty.intValue, bytes.Count);
var text = new NativeText(length, Allocator.Temp);
var text = new NativeText(length, Allocator.Persistent);
try
{
text.Length = length;
Expand Down
25 changes: 25 additions & 0 deletions Editor/CareBoo.AlgoSdk.Editor/HexDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Linq;
using UnityEditor;

namespace AlgoSdk.Editor
{
[CustomPropertyDrawer(typeof(Hex))]
public class HexDrawer : BytesTextDrawer
{
protected override List<byte> GetBytes(string s)
{
return Hex.FromString(s).Data.ToList();
}

protected override SerializedBytes GetSerializedBytes(SerializedProperty property)
{
return new SerializedVariableBytes(property, "data");
}

protected override string GetString(List<byte> bytes)
{
return new Hex(bytes.ToArray()).ToString();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Editor/CareBoo.AlgoSdk.Editor/MnemonicDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace AlgoSdk.Editor
{
[CustomPropertyDrawer(typeof(Mnemonic))]
public class MnemonicDrawer : BytesTextDrawer
public class MnemonicDrawer : FixedBytesTextDrawer
{
protected unsafe override List<byte> GetBytes(string s)
{
Expand Down
2 changes: 1 addition & 1 deletion Editor/CareBoo.AlgoSdk.Editor/PrivateKeyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace AlgoSdk.Editor
{
[CustomPropertyDrawer(typeof(PrivateKey))]
public class PrivateKeyDrawer : BytesTextDrawer<PrivateKey>
public class PrivateKeyDrawer : FixedBytesTextDrawer<PrivateKey>
{
protected override PrivateKey GetByteArray(string s)
{
Expand Down
81 changes: 64 additions & 17 deletions Editor/CareBoo.AlgoSdk.Editor/SerializedBytes.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,85 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;

namespace AlgoSdk.Editor
{
public class SerializedBytes
public abstract class SerializedBytes
{
static readonly Regex byteRegex = new Regex(@"byte\d\d\d\d", RegexOptions.Compiled);
SerializedObject serializedObject;
string rootPath;
protected SerializedObject serializedObject;
protected string rootPath;

public SerializedBytes(SerializedProperty root)
{
serializedObject = root.serializedObject;
rootPath = root.propertyPath;
}

public List<byte> GetBytes()
public SerializedProperty Property => serializedObject.FindProperty(rootPath);

public abstract IEnumerable<SerializedProperty> ByteProperties { get; }

public List<byte> GetBytes() => ByteProperties
.Select(prop => (byte)prop.intValue)
.ToList()
;

public abstract void SetBytes(List<byte> bytes);
}

public class SerializedFixedBytes : SerializedBytes
{
static readonly Regex byteRegex = new Regex(@"byte\d\d\d\d", RegexOptions.Compiled);

public SerializedFixedBytes(SerializedProperty root) : base(root) { }

public override IEnumerable<SerializedProperty> ByteProperties
{
get
{
foreach (SerializedProperty child in Property)
{
if (byteRegex.IsMatch(child.name) && child.propertyType == SerializedPropertyType.Integer)
{
yield return child;
}
}
}
}

public override void SetBytes(List<byte> bytes)
{
var props = ByteProperties.ToList();
for (var i = 0; i < bytes.Count; i++)
props[i].intValue = bytes[i];
}
}

public class SerializedVariableBytes : SerializedBytes
{
readonly string byteArrayName;
public SerializedVariableBytes(SerializedProperty root, string byteArrayName) : base(root)
{
this.byteArrayName = byteArrayName;
}

public override IEnumerable<SerializedProperty> ByteProperties
{
var bytes = new List<byte>();
var root = serializedObject.FindProperty(rootPath);
foreach (SerializedProperty child in root)
if (byteRegex.IsMatch(child.name) && child.propertyType == SerializedPropertyType.Integer)
bytes.Add((byte)child.intValue);
return bytes;
get
{
var arrayProp = Property.FindPropertyRelative(byteArrayName);
for (var i = 0; i < arrayProp.arraySize; i++)
yield return arrayProp.GetArrayElementAtIndex(i);
}
}

public void SetBytes(List<byte> bytes)
public override void SetBytes(List<byte> bytes)
{
var i = 0;
var root = serializedObject.FindProperty(rootPath);
foreach (SerializedProperty child in root)
if (byteRegex.IsMatch(child.name) && child.propertyType == SerializedPropertyType.Integer)
child.intValue = (int)bytes[i++];
var arrayProp = Property.FindPropertyRelative(byteArrayName);
arrayProp.arraySize = bytes.Count;
for (var i = 0; i < bytes.Count; i++)
arrayProp.GetArrayElementAtIndex(i).intValue = bytes[i];
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "CareBoo.AlgoSdk.WalletConnect.Editor",
"rootNamespace": "AlgoSdk.WalletConnect.Editor",
"references": [
"GUID:45ab5c0c2cb4a0e4ba897b731349c490",
"GUID:f0420f662311c4331b69b2075b5ab52a",
"GUID:7d7102a6f2a084a30ad9e7734dd7ec17",
"GUID:f51ebe6a0ceec4240a699833d6309b23"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/CareBoo.AlgoSdk.WalletConnect.Editor/UI.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="True">
<ui:Label text="Connect Account via WalletConnect" display-tooltip-when-elided="true" name="Title" style="-unity-text-align: upper-center; padding-bottom: 10px; padding-top: 5px; font-size: 14px;" />
<ui:VisualElement name="AssetReferenceContent" style="margin-top: 5px; margin-bottom: 5px;">
<uie:ObjectField label="Account Asset" allow-scene-objects="true" binding-path="asset" type="AlgoSdk.WalletConnect.WalletConnectAccountAsset, CareBoo.AlgoSdk.WalletConnect" focusable="true" name="AssetField" style="display: flex; visibility: visible;" />
<ui:TextField picking-mode="Ignore" label="Connection Status" value="filler text" text="No Connection" name="StatusField" readonly="true" binding-path="statusText" />
</ui:VisualElement>
<ui:VisualElement name="ConnectedContent" style="align-items: center; display: flex; visibility: visible; margin-top: 5px; margin-bottom: 5px;">
<ui:Button text="Connect New Wallet" display-tooltip-when-elided="true" name="ConnectNewWalletButton" />
</ui:VisualElement>
<ui:VisualElement name="ConfigureSessionContent" style="visibility: visible; display: flex; margin-top: 5px; margin-bottom: 5px;">
<ui:TextField picking-mode="Ignore" label="WalletConnect Bridge URL" value="filler text" text="https://bridge.walletconnect.org" name="BridgeUrlField" binding-path="account.sessionData.bridgeUrl" is-delayed="true" readonly="false" style="justify-content: flex-start;">
<ui:Button display-tooltip-when-elided="true" name="RandomizeButton" tooltip="Get a random, WalletConnect bridge url." />
</ui:TextField>
<uie:PropertyField name="DappMetaField" binding-path="account.sessionData.dappMeta" label="Dapp Information" />
<ui:VisualElement name="ButtonContent" style="align-items: center; margin-top: 5px; margin-bottom: 5px;">
<ui:Button text="Connect" display-tooltip-when-elided="true" name="StartSessionButton" />
</ui:VisualElement>
</ui:VisualElement>
<ui:VisualElement name="RequestingHandshakeContent" style="display: flex; margin-top: 5px; margin-bottom: 5px;">
<ui:VisualElement name="QrCodeContent" />
<ui:VisualElement name="HandshakeStatusContent" style="align-items: center; justify-content: center; flex-direction: column;">
<ui:Button text="Cancel" display-tooltip-when-elided="true" name="CancelHandshakeButton" style="max-width: 50%; flex-grow: 0;" />
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using UnityEditor;
using UnityEngine;

namespace AlgoSdk.WalletConnect.Editor
{
[CustomEditor(typeof(WalletConnectAccountAsset))]
public class WalletConnectAssetEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();

EditorGUILayout.Space();

if (GUILayout.Button("Connect Account")
&& AssetDatabase.TryGetGUIDAndLocalFileIdentifier<Object>(serializedObject.targetObject, out var assetGuid, out var _))
{
WalletConnectEditorWindow.Show(assetGuid);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 60c13af

Please sign in to comment.