mirror of
https://github.com/Qortal/chrome-extension.git
synced 2025-03-28 16:25:54 +00:00
added publish qortalrequest
This commit is contained in:
parent
9660e04d8d
commit
3694b4390a
@ -453,7 +453,7 @@ chrome.runtime?.onMessage.addListener(function (message, sender, sendResponse) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const UIQortalRequests = ['GET_USER_ACCOUNT', 'ENCRYPT_DATA', 'DECRYPT_DATA', 'SEND_COIN', 'GET_LIST_ITEMS', 'ADD_LIST_ITEMS', 'DELETE_LIST_ITEM']
|
const UIQortalRequests = ['GET_USER_ACCOUNT', 'ENCRYPT_DATA', 'DECRYPT_DATA', 'SEND_COIN', 'GET_LIST_ITEMS', 'ADD_LIST_ITEMS', 'DELETE_LIST_ITEM', 'PUBLISH_QDN_RESOURCE']
|
||||||
|
|
||||||
if (!window.hasAddedQortalListener) {
|
if (!window.hasAddedQortalListener) {
|
||||||
console.log("Listener added");
|
console.log("Listener added");
|
||||||
|
60
src/App.tsx
60
src/App.tsx
@ -1824,18 +1824,63 @@ function App() {
|
|||||||
{isShowQortalRequest && !isMainWindow && (
|
{isShowQortalRequest && !isMainWindow && (
|
||||||
<>
|
<>
|
||||||
<Spacer height="120px" />
|
<Spacer height="120px" />
|
||||||
|
<Box sx={{
|
||||||
|
display: 'flex',
|
||||||
|
justifyContent: 'center',
|
||||||
|
width: '100%'
|
||||||
|
}}>
|
||||||
<TextP
|
<TextP
|
||||||
sx={{
|
sx={{
|
||||||
textAlign: "center",
|
|
||||||
lineHeight: 1.2,
|
lineHeight: 1.2,
|
||||||
maxWidth: '90%'
|
maxWidth: '90%',
|
||||||
|
textAlign: 'center'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{messageQortalRequest?.text1}
|
{messageQortalRequest?.text1}
|
||||||
</TextP>
|
</TextP>
|
||||||
|
</Box>
|
||||||
<Spacer height="10px" />
|
<Spacer height="10px" />
|
||||||
|
<Box sx={{
|
||||||
|
display: 'flex',
|
||||||
|
justifyContent: 'flex-start',
|
||||||
|
width: '90%'
|
||||||
|
}}>
|
||||||
<TextP
|
<TextP
|
||||||
|
sx={{
|
||||||
|
lineHeight: 1.2,
|
||||||
|
fontSize: "16px",
|
||||||
|
fontWeight: "normal",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{messageQortalRequest?.text2}
|
||||||
|
</TextP>
|
||||||
|
</Box>
|
||||||
|
<Spacer height="15px" />
|
||||||
|
{messageQortalRequest?.text3 && (
|
||||||
|
<Box sx={{
|
||||||
|
display: 'flex',
|
||||||
|
justifyContent: 'flex-start',
|
||||||
|
width: '90%'
|
||||||
|
}}>
|
||||||
|
<TextP
|
||||||
|
sx={{
|
||||||
|
lineHeight: 1.2,
|
||||||
|
fontSize: "16px",
|
||||||
|
fontWeight: "normal",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{messageQortalRequest?.text3}
|
||||||
|
</TextP>
|
||||||
|
<Spacer height="15px" />
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
{messageQortalRequest?.text4 && (
|
||||||
|
<Box sx={{
|
||||||
|
display: 'flex',
|
||||||
|
justifyContent: 'flex-start',
|
||||||
|
width: '90%'
|
||||||
|
}}>
|
||||||
|
<TextP
|
||||||
sx={{
|
sx={{
|
||||||
textAlign: "center",
|
textAlign: "center",
|
||||||
lineHeight: 1.2,
|
lineHeight: 1.2,
|
||||||
@ -1844,9 +1889,12 @@ function App() {
|
|||||||
maxWidth: '90%'
|
maxWidth: '90%'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{messageQortalRequest?.text2}
|
{messageQortalRequest?.text4}
|
||||||
</TextP>
|
</TextP>
|
||||||
<Spacer height="15px" />
|
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
<Spacer height="15px" />
|
||||||
<TextP
|
<TextP
|
||||||
sx={{
|
sx={{
|
||||||
textAlign: "center",
|
textAlign: "center",
|
||||||
@ -1856,7 +1904,7 @@ function App() {
|
|||||||
maxWidth: '90%'
|
maxWidth: '90%'
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{messageQortalRequest?.text3}
|
{messageQortalRequest?.highlightedText}
|
||||||
</TextP>
|
</TextP>
|
||||||
{messageQortalRequest?.checkbox1 &&
|
{messageQortalRequest?.checkbox1 &&
|
||||||
(
|
(
|
||||||
|
@ -43,7 +43,7 @@ async function getSaveWallet() {
|
|||||||
throw new Error("No wallet saved");
|
throw new Error("No wallet saved");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async function getNameInfo() {
|
export async function getNameInfo() {
|
||||||
const wallet = await getSaveWallet();
|
const wallet = await getSaveWallet();
|
||||||
const address = wallet.address0;
|
const address = wallet.address0;
|
||||||
const validApi = await getBaseApi()
|
const validApi = await getBaseApi()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { addListItems, decryptData, deleteListItems, encryptData, getListItems, getUserAccount, sendCoin } from "./qortalRequests/get";
|
import { addListItems, decryptData, deleteListItems, encryptData, getListItems, getUserAccount, publishQDNResource, sendCoin } from "./qortalRequests/get";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -57,6 +57,10 @@ function getLocalStorage(key) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: GET_FRIENDS_LIST
|
||||||
|
// NOT SURE IF TO IMPLEMENT: LINK_TO_QDN_RESOURCE, QDN_RESOURCE_DISPLAYED, SET_TAB_NOTIFICATIONS
|
||||||
|
|
||||||
chrome?.runtime?.onMessage.addListener((request, sender, sendResponse) => {
|
chrome?.runtime?.onMessage.addListener((request, sender, sendResponse) => {
|
||||||
if (request) {
|
if (request) {
|
||||||
switch (request.action) {
|
switch (request.action) {
|
||||||
@ -136,6 +140,19 @@ chrome?.runtime?.onMessage.addListener((request, sender, sendResponse) => {
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case "PUBLISH_QDN_RESOURCE": {
|
||||||
|
const data = request.payload;
|
||||||
|
|
||||||
|
publishQDNResource(data)
|
||||||
|
.then((res) => {
|
||||||
|
sendResponse(res);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
sendResponse({ error: error.message });
|
||||||
|
});
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
case "SEND_COIN": {
|
case "SEND_COIN": {
|
||||||
const data = request.payload;
|
const data = request.payload;
|
||||||
const requiredFields = ["coin", "destinationAddress", "amount"];
|
const requiredFields = ["coin", "destinationAddress", "amount"];
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import {
|
import {
|
||||||
createEndpoint,
|
createEndpoint,
|
||||||
|
getFee,
|
||||||
getKeyPair,
|
getKeyPair,
|
||||||
getSaveWallet,
|
getSaveWallet,
|
||||||
removeDuplicateWindow,
|
removeDuplicateWindow,
|
||||||
} from "../background";
|
} from "../background";
|
||||||
|
import { getNameInfo } from "../backgroundFunctions/encryption";
|
||||||
import Base58 from "../deps/Base58";
|
import Base58 from "../deps/Base58";
|
||||||
import {
|
import {
|
||||||
base64ToUint8Array,
|
base64ToUint8Array,
|
||||||
@ -13,6 +15,7 @@ import {
|
|||||||
uint8ArrayStartsWith,
|
uint8ArrayStartsWith,
|
||||||
uint8ArrayToBase64,
|
uint8ArrayToBase64,
|
||||||
} from "../qdn/encryption/group-encryption";
|
} from "../qdn/encryption/group-encryption";
|
||||||
|
import { publishData } from "../qdn/publish/pubish";
|
||||||
import { getPermission, setPermission } from "../qortalRequests";
|
import { getPermission, setPermission } from "../qortalRequests";
|
||||||
import { fileToBase64 } from "../utils/fileReading";
|
import { fileToBase64 } from "../utils/fileReading";
|
||||||
|
|
||||||
@ -98,7 +101,7 @@ async function getUserPermission(payload: any) {
|
|||||||
payload: 30,
|
payload: 30,
|
||||||
});
|
});
|
||||||
res(true);
|
res(true);
|
||||||
}, 700);
|
}, 1000);
|
||||||
});
|
});
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
// Set a timeout for 1 second
|
// Set a timeout for 1 second
|
||||||
@ -220,33 +223,31 @@ export const getListItems = async (data) => {
|
|||||||
const errorMsg = `Missing fields: ${missingFieldsString}`;
|
const errorMsg = `Missing fields: ${missingFieldsString}`;
|
||||||
throw new Error(errorMsg);
|
throw new Error(errorMsg);
|
||||||
}
|
}
|
||||||
const value = await getPermission('qAPPAutoLists') || false;
|
const value = (await getPermission("qAPPAutoLists")) || false;
|
||||||
|
|
||||||
let skip = false;
|
let skip = false;
|
||||||
if (value) {
|
if (value) {
|
||||||
skip = true
|
skip = true;
|
||||||
}
|
}
|
||||||
let resPermission;
|
let resPermission;
|
||||||
let acceptedVar
|
let acceptedVar;
|
||||||
let checkbox1Var
|
let checkbox1Var;
|
||||||
if (!skip) {
|
if (!skip) {
|
||||||
resPermission = await getUserPermission({
|
resPermission = await getUserPermission({
|
||||||
text1: "Do you give this application permission to",
|
text1: "Do you give this application permission to",
|
||||||
text2: "Access the list",
|
text2: "Access the list",
|
||||||
text3: data.list_name,
|
highlightedText: data.list_name,
|
||||||
checkbox1: {
|
checkbox1: {
|
||||||
value: value,
|
value: value,
|
||||||
label: 'Always allow lists to be retrieved automatically'
|
label: "Always allow lists to be retrieved automatically",
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
const {accepted, checkbox1} = resPermission
|
const { accepted, checkbox1 } = resPermission;
|
||||||
acceptedVar = accepted
|
acceptedVar = accepted;
|
||||||
checkbox1Var = checkbox1
|
checkbox1Var = checkbox1;
|
||||||
setPermission('qAPPAutoLists', checkbox1)
|
setPermission("qAPPAutoLists", checkbox1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (acceptedVar || skip) {
|
if (acceptedVar || skip) {
|
||||||
const url = await createEndpoint(`/lists/${data.list_name}`);
|
const url = await createEndpoint(`/lists/${data.list_name}`);
|
||||||
console.log("url", url);
|
console.log("url", url);
|
||||||
@ -255,7 +256,7 @@ export const getListItems = async (data) => {
|
|||||||
if (!response.ok) throw new Error("Failed to fetch");
|
if (!response.ok) throw new Error("Failed to fetch");
|
||||||
|
|
||||||
const list = await response.json();
|
const list = await response.json();
|
||||||
console.log('list', list)
|
console.log("list", list);
|
||||||
return list;
|
return list;
|
||||||
} else {
|
} else {
|
||||||
throw new Error("User declined to share list");
|
throw new Error("User declined to share list");
|
||||||
@ -282,9 +283,9 @@ export const addListItems = async (data) => {
|
|||||||
const resPermission = await getUserPermission({
|
const resPermission = await getUserPermission({
|
||||||
text1: "Do you give this application permission to",
|
text1: "Do you give this application permission to",
|
||||||
text2: `Add the following to the list ${list_name}:`,
|
text2: `Add the following to the list ${list_name}:`,
|
||||||
text3: items.join(', ')
|
highlightedText: items.join(", "),
|
||||||
});
|
});
|
||||||
const {accepted} = resPermission
|
const { accepted } = resPermission;
|
||||||
|
|
||||||
if (accepted) {
|
if (accepted) {
|
||||||
const url = await createEndpoint(`/lists/${list_name}`);
|
const url = await createEndpoint(`/lists/${list_name}`);
|
||||||
@ -309,64 +310,171 @@ export const addListItems = async (data) => {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
res = await response.text();
|
res = await response.text();
|
||||||
}
|
}
|
||||||
return res
|
return res;
|
||||||
} else {
|
} else {
|
||||||
throw new Error("User declined add to list");
|
throw new Error("User declined add to list");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteListItems = async (data) => {
|
export const deleteListItems = async (data) => {
|
||||||
const requiredFields = ['list_name', 'item']
|
const requiredFields = ["list_name", "item"];
|
||||||
const missingFields: string[] = [];
|
const missingFields: string[] = [];
|
||||||
requiredFields.forEach((field) => {
|
requiredFields.forEach((field) => {
|
||||||
if (!data[field]) {
|
if (!data[field]) {
|
||||||
missingFields.push(field);
|
missingFields.push(field);
|
||||||
}
|
|
||||||
});
|
|
||||||
if (missingFields.length > 0) {
|
|
||||||
const missingFieldsString = missingFields.join(", ");
|
|
||||||
const errorMsg = `Missing fields: ${missingFieldsString}`;
|
|
||||||
throw new Error(errorMsg);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
if (missingFields.length > 0) {
|
||||||
|
const missingFieldsString = missingFields.join(", ");
|
||||||
|
const errorMsg = `Missing fields: ${missingFieldsString}`;
|
||||||
|
throw new Error(errorMsg);
|
||||||
|
}
|
||||||
|
|
||||||
const item = data.item;
|
const item = data.item;
|
||||||
const list_name = data.list_name;
|
const list_name = data.list_name;
|
||||||
|
|
||||||
const resPermission = await getUserPermission({
|
const resPermission = await getUserPermission({
|
||||||
text1: "Do you give this application permission to",
|
text1: "Do you give this application permission to",
|
||||||
text2: `Remove the following from the list ${list_name}:`,
|
text2: `Remove the following from the list ${list_name}:`,
|
||||||
text3: item
|
highlightedText: item,
|
||||||
|
});
|
||||||
|
const { accepted } = resPermission;
|
||||||
|
|
||||||
|
if (accepted) {
|
||||||
|
const url = await createEndpoint(`/lists/${list_name}`);
|
||||||
|
console.log("url", url);
|
||||||
|
const body = {
|
||||||
|
items: [item],
|
||||||
|
};
|
||||||
|
const bodyToString = JSON.stringify(body);
|
||||||
|
const response = await fetch(url, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: bodyToString,
|
||||||
});
|
});
|
||||||
const {accepted} = resPermission
|
|
||||||
|
|
||||||
if (accepted) {
|
console.log("response", response);
|
||||||
const url = await createEndpoint(`/lists/${list_name}`);
|
if (!response.ok) throw new Error("Failed to add to list");
|
||||||
console.log("url", url);
|
let res;
|
||||||
const body = {
|
try {
|
||||||
items: [item],
|
res = await response.clone().json();
|
||||||
};
|
} catch (e) {
|
||||||
const bodyToString = JSON.stringify(body);
|
res = await response.text();
|
||||||
const response = await fetch(url, {
|
}
|
||||||
method: "DELETE",
|
return res;
|
||||||
headers: {
|
} else {
|
||||||
"Content-Type": "application/json",
|
throw new Error("User declined add to list");
|
||||||
},
|
}
|
||||||
body: bodyToString,
|
};
|
||||||
|
|
||||||
|
export const publishQDNResource = async (data: any) => {
|
||||||
|
const requiredFields = ["service"];
|
||||||
|
const missingFields: string[] = [];
|
||||||
|
requiredFields.forEach((field) => {
|
||||||
|
if (!data[field]) {
|
||||||
|
missingFields.push(field);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (missingFields.length > 0) {
|
||||||
|
const missingFieldsString = missingFields.join(", ");
|
||||||
|
const errorMsg = `Missing fields: ${missingFieldsString}`;
|
||||||
|
throw new Error(errorMsg);
|
||||||
|
}
|
||||||
|
if (!data.file && !data.data64) {
|
||||||
|
throw new Error("No data or file was submitted");
|
||||||
|
}
|
||||||
|
// Use "default" if user hasn't specified an identifer
|
||||||
|
const service = data.service;
|
||||||
|
const registeredName = await getNameInfo();
|
||||||
|
const name = registeredName;
|
||||||
|
let identifier = data.identifier;
|
||||||
|
let data64 = data.data64;
|
||||||
|
const filename = data.filename;
|
||||||
|
const title = data.title;
|
||||||
|
const description = data.description;
|
||||||
|
const category = data.category;
|
||||||
|
const tag1 = data.tag1;
|
||||||
|
const tag2 = data.tag2;
|
||||||
|
const tag3 = data.tag3;
|
||||||
|
const tag4 = data.tag4;
|
||||||
|
const tag5 = data.tag5;
|
||||||
|
let feeAmount = null;
|
||||||
|
if (data.identifier == null) {
|
||||||
|
identifier = "default";
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
data.encrypt &&
|
||||||
|
(!data.publicKeys ||
|
||||||
|
(Array.isArray(data.publicKeys) && data.publicKeys.length === 0))
|
||||||
|
) {
|
||||||
|
throw new Error("Encrypting data requires public keys");
|
||||||
|
}
|
||||||
|
if (!data.encrypt && data.service.endsWith("_PRIVATE")) {
|
||||||
|
throw new Error("Only encrypted data can go into private services");
|
||||||
|
}
|
||||||
|
if (data.file) {
|
||||||
|
data64 = await fileToBase64(data.file);
|
||||||
|
}
|
||||||
|
if (data.encrypt) {
|
||||||
|
try {
|
||||||
|
const encryptDataResponse = encryptDataGroup({
|
||||||
|
data64,
|
||||||
|
publicKeys: data.publicKeys,
|
||||||
});
|
});
|
||||||
|
if (encryptDataResponse) {
|
||||||
console.log("response", response);
|
data64 = encryptDataResponse;
|
||||||
if (!response.ok) throw new Error("Failed to add to list");
|
|
||||||
let res;
|
|
||||||
try {
|
|
||||||
res = await response.clone().json();
|
|
||||||
} catch (e) {
|
|
||||||
res = await response.text();
|
|
||||||
}
|
}
|
||||||
return res
|
} catch (error) {
|
||||||
} else {
|
throw new Error(
|
||||||
throw new Error("User declined add to list");
|
error.message || "Upload failed due to failed encryption"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
const fee = await getFee('ARBITRARY')
|
||||||
|
|
||||||
|
const resPermission = await getUserPermission({
|
||||||
|
text1: "Do you give this application permission to publish to QDN?",
|
||||||
|
text2: `service: ${service}`,
|
||||||
|
text3: `identifier: ${identifier || null}`,
|
||||||
|
highlightedText: `isEncrypted: ${!!data.encrypt}`,
|
||||||
|
fee: fee.fee
|
||||||
|
});
|
||||||
|
const { accepted } = resPermission;
|
||||||
|
if (accepted) {
|
||||||
|
if (data.file && !data.encrypt) {
|
||||||
|
data64 = await fileToBase64(data.file);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const resPublish = await publishData({
|
||||||
|
registeredName: encodeURIComponent(name),
|
||||||
|
file: data64,
|
||||||
|
service: service,
|
||||||
|
identifier: encodeURIComponent(identifier),
|
||||||
|
uploadType: "file",
|
||||||
|
isBase64: true,
|
||||||
|
filename: filename,
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
category,
|
||||||
|
tag1,
|
||||||
|
tag2,
|
||||||
|
tag3,
|
||||||
|
tag4,
|
||||||
|
tag5,
|
||||||
|
apiVersion: 2,
|
||||||
|
withFee: true,
|
||||||
|
});
|
||||||
|
return resPublish;
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(error?.message || "Upload failed");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error("User declined request");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const sendCoin = async () => {
|
export const sendCoin = async () => {
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user