Generated.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Numerics;
  6. using System.Threading.Tasks;
  7. using Solana.Unity;
  8. using Solana.Unity.Programs.Abstract;
  9. using Solana.Unity.Programs.Utilities;
  10. using Solana.Unity.Rpc;
  11. using Solana.Unity.Rpc.Builders;
  12. using Solana.Unity.Rpc.Core.Http;
  13. using Solana.Unity.Rpc.Core.Sockets;
  14. using Solana.Unity.Rpc.Types;
  15. using Solana.Unity.Wallet;
  16. using GplSession;
  17. using GplSession.Program;
  18. using GplSession.Errors;
  19. using GplSession.Accounts;
  20. using GplSession.Types;
  21. namespace GplSession
  22. {
  23. namespace Accounts
  24. {
  25. public partial class SessionToken
  26. {
  27. public static ulong ACCOUNT_DISCRIMINATOR => 1081168673100727529UL;
  28. public static ReadOnlySpan<byte> ACCOUNT_DISCRIMINATOR_BYTES => new byte[]{233, 4, 115, 14, 46, 21, 1, 15};
  29. public static string ACCOUNT_DISCRIMINATOR_B58 => "fyZWTdUu1pS";
  30. public PublicKey Authority { get; set; }
  31. public PublicKey TargetProgram { get; set; }
  32. public PublicKey SessionSigner { get; set; }
  33. public long ValidUntil { get; set; }
  34. public static SessionToken Deserialize(ReadOnlySpan<byte> _data)
  35. {
  36. int offset = 0;
  37. ulong accountHashValue = _data.GetU64(offset);
  38. offset += 8;
  39. if (accountHashValue != ACCOUNT_DISCRIMINATOR)
  40. {
  41. return null;
  42. }
  43. SessionToken result = new SessionToken();
  44. result.Authority = _data.GetPubKey(offset);
  45. offset += 32;
  46. result.TargetProgram = _data.GetPubKey(offset);
  47. offset += 32;
  48. result.SessionSigner = _data.GetPubKey(offset);
  49. offset += 32;
  50. result.ValidUntil = _data.GetS64(offset);
  51. offset += 8;
  52. return result;
  53. }
  54. }
  55. }
  56. namespace Errors
  57. {
  58. public enum GplSessionErrorKind : uint
  59. {
  60. ValidityTooLong = 6000U,
  61. InvalidToken = 6001U,
  62. NoToken = 6002U
  63. }
  64. }
  65. namespace Types
  66. {
  67. }
  68. public partial class GplSessionClient : TransactionalBaseClient<GplSessionErrorKind>
  69. {
  70. public GplSessionClient(IRpcClient rpcClient, IStreamingRpcClient streamingRpcClient, PublicKey programId = null) : base(rpcClient, streamingRpcClient, programId ?? new PublicKey(GplSessionProgram.ID))
  71. {
  72. }
  73. public async Task<Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<SessionToken>>> GetSessionTokensAsync(string programAddress = GplSessionProgram.ID, Commitment commitment = Commitment.Confirmed)
  74. {
  75. var list = new List<Solana.Unity.Rpc.Models.MemCmp>{new Solana.Unity.Rpc.Models.MemCmp{Bytes = SessionToken.ACCOUNT_DISCRIMINATOR_B58, Offset = 0}};
  76. var res = await RpcClient.GetProgramAccountsAsync(programAddress, commitment, memCmpList: list);
  77. if (!res.WasSuccessful || !(res.Result?.Count > 0))
  78. return new Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<SessionToken>>(res);
  79. List<SessionToken> resultingAccounts = new List<SessionToken>(res.Result.Count);
  80. resultingAccounts.AddRange(res.Result.Select(result => SessionToken.Deserialize(Convert.FromBase64String(result.Account.Data[0]))));
  81. return new Solana.Unity.Programs.Models.ProgramAccountsResultWrapper<List<SessionToken>>(res, resultingAccounts);
  82. }
  83. public async Task<Solana.Unity.Programs.Models.AccountResultWrapper<SessionToken>> GetSessionTokenAsync(string accountAddress, Commitment commitment = Commitment.Finalized)
  84. {
  85. var res = await RpcClient.GetAccountInfoAsync(accountAddress, commitment);
  86. if (!res.WasSuccessful)
  87. return new Solana.Unity.Programs.Models.AccountResultWrapper<SessionToken>(res);
  88. var resultingAccount = SessionToken.Deserialize(Convert.FromBase64String(res.Result.Value.Data[0]));
  89. return new Solana.Unity.Programs.Models.AccountResultWrapper<SessionToken>(res, resultingAccount);
  90. }
  91. public async Task<SubscriptionState> SubscribeSessionTokenAsync(string accountAddress, Action<SubscriptionState, Solana.Unity.Rpc.Messages.ResponseValue<Solana.Unity.Rpc.Models.AccountInfo>, SessionToken> callback, Commitment commitment = Commitment.Finalized)
  92. {
  93. SubscriptionState res = await StreamingRpcClient.SubscribeAccountInfoAsync(accountAddress, (s, e) =>
  94. {
  95. SessionToken parsingResult = null;
  96. if (e.Value?.Data?.Count > 0)
  97. parsingResult = SessionToken.Deserialize(Convert.FromBase64String(e.Value.Data[0]));
  98. callback(s, e, parsingResult);
  99. }, commitment);
  100. return res;
  101. }
  102. protected override Dictionary<uint, ProgramError<GplSessionErrorKind>> BuildErrorsDictionary()
  103. {
  104. return new Dictionary<uint, ProgramError<GplSessionErrorKind>>{{6000U, new ProgramError<GplSessionErrorKind>(GplSessionErrorKind.ValidityTooLong, "Requested validity is too long")}, {6001U, new ProgramError<GplSessionErrorKind>(GplSessionErrorKind.InvalidToken, "Invalid session token")}, {6002U, new ProgramError<GplSessionErrorKind>(GplSessionErrorKind.NoToken, "No session token provided")}, };
  105. }
  106. }
  107. namespace Program
  108. {
  109. public class CreateSessionAccounts
  110. {
  111. public PublicKey SessionToken { get; set; }
  112. public PublicKey SessionSigner { get; set; }
  113. public PublicKey Authority { get; set; }
  114. public PublicKey TargetProgram { get; set; }
  115. public PublicKey SystemProgram { get; set; } = new PublicKey("11111111111111111111111111111111");
  116. }
  117. public class RevokeSessionAccounts
  118. {
  119. public PublicKey SessionToken { get; set; }
  120. public PublicKey Authority { get; set; }
  121. public PublicKey SystemProgram { get; set; } = new PublicKey("11111111111111111111111111111111");
  122. }
  123. public static class GplSessionProgram
  124. {
  125. public const string ID = "KeyspM2ssCJbqUhQ4k7sveSiY4WjnYsrXkC8oDbwde5";
  126. public static Solana.Unity.Rpc.Models.TransactionInstruction CreateSession(CreateSessionAccounts accounts, bool? top_up, long? valid_until, ulong? lamports, PublicKey programId = null)
  127. {
  128. programId ??= new(ID);
  129. List<Solana.Unity.Rpc.Models.AccountMeta> keys = new()
  130. {Solana.Unity.Rpc.Models.AccountMeta.Writable(accounts.SessionToken, false), Solana.Unity.Rpc.Models.AccountMeta.Writable(accounts.SessionSigner, true), Solana.Unity.Rpc.Models.AccountMeta.Writable(accounts.Authority, true), Solana.Unity.Rpc.Models.AccountMeta.ReadOnly(accounts.TargetProgram, false), Solana.Unity.Rpc.Models.AccountMeta.ReadOnly(accounts.SystemProgram, false)};
  131. byte[] _data = new byte[1200];
  132. int offset = 0;
  133. _data.WriteU64(16391441928816673266UL, offset);
  134. offset += 8;
  135. if (top_up != null)
  136. {
  137. _data.WriteU8(1, offset);
  138. offset += 1;
  139. _data.WriteBool(top_up.Value, offset);
  140. offset += 1;
  141. }
  142. else
  143. {
  144. _data.WriteU8(0, offset);
  145. offset += 1;
  146. }
  147. if (valid_until != null)
  148. {
  149. _data.WriteU8(1, offset);
  150. offset += 1;
  151. _data.WriteS64(valid_until.Value, offset);
  152. offset += 8;
  153. }
  154. else
  155. {
  156. _data.WriteU8(0, offset);
  157. offset += 1;
  158. }
  159. if (lamports != null)
  160. {
  161. _data.WriteU8(1, offset);
  162. offset += 1;
  163. _data.WriteU64(lamports.Value, offset);
  164. offset += 8;
  165. }
  166. else
  167. {
  168. _data.WriteU8(0, offset);
  169. offset += 1;
  170. }
  171. byte[] resultData = new byte[offset];
  172. Array.Copy(_data, resultData, offset);
  173. return new Solana.Unity.Rpc.Models.TransactionInstruction{Keys = keys, ProgramId = programId.KeyBytes, Data = resultData};
  174. }
  175. public static Solana.Unity.Rpc.Models.TransactionInstruction RevokeSession(RevokeSessionAccounts accounts, PublicKey programId = null)
  176. {
  177. programId ??= new(ID);
  178. List<Solana.Unity.Rpc.Models.AccountMeta> keys = new()
  179. {Solana.Unity.Rpc.Models.AccountMeta.Writable(accounts.SessionToken, false), Solana.Unity.Rpc.Models.AccountMeta.Writable(accounts.Authority, false), Solana.Unity.Rpc.Models.AccountMeta.ReadOnly(accounts.SystemProgram, false)};
  180. byte[] _data = new byte[1200];
  181. int offset = 0;
  182. _data.WriteU64(13981146387719806038UL, offset);
  183. offset += 8;
  184. byte[] resultData = new byte[offset];
  185. Array.Copy(_data, resultData, offset);
  186. return new Solana.Unity.Rpc.Models.TransactionInstruction{Keys = keys, ProgramId = programId.KeyBytes, Data = resultData};
  187. }
  188. }
  189. }
  190. }