Skip to content

Commit

Permalink
Implemented Onchain and IPFS uploaders;
Browse files Browse the repository at this point in the history
  • Loading branch information
k-karuna committed Jul 11, 2023
1 parent 3ddf4f3 commit 64cfaa1
Show file tree
Hide file tree
Showing 41 changed files with 499 additions and 232 deletions.
File renamed without changes.
35 changes: 35 additions & 0 deletions Runtime/Scripts/FileUploaders/BaseUploader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections;
using TezosSDK.Scripts.FileUploaders.IPFS;
using UnityEngine;

namespace TezosSDK.Scripts.FileUploaders
{
public abstract class BaseUploader : MonoBehaviour
{
public string SupportedFileExtensions { get; } = ".jpg, .jpeg, .png";

private void Start()
{
DontDestroyOnLoad(gameObject);
}
}

public interface IPinataUploader : IBaseUploader
{
PinataCredentials PinataCredentials { get; set; }
}

public interface IBaseUploader
{
string SupportedFileExtensions { get; }

/// <summary>
/// Upload file that user will select through native menu file picker.
/// </summary>
/// <param name="callback">
/// Executes after asset uploaded with data address.
/// </param>
IEnumerator UploadFile(Action<string> callback);
}
}
3 changes: 3 additions & 0 deletions Runtime/Scripts/FileUploaders/IPFS.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
Expand Up @@ -3,15 +3,18 @@
using System.Collections;
using System.IO;
using System.Text.Json;
using TezosSDK.Scripts.IpfsUploader;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
using Logger = TezosSDK.Helpers.Logger;

namespace TezosSDK.Scripts.IpfsUploader
namespace TezosSDK.Scripts.FileUploaders.IPFS
{
public class EditorUploader : BaseUploader, IFileUploader
public class EditorPinataUploader : BaseUploader, IPinataUploader
{
public PinataCredentials PinataCredentials { get; set; }

public IEnumerator UploadFile(Action<string> callback)
{
yield return null;
Expand All @@ -28,8 +31,8 @@ public IEnumerator UploadFile(Action<string> callback)
var form = new WWWForm();
form.AddBinaryData("file", File.ReadAllBytes(path), filename);

var request = UnityWebRequest.Post(ApiUrl, form);
request.SetRequestHeader("Authorization", $"Bearer {ApiKey}");
var request = UnityWebRequest.Post(PinataCredentials.ApiUrl, form);
request.SetRequestHeader("Authorization", $"Bearer {PinataCredentials.ApiKey}");
yield return request.SendWebRequest();

if (request.result == UnityWebRequest.Result.Success)
Expand All @@ -44,4 +47,4 @@ public IEnumerator UploadFile(Action<string> callback)
}
}
}
#endif
#endif
14 changes: 14 additions & 0 deletions Runtime/Scripts/FileUploaders/IPFS/PinataCredentials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace TezosSDK.Scripts.FileUploaders.IPFS
{
public class PinataCredentials
{
public string ApiUrl { get; }
public string ApiKey { get; }

public PinataCredentials(string apiKey)
{
ApiUrl = "https://api.pinata.cloud/pinning/pinFileToIPFS";
ApiKey = apiKey;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Scripts/FileUploaders/IPFS/PinataCredentials.cs.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
Expand Up @@ -2,44 +2,49 @@
using System.Collections;
using System.Runtime.InteropServices;
using System.Text.Json;
using TezosSDK.Scripts.IpfsUploader;
using UnityEngine;

namespace TezosSDK.Scripts.IpfsUploader
namespace TezosSDK.Scripts.FileUploaders.IPFS
{
public class WebUploader : BaseUploader, IFileUploader
public class WebPinataUploader : BaseUploader, IPinataUploader
{
public PinataCredentials PinataCredentials { get; set; }

public void FileRequestCallback(string path)
{
WebUploaderHelper.SetResult(path);
WebPinataUploaderHelper.SetResult(path);
}

public IEnumerator UploadFile(Action<string> callback)
{
yield return null;
WebUploaderHelper.RequestFile(callback, SupportedFileExtensions);
WebPinataUploaderHelper.RequestFile(callback, SupportedFileExtensions);
}
}
public static class WebUploaderHelper

public static class WebPinataUploaderHelper
{
private static Action<string> _responseCallback;

public static WebUploader InitWebFileLoader()
public static IPinataUploader GetUploader(string apiKey)
{
const string callbackObjectName = nameof(WebUploader);
const string callbackMethodName = nameof(WebUploader.FileRequestCallback);
const string callbackObjectName = nameof(WebPinataUploader);
const string callbackMethodName = nameof(WebPinataUploader.FileRequestCallback);

var webUploaderGameObject = GameObject.Find(nameof(WebUploader));
var webUploaderGameObject = GameObject.Find(nameof(WebPinataUploader));
var webFileUploader = webUploaderGameObject != null
? webUploaderGameObject.GetComponent<WebUploader>()
: new GameObject(nameof(WebUploader)).AddComponent<WebUploader>();
? webUploaderGameObject.GetComponent<WebPinataUploader>()
: new GameObject(nameof(WebPinataUploader)).AddComponent<WebPinataUploader>();

JsInitFileLoader(
webFileUploader.PinataCredentials = new PinataCredentials(apiKey);

JsInitPinataUploader(
callbackObjectName,
callbackMethodName,
webFileUploader.ApiUrl,
webFileUploader.ApiKey);
webFileUploader.PinataCredentials.ApiUrl,
webFileUploader.PinataCredentials.ApiKey);

return webFileUploader;
}

Expand All @@ -48,7 +53,7 @@ public static void RequestFile(Action<string> callback, string extensions)
JsRequestUserFile(extensions);
_responseCallback = callback;
}

public static void SetResult(string response)
{
var ipfsResponse = JsonSerializer.Deserialize<IpfsResponse>(response);
Expand All @@ -62,7 +67,7 @@ private static void Dispose()
}

[DllImport("__Internal")]
private static extern void JsInitFileLoader(
private static extern void JsInitPinataUploader(
string objectName,
string methodName,
string apiUrl,
Expand Down
29 changes: 29 additions & 0 deletions Runtime/Scripts/FileUploaders/JSFileUploader.jslib
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mergeInto(LibraryManager.library, {
JsInitPinataUploader: function (
callbackObjectName,
callbackMethodName,
apiUrl,
apiKey
) {
InitIpfsUploader({
CallbackObjectName: UTF8ToString(callbackObjectName),
CallbackMethodName: UTF8ToString(callbackMethodName),
ApiUrl: UTF8ToString(apiUrl),
ApiKey: UTF8ToString(apiKey),
});
},

JsInitBase64Uploader: function (
callbackObjectName,
callbackMethodName
) {
InitBase64Uploader({
CallbackObjectName: UTF8ToString(callbackObjectName),
CallbackMethodName: UTF8ToString(callbackMethodName)
});
},

JsRequestUserFile: function (extensions) {
FileUploader.RequestUserFile(UTF8ToString(extensions));
},
});
3 changes: 3 additions & 0 deletions Runtime/Scripts/FileUploaders/OnChain.meta

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

38 changes: 38 additions & 0 deletions Runtime/Scripts/FileUploaders/OnChain/EditorBase64Uploader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#if UNITY_EDITOR
using System;
using System.Collections;
using System.IO;
using UnityEditor;


namespace TezosSDK.Scripts.FileUploaders.OnChain
{
public class EditorBase64Uploader : BaseUploader, IBaseUploader
{
public IEnumerator UploadFile(Action<string> callback)
{
yield return null;
var imagePath = EditorUtility.OpenFilePanel(
"Select image",
string.Empty,
SupportedFileExtensions
.Replace(".", string.Empty)
.Replace(" ", string.Empty)
);
callback.Invoke(ConvertImageToBase64(imagePath));
}

private static string ConvertImageToBase64(string imagePath)
{
var fileExtension = Path
.GetExtension(imagePath)
.Replace(".", string.Empty)
.ToLower();

var imageBytes = File.ReadAllBytes(imagePath);
var base64String = Convert.ToBase64String(imageBytes);
return $"data:image/{fileExtension};base64,{base64String}";
}
}
}
#endif

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

69 changes: 69 additions & 0 deletions Runtime/Scripts/FileUploaders/OnChain/WebBase64Uploader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections;
using System.Runtime.InteropServices;
using Logger = TezosSDK.Helpers.Logger;
using UnityEngine;

namespace TezosSDK.Scripts.FileUploaders.OnChain
{
public class WebBase64Uploader : BaseUploader, IBaseUploader
{
public void FileRequestCallback(string path)
{
WebBase64UploaderHelper.SetResult(path);
}

public IEnumerator UploadFile(Action<string> callback)
{
yield return null;
WebBase64UploaderHelper.RequestFile(callback, SupportedFileExtensions);
}
}

public static class WebBase64UploaderHelper
{
private static Action<string> _responseCallback;

public static IBaseUploader GetUploader()
{
const string callbackObjectName = nameof(WebBase64Uploader);
const string callbackMethodName = nameof(WebBase64Uploader.FileRequestCallback);

var webUploaderGameObject = GameObject.Find(nameof(WebBase64Uploader));
var uploader = webUploaderGameObject != null
? webUploaderGameObject.GetComponent<WebBase64Uploader>()
: new GameObject(nameof(WebBase64Uploader)).AddComponent<WebBase64Uploader>();

JsInitBase64Uploader(
callbackObjectName,
callbackMethodName);

return uploader;
}

public static void RequestFile(Action<string> callback, string extensions)
{
JsRequestUserFile(extensions);
_responseCallback = callback;
}

public static void SetResult(string response)
{
_responseCallback.Invoke(response);
Dispose();
}

private static void Dispose()
{
_responseCallback = null;
}

[DllImport("__Internal")]
private static extern void JsInitBase64Uploader(
string objectName,
string methodName);

[DllImport("__Internal")]
private static extern void JsRequestUserFile(string extensions);
}
}

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

44 changes: 44 additions & 0 deletions Runtime/Scripts/FileUploaders/UploaderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using TezosSDK.Scripts.FileUploaders.IPFS;
using TezosSDK.Scripts.FileUploaders.OnChain;
using UnityEngine;

namespace TezosSDK.Scripts.FileUploaders
{
public static class UploaderFactory
{
/// <summary>
/// Cross-platform image uploader to IPFS network via Pinata service.
/// </summary>
/// <param name="apiKey">API key from https://app.pinata.cloud/developers/api-keys</param>
public static IBaseUploader GetPinataUploader(string apiKey)
{
IPinataUploader uploader = null;

#if UNITY_WEBGL && !UNITY_EDITOR
uploader = WebPinataUploaderHelper.GetUploader(apiKey);
#elif UNITY_EDITOR
var editorUploaderGameObject = GameObject.Find(nameof(EditorPinataUploader));
uploader = editorUploaderGameObject != null
? editorUploaderGameObject.GetComponent<EditorPinataUploader>()
: new GameObject(nameof(EditorPinataUploader)).AddComponent<EditorPinataUploader>();
uploader.PinataCredentials = new PinataCredentials(apiKey);
#endif
return uploader;
}

public static IBaseUploader GetOnchainUploader()
{
IBaseUploader uploader = null;

#if UNITY_WEBGL && !UNITY_EDITOR
uploader = WebBase64UploaderHelper.GetUploader();
#elif UNITY_EDITOR
var editorUploaderGameObject = GameObject.Find(nameof(EditorBase64Uploader));
uploader = editorUploaderGameObject != null
? editorUploaderGameObject.GetComponent<EditorBase64Uploader>()
: new GameObject(nameof(EditorBase64Uploader)).AddComponent<EditorBase64Uploader>();
#endif
return uploader;
}
}
}
Loading

0 comments on commit 64cfaa1

Please sign in to comment.