World.cs 10 KB

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