-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnftUtils.ts
More file actions
94 lines (82 loc) · 2.55 KB
/
Copy pathnftUtils.ts
File metadata and controls
94 lines (82 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* NFT Utilities using Metaplex Core
* Creates instructions compatible with Lazorkit's signAndSendTransaction
*/
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import {
create as createAsset,
mplCore,
} from '@metaplex-foundation/mpl-core';
import {
generateSigner,
publicKey,
createNoopSigner,
signerIdentity,
} from '@metaplex-foundation/umi';
import {
toWeb3JsInstruction,
toWeb3JsKeypair,
} from '@metaplex-foundation/umi-web3js-adapters';
import { TransactionInstruction, Keypair } from '@solana/web3.js';
import { CONFIG } from '../config';
export interface MintNFTResult {
instructions: TransactionInstruction[];
assetAddress: string;
assetKeypair: Keypair;
}
/**
* Create a configured Umi instance for a specific wallet
* Uses a noop signer since Lazorkit handles actual signing
*/
function createConfiguredUmi(walletAddress: string) {
const umi = createUmi(CONFIG.RPC_URL)
.use(mplCore())
.use(signerIdentity(createNoopSigner(publicKey(walletAddress))));
return umi;
}
/**
* Build instructions for minting a Metaplex Core NFT
* Returns web3.js compatible instructions for Lazorkit
*/
export async function buildMintNFTInstructions(
ownerAddress: string,
metadataUri: string,
name: string,
options: {
soulbound?: boolean;
} = {}
): Promise<MintNFTResult> {
// Create Umi instance configured for this wallet
const umi = createConfiguredUmi(ownerAddress);
// Generate a new keypair for the asset account
const assetSigner = generateSigner(umi);
// Build the create instruction with optional plugins
const plugins: any[] = [];
// Add soulbound plugin if requested
if (options.soulbound) {
plugins.push({
type: 'PermanentFreezeDelegate',
frozen: true,
authority: { type: 'None' },
});
}
// Build the transaction
const builder = createAsset(umi, {
asset: assetSigner,
name: name,
uri: metadataUri,
owner: publicKey(ownerAddress),
plugins: plugins.length > 0 ? plugins : undefined,
});
// Get Umi instructions
const umiInstructions = builder.getInstructions();
// Convert to Web3.js instructions
const web3Instructions = umiInstructions.map(toWeb3JsInstruction);
// Get the asset keypair for signing
const assetKeypair = toWeb3JsKeypair(assetSigner);
return {
instructions: web3Instructions,
assetAddress: assetSigner.publicKey.toString(),
assetKeypair: assetKeypair,
};
}