123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- #pragma warning disable CS1591
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Solana.Unity.Programs;
- using Solana.Unity.Wallet;
- using Solana.Unity.Rpc.Models;
- using GplSession.Program;
- namespace World
- {
- namespace Program
- {
- public partial class WorldProgram
- {
- public static Solana.Unity.Rpc.Models.TransactionInstruction AddEntity(AddEntityAccounts accounts, PublicKey programId = null)
- {
- programId ??= new(ID);
- return AddEntity(accounts, (byte[]) null, programId);
- }
- public static Solana.Unity.Rpc.Models.TransactionInstruction AddEntity(AddEntityAccounts accounts, string extraSeed, PublicKey programId = null)
- {
- programId ??= new(ID);
- return AddEntity(accounts, System.Text.Encoding.UTF8.GetBytes(extraSeed), programId);
- }
- public static PublicKey FindSessionTokenPda(PublicKey sessionSigner, PublicKey authority)
- {
- PublicKey.TryFindProgramAddress(new[]
- {
- Encoding.UTF8.GetBytes("session_token"),
- new PublicKey(WorldProgram.ID).KeyBytes,
- sessionSigner.KeyBytes,
- authority.KeyBytes
- }, new PublicKey(GplSessionProgram.ID), out var pda, out _);
- return pda;
- }
- public static PublicKey FindRegistryPda()
- {
- PublicKey.TryFindProgramAddress(new[]
- {
- Encoding.UTF8.GetBytes("registry"),
- }, new PublicKey(ID), out var pda, out _);
- return pda;
- }
- public static PublicKey FindWorldPda(UInt64 worldId)
- {
- PublicKey.TryFindProgramAddress(new[]
- {
- Encoding.UTF8.GetBytes("world"),
- BitConverter.GetBytes(worldId).Reverse().ToArray()
- }, new PublicKey(ID), out var pda, out _);
- return pda;
- }
- public static PublicKey FindEntityPda(UInt64 worldId, UInt64 entityId)
- {
- PublicKey.TryFindProgramAddress(new[]
- {
- Encoding.UTF8.GetBytes("entity"),
- BitConverter.GetBytes(worldId).Reverse().ToArray(),
- BitConverter.GetBytes(entityId).Reverse().ToArray()
- }, new PublicKey(ID), out var pda, out _);
- return pda;
- }
- public static PublicKey FindEntityPda(UInt64 worldId, byte[] seed)
- {
- var ZeroArray = new byte[8];
- Array.Fill(ZeroArray, (byte) 0);
- PublicKey.TryFindProgramAddress(new[]
- {
- Encoding.UTF8.GetBytes("entity"),
- BitConverter.GetBytes(worldId).Reverse().ToArray(),
- ZeroArray,
- seed
- }, new PublicKey(ID), out var pda, out _);
- return pda;
- }
- public static PublicKey FindEntityPda(UInt64 worldId, string seed)
- {
- return FindEntityPda(worldId, System.Text.Encoding.UTF8.GetBytes(seed));
- }
- public static PublicKey FindComponentProgramDataPda(PublicKey componentProgramId)
- {
- PublicKey.TryFindProgramAddress(new[]
- {
- componentProgramId.KeyBytes,
- }, new PublicKey("BPFLoaderUpgradeab1e11111111111111111111111"), out var pda, out _);
- return pda;
- }
- public static PublicKey FindComponentPda(
- PublicKey componentProgramId,
- PublicKey entity
- ) {
- return FindComponentPda(componentProgramId, entity, "");
- }
- public static PublicKey FindComponentPda(
- PublicKey componentProgramId,
- PublicKey entity,
- string seed
- ) {
- return FindComponentPda(componentProgramId, entity, System.Text.Encoding.UTF8.GetBytes(seed));
- }
- public static PublicKey FindComponentPda(
- PublicKey componentProgramId,
- PublicKey entity,
- byte[] seed)
- {
- PublicKey.TryFindProgramAddress(new[]
- {
- seed, entity.KeyBytes
- }, componentProgramId, out var pda, out _);
- return pda;
- }
- public static readonly PublicKey DelegationProgram = new("DELeGGvXpWV2fqJUhqcF5ZSYMS4JTLjteaAMARRSaeSh");
- public static PublicKey FindDelegationProgramPda(string seed, PublicKey account)
- {
- PublicKey.TryFindProgramAddress(new[]
- {
- Encoding.UTF8.GetBytes(seed), account.KeyBytes
- }, DelegationProgram, out var pda, out _);
- return pda;
- }
- public static PublicKey FindBufferPda(PublicKey account, PublicKey owner)
- {
- PublicKey.TryFindProgramAddress(new[]
- {
- Encoding.UTF8.GetBytes("buffer"), account.KeyBytes
- }, owner, out var pda, out _);
- return pda;
- }
- /// <summary>
- /// Convenience bundle for defining an entity and the associated components.
- /// </summary>
- [Obsolete("Use Bolt.World.EntityType instead.")]
- public class EntityType{
- public PublicKey[] Components { get; set; }
- public string[] Seeds { get; set; }
- public PublicKey Entity { get; set; }
- public EntityType(PublicKey entity, PublicKey[] componentsIds)
- {
- Components = componentsIds;
- Seeds = new string[Components.Length];
- Entity = entity;
- Array.Fill(Seeds, "");
- }
- public EntityType(PublicKey entity, PublicKey[] componentsIds, string[] seeds)
- {
- Components = componentsIds;
- Seeds = seeds;
- Entity = entity;
- if (Seeds.Length != Components.Length)
- {
- throw new ArgumentException("Seeds must be the same length as components");
- }
- }
- public int ComponentsLength()
- {
- return Components.Length;
- }
- public PublicKey[] GetComponentsIds()
- {
- return Components;
- }
- public PublicKey[] GetComponentsPdas()
- {
- PublicKey[] pdas = new PublicKey[Components.Length];
- for (int i = 0; i < Components.Length; i++)
- {
- pdas[i] = FindComponentPda(Components[i], Entity, Seeds[i]);
- }
- return pdas;
- }
- }
- [Obsolete("Use Bolt.World.ApplySystem instead.")]
- public static Solana.Unity.Rpc.Models.TransactionInstruction ApplySystem(
- PublicKey world,
- PublicKey system,
- EntityType[] systemInput,
- byte[] args,
- PublicKey authority,
- PublicKey sessionToken = null,
- PublicKey programId = null)
- {
- programId ??= new(WorldProgram.ID);
- List<PublicKey> componentIds = new List<PublicKey>();
- List<PublicKey> componentPdas = new List<PublicKey>();
- foreach (var entity in systemInput)
- {
- componentIds.AddRange(entity.GetComponentsIds());
- componentPdas.AddRange(entity.GetComponentsPdas());
- }
- if (componentIds.Count != componentPdas.Count)
- {
- throw new ArgumentException("Component IDs and PDAs must be the same length");
- }
- Solana.Unity.Rpc.Models.TransactionInstruction instruction;
- if (sessionToken != null) {
- var apply = new ApplyWithSessionAccounts() {
- BoltSystem = system,
- Authority = authority,
- World = world,
- SessionToken = sessionToken,
- };
- instruction = ApplyWithSession(apply, args, programId);
- } else {
- var apply = new ApplyAccounts() {
- BoltSystem = system,
- Authority = authority,
- World = world,
- };
- instruction = Apply(apply, args, programId);
- }
- for (int i = 0; i < componentIds.Count; i++) {
- instruction.Keys.Add(AccountMeta.ReadOnly(componentIds[i], false));
- instruction.Keys.Add(AccountMeta.Writable(componentPdas[i], false));
- }
- if (componentIds.Count > 0) {
- // program id delimits the end of the component list
- instruction.Keys.Add(AccountMeta.ReadOnly(new PublicKey(WorldProgram.ID), false));
- }
- return instruction;
- }
- }
- }
- }
|