Skip to content

Commit

Permalink
Merge pull request #158 from ChainSafe/feat/ipfs
Browse files Browse the repository at this point in the history
Add IPFS upload feature
  • Loading branch information
LuaxY authored Jun 16, 2022
2 parents be3f092 + f34ed4d commit aa975ae
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Web3Unity/Scripts/Library/IPFS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;

public class IPFS
{
private string _apiKey;
private static readonly string host = "https://api.chainsafe.io";

[System.Serializable]
private class Response<T>
{
public T response;

[System.Serializable]
public struct Error
{
public int code;

public string message;
// public Array<Object> details;
}

public Error error;
}

[System.Serializable]
public class GetFileInfoResponse
{
[System.Serializable]
public class Content
{
public string cid;
}

public Content content;
}

public IPFS(string apiKey)
{
_apiKey = apiKey;
}

public async Task<string> Upload(string bucketId, string path, string filename, byte[] content, string contentType)
{
var formUpload = new List<IMultipartFormSection>
{
new MultipartFormDataSection("path=" + path),
new MultipartFormFileSection("file", content, filename, contentType)
};

using var requestUpload = UnityWebRequest.Post(host + "/api/v1/bucket/" + bucketId + "/upload", formUpload);
requestUpload.SetRequestHeader("Authorization", "Bearer " + _apiKey);
await requestUpload.SendWebRequest();

if (requestUpload.result != UnityWebRequest.Result.Success)
{
throw new WebException(requestUpload.error);
}

// var jsonFile ="{\"path\": \""+path+"/"+filename+"\", \"source\": \""+bucketId+"\"}";
var jsonFile ="{\"path\": \""+filename+"\", \"source\": \""+bucketId+"\"}";

using var requestFile = new UnityWebRequest(host + "/api/v1/bucket/" + bucketId + "/file", "POST");
requestFile.SetRequestHeader("Authorization", "Bearer " + _apiKey);
requestFile.SetRequestHeader("Content-Type", "application/json");
requestFile.uploadHandler = new UploadHandlerRaw(new System.Text.UTF8Encoding().GetBytes(jsonFile));
requestFile.downloadHandler = new DownloadHandlerBuffer();
await requestFile.SendWebRequest();

if (requestFile.result != UnityWebRequest.Result.Success)
{
throw new WebException(requestFile.error);
}

var data = JsonUtility.FromJson<GetFileInfoResponse>(requestFile.downloadHandler.text);
return data.content.cid;
}
}
3 changes: 3 additions & 0 deletions Web3Unity/Scripts/Library/IPFS.cs.meta

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

3 changes: 3 additions & 0 deletions Web3Unity/Scripts/Prefabs/IPFS.meta

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

17 changes: 17 additions & 0 deletions Web3Unity/Scripts/Prefabs/IPFS/IPFSUploadExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IPFSUploadExample : MonoBehaviour
{
private const string apiKey = "YOUR_CHAINSAFE_STORE_API_KEY";

async void Start()
{
var data = System.Text.Encoding.UTF8.GetBytes("YOUR_DATA");

var ipfs = new IPFS(apiKey);
var cid = await ipfs.Upload("BUCKET_ID", "/PATH", "FILENAME.ext", data, "application/octet-stream");
print(cid);
}
}
11 changes: 11 additions & 0 deletions Web3Unity/Scripts/Prefabs/IPFS/IPFSUploadExample.cs.meta

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

57 changes: 57 additions & 0 deletions Web3Unity/Scripts/Prefabs/Minter/MintIPFS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections;
using System.Collections.Generic;
using Models;
using Newtonsoft.Json;
using UnityEngine;

public class MintIPFS : MonoBehaviour
{

// Start is called before the first frame update
public string chain = "ethereum";
public string network = "rinkeby"; // mainnet ropsten kovan rinkeby goerli
public string account = "0x7259E32e35cf880aEACfbD412E7F4Baa8606e04c";
public string to = "0x7259E32e35cf880aEACfbD412E7F4Baa8606e04c";

async public void MintButtonIPFS()
{
var data = System.Text.Encoding.UTF8.GetBytes("YOUR_DATA");

IPFS ipfs = new IPFS("YOUR_CHAINSAFE_STORE_API_KEY");
string cid = await ipfs.Upload("BUCKET_ID", "/PATH", "FILENAME.ext", data, "application/octet-stream");

Debug.Log("IPFS CID: " + cid);

CreateMintModel.Response nftResponse = await EVM.CreateMint(chain, network, account, to, cid);
if (nftResponse != null)
{
Debug.Log("CID: " + nftResponse.cid);
Debug.Log("Connection: " + nftResponse.connection);
Debug.Log("TC Account: " + nftResponse.tx.account);
Debug.Log("TX Data: " + nftResponse.tx.data);
Debug.Log("TX Value: " + nftResponse.tx.value);
Debug.Log("TX Gas Limit: " + nftResponse.tx.gasLimit);
Debug.Log("TX Gas Price: " + nftResponse.tx.gasPrice);
Debug.Log("Hashed Unsigned TX: " + nftResponse.hashedUnsignedTx);
string chainId = await EVM.ChainId(chain, network, "");
Debug.Log("Chain Id: " + chainId);
string gasPrice1 = await EVM.GasPrice(chain, network, "");
Debug.Log("Gas Price: " + gasPrice1);

// private key of account
string privateKey = "ADD_YOUR_PRIVATE_KEY";
Debug.Log("Account: " + account);
string transaction = await EVM.CreateTransaction(chain, network, nftResponse.tx.account,
nftResponse.tx.to, nftResponse.tx.value, nftResponse.tx.data,
nftResponse.tx.gasPrice, nftResponse.tx.gasLimit);
Debug.Log("Transaction: " + transaction);
string signature = Web3PrivateKey.SignTransaction(privateKey, transaction, chainId);
print("Signature: " + signature);
//string rpc = "";
string responseBroadcast = await EVM.BroadcastTransaction(chain, network, nftResponse.tx.account,
nftResponse.tx.to, nftResponse.tx.value, nftResponse.tx.data, signature,
nftResponse.tx.gasPrice, nftResponse.tx.gasLimit, "");
print("Response: " + responseBroadcast);
}
}
}
3 changes: 3 additions & 0 deletions Web3Unity/Scripts/Prefabs/Minter/MintIPFS.cs.meta

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

0 comments on commit aa975ae

Please sign in to comment.