2022-10-31 18:56:45 +02:00
|
|
|
const getApiKey = () => {
|
|
|
|
const myNode =
|
|
|
|
window.parent.reduxStore.getState().app.nodeConfig.knownNodes[
|
|
|
|
window.parent.reduxStore.getState().app.nodeConfig.node
|
|
|
|
]
|
|
|
|
let apiKey = myNode.apiKey
|
|
|
|
return apiKey
|
|
|
|
}
|
|
|
|
|
|
|
|
export const publishData = async ({
|
|
|
|
registeredName,
|
|
|
|
path,
|
|
|
|
file,
|
|
|
|
service,
|
|
|
|
identifier,
|
|
|
|
parentEpml,
|
|
|
|
uploadType,
|
|
|
|
selectedAddress,
|
2023-02-18 19:17:50 +02:00
|
|
|
worker,
|
|
|
|
isBase64,
|
2023-02-24 14:56:52 +00:00
|
|
|
metaData,
|
|
|
|
apiVersion
|
2022-10-31 18:56:45 +02:00
|
|
|
}) => {
|
|
|
|
const validateName = async (receiverName) => {
|
|
|
|
let nameRes = await parentEpml.request("apiCall", {
|
|
|
|
type: "api",
|
|
|
|
url: `/names/${receiverName}`,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nameRes
|
|
|
|
}
|
|
|
|
|
|
|
|
const convertBytesForSigning = async (transactionBytesBase58) => {
|
|
|
|
let convertedBytes = await parentEpml.request("apiCall", {
|
|
|
|
type: "api",
|
|
|
|
method: "POST",
|
|
|
|
url: `/transactions/convert`,
|
|
|
|
body: `${transactionBytesBase58}`,
|
|
|
|
})
|
|
|
|
return convertedBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
const signAndProcess = async (transactionBytesBase58) => {
|
|
|
|
let convertedBytesBase58 = await convertBytesForSigning(
|
|
|
|
transactionBytesBase58
|
|
|
|
)
|
|
|
|
if (convertedBytesBase58.error) {
|
2023-02-18 19:35:01 +02:00
|
|
|
throw new Error('Error when signing');
|
2022-10-31 18:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const convertedBytes =
|
|
|
|
window.parent.Base58.decode(convertedBytesBase58)
|
2022-11-07 23:10:17 +02:00
|
|
|
let nonce = null
|
|
|
|
const computPath =window.parent.location.origin + '/memory-pow/memory-pow.wasm.full'
|
|
|
|
await new Promise((res, rej) => {
|
|
|
|
|
|
|
|
worker.postMessage({convertedBytes, path: computPath});
|
|
|
|
|
|
|
|
worker.onmessage = e => {
|
|
|
|
|
|
|
|
worker.terminate()
|
|
|
|
|
|
|
|
nonce = e.data.nonce
|
|
|
|
res()
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2022-10-31 18:56:45 +02:00
|
|
|
let response = await parentEpml.request("sign_arbitrary", {
|
|
|
|
nonce: selectedAddress.nonce,
|
|
|
|
arbitraryBytesBase58: transactionBytesBase58,
|
|
|
|
arbitraryBytesForSigningBase58: convertedBytesBase58,
|
|
|
|
arbitraryNonce: nonce,
|
2023-02-24 14:56:52 +00:00
|
|
|
apiVersion: apiVersion ? apiVersion : null
|
2022-10-31 18:56:45 +02:00
|
|
|
})
|
|
|
|
let myResponse = { error: "" }
|
|
|
|
if (response === false) {
|
2023-02-18 19:35:01 +02:00
|
|
|
throw new Error('Error when signing');
|
2022-10-31 18:56:45 +02:00
|
|
|
} else {
|
|
|
|
myResponse = response
|
|
|
|
}
|
|
|
|
|
|
|
|
return myResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
const validate = async () => {
|
|
|
|
let validNameRes = await validateName(registeredName)
|
|
|
|
if (validNameRes.error) {
|
2023-02-18 19:35:01 +02:00
|
|
|
throw new Error('Name not found');
|
2022-10-31 18:56:45 +02:00
|
|
|
}
|
|
|
|
let transactionBytes = await uploadData(registeredName, path, file)
|
|
|
|
if (transactionBytes.error) {
|
2023-02-18 19:35:01 +02:00
|
|
|
throw new Error('Error when uploading');
|
2022-10-31 18:56:45 +02:00
|
|
|
} else if (
|
|
|
|
transactionBytes.includes("Error 500 Internal Server Error")
|
|
|
|
) {
|
2023-02-18 19:35:01 +02:00
|
|
|
throw new Error('Error when uploading');
|
2022-10-31 18:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let signAndProcessRes = await signAndProcess(transactionBytes)
|
|
|
|
if (signAndProcessRes.error) {
|
2023-02-18 19:35:01 +02:00
|
|
|
throw new Error('Error when signing');
|
2022-10-31 18:56:45 +02:00
|
|
|
}
|
2023-02-18 19:35:01 +02:00
|
|
|
return signAndProcessRes
|
2022-10-31 18:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const uploadData = async (registeredName, path, file) => {
|
|
|
|
if (identifier != null && identifier.trim().length > 0) {
|
|
|
|
let postBody = path
|
|
|
|
let urlSuffix = ""
|
|
|
|
if (file != null) {
|
|
|
|
// If we're sending zipped data, make sure to use the /zip version of the POST /arbitrary/* API
|
|
|
|
if (uploadType === "zip") {
|
|
|
|
urlSuffix = "/zip"
|
|
|
|
}
|
|
|
|
// If we're sending file data, use the /base64 version of the POST /arbitrary/* API
|
|
|
|
else if (uploadType === "file") {
|
|
|
|
urlSuffix = "/base64"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Base64 encode the file to work around compatibility issues between javascript and java byte arrays
|
2023-02-18 19:17:50 +02:00
|
|
|
if(isBase64){
|
|
|
|
postBody = file
|
|
|
|
}
|
|
|
|
if(!isBase64){
|
2022-10-31 18:56:45 +02:00
|
|
|
let fileBuffer = new Uint8Array(await file.arrayBuffer())
|
|
|
|
postBody = Buffer.from(fileBuffer).toString("base64")
|
2023-02-18 19:17:50 +02:00
|
|
|
}
|
|
|
|
|
2022-10-31 18:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let uploadDataUrl = `/arbitrary/${service}/${registeredName}${urlSuffix}?apiKey=${getApiKey()}`
|
|
|
|
if (identifier != null && identifier.trim().length > 0) {
|
|
|
|
uploadDataUrl = `/arbitrary/${service}/${registeredName}/${identifier}${urlSuffix}?apiKey=${getApiKey()}`
|
2023-02-18 19:17:50 +02:00
|
|
|
|
|
|
|
if(metaData){
|
|
|
|
uploadDataUrl = `/arbitrary/${service}/${registeredName}/${identifier}${urlSuffix}?${metaData}&apiKey=${getApiKey()}`
|
|
|
|
|
|
|
|
}
|
2022-10-31 18:56:45 +02:00
|
|
|
}
|
2023-02-18 19:17:50 +02:00
|
|
|
|
2022-10-31 18:56:45 +02:00
|
|
|
let uploadDataRes = await parentEpml.request("apiCall", {
|
|
|
|
type: "api",
|
|
|
|
method: "POST",
|
|
|
|
url: `${uploadDataUrl}`,
|
|
|
|
body: `${postBody}`,
|
|
|
|
})
|
|
|
|
return uploadDataRes
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 23:10:17 +02:00
|
|
|
try {
|
2023-02-23 23:20:15 +00:00
|
|
|
const validateRes = await validate()
|
|
|
|
return validateRes
|
2022-11-07 23:10:17 +02:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error.message)
|
|
|
|
}
|
|
|
|
|
2022-10-31 18:56:45 +02:00
|
|
|
}
|