Skip to content

Commit c95d9db

Browse files
authored
Hyperplay Integration (#88)
1 parent 301cf43 commit c95d9db

20 files changed

+857
-14
lines changed

Assets/Thirdweb/Core/Scripts/Hyperplay.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Threading.Tasks;
2+
using Nethereum.JsonRpc.Client.RpcMessages;
3+
using UnityEngine;
4+
using UnityEngine.Networking;
5+
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Linq;
7+
8+
namespace Thirdweb.Hyperplay
9+
{
10+
public class Hyperplay
11+
{
12+
public string[] Accounts { get; private set; }
13+
public string ChainId { get; private set; }
14+
15+
public Hyperplay(string chainId)
16+
{
17+
ChainId = chainId;
18+
Accounts = null;
19+
}
20+
21+
internal async Task Initialize()
22+
{
23+
Accounts = (await Request(new RpcRequestMessage(-1, "eth_accounts"))).GetResult<string[]>();
24+
}
25+
26+
internal async Task<RpcResponseMessage> Request(RpcRequestMessage message)
27+
{
28+
HyperplayRequest hyperplayRequest = new HyperplayRequest() { Method = message.Method, Params = message.RawParameters };
29+
string jsonString = JsonConvert.SerializeObject(hyperplayRequest);
30+
byte[] jsonBytes = System.Text.Encoding.UTF8.GetBytes(jsonString);
31+
using (var request = new UnityWebRequest("localhost:9680/rpcRaw", "POST"))
32+
{
33+
request.uploadHandler = new UploadHandlerRaw(jsonBytes);
34+
request.downloadHandler = new DownloadHandlerBuffer();
35+
request.SetRequestHeader("Content-Type", "application/json");
36+
await request.SendWebRequest();
37+
if (request.result != UnityWebRequest.Result.Success)
38+
{
39+
Debug.LogError(request.error);
40+
throw new UnityException("RPC request failed: " + request.error);
41+
}
42+
var hyperplayResult = JsonConvert.DeserializeObject<HyperplayResult>(request.downloadHandler.text);
43+
try
44+
{
45+
return new RpcResponseMessage(message.Id, JsonConvert.DeserializeObject<JToken>(hyperplayResult.Result.ToString()));
46+
}
47+
catch
48+
{
49+
return new RpcResponseMessage(message.Id, hyperplayResult.Result.ToString());
50+
}
51+
}
52+
}
53+
}
54+
55+
[System.Serializable]
56+
public struct HyperplayRequest
57+
{
58+
[JsonProperty("method")]
59+
public string Method { get; set; }
60+
61+
[JsonProperty("params")]
62+
public object Params { get; set; }
63+
}
64+
65+
[System.Serializable]
66+
public struct HyperplayResult
67+
{
68+
[JsonProperty("result")]
69+
public object Result { get; set; }
70+
71+
[JsonProperty("id")]
72+
public long Id { get; set; }
73+
74+
[JsonProperty("jsonrpc")]
75+
public string JsonRpc { get; set; }
76+
}
77+
}

Assets/Thirdweb/Core/Scripts/Hyperplay/Hyperplay.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Thirdweb/Core/Scripts/Hyperplay/Nethereum.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Nethereum.JsonRpc.Client;
2+
using Nethereum.RPC.Accounts;
3+
using Nethereum.RPC.AccountSigning;
4+
using Nethereum.RPC.NonceServices;
5+
using Nethereum.RPC.TransactionManagers;
6+
7+
namespace Thirdweb.Hyperplay
8+
{
9+
public class HyperplayAccount : IAccount
10+
{
11+
private readonly Hyperplay _wallet;
12+
private readonly IClient _client;
13+
14+
public string Address
15+
{
16+
get { return _wallet.Accounts[0]; }
17+
}
18+
19+
public ITransactionManager TransactionManager { get; }
20+
public INonceService NonceService { get; set; }
21+
public IAccountSigningService AccountSigningService { get; }
22+
23+
public IClient Client
24+
{
25+
get { return _client; }
26+
}
27+
28+
public HyperplayAccount(Hyperplay wallet, IClient client)
29+
{
30+
_wallet = wallet;
31+
_client = client;
32+
TransactionManager = new HyperplayTransactionManager(this);
33+
NonceService = new InMemoryNonceService(Address, client);
34+
AccountSigningService = new AccountSigningService(client);
35+
}
36+
}
37+
}

Assets/Thirdweb/Core/Scripts/Hyperplay/Nethereum/HyperplayAccount.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Nethereum.JsonRpc.Client;
5+
using Nethereum.JsonRpc.Client.RpcMessages;
6+
7+
namespace Thirdweb.Hyperplay
8+
{
9+
public class HyperplayClient : ClientBase
10+
{
11+
private Hyperplay _hyperplay;
12+
13+
public HyperplayClient(Hyperplay Hyperplay)
14+
{
15+
this._hyperplay = Hyperplay;
16+
}
17+
18+
private static readonly Random rng = new Random();
19+
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
20+
21+
public static long GenerateRpcId()
22+
{
23+
var date = (long)((DateTime.UtcNow - UnixEpoch).TotalMilliseconds) * (10L * 10L * 10L);
24+
var extra = (long)Math.Floor(rng.NextDouble() * (10.0 * 10.0 * 10.0));
25+
return date + extra;
26+
}
27+
28+
protected override async Task<RpcResponseMessage> SendAsync(RpcRequestMessage message, string route = null)
29+
{
30+
message.Id = GenerateRpcId();
31+
return await _hyperplay.Request(message);
32+
}
33+
34+
protected override Task<RpcResponseMessage[]> SendAsync(RpcRequestMessage[] requests)
35+
{
36+
return Task.WhenAll(requests.Select(r => SendAsync(r)));
37+
}
38+
}
39+
}

Assets/Thirdweb/Core/Scripts/Hyperplay/Nethereum/HyperplayClient.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Nethereum.Web3;
2+
3+
namespace Thirdweb.Hyperplay
4+
{
5+
public static class HyperplayNEthereumExtensions
6+
{
7+
public static Web3 CreateWeb3(this Hyperplay Hyperplay)
8+
{
9+
var client = new HyperplayClient(Hyperplay);
10+
var account = new HyperplayAccount(Hyperplay, client);
11+
return new Web3(account, client);
12+
}
13+
}
14+
}

Assets/Thirdweb/Core/Scripts/Hyperplay/Nethereum/HyperplayNEthereumExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Thirdweb.Hyperplay
2+
{
3+
public class HyperplayTransactionManager : Nethereum.RPC.TransactionManagers.TransactionManager
4+
{
5+
public HyperplayTransactionManager(HyperplayAccount account)
6+
: base(account.Client)
7+
{
8+
Account = account;
9+
}
10+
}
11+
}

Assets/Thirdweb/Core/Scripts/Hyperplay/Nethereum/HyperplayTransactionManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Thirdweb/Core/Scripts/ThirdwebSession.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public async Task<string> Connect(WalletConnection walletConnection)
7676
throw new UnityException("Smart wallet config is required for smart wallet connection method!");
7777
ActiveWallet = new ThirdwebSmartWallet(ActiveWallet, Options.smartWalletConfig.Value);
7878
break;
79+
case WalletProvider.Hyperplay:
80+
ActiveWallet = new ThirdwebHyperplay(ChainId.ToString());
81+
break;
7982
default:
8083
throw new UnityException("This wallet connection method is not supported on this platform!");
8184
}

Assets/Thirdweb/Core/Scripts/Wallet.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ public enum WalletProvider
531531
MagicLink,
532532
LocalWallet,
533533
SmartWallet,
534-
Paper
534+
Paper,
535+
Hyperplay
535536
}
536537
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Threading.Tasks;
2+
using Nethereum.Web3;
3+
using Nethereum.Web3.Accounts;
4+
using Thirdweb.Hyperplay;
5+
6+
namespace Thirdweb.Wallets
7+
{
8+
public class ThirdwebHyperplay : IThirdwebWallet
9+
{
10+
private Web3 _web3;
11+
private WalletProvider _provider;
12+
private WalletProvider _signerProvider;
13+
private Hyperplay.Hyperplay _hyperPlay;
14+
15+
public ThirdwebHyperplay(string chainId)
16+
{
17+
_web3 = null;
18+
_provider = WalletProvider.Hyperplay;
19+
_signerProvider = WalletProvider.Hyperplay;
20+
_hyperPlay = new Hyperplay.Hyperplay(chainId);
21+
}
22+
23+
public async Task<string> Connect(WalletConnection walletConnection, string rpc)
24+
{
25+
await _hyperPlay.Initialize();
26+
_web3 = _hyperPlay.CreateWeb3();
27+
return _hyperPlay.Accounts[0];
28+
}
29+
30+
public Task Disconnect()
31+
{
32+
_web3 = null;
33+
return Task.CompletedTask;
34+
}
35+
36+
public Account GetLocalAccount()
37+
{
38+
return null;
39+
}
40+
41+
public Task<string> GetAddress()
42+
{
43+
var addy = _hyperPlay.Accounts[0];
44+
if (addy != null)
45+
addy = addy.ToChecksumAddress();
46+
return Task.FromResult(addy);
47+
}
48+
49+
public async Task<string> GetSignerAddress()
50+
{
51+
return await GetAddress();
52+
}
53+
54+
public WalletProvider GetProvider()
55+
{
56+
return _provider;
57+
}
58+
59+
public WalletProvider GetSignerProvider()
60+
{
61+
return _signerProvider;
62+
}
63+
64+
public Task<Web3> GetWeb3()
65+
{
66+
return Task.FromResult(_web3);
67+
}
68+
69+
public async Task<Web3> GetSignerWeb3()
70+
{
71+
return await GetWeb3();
72+
}
73+
74+
public Task<bool> IsConnected()
75+
{
76+
return Task.FromResult(_web3 != null);
77+
}
78+
}
79+
}

Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebHyperplay.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)