import { Lux, BinTools, BN, Buffer } from "lux/dist" import { XVMAPI, KeyChain, SECPTransferOutput, SECPTransferInput, TransferableOutput, TransferableInput, UTXOSet, UTXO, AmountOutput, UnsignedTx, Tx, CreateAssetTx, InitialStates, XVMConstants, MinterSet, NFTMintOutput } from "lux/dist/apis/xvm" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults } from "lux/dist/utils" const ip: string = "localhost" const port: number = 9650 const protocol: string = "http" const networkID: number = 1337 const lux: Lux = new Lux(ip, port, protocol, networkID) const xchain: XVMAPI = lux.XChain() const bintools: BinTools = BinTools.getInstance() const xKeychain: KeyChain = xchain.keyChain() const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` xKeychain.importKey(privKey) const xAddresses: Buffer[] = xchain.keyChain().getAddresses() const xAddressStrings: string[] = xchain.keyChain().getAddressStrings() const blockchainID: string = Defaults.network[networkID].X.blockchainID const luxAssetID: string = Defaults.network[networkID].X.luxAssetID const luxAssetIDBuf: Buffer = bintools.cb58Decode(luxAssetID) const outputs: TransferableOutput[] = [] const inputs: TransferableInput[] = [] const fee: BN = xchain.getDefaultTxFee() const threshold: number = 1 const locktime: BN = new BN(0) const memo: Buffer = Buffer.from("XVM manual CreateAssetTx to create an NFT") const name: string = "non fungible token" const symbol: string = "NFT" const denomination: number = 0 // NFTs are non-fungible const groupID: number = 0 // Uncomment for codecID 00 01 // const codecID: number = 1 const main = async (): Promise => { const getBalanceResponse: any = await xchain.getBalance( xAddressStrings[0], luxAssetID ) const balance: BN = new BN(getBalanceResponse.balance) const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput( balance.sub(fee), xAddresses, locktime, threshold ) // Uncomment for codecID 00 01 // secpTransferOutput.setCodecID(codecID) const transferableOutput: TransferableOutput = new TransferableOutput( luxAssetIDBuf, secpTransferOutput ) outputs.push(transferableOutput) const xvmUTXOResponse: any = await xchain.getUTXOs(xAddressStrings) const utxoSet: UTXOSet = xvmUTXOResponse.utxos const utxos: UTXO[] = utxoSet.getAllUTXOs() utxos.forEach((utxo: UTXO): void => { const outputID: number = utxo.getOutput().getTypeID() const assetID: Buffer = utxo.getAssetID() if ( outputID === 7 && assetID.toString("hex") === luxAssetIDBuf.toString("hex") ) { const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput const amt: BN = amountOutput.getAmount().clone() const txid: Buffer = utxo.getTxID() const outputidx: Buffer = utxo.getOutputIdx() const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt) // Uncomment for codecID 00 01 // secpTransferInput.setCodecID(codecID) secpTransferInput.addSignatureIdx(0, xAddresses[0]) const input: TransferableInput = new TransferableInput( txid, outputidx, luxAssetIDBuf, secpTransferInput ) inputs.push(input) } }) const initialStates: InitialStates = new InitialStates() const minterSets: MinterSet[] = [new MinterSet(threshold, xAddresses)] for (let i: number = 0; i <= 5; i++) { const nftMintOutput: NFTMintOutput = new NFTMintOutput( groupID, minterSets[0].getMinters(), locktime, minterSets[0].getThreshold() ) // Uncomment for codecID 00 01 // nftMintOutput.setCodecID(codecID) initialStates.addOutput(nftMintOutput, XVMConstants.NFTFXID) } const createAssetTx: CreateAssetTx = new CreateAssetTx( networkID, bintools.cb58Decode(blockchainID), outputs, inputs, memo, name, symbol, denomination, initialStates ) // Uncomment for codecID 00 01 // createAssetTx.setCodecID(codecID) const unsignedTx: UnsignedTx = new UnsignedTx(createAssetTx) const tx: Tx = unsignedTx.sign(xKeychain) const txid: string = await xchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()