3land_sdk 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +352 -0
- package/assets/3land_rebrand.gif +0 -0
- package/assets/battle_sol.gif +0 -0
- package/assets/ds.jpeg +0 -0
- package/assets/future.webp +0 -0
- package/assets/music.mp3 +0 -0
- package/assets/niicl.gif +0 -0
- package/assets/og.png +0 -0
- package/assets/phone.glb +0 -0
- package/assets/testvid.mp4 +0 -0
- package/idl.json +7652 -0
- package/package.json +35 -0
- package/src/index.ts +89 -0
- package/src/library/Irys/irys.ts +412 -0
- package/src/library/Store.ts +613 -0
- package/src/library/implementation/storeImplementation.ts +385 -0
- package/src/library/instructions/store/buySingleEdition.ts +227 -0
- package/src/library/instructions/store/createSingleEdition.ts +49 -0
- package/src/library/instructions/store/createStore.ts +19 -0
- package/src/library/instructions/store/idl.ts +7622 -0
- package/src/library/instructions/store/uploadFilesIryis.ts +133 -0
- package/src/types/accounts/ArchiveDeposit.ts +207 -0
- package/src/types/accounts/Authority.ts +73 -0
- package/src/types/accounts/BurnDeposit.ts +111 -0
- package/src/types/accounts/BurnProgress.ts +97 -0
- package/src/types/accounts/BuyHistory.ts +121 -0
- package/src/types/accounts/BuyTrack.ts +94 -0
- package/src/types/accounts/Card.ts +185 -0
- package/src/types/accounts/CollectionRegistry.ts +145 -0
- package/src/types/accounts/CollectorRegistry.ts +145 -0
- package/src/types/accounts/CreatorRegistry.ts +153 -0
- package/src/types/accounts/CurrencyArtistProof.ts +113 -0
- package/src/types/accounts/GenericUser.ts +185 -0
- package/src/types/accounts/ItemArchive.ts +223 -0
- package/src/types/accounts/ItemReserveList.ts +87 -0
- package/src/types/accounts/ItemReserveList2.ts +73 -0
- package/src/types/accounts/LutVault.ts +97 -0
- package/src/types/accounts/Pack.ts +367 -0
- package/src/types/accounts/PackContent.ts +97 -0
- package/src/types/accounts/PackOpenHolder.ts +141 -0
- package/src/types/accounts/PackReceipt.ts +145 -0
- package/src/types/accounts/PackTraits.ts +101 -0
- package/src/types/accounts/Payment.ts +97 -0
- package/src/types/accounts/PoolVault.ts +177 -0
- package/src/types/accounts/RevealForMe.ts +139 -0
- package/src/types/accounts/SecureHolder.ts +97 -0
- package/src/types/accounts/Single.ts +301 -0
- package/src/types/accounts/Store.ts +180 -0
- package/src/types/accounts/StoresHolder.ts +131 -0
- package/src/types/accounts/ThreeId.ts +189 -0
- package/src/types/accounts/TokenManager.ts +217 -0
- package/src/types/accounts/ZeroCard.ts +73 -0
- package/src/types/accounts/ZeroContent.ts +89 -0
- package/src/types/accounts/ZeroOpenHolder.ts +183 -0
- package/src/types/accounts/index.ts +84 -0
- package/src/types/errors/anchor.ts +773 -0
- package/src/types/errors/index.ts +59 -0
- package/src/types/implementation/implementationTypes.ts +27 -0
- package/src/types/instructions/addBurn.ts +56 -0
- package/src/types/instructions/adjustDepositBurn.ts +33 -0
- package/src/types/instructions/archiveItem.ts +60 -0
- package/src/types/instructions/assignPackDelegate.ts +30 -0
- package/src/types/instructions/burnPay.ts +55 -0
- package/src/types/instructions/buyPack.ts +69 -0
- package/src/types/instructions/buyPay.ts +71 -0
- package/src/types/instructions/buyToken.ts +64 -0
- package/src/types/instructions/claimZeroCard.ts +106 -0
- package/src/types/instructions/closeDepositBurn.ts +47 -0
- package/src/types/instructions/closeRegisterTraits.ts +28 -0
- package/src/types/instructions/createCard.ts +56 -0
- package/src/types/instructions/createCollection.ts +68 -0
- package/src/types/instructions/createPack.ts +71 -0
- package/src/types/instructions/createSingle.ts +71 -0
- package/src/types/instructions/createStore.ts +50 -0
- package/src/types/instructions/createTokenLauncher.ts +82 -0
- package/src/types/instructions/createZeroCard.ts +76 -0
- package/src/types/instructions/deleteCard.ts +32 -0
- package/src/types/instructions/deletePack.ts +40 -0
- package/src/types/instructions/deleteSingle.ts +36 -0
- package/src/types/instructions/deleteTokenLauncher.ts +38 -0
- package/src/types/instructions/depositBurn.ts +55 -0
- package/src/types/instructions/donateUser.ts +84 -0
- package/src/types/instructions/feedPool.ts +61 -0
- package/src/types/instructions/feedTraits.ts +44 -0
- package/src/types/instructions/feedTree.ts +63 -0
- package/src/types/instructions/flagPack.ts +50 -0
- package/src/types/instructions/flagSingle.ts +47 -0
- package/src/types/instructions/gatePay.ts +59 -0
- package/src/types/instructions/index.ts +140 -0
- package/src/types/instructions/initialize.ts +46 -0
- package/src/types/instructions/openPack.ts +79 -0
- package/src/types/instructions/prepareCreator.ts +30 -0
- package/src/types/instructions/preparePack.ts +51 -0
- package/src/types/instructions/prepareTraits.ts +28 -0
- package/src/types/instructions/printPack.ts +95 -0
- package/src/types/instructions/printSingle.ts +108 -0
- package/src/types/instructions/recoverDepositBurn.ts +80 -0
- package/src/types/instructions/registerActivity.ts +43 -0
- package/src/types/instructions/registerCollector.ts +63 -0
- package/src/types/instructions/registerCreator.ts +48 -0
- package/src/types/instructions/registerGenericUser.ts +94 -0
- package/src/types/instructions/registerPackUploads.ts +44 -0
- package/src/types/instructions/registerTraits.ts +42 -0
- package/src/types/instructions/registerUser.ts +50 -0
- package/src/types/instructions/storeLutFor.ts +44 -0
- package/src/types/instructions/unwrapAndDestroy.ts +56 -0
- package/src/types/instructions/updateCard.ts +54 -0
- package/src/types/instructions/updatePack.ts +71 -0
- package/src/types/instructions/updateSingle.ts +64 -0
- package/src/types/instructions/updateStore.ts +47 -0
- package/src/types/instructions/withdrawFromStore.ts +26 -0
- package/src/types/programId.ts +42 -0
- package/src/types/types/AccountClass.ts +868 -0
- package/src/types/types/AccountHasher.ts +89 -0
- package/src/types/types/ActionAfter.ts +202 -0
- package/src/types/types/AllowedCurrencyArgs.ts +53 -0
- package/src/types/types/AuthorityGateTypes.ts +90 -0
- package/src/types/types/BurnCount.ts +60 -0
- package/src/types/types/BurnState.ts +88 -0
- package/src/types/types/BurnTrack.ts +64 -0
- package/src/types/types/BurnType.ts +118 -0
- package/src/types/types/BurnTypeBurn.ts +56 -0
- package/src/types/types/BuyHistoryClass.ts +90 -0
- package/src/types/types/CardClass.ts +56 -0
- package/src/types/types/CardState.ts +238 -0
- package/src/types/types/CardTrack.ts +76 -0
- package/src/types/types/Category.ts +72 -0
- package/src/types/types/CnftData.ts +85 -0
- package/src/types/types/CollectTrack.ts +61 -0
- package/src/types/types/Collection.ts +64 -0
- package/src/types/types/CompactCnftData.ts +76 -0
- package/src/types/types/CoolTimePerAmountArgs.ts +72 -0
- package/src/types/types/Creator.ts +72 -0
- package/src/types/types/CreatorGateArgs.ts +53 -0
- package/src/types/types/CurrencyArtistProof.ts +85 -0
- package/src/types/types/CurrencyType.ts +259 -0
- package/src/types/types/DateTraitInit.ts +61 -0
- package/src/types/types/DateTraitInitMap.ts +72 -0
- package/src/types/types/Deposit.ts +85 -0
- package/src/types/types/DepositFormat.ts +90 -0
- package/src/types/types/DepositState.ts +88 -0
- package/src/types/types/DepositSubtype.ts +90 -0
- package/src/types/types/DepositTrack.ts +69 -0
- package/src/types/types/DepositTrackType.ts +120 -0
- package/src/types/types/DepositType.ts +245 -0
- package/src/types/types/Earnings.ts +37 -0
- package/src/types/types/EditionStoreType.ts +120 -0
- package/src/types/types/EncryptedPayload.ts +61 -0
- package/src/types/types/ExtraParameter.ts +117 -0
- package/src/types/types/FakeBurnCount.ts +64 -0
- package/src/types/types/FakeDeposit.ts +64 -0
- package/src/types/types/FakeTraitPair.ts +61 -0
- package/src/types/types/FakeTraitValue.ts +61 -0
- package/src/types/types/FakeVolumeTrack.ts +64 -0
- package/src/types/types/FeeType.ts +118 -0
- package/src/types/types/FeedingTraits.ts +57 -0
- package/src/types/types/Filter.ts +64 -0
- package/src/types/types/FilterType.ts +56 -0
- package/src/types/types/GeneralStore.ts +56 -0
- package/src/types/types/GenericStore.ts +64 -0
- package/src/types/types/GenericValue.ts +172 -0
- package/src/types/types/GlobalFee.ts +85 -0
- package/src/types/types/GlobalState.ts +148 -0
- package/src/types/types/IndexDate.ts +94 -0
- package/src/types/types/IndexDates.ts +64 -0
- package/src/types/types/Item.ts +53 -0
- package/src/types/types/ItemClass.ts +88 -0
- package/src/types/types/ItemState.ts +298 -0
- package/src/types/types/ItemTrack.ts +76 -0
- package/src/types/types/ListingPerWalletArgs.ts +53 -0
- package/src/types/types/MemeVaultProof.ts +64 -0
- package/src/types/types/MetadataArgs.ts +207 -0
- package/src/types/types/NonFungibleTraitInit.ts +64 -0
- package/src/types/types/NonFungibleTraitInitMap.ts +84 -0
- package/src/types/types/OnlyBetweenTimesArgs.ts +76 -0
- package/src/types/types/OnlyTheseDOWArgs.ts +53 -0
- package/src/types/types/PackConfig.ts +98 -0
- package/src/types/types/PackOpenHolderState.ts +120 -0
- package/src/types/types/PackRule.ts +85 -0
- package/src/types/types/PackState.ts +118 -0
- package/src/types/types/PackType.ts +118 -0
- package/src/types/types/PaymentClass.ts +118 -0
- package/src/types/types/PoolAccess.ts +88 -0
- package/src/types/types/PoolState.ts +88 -0
- package/src/types/types/PoolType.ts +88 -0
- package/src/types/types/Popularity.ts +76 -0
- package/src/types/types/PopularityState.ts +630 -0
- package/src/types/types/PreviousDonationRecord.ts +76 -0
- package/src/types/types/Price.ts +64 -0
- package/src/types/types/PriceRule.ts +216 -0
- package/src/types/types/RecoverDeposit.ts +56 -0
- package/src/types/types/Rule.ts +199 -0
- package/src/types/types/SaleConfig.ts +102 -0
- package/src/types/types/SaleTrack.ts +85 -0
- package/src/types/types/SaleType.ts +148 -0
- package/src/types/types/Seed.ts +61 -0
- package/src/types/types/SelectedCard.ts +61 -0
- package/src/types/types/SelectedZeroCard.ts +61 -0
- package/src/types/types/SemiFungibleTraitInit.ts +61 -0
- package/src/types/types/SemiFungibleTraitInitMap.ts +72 -0
- package/src/types/types/ShortCreator.ts +64 -0
- package/src/types/types/ShortMetadata.ts +145 -0
- package/src/types/types/ShortMetadataArgs.ts +111 -0
- package/src/types/types/StoreConfig.ts +98 -0
- package/src/types/types/StoreRule.ts +145 -0
- package/src/types/types/SuperCategory.ts +61 -0
- package/src/types/types/TightCardMetadata.ts +120 -0
- package/src/types/types/TimeRangeType.ts +120 -0
- package/src/types/types/TimedPerWalletArgs.ts +53 -0
- package/src/types/types/TokenMetadata.ts +72 -0
- package/src/types/types/TokenProgramVersion.ts +90 -0
- package/src/types/types/TokenStandard.ts +150 -0
- package/src/types/types/TokenState.ts +88 -0
- package/src/types/types/TokenType.ts +88 -0
- package/src/types/types/TotalPerTimeArgs.ts +85 -0
- package/src/types/types/TotalPerTimeWalletArgs.ts +61 -0
- package/src/types/types/TrackRegistry.ts +90 -0
- package/src/types/types/TraitInit.ts +210 -0
- package/src/types/types/TraitPass.ts +60 -0
- package/src/types/types/TraitPassType.ts +225 -0
- package/src/types/types/TraitType.ts +267 -0
- package/src/types/types/TraitValue.ts +72 -0
- package/src/types/types/UnlocksAfter.ts +172 -0
- package/src/types/types/UnwrapMetadata.ts +156 -0
- package/src/types/types/UseMethod.ts +118 -0
- package/src/types/types/Uses.ts +76 -0
- package/src/types/types/VerifyCollectionMetadata.ts +109 -0
- package/src/types/types/VolumeTrack.ts +61 -0
- package/src/types/types/WrappedSource.ts +85 -0
- package/src/types/types/ZeroConfig.ts +85 -0
- package/src/types/types/index.ts +798 -0
- package/src/utility/Connection.ts +9 -0
- package/src/utility/Holders.ts +8 -0
- package/src/utility/PdaManager.ts +230 -0
- package/src/utility/config.ts +32 -0
- package/src/utility/utils.ts +191 -0
- package/src/utility/validation.ts +229 -0
- 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
|
+
};
|