3land_sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (238) hide show
  1. package/README.md +352 -0
  2. package/assets/3land_rebrand.gif +0 -0
  3. package/assets/battle_sol.gif +0 -0
  4. package/assets/ds.jpeg +0 -0
  5. package/assets/future.webp +0 -0
  6. package/assets/music.mp3 +0 -0
  7. package/assets/niicl.gif +0 -0
  8. package/assets/og.png +0 -0
  9. package/assets/phone.glb +0 -0
  10. package/assets/testvid.mp4 +0 -0
  11. package/idl.json +7652 -0
  12. package/package.json +35 -0
  13. package/src/index.ts +89 -0
  14. package/src/library/Irys/irys.ts +412 -0
  15. package/src/library/Store.ts +613 -0
  16. package/src/library/implementation/storeImplementation.ts +385 -0
  17. package/src/library/instructions/store/buySingleEdition.ts +227 -0
  18. package/src/library/instructions/store/createSingleEdition.ts +49 -0
  19. package/src/library/instructions/store/createStore.ts +19 -0
  20. package/src/library/instructions/store/idl.ts +7622 -0
  21. package/src/library/instructions/store/uploadFilesIryis.ts +133 -0
  22. package/src/types/accounts/ArchiveDeposit.ts +207 -0
  23. package/src/types/accounts/Authority.ts +73 -0
  24. package/src/types/accounts/BurnDeposit.ts +111 -0
  25. package/src/types/accounts/BurnProgress.ts +97 -0
  26. package/src/types/accounts/BuyHistory.ts +121 -0
  27. package/src/types/accounts/BuyTrack.ts +94 -0
  28. package/src/types/accounts/Card.ts +185 -0
  29. package/src/types/accounts/CollectionRegistry.ts +145 -0
  30. package/src/types/accounts/CollectorRegistry.ts +145 -0
  31. package/src/types/accounts/CreatorRegistry.ts +153 -0
  32. package/src/types/accounts/CurrencyArtistProof.ts +113 -0
  33. package/src/types/accounts/GenericUser.ts +185 -0
  34. package/src/types/accounts/ItemArchive.ts +223 -0
  35. package/src/types/accounts/ItemReserveList.ts +87 -0
  36. package/src/types/accounts/ItemReserveList2.ts +73 -0
  37. package/src/types/accounts/LutVault.ts +97 -0
  38. package/src/types/accounts/Pack.ts +367 -0
  39. package/src/types/accounts/PackContent.ts +97 -0
  40. package/src/types/accounts/PackOpenHolder.ts +141 -0
  41. package/src/types/accounts/PackReceipt.ts +145 -0
  42. package/src/types/accounts/PackTraits.ts +101 -0
  43. package/src/types/accounts/Payment.ts +97 -0
  44. package/src/types/accounts/PoolVault.ts +177 -0
  45. package/src/types/accounts/RevealForMe.ts +139 -0
  46. package/src/types/accounts/SecureHolder.ts +97 -0
  47. package/src/types/accounts/Single.ts +301 -0
  48. package/src/types/accounts/Store.ts +180 -0
  49. package/src/types/accounts/StoresHolder.ts +131 -0
  50. package/src/types/accounts/ThreeId.ts +189 -0
  51. package/src/types/accounts/TokenManager.ts +217 -0
  52. package/src/types/accounts/ZeroCard.ts +73 -0
  53. package/src/types/accounts/ZeroContent.ts +89 -0
  54. package/src/types/accounts/ZeroOpenHolder.ts +183 -0
  55. package/src/types/accounts/index.ts +84 -0
  56. package/src/types/errors/anchor.ts +773 -0
  57. package/src/types/errors/index.ts +59 -0
  58. package/src/types/implementation/implementationTypes.ts +27 -0
  59. package/src/types/instructions/addBurn.ts +56 -0
  60. package/src/types/instructions/adjustDepositBurn.ts +33 -0
  61. package/src/types/instructions/archiveItem.ts +60 -0
  62. package/src/types/instructions/assignPackDelegate.ts +30 -0
  63. package/src/types/instructions/burnPay.ts +55 -0
  64. package/src/types/instructions/buyPack.ts +69 -0
  65. package/src/types/instructions/buyPay.ts +71 -0
  66. package/src/types/instructions/buyToken.ts +64 -0
  67. package/src/types/instructions/claimZeroCard.ts +106 -0
  68. package/src/types/instructions/closeDepositBurn.ts +47 -0
  69. package/src/types/instructions/closeRegisterTraits.ts +28 -0
  70. package/src/types/instructions/createCard.ts +56 -0
  71. package/src/types/instructions/createCollection.ts +68 -0
  72. package/src/types/instructions/createPack.ts +71 -0
  73. package/src/types/instructions/createSingle.ts +71 -0
  74. package/src/types/instructions/createStore.ts +50 -0
  75. package/src/types/instructions/createTokenLauncher.ts +82 -0
  76. package/src/types/instructions/createZeroCard.ts +76 -0
  77. package/src/types/instructions/deleteCard.ts +32 -0
  78. package/src/types/instructions/deletePack.ts +40 -0
  79. package/src/types/instructions/deleteSingle.ts +36 -0
  80. package/src/types/instructions/deleteTokenLauncher.ts +38 -0
  81. package/src/types/instructions/depositBurn.ts +55 -0
  82. package/src/types/instructions/donateUser.ts +84 -0
  83. package/src/types/instructions/feedPool.ts +61 -0
  84. package/src/types/instructions/feedTraits.ts +44 -0
  85. package/src/types/instructions/feedTree.ts +63 -0
  86. package/src/types/instructions/flagPack.ts +50 -0
  87. package/src/types/instructions/flagSingle.ts +47 -0
  88. package/src/types/instructions/gatePay.ts +59 -0
  89. package/src/types/instructions/index.ts +140 -0
  90. package/src/types/instructions/initialize.ts +46 -0
  91. package/src/types/instructions/openPack.ts +79 -0
  92. package/src/types/instructions/prepareCreator.ts +30 -0
  93. package/src/types/instructions/preparePack.ts +51 -0
  94. package/src/types/instructions/prepareTraits.ts +28 -0
  95. package/src/types/instructions/printPack.ts +95 -0
  96. package/src/types/instructions/printSingle.ts +108 -0
  97. package/src/types/instructions/recoverDepositBurn.ts +80 -0
  98. package/src/types/instructions/registerActivity.ts +43 -0
  99. package/src/types/instructions/registerCollector.ts +63 -0
  100. package/src/types/instructions/registerCreator.ts +48 -0
  101. package/src/types/instructions/registerGenericUser.ts +94 -0
  102. package/src/types/instructions/registerPackUploads.ts +44 -0
  103. package/src/types/instructions/registerTraits.ts +42 -0
  104. package/src/types/instructions/registerUser.ts +50 -0
  105. package/src/types/instructions/storeLutFor.ts +44 -0
  106. package/src/types/instructions/unwrapAndDestroy.ts +56 -0
  107. package/src/types/instructions/updateCard.ts +54 -0
  108. package/src/types/instructions/updatePack.ts +71 -0
  109. package/src/types/instructions/updateSingle.ts +64 -0
  110. package/src/types/instructions/updateStore.ts +47 -0
  111. package/src/types/instructions/withdrawFromStore.ts +26 -0
  112. package/src/types/programId.ts +42 -0
  113. package/src/types/types/AccountClass.ts +868 -0
  114. package/src/types/types/AccountHasher.ts +89 -0
  115. package/src/types/types/ActionAfter.ts +202 -0
  116. package/src/types/types/AllowedCurrencyArgs.ts +53 -0
  117. package/src/types/types/AuthorityGateTypes.ts +90 -0
  118. package/src/types/types/BurnCount.ts +60 -0
  119. package/src/types/types/BurnState.ts +88 -0
  120. package/src/types/types/BurnTrack.ts +64 -0
  121. package/src/types/types/BurnType.ts +118 -0
  122. package/src/types/types/BurnTypeBurn.ts +56 -0
  123. package/src/types/types/BuyHistoryClass.ts +90 -0
  124. package/src/types/types/CardClass.ts +56 -0
  125. package/src/types/types/CardState.ts +238 -0
  126. package/src/types/types/CardTrack.ts +76 -0
  127. package/src/types/types/Category.ts +72 -0
  128. package/src/types/types/CnftData.ts +85 -0
  129. package/src/types/types/CollectTrack.ts +61 -0
  130. package/src/types/types/Collection.ts +64 -0
  131. package/src/types/types/CompactCnftData.ts +76 -0
  132. package/src/types/types/CoolTimePerAmountArgs.ts +72 -0
  133. package/src/types/types/Creator.ts +72 -0
  134. package/src/types/types/CreatorGateArgs.ts +53 -0
  135. package/src/types/types/CurrencyArtistProof.ts +85 -0
  136. package/src/types/types/CurrencyType.ts +259 -0
  137. package/src/types/types/DateTraitInit.ts +61 -0
  138. package/src/types/types/DateTraitInitMap.ts +72 -0
  139. package/src/types/types/Deposit.ts +85 -0
  140. package/src/types/types/DepositFormat.ts +90 -0
  141. package/src/types/types/DepositState.ts +88 -0
  142. package/src/types/types/DepositSubtype.ts +90 -0
  143. package/src/types/types/DepositTrack.ts +69 -0
  144. package/src/types/types/DepositTrackType.ts +120 -0
  145. package/src/types/types/DepositType.ts +245 -0
  146. package/src/types/types/Earnings.ts +37 -0
  147. package/src/types/types/EditionStoreType.ts +120 -0
  148. package/src/types/types/EncryptedPayload.ts +61 -0
  149. package/src/types/types/ExtraParameter.ts +117 -0
  150. package/src/types/types/FakeBurnCount.ts +64 -0
  151. package/src/types/types/FakeDeposit.ts +64 -0
  152. package/src/types/types/FakeTraitPair.ts +61 -0
  153. package/src/types/types/FakeTraitValue.ts +61 -0
  154. package/src/types/types/FakeVolumeTrack.ts +64 -0
  155. package/src/types/types/FeeType.ts +118 -0
  156. package/src/types/types/FeedingTraits.ts +57 -0
  157. package/src/types/types/Filter.ts +64 -0
  158. package/src/types/types/FilterType.ts +56 -0
  159. package/src/types/types/GeneralStore.ts +56 -0
  160. package/src/types/types/GenericStore.ts +64 -0
  161. package/src/types/types/GenericValue.ts +172 -0
  162. package/src/types/types/GlobalFee.ts +85 -0
  163. package/src/types/types/GlobalState.ts +148 -0
  164. package/src/types/types/IndexDate.ts +94 -0
  165. package/src/types/types/IndexDates.ts +64 -0
  166. package/src/types/types/Item.ts +53 -0
  167. package/src/types/types/ItemClass.ts +88 -0
  168. package/src/types/types/ItemState.ts +298 -0
  169. package/src/types/types/ItemTrack.ts +76 -0
  170. package/src/types/types/ListingPerWalletArgs.ts +53 -0
  171. package/src/types/types/MemeVaultProof.ts +64 -0
  172. package/src/types/types/MetadataArgs.ts +207 -0
  173. package/src/types/types/NonFungibleTraitInit.ts +64 -0
  174. package/src/types/types/NonFungibleTraitInitMap.ts +84 -0
  175. package/src/types/types/OnlyBetweenTimesArgs.ts +76 -0
  176. package/src/types/types/OnlyTheseDOWArgs.ts +53 -0
  177. package/src/types/types/PackConfig.ts +98 -0
  178. package/src/types/types/PackOpenHolderState.ts +120 -0
  179. package/src/types/types/PackRule.ts +85 -0
  180. package/src/types/types/PackState.ts +118 -0
  181. package/src/types/types/PackType.ts +118 -0
  182. package/src/types/types/PaymentClass.ts +118 -0
  183. package/src/types/types/PoolAccess.ts +88 -0
  184. package/src/types/types/PoolState.ts +88 -0
  185. package/src/types/types/PoolType.ts +88 -0
  186. package/src/types/types/Popularity.ts +76 -0
  187. package/src/types/types/PopularityState.ts +630 -0
  188. package/src/types/types/PreviousDonationRecord.ts +76 -0
  189. package/src/types/types/Price.ts +64 -0
  190. package/src/types/types/PriceRule.ts +216 -0
  191. package/src/types/types/RecoverDeposit.ts +56 -0
  192. package/src/types/types/Rule.ts +199 -0
  193. package/src/types/types/SaleConfig.ts +102 -0
  194. package/src/types/types/SaleTrack.ts +85 -0
  195. package/src/types/types/SaleType.ts +148 -0
  196. package/src/types/types/Seed.ts +61 -0
  197. package/src/types/types/SelectedCard.ts +61 -0
  198. package/src/types/types/SelectedZeroCard.ts +61 -0
  199. package/src/types/types/SemiFungibleTraitInit.ts +61 -0
  200. package/src/types/types/SemiFungibleTraitInitMap.ts +72 -0
  201. package/src/types/types/ShortCreator.ts +64 -0
  202. package/src/types/types/ShortMetadata.ts +145 -0
  203. package/src/types/types/ShortMetadataArgs.ts +111 -0
  204. package/src/types/types/StoreConfig.ts +98 -0
  205. package/src/types/types/StoreRule.ts +145 -0
  206. package/src/types/types/SuperCategory.ts +61 -0
  207. package/src/types/types/TightCardMetadata.ts +120 -0
  208. package/src/types/types/TimeRangeType.ts +120 -0
  209. package/src/types/types/TimedPerWalletArgs.ts +53 -0
  210. package/src/types/types/TokenMetadata.ts +72 -0
  211. package/src/types/types/TokenProgramVersion.ts +90 -0
  212. package/src/types/types/TokenStandard.ts +150 -0
  213. package/src/types/types/TokenState.ts +88 -0
  214. package/src/types/types/TokenType.ts +88 -0
  215. package/src/types/types/TotalPerTimeArgs.ts +85 -0
  216. package/src/types/types/TotalPerTimeWalletArgs.ts +61 -0
  217. package/src/types/types/TrackRegistry.ts +90 -0
  218. package/src/types/types/TraitInit.ts +210 -0
  219. package/src/types/types/TraitPass.ts +60 -0
  220. package/src/types/types/TraitPassType.ts +225 -0
  221. package/src/types/types/TraitType.ts +267 -0
  222. package/src/types/types/TraitValue.ts +72 -0
  223. package/src/types/types/UnlocksAfter.ts +172 -0
  224. package/src/types/types/UnwrapMetadata.ts +156 -0
  225. package/src/types/types/UseMethod.ts +118 -0
  226. package/src/types/types/Uses.ts +76 -0
  227. package/src/types/types/VerifyCollectionMetadata.ts +109 -0
  228. package/src/types/types/VolumeTrack.ts +61 -0
  229. package/src/types/types/WrappedSource.ts +85 -0
  230. package/src/types/types/ZeroConfig.ts +85 -0
  231. package/src/types/types/index.ts +798 -0
  232. package/src/utility/Connection.ts +9 -0
  233. package/src/utility/Holders.ts +8 -0
  234. package/src/utility/PdaManager.ts +230 -0
  235. package/src/utility/config.ts +32 -0
  236. package/src/utility/utils.ts +191 -0
  237. package/src/utility/validation.ts +229 -0
  238. package/tsconfig.json +12 -0
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "3land_sdk",
3
+ "version": "1.0.0",
4
+ "description": "sdk for interacting with 3land's solana program",
5
+ "main": "./src/library/implementation/storeImplementation.ts",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "npx tsx ./src/index.ts"
9
+ },
10
+ "author": "biccs",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "@coral-xyz/borsh": "^0.30.1",
14
+ "@irys/sdk": "^0.2.11",
15
+ "@metaplex-foundation/beet": "^0.7.2",
16
+ "@metaplex-foundation/mpl-token-metadata": "^2.13.0",
17
+ "@project-serum/anchor": "^0.26.0",
18
+ "@solana/web3.js": "^1.95.4",
19
+ "bn": "^1.0.5",
20
+ "bn.js": "^5.2.1",
21
+ "bs58": "^6.0.0",
22
+ "cyrb53": "^1.0.0",
23
+ "fs": "^0.0.1-security",
24
+ "irys": "^0.0.1",
25
+ "node-fetch": "^3.3.2",
26
+ "ts-node": "^10.9.2",
27
+ "tweetnacl": "^1.0.3"
28
+ },
29
+ "devDependencies": {
30
+ "@types/bn.js": "^5.1.6",
31
+ "@types/node": "^22.7.7",
32
+ "tsx": "^4.19.1",
33
+ "typescript": "^5.6.3"
34
+ }
35
+ }
package/src/index.ts ADDED
@@ -0,0 +1,89 @@
1
+ import { SendTransactionError } from "@solana/web3.js";
2
+ import {
3
+ createSingleImp,
4
+ createStoreImp,
5
+ buySingleImp,
6
+ handleError,
7
+ } from "./library/implementation/storeImplementation";
8
+ import {
9
+ CreateCollectionOptions,
10
+ CreateSingleOptions,
11
+ CreateStoreParams,
12
+ StoreInitOptions,
13
+ } from "./types/implementation/implementationTypes";
14
+
15
+ async function main() {
16
+ // const options: StoreInitOptions = {
17
+ // walletPath: "", //route to keypair.json generated from the solana cli
18
+ // };
19
+
20
+ const optionsWithBase58: StoreInitOptions = {
21
+ privateKey:
22
+ "2ixmpz6W9aAE7HDqCJYy1tsqQcAHhkuP54jTPGnTyqvYHJearwT16DzmkkaQLARB9TshZvoS3WE5dQg183wryVCC",
23
+ };
24
+
25
+ const storeSetup: CreateStoreParams = {
26
+ storeName: "Super cool store",
27
+ storeFee: 5,
28
+ };
29
+
30
+ const collectionOpts: CreateCollectionOptions = {
31
+ collectionName: "Super awesome Collection",
32
+ collectionSymbol: "SAC",
33
+ collectionDescription: "This is a collection for the cool guys",
34
+ };
35
+
36
+ const createItemOptions: CreateSingleOptions = {
37
+ itemName: "supercoolitem7",
38
+ sellerFee: 500,
39
+ itemSymbol: "SCI7",
40
+ itemDescription: "implementing link imgs",
41
+ traits: [
42
+ { trait_type: "type", value: "cool" },
43
+ { trait_type: "creator", value: "me" },
44
+ ],
45
+ price: 100000000, //100000000 == 0.1 sol
46
+ mainImageUrl:
47
+ "https://arweave.net/FMkKYYsheEImBfejYaPPoJbI3CxJxunwvErD9VYzxOY?ext=jpeg",
48
+ };
49
+
50
+ try {
51
+ //image url: https://arweave.net/FMkKYYsheEImBfejYaPPoJbI3CxJxunwvErD9VYzxOY?ext=jpeg
52
+ //image aI: https://oaidalleapiprodscus.blob.core.windows.net/private/org-gqKriRsgW5z3sjYXrQv0vXPk/user-dhUswyb2oCg8NHc1yhPldLys/img-vX5am1Oagbimd9uSWnrSlUPb.png?st=2024-12-29T02%3A12%3A51Z&se=2024-12-29T04%3A12%3A51Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-12-28T21%3A04%3A53Z&ske=2024-12-29T21%3A04%3A53Z&sks=b&skv=2024-08-04&sig=ltGieu7AXXJUhIF7hyTPXmnnFVKTu5BwmifIB3kLqAY%3D
53
+ // Create store
54
+ //const storeResult = await createStoreTest(optionsWithBase58, storeSetup);
55
+ //console.log("Store created. Transaction ID:", storeResult.transactionId);
56
+ const landStoreMainnet = "AmQNs2kgw4LvS9sm6yE9JJ4Hs3JpVu65eyx9pxMG2xA";
57
+ const landStoreDevnet = "GyPCu89S63P9NcCQAtuSJesiefhhgpGWrNVJs4bF2cSK";
58
+ // Create Collection
59
+ // const collection = await createCollectionTest(
60
+ // optionsWithBase58,
61
+ // collectionOpts
62
+ // );
63
+ // console.log("collection mint: ", collection);
64
+ // Create single edition
65
+ // const storeAccount = "3MwBR619SgJ35ek7vDLxxE5QvBaNq1fmmEZSXKW2X3Lf"; //"P1c4bboejX24NbY3vMw8EncKVmvcGEryznWLs4PGp9j"; //current store created for testing
66
+ const collectionAccount = "Fpm8XgXEuNxxjmqUQuqEFkGusiSsKM6astUGPs5U9x6v"; //"2rQq34FJG1613i7H8cDfxuGCtEjJmFAUNbAPJqK699oD";
67
+ const singleEditionResult = await createSingleImp(
68
+ optionsWithBase58,
69
+ landStoreDevnet,
70
+ collectionAccount,
71
+ createItemOptions
72
+ );
73
+ console.log(
74
+ "Single edition created. Transaction ID:",
75
+ singleEditionResult.transactionId
76
+ );
77
+ // Buy single edition
78
+ // const itemAccount = "8iUHPXuZWQdSGTV9X8hPdUgxSLfdXX7YjZYMck2TALBc"; //"7BhKXmc5obiwn5hhrUhErVBrAT7TErYcTpRYE8ggfjKV"; //current item created for testing
79
+ // const buyResult = await buySingleTest(options, itemAccount);
80
+ // console.log(
81
+ // "Single edition purchased. Transaction ID:",
82
+ // buyResult.transactionId
83
+ // );
84
+ } catch (error) {
85
+ handleError(error);
86
+ }
87
+ }
88
+
89
+ main();
@@ -0,0 +1,412 @@
1
+ import { WebIrys, NodeIrys } from "@irys/sdk";
2
+ import { sleep, nowS } from "../../utility/utils";
3
+ import { toPublicKey } from "../../utility/PdaManager";
4
+ import crypto from "crypto";
5
+ import { SystemProgram, Keypair, PublicKey } from "@solana/web3.js";
6
+ import nacl from "tweetnacl";
7
+
8
+ interface IrysTransaction {
9
+ upload(): Promise<any>;
10
+ // Add other transaction properties you're using
11
+ }
12
+
13
+ interface IrysFile {
14
+ type: string;
15
+ name: string;
16
+ nonce?: string;
17
+ arrayBuffer(): Promise<ArrayBuffer>;
18
+ is_metadata?: boolean;
19
+ status?: string;
20
+ arweave?: string;
21
+ data?: any;
22
+ payload?: boolean;
23
+ transaction?: string | IrysTransaction; // Can be either string or transaction object
24
+ irys?: {
25
+ id: string;
26
+ size: number;
27
+ url: string;
28
+ extension: string | null;
29
+ nonce: string;
30
+ transaction: IrysTransaction;
31
+ irys_wallet: string;
32
+ price: number;
33
+ slippage_fee: number;
34
+ };
35
+ }
36
+
37
+ export interface UploadOptions {
38
+ uuid: string;
39
+ signature: any;
40
+ // files: IrysFile[];
41
+ }
42
+
43
+ interface RegisterOptions {
44
+ files: IrysFile[];
45
+ uuid: string;
46
+ }
47
+
48
+ interface FundingInstructionsOptions {
49
+ files: any; //IrysFile[] | undefined
50
+ payer: string | PublicKey | boolean;
51
+ }
52
+
53
+ export class IrysHelper {
54
+ private irys!: WebIrys | NodeIrys;
55
+ private wallet!: Keypair;
56
+ private owner!: string;
57
+ private files_bridge: { [key: string]: IrysFile };
58
+
59
+ constructor() {
60
+ this.files_bridge = {};
61
+ }
62
+
63
+ // private irys?: WebIrys;
64
+ // private wallet?: Keypair;
65
+ // private owner?: string;
66
+
67
+ private ensureInitialized(): void {
68
+ if (!this.irys || !this.wallet || !this.owner) {
69
+ throw new Error(
70
+ "IrysHelper not properly initialized. Call init() first."
71
+ );
72
+ }
73
+ }
74
+
75
+ // async verifyBalance(id: any): Promise<boolean> {
76
+ // this.ensureInitialized();
77
+ // try {
78
+ // const fundtx = await this.irys.fund(id);
79
+ // return !!fundtx;
80
+ // } catch (e) {
81
+ // console.log("CANNOT VERIFY FUND", e);
82
+ // }
83
+ // return false;
84
+ // }
85
+
86
+ // async getBalance(): Promise<any> {
87
+ // this.ensureInitialized();
88
+ // return this.irys.getLoadedBalance();
89
+ // }
90
+
91
+ async verifyBalance(id: any) {
92
+ try {
93
+ const submited = await this.irys.funder.submitTransaction(id);
94
+ return submited;
95
+ } catch (e) {
96
+ console.log("CANNOT VERIFY FUND", e);
97
+ }
98
+ return false;
99
+ }
100
+ async getBalance() {
101
+ return this.irys.getLoadedBalance();
102
+ }
103
+
104
+ async bundle(file: IrysFile, is_metadata = false): Promise<IrysFile> {
105
+ console.log("BUNDLE: ", file);
106
+ console.log("BUNDLE method: ", file.arrayBuffer());
107
+
108
+ this.ensureInitialized();
109
+ try {
110
+ const { type, name } = file;
111
+ const nonce =
112
+ file.nonce || crypto.randomBytes(32).toString("base64").slice(0, 32);
113
+ const tags = [{ name: "Content-Type", value: type }];
114
+ const irys_wallet = this.irys.address;
115
+ const arrayBuffer = await file.arrayBuffer();
116
+ console.log("main image buffer 2: ", arrayBuffer);
117
+ const buffer = Buffer.from(arrayBuffer);
118
+ console.log("main image buffer 3: ", arrayBuffer);
119
+
120
+ let transaction = this.irys.createTransaction(buffer, {
121
+ anchor: nonce,
122
+ tags,
123
+ });
124
+
125
+ console.log("TRANSACTION: ", transaction);
126
+
127
+ const { size } = transaction;
128
+ const price = await this.irys.getPrice(transaction.size);
129
+ const slippage_fee = Math.round(price.div(6).toNumber());
130
+ await transaction.sign();
131
+
132
+ const extension = this.getFileExtension(file);
133
+ const id = transaction?.id;
134
+ if (!id) throw "No id";
135
+
136
+ const url =
137
+ "https://arweave.net/" +
138
+ id +
139
+ (extension && !file.is_metadata && !is_metadata
140
+ ? "?ext=" + extension
141
+ : "");
142
+
143
+ file.payload = false;
144
+ file.irys = {
145
+ id,
146
+ size,
147
+ url,
148
+ extension,
149
+ nonce,
150
+ transaction,
151
+ irys_wallet,
152
+ price: price.toNumber(),
153
+ slippage_fee,
154
+ };
155
+
156
+ return file;
157
+ } catch (e) {
158
+ console.log("ERRORE", e);
159
+ throw new Error("error in bundle Irys");
160
+ }
161
+ }
162
+
163
+ getFileExtension(file: IrysFile): string | null {
164
+ if (!file) {
165
+ return null;
166
+ }
167
+
168
+ // Try to get extension from filename
169
+ if (file.name) {
170
+ const parts = file.name.split(".");
171
+ if (parts.length > 1) {
172
+ const extension = parts.pop(); // Get last part
173
+ if (extension) {
174
+ return extension;
175
+ }
176
+ }
177
+ }
178
+
179
+ // Fallback to MIME type
180
+ if (file.type) {
181
+ const parts = file.type.split("/");
182
+ if (parts.length > 1) {
183
+ return parts[1];
184
+ }
185
+ }
186
+
187
+ return null;
188
+ }
189
+
190
+ private parseTransaction(transactionStr: any): string | null {
191
+ try {
192
+ const parsed = JSON.parse(transactionStr);
193
+ return Array.isArray(parsed) ? parsed[0] : null;
194
+ } catch (e) {
195
+ console.log("Error parsing transaction:", e);
196
+ return null;
197
+ }
198
+ }
199
+
200
+ async getFundingInstructions({ files, payer }: FundingInstructionsOptions) {
201
+ this.ensureInitialized();
202
+ if (!payer) payer = this.owner;
203
+
204
+ let bytes = 0;
205
+ let price: any = false;
206
+ if (!files) return;
207
+ for (const file of files) {
208
+ if (!file.irys) {
209
+ throw new Error("File not properly bundled");
210
+ }
211
+ const files_price = await this.irys.getPrice(file.irys.size);
212
+ if (!price) {
213
+ price = files_price;
214
+ } else {
215
+ price = price.plus(files_price);
216
+ }
217
+ bytes += file.irys.size;
218
+ file.irys.price = price.toNumber();
219
+ }
220
+
221
+ const irys_address = await this.irys.utils.getBundlerAddress("solana");
222
+ const slippage_fee = Math.round(price.div(6).toNumber());
223
+ price = price.plus(slippage_fee);
224
+ const from_user_to_manager = SystemProgram.transfer({
225
+ fromPubkey: toPublicKey(payer),
226
+ toPubkey: this.wallet.publicKey,
227
+ lamports: price,
228
+ });
229
+
230
+ const from_manager_to_irys = SystemProgram.transfer({
231
+ fromPubkey: this.wallet.publicKey,
232
+ toPubkey: toPublicKey(irys_address),
233
+ lamports: price,
234
+ });
235
+
236
+ return {
237
+ instructions: [from_user_to_manager, from_manager_to_irys],
238
+ bytes,
239
+ price: price.toNumber(),
240
+ };
241
+ }
242
+
243
+ async generateWallet(): Promise<Keypair> {
244
+ return Keypair.generate();
245
+ }
246
+
247
+ async getWallet(): Promise<Keypair> {
248
+ return this.wallet;
249
+ }
250
+
251
+ private arweaveToID(x: string): string {
252
+ return "irys-preupload-" + x;
253
+ }
254
+
255
+ async uploadFiles({ uuid, signature }: UploadOptions) {
256
+ this.ensureInitialized();
257
+ await this.irys.ready();
258
+
259
+ const files = [];
260
+ for (const file in this.files_bridge) {
261
+ files.push(this.files_bridge[file]);
262
+ }
263
+
264
+ await this.verifyBalance(signature);
265
+
266
+ const errors: string[] = [];
267
+ const succeeds: string[] = [];
268
+
269
+ for (const _file of files) {
270
+ if (!_file.irys) {
271
+ continue;
272
+ }
273
+
274
+ if (_file.status == "uploaded" && _file.arweave) {
275
+ succeeds.push(_file.arweave);
276
+ continue;
277
+ }
278
+
279
+ const blob = this.files_bridge[this.arweaveToID(_file.irys.id)];
280
+
281
+ if (!blob) {
282
+ errors.push(_file.irys.id);
283
+ continue;
284
+ }
285
+
286
+ blob.nonce = _file.irys.nonce;
287
+ const bundled = await this.bundle(blob);
288
+
289
+ if (!bundled || !bundled.irys || bundled.irys.id != _file.irys.id) {
290
+ errors.push(_file.irys.id);
291
+ continue;
292
+ }
293
+
294
+ try {
295
+ const subida = await bundled.irys.transaction.upload();
296
+ if (subida) {
297
+ succeeds.push(bundled.irys.id);
298
+ } else {
299
+ throw "";
300
+ }
301
+ } catch (e) {
302
+ const error = e + "";
303
+ if (error.includes("already received")) {
304
+ succeeds.push(bundled.irys.id);
305
+ } else {
306
+ errors.push(_file.irys.id);
307
+ }
308
+ }
309
+ await sleep(100);
310
+ }
311
+
312
+ const balance = await this.getBalance();
313
+ return { errors, succeeds };
314
+ }
315
+
316
+ async clean(): Promise<void> {}
317
+
318
+ async registerFiles({ files, uuid }: RegisterOptions): Promise<number> {
319
+ this.ensureInitialized();
320
+ const owner = this.owner;
321
+ this.files_bridge = {};
322
+
323
+ for (const [index, _file] of files.entries()) {
324
+ if (!_file.irys) {
325
+ continue;
326
+ }
327
+
328
+ const arweave = _file.irys.id;
329
+ const data = {
330
+ type: _file.type,
331
+ nonce: _file.irys.nonce,
332
+ size: _file.irys.size,
333
+ fee_at_submit: _file.irys.price,
334
+ slippage_fee: _file.irys.slippage_fee,
335
+ };
336
+
337
+ const tosave = {
338
+ owner,
339
+ arweave,
340
+ uuid,
341
+ status: "waiting",
342
+ date: nowS(),
343
+ data,
344
+ payload: _file.payload,
345
+ };
346
+
347
+ this.files_bridge[this.arweaveToID(arweave)] = _file;
348
+ }
349
+
350
+ return files.length;
351
+ }
352
+
353
+ async sync(address: string): Promise<boolean> {
354
+ if (this.owner != address) {
355
+ return true;
356
+ }
357
+ return true;
358
+ }
359
+
360
+ async init(
361
+ address: string,
362
+ options: { arweave_rpc?: string; rpc?: string }
363
+ ): Promise<boolean> {
364
+ const irys_network =
365
+ options?.arweave_rpc || "https://arweave.devnet.irys.xyz";
366
+ const rpc = options?.rpc || "https://api.devnet.solana.com";
367
+
368
+ const wallet = await this.generateWallet();
369
+ if (!wallet) return false;
370
+ this.wallet = wallet;
371
+
372
+ const provider = {
373
+ publicKey: wallet.publicKey,
374
+ signMessage: async (message: Uint8Array) => {
375
+ return nacl.sign.detached(message, this.wallet.secretKey);
376
+ },
377
+ };
378
+
379
+ this.owner = address;
380
+
381
+ this.irys = new NodeIrys({
382
+ // url: irys_network,
383
+ token: "solana",
384
+ key: wallet.secretKey,
385
+ network: "devnet",
386
+ config: { providerUrl: irys_network },
387
+ // wallet: { rpcUrl: rpc, provider },
388
+ });
389
+
390
+ await this.irys.ready();
391
+ const to = await this.irys.utils.getBundlerAddress("solana");
392
+ const bal = await this.getBalance();
393
+ return true;
394
+ }
395
+ }
396
+
397
+ let global: IrysHelper | null = null;
398
+
399
+ export const init = async (
400
+ address: string,
401
+ options: { arweave_rpc?: string; rpc?: string }
402
+ ): Promise<IrysHelper | null> => {
403
+ if (global) {
404
+ const g = await global.sync(address);
405
+ if (!g) global = null;
406
+ return global;
407
+ }
408
+ global = new IrysHelper();
409
+ const g = await global.init(address, options);
410
+ if (!g) global = null;
411
+ return global;
412
+ };