-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from ChainSafe/feat/ipfs
Add IPFS upload feature
- Loading branch information
Showing
7 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.