mirror of
https://github.com/Qortal/chrome-extension.git
synced 2025-02-14 11:15:49 +00:00
draft completion
This commit is contained in:
parent
62aed9fea2
commit
f6c0afbc40
@ -216,5 +216,13 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
|
||||
type: "LOGOUT",
|
||||
from: 'qortal'
|
||||
}, "*");
|
||||
} else if (message.type === "RESPONSE_FOR_TRADES") {
|
||||
// Notify the web page
|
||||
window.postMessage({
|
||||
type: "RESPONSE_FOR_TRADES",
|
||||
from: 'qortal',
|
||||
payload: message.message
|
||||
}, "*");
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -22,6 +22,9 @@ const apiEndpoints = [
|
||||
"https://apinode4.qortalnodes.live",
|
||||
];
|
||||
|
||||
const buyTradeNodeBaseUrl = 'https://appnode.qortal.org'
|
||||
const proxyAccountAddress = 'QXPejUe5Za1KD3zCMViWCX35AreMQ9H7ku'
|
||||
const proxyAccountPublicKey = '5hP6stDWybojoDw5t8z9D51nV945oMPX7qBd29rhX1G7'
|
||||
const pendingResponses = new Map();
|
||||
|
||||
// Function to check each API endpoint
|
||||
@ -109,9 +112,8 @@ async function connection(hostname) {
|
||||
}
|
||||
|
||||
async function getTradeInfo(qortalAtAddress) {
|
||||
const validApi = await findUsableApi();
|
||||
const response = await fetch(validApi + "/crosschain/trade/" + qortalAtAddress);
|
||||
|
||||
const response = await fetch(buyTradeNodeBaseUrl + "/crosschain/trade/" + qortalAtAddress);
|
||||
console.log({})
|
||||
if (!response?.ok) throw new Error("Cannot crosschain trade information");
|
||||
const data = await response.json();
|
||||
return data;
|
||||
@ -296,6 +298,7 @@ async function decryptWallet({password, wallet, walletVersion}) {
|
||||
}
|
||||
|
||||
async function signChatFunc(chatBytesArray, chatNonce, validApi, keyPair) {
|
||||
console.log({ chatBytesArray, chatNonce, validApi, keyPair })
|
||||
let response
|
||||
try {
|
||||
const signedChatBytes = signChat(
|
||||
@ -376,11 +379,13 @@ async function sendChat({qortAddress, recipientPublicKey, message }){
|
||||
publicKey: uint8PublicKey
|
||||
};
|
||||
const difficulty = 8;
|
||||
const callRequest = `curl -X 'POST' 'http://localhost:12391/crosschain/tradebot/respond' -H 'accept: text/plain' -H 'X-API-KEY: keykeykeykey' -H 'Content-Type: application/json' -d '{ "atAddress": "${message.atAddress}", "foreignKey": "${message.foreignKey}", "receivingAddress": "${message.receivingAddress}" }'`;
|
||||
|
||||
// Construct the final JSON object
|
||||
const jsonData = {
|
||||
atAddress: message.atAddress,
|
||||
foreignKey: message.foreignKey,
|
||||
receivingAddress: message.receivingAddress
|
||||
};
|
||||
const finalJson = {
|
||||
callRequest: callRequest,
|
||||
callRequest: jsonData,
|
||||
extra: "whatever additional data goes here"
|
||||
};
|
||||
const messageStringified = JSON.stringify(finalJson)
|
||||
@ -409,6 +414,9 @@ const finalJson = {
|
||||
let _response = await signChatFunc(chatBytesArray,
|
||||
nonce, "https://appnode.qortal.org", keyPair
|
||||
)
|
||||
if (_response?.error) {
|
||||
throw new Error(_response?.message)
|
||||
}
|
||||
return _response
|
||||
}
|
||||
|
||||
@ -424,16 +432,17 @@ async function createBuyOrderTx({crosschainAtInfo}){
|
||||
foreignKey: parsedData.ltcPrivateKey,
|
||||
receivingAddress: address
|
||||
}
|
||||
const res = await sendChat({qortAddress: "QXPejUe5Za1KD3zCMViWCX35AreMQ9H7ku", recipientPublicKey: "5hP6stDWybojoDw5t8z9D51nV945oMPX7qBd29rhX1G7", message })
|
||||
const res = await sendChat({ qortAddress: proxyAccountAddress, recipientPublicKey: proxyAccountPublicKey, message })
|
||||
if (res?.signature) {
|
||||
const decryptedMessage = await listenForChatMessageForBuyOrder({
|
||||
nodeBaseUrl: "https://appnode.qortal.org",
|
||||
senderAddress: "QXPejUe5Za1KD3zCMViWCX35AreMQ9H7ku",
|
||||
senderPublicKey: "5hP6stDWybojoDw5t8z9D51nV945oMPX7qBd29rhX1G7",
|
||||
|
||||
listenForChatMessageForBuyOrder({
|
||||
nodeBaseUrl: buyTradeNodeBaseUrl,
|
||||
senderAddress: proxyAccountAddress,
|
||||
senderPublicKey: proxyAccountPublicKey,
|
||||
signature: res?.signature,
|
||||
|
||||
})
|
||||
return decryptedMessage
|
||||
return { atAddress: crosschainAtInfo.qortalAtAddress, chatSignature: res?.signature, node: buyTradeNodeBaseUrl }
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
@ -513,6 +522,58 @@ function fetchMessages(apiCall) {
|
||||
});
|
||||
}
|
||||
|
||||
function fetchMessagesForBuyOrders(apiCall, signature, senderPublicKey) {
|
||||
let retryDelay = 2000; // Start with a 2-second delay
|
||||
const maxDuration = 360000 * 2; // Maximum duration set to 12 minutes
|
||||
const startTime = Date.now(); // Record the start time
|
||||
let triedChatMessage = []
|
||||
// Promise to handle polling logic
|
||||
return new Promise((resolve, reject) => {
|
||||
const attemptFetch = async () => {
|
||||
if (Date.now() - startTime > maxDuration) {
|
||||
return reject(new Error("Maximum polling time exceeded"));
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(apiCall);
|
||||
let data = await response.json();
|
||||
data = data.filter((item) => !triedChatMessage.includes(item.signature))
|
||||
console.log({data})
|
||||
if (data && data.length > 0) {
|
||||
const encodedMessageObj = data[0]
|
||||
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 decodedMessage = decryptChatMessage(encodedMessageObj.data, keyPair.privateKey, senderPublicKey, encodedMessageObj.reference)
|
||||
const parsedMessage = JSON.parse(decodedMessage)
|
||||
console.log({parsedMessage})
|
||||
if (parsedMessage?.extra?.chatRequestSignature === signature) {
|
||||
resolve(parsedMessage);
|
||||
} else {
|
||||
triedChatMessage.push(encodedMessageObj.signature)
|
||||
setTimeout(attemptFetch, retryDelay);
|
||||
retryDelay = Math.min(retryDelay * 2, 360000); // Ensure delay does not exceed 6 minutes
|
||||
}
|
||||
// Resolve the promise when data is found
|
||||
} else {
|
||||
setTimeout(attemptFetch, retryDelay);
|
||||
retryDelay = Math.min(retryDelay * 2, 360000); // Ensure delay does not exceed 6 minutes
|
||||
}
|
||||
} catch (error) {
|
||||
reject(error); // Reject the promise on error
|
||||
}
|
||||
};
|
||||
|
||||
attemptFetch(); // Initial call to start the polling
|
||||
});
|
||||
}
|
||||
|
||||
async function listenForChatMessage({ nodeBaseUrl, senderAddress, senderPublicKey, timestamp }) {
|
||||
try {
|
||||
let validApi = "";
|
||||
@ -564,20 +625,24 @@ async function listenForChatMessageForBuyOrder({ nodeBaseUrl, senderAddress, sen
|
||||
const before = Date.now() + 1200000
|
||||
const after = Date.now()
|
||||
const apiCall = `${validApi}/chat/messages?involving=${senderAddress}&involving=${address}&reverse=true&limit=1&before=${before}&after=${after}`;
|
||||
const encodedMessageObj = await fetchMessages(apiCall)
|
||||
const parsedMessageObj = await fetchMessagesForBuyOrders(apiCall, signature, senderPublicKey)
|
||||
|
||||
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 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 decodedMessage = decryptChatMessage(encodedMessageObj.data, keyPair.privateKey, senderPublicKey, encodedMessageObj.reference)
|
||||
const parsedMessage = JSON.parse(decodedMessage)
|
||||
return parsedMessage
|
||||
// const decodedMessage = decryptChatMessage(encodedMessageObj.data, keyPair.privateKey, senderPublicKey, encodedMessageObj.reference)
|
||||
// const parsedMessage = JSON.parse(decodedMessage)
|
||||
chrome.tabs.query({}, function (tabs) {
|
||||
tabs.forEach(tab => {
|
||||
chrome.tabs.sendMessage(tab.id, { type: "RESPONSE_FOR_TRADES", message: parsedMessageObj });
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
throw new Error(error.message);
|
||||
|
@ -4,7 +4,6 @@ import nacl from '../deps/nacl-fast'
|
||||
import utils from '../utils/utils'
|
||||
|
||||
export const signChat = (chatBytes, nonce, keyPair) => {
|
||||
|
||||
if (!chatBytes) {
|
||||
throw new Error('Chat Bytes not defined')
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user