added deploy at qortalrequest

This commit is contained in:
PhilReact 2024-10-16 03:19:55 +03:00
parent 12013947af
commit 82fba43838
5 changed files with 171 additions and 6 deletions

View File

@ -774,7 +774,7 @@ async function storeFilesInIndexedDB(obj) {
const UIQortalRequests = ['GET_USER_ACCOUNT', 'DECRYPT_DATA', 'SEND_COIN', 'GET_LIST_ITEMS', 'ADD_LIST_ITEMS', 'DELETE_LIST_ITEM', 'VOTE_ON_POLL', 'CREATE_POLL', 'SEND_CHAT_MESSAGE', 'JOIN_GROUP']
const UIQortalRequests = ['GET_USER_ACCOUNT', 'DECRYPT_DATA', 'SEND_COIN', 'GET_LIST_ITEMS', 'ADD_LIST_ITEMS', 'DELETE_LIST_ITEM', 'VOTE_ON_POLL', 'CREATE_POLL', 'SEND_CHAT_MESSAGE', 'JOIN_GROUP', 'DEPLOY_AT']
if (!window.hasAddedQortalListener) {
console.log("Listener added");

View File

@ -1,4 +1,4 @@
import { addListItems, createPoll, decryptData, deleteListItems, encryptData, getListItems, getUserAccount, joinGroup, publishMultipleQDNResources, publishQDNResource, saveFile, sendChatMessage, sendCoin, voteOnPoll } from "./qortalRequests/get";
import { addListItems, createPoll, decryptData, deleteListItems, deployAt, encryptData, getListItems, getUserAccount, joinGroup, publishMultipleQDNResources, publishQDNResource, saveFile, sendChatMessage, sendCoin, voteOnPoll } from "./qortalRequests/get";
@ -231,6 +231,19 @@ chrome?.runtime?.onMessage.addListener((request, sender, sendResponse) => {
break;
}
case "DEPLOY_AT": {
const data = request.payload;
deployAt(data, sender)
.then((res) => {
sendResponse(res);
})
.catch((error) => {
sendResponse({ error: error.message });
});
break;
}
case "SEND_COIN": {
const data = request.payload;
const requiredFields = ["coin", "destinationAddress", "amount"];

View File

@ -70,6 +70,57 @@ const _createPoll = async (pollName, pollDescription, options) => {
}
};
const _deployAt= async(name, description, tags, creationBytes, amount, assetId, atType)=> {
const fee = await getFee("DEPLOY_AT");
const resPermission = await getUserPermission({
text1: "Would you like to deploy this AT?",
text2: `Name: ${name}`,
text3: `Description: ${description}`,
fee: fee.fee,
});
const { accepted } = resPermission;
if (accepted) {
const wallet = await getSaveWallet();
const address = wallet.address0;
const lastReference = await getLastRef();
const resKeyPair = await getKeyPair();
const parsedData = JSON.parse(resKeyPair);
const uint8PrivateKey = Base58.decode(parsedData.privateKey);
const uint8PublicKey = Base58.decode(parsedData.publicKey);
const keyPair = {
privateKey: uint8PrivateKey,
publicKey: uint8PublicKey,
};
const tx = await createTransaction(16, keyPair, {
fee: fee.fee,
rName: name,
rDescription: description,
rTags: tags,
rAmount: amount,
rAssetId: assetId,
rCreationBytes: creationBytes,
atType: atType,
lastReference: lastReference,
});
const signedBytes = Base58.encode(tx.signedBytes);
const res = await processTransactionVersion2(signedBytes);
if (!res?.signature)
throw new Error(res?.message || "Transaction was not able to be processed");
return res;
} else {
throw new Error("User declined transaction");
}
}
const _voteOnPoll = async (pollName, optionIndex, optionName) => {
const fee = await getFee("VOTE_ON_POLL");
@ -484,7 +535,7 @@ export const deleteListItems = async (data) => {
}
return res;
} else {
throw new Error("User declined add to list");
throw new Error("User declined delete from list");
}
};
@ -1065,7 +1116,7 @@ export const sendChatMessage = async (data) => {
throw new Error("Please enter a recipient or groupId");
}
} else {
throw new Error("User declined add to list");
throw new Error("User declined to send message");
}
};
@ -1118,7 +1169,7 @@ export const joinGroup = async (data) => {
throw new Error(error?.message || 'Failed to join the group.')
}
} else {
throw new Error("User declined add to list");
throw new Error("User declined to join group");
}
};
@ -1175,7 +1226,7 @@ export const joinGroup = async (data) => {
} ,sender)
return true
} else {
throw new Error("User declined add to list");
throw new Error("User declined to save file");
}
@ -1186,6 +1237,27 @@ export const joinGroup = async (data) => {
};
export const deployAt = async (data)=> {
const requiredFields = ['name', 'description', 'tags', 'creationBytes', 'amount', 'assetId', 'type']
const missingFields: string[] = []
requiredFields.forEach((field) => {
if (!data[field] && data[field] !== 0) {
missingFields.push(field)
}
})
if (missingFields.length > 0) {
const missingFieldsString = missingFields.join(', ')
const errorMsg = `Missing fields: ${missingFieldsString}`
throw new Error(errorMsg)
}
try {
const resDeployAt = await _deployAt(data.name, data.description, data.tags, data.creationBytes, data.amount, data.assetId, data.type)
return resDeployAt
} catch (error) {
throw new Error(error?.message || 'Failed to join the group.')
}
}
export const sendCoin = async () => {
try {
const wallet = await getSaveWallet();

View File

@ -0,0 +1,78 @@
// @ts-nocheck
import TransactionBase from './TransactionBase'
import { QORT_DECIMALS } from '../constants/constants'
export default class DeployAtTransaction extends TransactionBase {
constructor() {
super()
this.type = 16
}
set fee(fee) {
this._fee = fee * QORT_DECIMALS
this._feeBytes = this.constructor.utils.int64ToBytes(this._fee)
}
set rAmount(rAmount) {
this._rAmount = Math.round(rAmount * QORT_DECIMALS)
this._rAmountBytes = this.constructor.utils.int64ToBytes(this._rAmount)
}
set rName(rName) {
this._rName = rName
this._rNameBytes = this.constructor.utils.stringtoUTF8Array(this._rName.toLocaleLowerCase())
this._rNameLength = this.constructor.utils.int32ToBytes(this._rNameBytes.length)
}
set rDescription(rDescription) {
this._rDescription = rDescription
this._rDescriptionBytes = this.constructor.utils.stringtoUTF8Array(this._rDescription.toLocaleLowerCase())
this._rDescriptionLength = this.constructor.utils.int32ToBytes(this._rDescriptionBytes.length)
}
set atType(atType) {
this._atType = atType
this._atTypeBytes = this.constructor.utils.stringtoUTF8Array(this._atType)
this._atTypeLength = this.constructor.utils.int32ToBytes(this._atTypeBytes.length)
}
set rTags(rTags) {
this._rTags = rTags
this._rTagsBytes = this.constructor.utils.stringtoUTF8Array(this._rTags.toLocaleLowerCase())
this._rTagsLength = this.constructor.utils.int32ToBytes(this._rTagsBytes.length)
}
set rCreationBytes(rCreationBytes) {
const decode = this.constructor.Base58.decode(rCreationBytes)
this._rCreationBytes = this.constructor.utils.stringtoUTF8Array(decode)
this._rCreationBytesLength = this.constructor.utils.int32ToBytes(this._rCreationBytes.length)
}
set rAssetId(rAssetId) {
this._rAssetId = this.constructor.utils.int64ToBytes(rAssetId)
}
get params() {
const params = super.params
params.push(
this._rNameLength,
this._rNameBytes,
this._rDescriptionLength,
this._rDescriptionBytes,
this._atTypeLength,
this._atTypeBytes,
this._rTagsLength,
this._rTagsBytes,
this._rCreationBytesLength,
this._rCreationBytes,
this._rAmountBytes,
this._rAssetId,
this._feeBytes
)
return params
}
}

View File

@ -16,6 +16,7 @@ import RemoveGroupAdminTransaction from './RemoveGroupAdminTransaction.js'
import RegisterNameTransaction from './RegisterNameTransaction.js'
import VoteOnPollTransaction from './VoteOnPollTransaction.js'
import CreatePollTransaction from './CreatePollTransaction.js'
import DeployAtTransaction from './DeployAtTransaction.js'
export const transactionTypes = {
@ -23,6 +24,7 @@ export const transactionTypes = {
2: PaymentTransaction,
8: CreatePollTransaction,
9: VoteOnPollTransaction,
16: DeployAtTransaction,
18: ChatTransaction,
181: GroupChatTransaction,
22: CreateGroupTransaction,