mirror of
https://github.com/Qortal/qortal-ui.git
synced 2025-04-24 03:47:53 +00:00
add publish-data function
This commit is contained in:
parent
43169de98b
commit
48af282ba0
@ -9,6 +9,8 @@ registerTranslateConfig({
|
|||||||
|
|
||||||
import '@material/mwc-button'
|
import '@material/mwc-button'
|
||||||
import '@material/mwc-icon'
|
import '@material/mwc-icon'
|
||||||
|
import WebWorker from 'web-worker:./computePowWorkerFile.src.js';
|
||||||
|
import {publishData} from '../../../utils/publish-image.js';
|
||||||
|
|
||||||
const parentEpml = new Epml({ type: 'WINDOW', source: window.parent })
|
const parentEpml = new Epml({ type: 'WINDOW', source: window.parent })
|
||||||
|
|
||||||
@ -277,12 +279,42 @@ class WebBrowser extends LitElement {
|
|||||||
|
|
||||||
case actions.PUBLISH_QDN_RESOURCE:
|
case actions.PUBLISH_QDN_RESOURCE:
|
||||||
// Use "default" if user hasn't specified an identifer
|
// Use "default" if user hasn't specified an identifer
|
||||||
if (data.identifier == null) {
|
const service = data.service
|
||||||
data.identifier = "default";
|
const name = data.name
|
||||||
|
let identifier = data.identifier
|
||||||
|
const data64 = data.data64
|
||||||
|
if(!service || !name || !data64){
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
if (data.identifier == null) {
|
||||||
|
identifier = "default";
|
||||||
|
}
|
||||||
|
|
||||||
console.log('hello')
|
console.log('hello')
|
||||||
const result = await showModalAndWait(actions.PUBLISH_QDN_RESOURCE);
|
const result = await showModalAndWait(actions.PUBLISH_QDN_RESOURCE);
|
||||||
|
console.log({result})
|
||||||
if (result.action === 'accept') {
|
if (result.action === 'accept') {
|
||||||
|
const worker = new WebWorker();
|
||||||
|
console.log({worker})
|
||||||
|
try {
|
||||||
|
await publishData({
|
||||||
|
registeredName: name,
|
||||||
|
file: data64,
|
||||||
|
service: service,
|
||||||
|
identifier: identifier,
|
||||||
|
parentEpml,
|
||||||
|
uploadType: 'file',
|
||||||
|
selectedAddress: this.selectedAddress,
|
||||||
|
worker: worker,
|
||||||
|
isBase64: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
worker.terminate();
|
||||||
|
} catch (error) {
|
||||||
|
worker.terminate();
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
console.log('User accepted:', result.userData);
|
console.log('User accepted:', result.userData);
|
||||||
} else if (result.action === 'reject') {
|
} else if (result.action === 'reject') {
|
||||||
console.log('User rejected');
|
console.log('User rejected');
|
||||||
|
@ -0,0 +1,92 @@
|
|||||||
|
import { Sha256 } from 'asmcrypto.js'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function sbrk(size, heap){
|
||||||
|
let brk = 512 * 1024 // stack top
|
||||||
|
let old = brk
|
||||||
|
brk += size
|
||||||
|
|
||||||
|
if (brk > heap.length)
|
||||||
|
throw new Error('heap exhausted')
|
||||||
|
|
||||||
|
return old
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
self.addEventListener('message', async e => {
|
||||||
|
const response = await computePow(e.data.convertedBytes, e.data.path)
|
||||||
|
postMessage(response)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
const memory = new WebAssembly.Memory({ initial: 256, maximum: 256 })
|
||||||
|
const heap = new Uint8Array(memory.buffer)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const computePow = async (convertedBytes, path) => {
|
||||||
|
|
||||||
|
|
||||||
|
let response = null
|
||||||
|
|
||||||
|
await new Promise((resolve, reject)=> {
|
||||||
|
|
||||||
|
const _convertedBytesArray = Object.keys(convertedBytes).map(
|
||||||
|
function (key) {
|
||||||
|
return convertedBytes[key]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const convertedBytesArray = new Uint8Array(_convertedBytesArray)
|
||||||
|
const convertedBytesHash = new Sha256()
|
||||||
|
.process(convertedBytesArray)
|
||||||
|
.finish().result
|
||||||
|
const hashPtr = sbrk(32, heap)
|
||||||
|
const hashAry = new Uint8Array(
|
||||||
|
memory.buffer,
|
||||||
|
hashPtr,
|
||||||
|
32
|
||||||
|
)
|
||||||
|
|
||||||
|
hashAry.set(convertedBytesHash)
|
||||||
|
const difficulty = 14
|
||||||
|
const workBufferLength = 8 * 1024 * 1024
|
||||||
|
const workBufferPtr = sbrk(
|
||||||
|
workBufferLength,
|
||||||
|
heap
|
||||||
|
)
|
||||||
|
|
||||||
|
const importObject = {
|
||||||
|
env: {
|
||||||
|
memory: memory
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
function loadWebAssembly(filename, imports) {
|
||||||
|
return fetch(filename)
|
||||||
|
.then(response => response.arrayBuffer())
|
||||||
|
.then(buffer => WebAssembly.compile(buffer))
|
||||||
|
.then(module => {
|
||||||
|
return new WebAssembly.Instance(module, importObject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
loadWebAssembly(path)
|
||||||
|
.then(wasmModule => {
|
||||||
|
response = {
|
||||||
|
nonce : wasmModule.exports.compute2(hashPtr, workBufferPtr, workBufferLength, difficulty),
|
||||||
|
|
||||||
|
}
|
||||||
|
resolve()
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
return response
|
||||||
|
}
|
@ -16,7 +16,9 @@ export const publishData = async ({
|
|||||||
parentEpml,
|
parentEpml,
|
||||||
uploadType,
|
uploadType,
|
||||||
selectedAddress,
|
selectedAddress,
|
||||||
worker
|
worker,
|
||||||
|
isBase64,
|
||||||
|
metaData
|
||||||
}) => {
|
}) => {
|
||||||
const validateName = async (receiverName) => {
|
const validateName = async (receiverName) => {
|
||||||
let nameRes = await parentEpml.request("apiCall", {
|
let nameRes = await parentEpml.request("apiCall", {
|
||||||
@ -115,16 +117,26 @@ export const publishData = async ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Base64 encode the file to work around compatibility issues between javascript and java byte arrays
|
// Base64 encode the file to work around compatibility issues between javascript and java byte arrays
|
||||||
|
if(isBase64){
|
||||||
|
postBody = file
|
||||||
|
}
|
||||||
|
if(!isBase64){
|
||||||
let fileBuffer = new Uint8Array(await file.arrayBuffer())
|
let fileBuffer = new Uint8Array(await file.arrayBuffer())
|
||||||
postBody = Buffer.from(fileBuffer).toString("base64")
|
postBody = Buffer.from(fileBuffer).toString("base64")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let uploadDataUrl = `/arbitrary/${service}/${registeredName}${urlSuffix}?apiKey=${getApiKey()}`
|
let uploadDataUrl = `/arbitrary/${service}/${registeredName}${urlSuffix}?apiKey=${getApiKey()}`
|
||||||
if (identifier != null && identifier.trim().length > 0) {
|
if (identifier != null && identifier.trim().length > 0) {
|
||||||
uploadDataUrl = `/arbitrary/${service}/${registeredName}/${identifier}${urlSuffix}?apiKey=${getApiKey()}`
|
uploadDataUrl = `/arbitrary/${service}/${registeredName}/${identifier}${urlSuffix}?apiKey=${getApiKey()}`
|
||||||
|
|
||||||
|
if(metaData){
|
||||||
|
uploadDataUrl = `/arbitrary/${service}/${registeredName}/${identifier}${urlSuffix}?${metaData}&apiKey=${getApiKey()}`
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let uploadDataRes = await parentEpml.request("apiCall", {
|
let uploadDataRes = await parentEpml.request("apiCall", {
|
||||||
type: "api",
|
type: "api",
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user