World.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Solana.Unity.Programs;
  7. using Solana.Unity.Wallet;
  8. using Solana.Unity.Rpc.Models;
  9. using GplSession.Program;
  10. namespace World
  11. {
  12. namespace Program
  13. {
  14. public partial class WorldProgram
  15. {
  16. public static Solana.Unity.Rpc.Models.TransactionInstruction AddEntity(AddEntityAccounts accounts, PublicKey programId = null)
  17. {
  18. programId ??= new(ID);
  19. return AddEntity(accounts, (byte[]) null, programId);
  20. }
  21. public static Solana.Unity.Rpc.Models.TransactionInstruction AddEntity(AddEntityAccounts accounts, string extraSeed, PublicKey programId = null)
  22. {
  23. programId ??= new(ID);
  24. return AddEntity(accounts, System.Text.Encoding.UTF8.GetBytes(extraSeed), programId);
  25. }
  26. public static PublicKey FindSessionTokenPda(PublicKey sessionSigner, PublicKey authority)
  27. {
  28. PublicKey.TryFindProgramAddress(new[]
  29. {
  30. Encoding.UTF8.GetBytes("session_token"),
  31. new PublicKey(WorldProgram.ID).KeyBytes,
  32. sessionSigner.KeyBytes,
  33. authority.KeyBytes
  34. }, new PublicKey(GplSessionProgram.ID), out var pda, out _);
  35. return pda;
  36. }
  37. public static PublicKey FindRegistryPda()
  38. {
  39. PublicKey.TryFindProgramAddress(new[]
  40. {
  41. Encoding.UTF8.GetBytes("registry"),
  42. }, new PublicKey(ID), out var pda, out _);
  43. return pda;
  44. }
  45. public static PublicKey FindWorldPda(UInt64 worldId)
  46. {
  47. PublicKey.TryFindProgramAddress(new[]
  48. {
  49. Encoding.UTF8.GetBytes("world"),
  50. BitConverter.GetBytes(worldId).Reverse().ToArray()
  51. }, new PublicKey(ID), out var pda, out _);
  52. return pda;
  53. }
  54. public static PublicKey FindEntityPda(UInt64 worldId, UInt64 entityId)
  55. {
  56. PublicKey.TryFindProgramAddress(new[]
  57. {
  58. Encoding.UTF8.GetBytes("entity"),
  59. BitConverter.GetBytes(worldId).Reverse().ToArray(),
  60. BitConverter.GetBytes(entityId).Reverse().ToArray()
  61. }, new PublicKey(ID), out var pda, out _);
  62. return pda;
  63. }
  64. public static PublicKey FindEntityPda(UInt64 worldId, byte[] seed)
  65. {
  66. var ZeroArray = new byte[8];
  67. Array.Fill(ZeroArray, (byte) 0);
  68. PublicKey.TryFindProgramAddress(new[]
  69. {
  70. Encoding.UTF8.GetBytes("entity"),
  71. BitConverter.GetBytes(worldId).Reverse().ToArray(),
  72. ZeroArray,
  73. seed
  74. }, new PublicKey(ID), out var pda, out _);
  75. return pda;
  76. }
  77. public static PublicKey FindEntityPda(UInt64 worldId, string seed)
  78. {
  79. return FindEntityPda(worldId, System.Text.Encoding.UTF8.GetBytes(seed));
  80. }
  81. public static PublicKey FindComponentProgramDataPda(PublicKey componentProgramId)
  82. {
  83. PublicKey.TryFindProgramAddress(new[]
  84. {
  85. componentProgramId.KeyBytes,
  86. }, new PublicKey("BPFLoaderUpgradeab1e11111111111111111111111"), out var pda, out _);
  87. return pda;
  88. }
  89. public static PublicKey FindComponentPda(
  90. PublicKey componentProgramId,
  91. PublicKey entity
  92. ) {
  93. return FindComponentPda(componentProgramId, entity, "");
  94. }
  95. public static PublicKey FindComponentPda(
  96. PublicKey componentProgramId,
  97. PublicKey entity,
  98. string seed
  99. ) {
  100. return FindComponentPda(componentProgramId, entity, System.Text.Encoding.UTF8.GetBytes(seed));
  101. }
  102. public static PublicKey FindComponentPda(
  103. PublicKey componentProgramId,
  104. PublicKey entity,
  105. byte[] seed)
  106. {
  107. PublicKey.TryFindProgramAddress(new[]
  108. {
  109. seed, entity.KeyBytes
  110. }, componentProgramId, out var pda, out _);
  111. return pda;
  112. }
  113. public static readonly PublicKey DelegationProgram = new("DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh");
  114. public static PublicKey FindDelegationProgramPda(string seed, PublicKey account)
  115. {
  116. PublicKey.TryFindProgramAddress(new[]
  117. {
  118. Encoding.UTF8.GetBytes(seed), account.KeyBytes
  119. }, DelegationProgram, out var pda, out _);
  120. return pda;
  121. }
  122. public static PublicKey FindBufferPda(PublicKey account, PublicKey owner)
  123. {
  124. PublicKey.TryFindProgramAddress(new[]
  125. {
  126. Encoding.UTF8.GetBytes("buffer"), account.KeyBytes
  127. }, owner, out var pda, out _);
  128. return pda;
  129. }
  130. /// <summary>
  131. /// Convenience bundle for defining an entity and the associated components.
  132. /// </summary>
  133. [Obsolete("Use Bolt.World.EntityType instead.")]
  134. public class EntityType{
  135. public PublicKey[] Components { get; set; }
  136. public string[] Seeds { get; set; }
  137. public PublicKey Entity { get; set; }
  138. public EntityType(PublicKey entity, PublicKey[] componentsIds)
  139. {
  140. Components = componentsIds;
  141. Seeds = new string[Components.Length];
  142. Entity = entity;
  143. Array.Fill(Seeds, "");
  144. }
  145. public EntityType(PublicKey entity, PublicKey[] componentsIds, string[] seeds)
  146. {
  147. Components = componentsIds;
  148. Seeds = seeds;
  149. Entity = entity;
  150. if (Seeds.Length != Components.Length)
  151. {
  152. throw new ArgumentException("Seeds must be the same length as components");
  153. }
  154. }
  155. public int ComponentsLength()
  156. {
  157. return Components.Length;
  158. }
  159. public PublicKey[] GetComponentsIds()
  160. {
  161. return Components;
  162. }
  163. public PublicKey[] GetComponentsPdas()
  164. {
  165. PublicKey[] pdas = new PublicKey[Components.Length];
  166. for (int i = 0; i < Components.Length; i++)
  167. {
  168. pdas[i] = FindComponentPda(Components[i], Entity, Seeds[i]);
  169. }
  170. return pdas;
  171. }
  172. }
  173. [Obsolete("Use Bolt.World.ApplySystem instead.")]
  174. public static Solana.Unity.Rpc.Models.TransactionInstruction ApplySystem(
  175. PublicKey world,
  176. PublicKey system,
  177. EntityType[] systemInput,
  178. byte[] args,
  179. PublicKey authority,
  180. PublicKey sessionToken = null,
  181. PublicKey programId = null)
  182. {
  183. programId ??= new(WorldProgram.ID);
  184. List<PublicKey> componentIds = new List<PublicKey>();
  185. List<PublicKey> componentPdas = new List<PublicKey>();
  186. foreach (var entity in systemInput)
  187. {
  188. componentIds.AddRange(entity.GetComponentsIds());
  189. componentPdas.AddRange(entity.GetComponentsPdas());
  190. }
  191. if (componentIds.Count != componentPdas.Count)
  192. {
  193. throw new ArgumentException("Component IDs and PDAs must be the same length");
  194. }
  195. Solana.Unity.Rpc.Models.TransactionInstruction instruction;
  196. if (sessionToken != null) {
  197. var apply = new ApplyWithSessionAccounts() {
  198. BoltSystem = system,
  199. Authority = authority,
  200. World = world,
  201. SessionToken = sessionToken,
  202. };
  203. instruction = ApplyWithSession(apply, args, programId);
  204. } else {
  205. var apply = new ApplyAccounts() {
  206. BoltSystem = system,
  207. Authority = authority,
  208. World = world,
  209. };
  210. instruction = Apply(apply, args, programId);
  211. }
  212. for (int i = 0; i < componentIds.Count; i++) {
  213. instruction.Keys.Add(AccountMeta.ReadOnly(componentIds[i], false));
  214. instruction.Keys.Add(AccountMeta.Writable(componentPdas[i], false));
  215. }
  216. if (componentIds.Count > 0) {
  217. // program id delimits the end of the component list
  218. instruction.Keys.Add(AccountMeta.ReadOnly(new PublicKey(WorldProgram.ID), false));
  219. }
  220. return instruction;
  221. }
  222. }
  223. }
  224. }