//tips
//unityにおけるブロックチェーン決済の流れ確認
イーサリアム通貨の送金デモ構成のスクリプトの一つであるEtherTransferCoroutinesUnityWebRequest に出てきた、下記の名前空間の引用箇所を把握していく。
using Nethereum.JsonRpc.UnityClient;
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Util;
まずは下記部分の確認。
var ethTransfer = new EthTransferUnityRequest(Url, PrivateKey);
EthTransferUnityRequestを自身のアセットフォルダで検索しても出てこないので検索すると、githubにて下記のフォルダ内にあり、内容としては Nethereum.JsonRpc.UnityClientの名前空間の中に記述されていることがわかった。
Nethereum/src/Nethereum.Unity/EthTransferUnityRequest.cs
Unityの中で再度検索してみるとNethereum.JsonRpc.・・・のpluginsが確認でき、.dllでアセット内では表示されており、内容は見れない。
Dllとはダイナミックリンクライブラリと呼ばれる装備型のプログラムの部品でプログラムを動かすときに使用されるプログラムの部品ファイルとなる。
独自のコードを開発するときに、Unity でサポートされていないコンパイラーを使用したい場合は、コードを DLL にコンパイルし Unity プロジェクトに加えるよう。
Githubのコードから確認すると、下記のようになっており、
public EthTransferUnityRequest(string url, string privateKey, BigInteger? chainId)
{
_transactionSignedUnityRequest = new TransactionSignedUnityRequest(url, privateKey, chainId);
}
transactionSignedUnityRequestを名前空間Nethereum.JsonRpc.UnityClientでうまく加工するために使用されており、TransferEther(string toAddress, decimal etherAmount, decimal? gasPriceGwei = null, BigInteger? gas = null)の返り値や条件に使われている。
namespace Nethereum.JsonRpc.UnityClient
{
public class EthTransferUnityRequest : UnityRequest<string>
{
private TransactionSignedUnityRequest _transactionSignedUnityRequest;
public EthTransferUnityRequest(string url, string privateKey, BigInteger? chainId)
{
_transactionSignedUnityRequest = new TransactionSignedUnityRequest(url, privateKey, chainId);
}
public IEnumerator TransferEther(string toAddress, decimal etherAmount, decimal? gasPriceGwei = null, BigInteger? gas = null)
{
var transactionInput =
EtherTransferTransactionInputBuilder.CreateTransactionInput(null, toAddress, etherAmount,
gasPriceGwei, gas);
yield return _transactionSignedUnityRequest.SignAndSendTransaction(transactionInput);
if (_transactionSignedUnityRequest.Exception == null)
{
this.Result = _transactionSignedUnityRequest.Result;
}
else
{
this.Exception = _transactionSignedUnityRequest.Exception;
}
}
}
この引数を持ち変数を
var transactionInput =
EtherTransferTransactionInputBuilder.CreateTransactionInput(null, toAddress, etherAmount,
gasPriceGwei, gas);
yield return _transactionSignedUnityRequest.SignAndSendTransaction(transactionInput);
の部分でうまく活用しているはずなので、EtherTransferTransactionInputBuilder.CreateTransactionInputの中身も見ていく。
yield returnの後に処理を追加することで処理が終わるまでコルーチンを中断できるので便利。
これは名前空間Nethereum.RPC.TransactionManagersの中に格納されており、ここでは送金料などがシンプルに16進数への変換が行われている。
namespace Nethereum.RPC.TransactionManagers
{
public class EtherTransferTransactionInputBuilder
{
public static TransactionInput CreateTransactionInput(string fromAddress, string toAddress, decimal etherAmount, decimal? gasPriceGwei = null, BigInteger? gas = null, BigInteger? nonce = null)
{
if (string.IsNullOrEmpty(toAddress)) throw new ArgumentNullException(nameof(toAddress));
if (etherAmount <= 0) throw new ArgumentOutOfRangeException(nameof(etherAmount));
if (gasPriceGwei <= 0) throw new ArgumentOutOfRangeException(nameof(gasPriceGwei));
var transactionInput = new TransactionInput()
{
To = toAddress,
From = fromAddress,
GasPrice = gasPriceGwei == null ? null : new HexBigInteger(UnitConversion.Convert.ToWei(gasPriceGwei.Value, UnitConversion.EthUnit.Gwei)),
Value = new HexBigInteger(UnitConversion.Convert.ToWei(etherAmount)),
Gas = gas == null ? null : new HexBigInteger(gas.Value),
Nonce = nonce == null ? null : new HexBigInteger(nonce.Value)
};
return transactionInput;
}
}
次に16進数に変換されたものを
_transactionSignedUnityRequest.SignAndSendTransaction(transactionInput);
でハッシュ化するかと思うのでこちらも確認。ただ、こちらはhex変換のみでshaのハッシュ化につながるものが見つからない。この段階では16進数で各トランザクション情報が格納されたオブジェクトでしかない。
この段階ではハッシュ化しないと考え次のオブジェクト TransactionReceiptPollingRequestを確認する。
これは通信でトランザクションに追加を行うというもの。
リクエスト自体は下記のようにweb3ClientVersionというRPC対応オブジェクトを作成し、json形式で行うよう。この部分を突っ込んで理解すればインターネット経由でのブロックチェーン接続またはインターネット上の異なるネットワーク接続を再現できるかもしれない。json通信をする上での設定を行なっている。
public class Web3ClientVersionUnityRequest:UnityRpcClient<System.String>
{
private readonly Nethereum.RPC.Web3.Web3ClientVersion _web3ClientVersion;
public Web3ClientVersionUnityRequest(string url, JsonSerializerSettings jsonSerializerSettings = null):base(url, jsonSerializerSettings)
{
_web3ClientVersion = new Nethereum.RPC.Web3.Web3ClientVersion(null);
}
public IEnumerator SendRequest()
{
var request = _web3ClientVersion.BuildRequest();
yield return SendRequest(request);
ちなみに、三項演算子?マークは、左辺を評価して真であれば、?の右辺を実行し,偽であれば:の右辺を実行。
最後にEthGetBalanceUnityRequestオブジェクトの中身を確認。これはnamespace Nethereum.JsonRpc.UnityClientの中に記述されている。