use chia_bls::PublicKey;
use chia_protocol::{Bytes32, Coin, CoinSpend};
use chia_puzzle_types::nft::NftMetadata;
use chia_puzzle_types::standard::StandardArgs;
use chia_wallet_sdk::driver::{Launcher, Nft, NftMint, SpendContext, StandardLayer};
use chia_wallet_sdk::types::Conditions;
pub(crate) const RECIPIENT_PUZZLE_HASH: Bytes32 = Bytes32::new([0x7c; 32]);
fn p2_puzzle_hash(owner_key: PublicKey) -> Bytes32 {
Bytes32::from(StandardArgs::curry_tree_hash(owner_key).to_bytes())
}
fn mint_parts(owner_key: PublicKey, owner: Bytes32, funding_seed: u8) -> (SpendContext, Nft) {
let mut ctx = SpendContext::new();
let funding = Coin::new(
Bytes32::new([funding_seed; 32]),
p2_puzzle_hash(owner_key),
1,
);
let metadata = ctx
.alloc_hashed(&NftMetadata::default())
.expect("the default NFT metadata allocates");
let (mint_conditions, nft) = Launcher::new(funding.coin_id(), 1)
.mint_nft(&mut ctx, &NftMint::new(metadata, owner, 300, None))
.expect("the canonical sdk NFT mint builder");
StandardLayer::new(owner_key)
.spend(&mut ctx, funding, mint_conditions)
.expect("the funding coin spends under the standard layer");
(ctx, nft)
}
pub(crate) fn nft_mint_to(
owner_key: PublicKey,
owner: Bytes32,
funding_seed: u8,
) -> Vec<CoinSpend> {
let (mut ctx, _nft) = mint_parts(owner_key, owner, funding_seed);
ctx.take()
}
pub(crate) fn nft_transfer_to(
owner_key: PublicKey,
destination: Bytes32,
funding_seed: u8,
) -> Vec<CoinSpend> {
let owner = p2_puzzle_hash(owner_key);
let (mut ctx, nft) = mint_parts(owner_key, owner, funding_seed);
let _settled = nft
.transfer(
&mut ctx,
&StandardLayer::new(owner_key),
destination,
Conditions::new(),
)
.expect("the canonical sdk NFT transfer builder");
ctx.take()
}
pub(crate) fn launcher_id(owner_key: PublicKey, owner: Bytes32, funding_seed: u8) -> Bytes32 {
mint_parts(owner_key, owner, funding_seed)
.1
.info
.launcher_id
}