mirror of
https://github.com/Qortal/qortal-ui.git
synced 2025-02-12 02:05:51 +00:00
add publicize transaction in background
This commit is contained in:
parent
1d6df0cd0f
commit
9b46a13d9a
@ -77,7 +77,8 @@
|
|||||||
"rollup-plugin-postcss": "4.0.2",
|
"rollup-plugin-postcss": "4.0.2",
|
||||||
"rollup-plugin-progress": "1.1.2",
|
"rollup-plugin-progress": "1.1.2",
|
||||||
"rollup-plugin-scss": "3.0.0",
|
"rollup-plugin-scss": "3.0.0",
|
||||||
"rollup-plugin-terser": "7.0.2"
|
"rollup-plugin-terser": "7.0.2",
|
||||||
|
"rollup-plugin-web-worker-loader": "^1.6.1"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=16.15.0"
|
"node": ">=16.15.0"
|
||||||
|
@ -3,6 +3,8 @@ import { connect } from 'pwa-helpers'
|
|||||||
import { store } from '../store.js'
|
import { store } from '../store.js'
|
||||||
import { doPageUrl } from '../redux/app/app-actions.js'
|
import { doPageUrl } from '../redux/app/app-actions.js'
|
||||||
import { translate, translateUnsafeHTML } from 'lit-translate'
|
import { translate, translateUnsafeHTML } from 'lit-translate'
|
||||||
|
import WebWorker from 'web-worker:./computePowWorker.js';
|
||||||
|
import { routes } from '../plugins/routes.js';
|
||||||
|
|
||||||
import '@material/mwc-icon'
|
import '@material/mwc-icon'
|
||||||
import '@material/mwc-button'
|
import '@material/mwc-button'
|
||||||
@ -94,6 +96,8 @@ class AppInfo extends connect(store)(LitElement) {
|
|||||||
this.nodeStatus = {}
|
this.nodeStatus = {}
|
||||||
this.pageUrl = ''
|
this.pageUrl = ''
|
||||||
this.theme = localStorage.getItem('qortalTheme') ? localStorage.getItem('qortalTheme') : 'light'
|
this.theme = localStorage.getItem('qortalTheme') ? localStorage.getItem('qortalTheme') : 'light'
|
||||||
|
this.publicKeyisOnChainConfirmation = false
|
||||||
|
this.interval
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@ -107,9 +111,109 @@ class AppInfo extends connect(store)(LitElement) {
|
|||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async confirmPublicKeyOnChain(address) {
|
||||||
|
const _computePow2 = async (chatBytes) => {
|
||||||
|
const difficulty = 15;
|
||||||
|
const path = window.parent.location.origin + '/memory-pow/memory-pow.wasm.full'
|
||||||
|
const worker = new WebWorker();
|
||||||
|
let nonce = null
|
||||||
|
let chatBytesArray = null
|
||||||
|
await new Promise((res, rej) => {
|
||||||
|
worker.postMessage({chatBytes, path, difficulty});
|
||||||
|
|
||||||
|
worker.onmessage = e => {
|
||||||
|
worker.terminate()
|
||||||
|
chatBytesArray = e.data.chatBytesArray
|
||||||
|
nonce = e.data.nonce
|
||||||
|
res()
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
let _response = await routes.sign_chat({
|
||||||
|
data: {
|
||||||
|
nonce: store.getState().app.selectedAddress.nonce,
|
||||||
|
chatBytesArray: chatBytesArray,
|
||||||
|
chatNonce: nonce
|
||||||
|
},
|
||||||
|
|
||||||
|
});
|
||||||
|
return _response
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
let stop = false
|
||||||
|
const checkPublicKey = async () => {
|
||||||
|
if (!stop) {
|
||||||
|
stop = true;
|
||||||
|
try {
|
||||||
|
const myNode = store.getState().app.nodeConfig.knownNodes[store.getState().app.nodeConfig.node];
|
||||||
|
const nodeUrl = myNode.protocol + '://' + myNode.domain + ':' + myNode.port;
|
||||||
|
const url = `${nodeUrl}/addresses/publickey/${address}`;
|
||||||
|
const res = await fetch(url)
|
||||||
|
let data = ''
|
||||||
|
try {
|
||||||
|
data = await res.text();
|
||||||
|
} catch (error) {
|
||||||
|
data = {
|
||||||
|
error: 'error'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(data === 'false'){
|
||||||
|
let _reference = new Uint8Array(64);
|
||||||
|
window.crypto.getRandomValues(_reference);
|
||||||
|
let reference = window.parent.Base58.encode(_reference);
|
||||||
|
const chatRes = await routes.chat({
|
||||||
|
data: {
|
||||||
|
type: 19,
|
||||||
|
nonce: store.getState().app.selectedAddress.nonce,
|
||||||
|
params: {
|
||||||
|
lastReference: reference,
|
||||||
|
proofOfWorkNonce: 0,
|
||||||
|
fee: 0,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
|
||||||
|
},
|
||||||
|
disableModal: true
|
||||||
|
},
|
||||||
|
disableModal: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const powRes = await _computePow2(chatRes)
|
||||||
|
if(powRes === true){
|
||||||
|
clearInterval(this.interval)
|
||||||
|
|
||||||
|
this.publicKeyisOnChainConfirmation = true
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data.error && data !== 'false' && data) {
|
||||||
|
clearInterval(this.interval)
|
||||||
|
|
||||||
|
this.publicKeyisOnChainConfirmation = true
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
}
|
||||||
|
stop = false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.interval = setInterval(checkPublicKey, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
firstUpdated() {
|
firstUpdated() {
|
||||||
this.getNodeInfo()
|
this.getNodeInfo()
|
||||||
this.getCoreInfo()
|
this.getCoreInfo()
|
||||||
|
try {
|
||||||
|
this.confirmPublicKeyOnChain(store.getState().app.selectedAddress.address)
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
this.getNodeInfo()
|
this.getNodeInfo()
|
||||||
|
82
qortal-ui-core/src/components/computePowWorker.js
Normal file
82
qortal-ui-core/src/components/computePowWorker.js
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
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.chatBytes, e.data.path, e.data.difficulty)
|
||||||
|
postMessage(response)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
const memory = new WebAssembly.Memory({ initial: 256, maximum: 256 })
|
||||||
|
const heap = new Uint8Array(memory.buffer)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const computePow = async (chatBytes, path, difficulty) => {
|
||||||
|
|
||||||
|
let response = null
|
||||||
|
|
||||||
|
await new Promise((resolve, reject)=> {
|
||||||
|
|
||||||
|
const _chatBytesArray = Object.keys(chatBytes).map(function (key) { return chatBytes[key]; });
|
||||||
|
const chatBytesArray = new Uint8Array(_chatBytesArray);
|
||||||
|
const chatBytesHash = new Sha256().process(chatBytesArray).finish().result;
|
||||||
|
const hashPtr = sbrk(32, heap);
|
||||||
|
const hashAry = new Uint8Array(memory.buffer, hashPtr, 32);
|
||||||
|
hashAry.set(chatBytesHash);
|
||||||
|
|
||||||
|
|
||||||
|
const workBufferLength = 8 * 1024 * 1024;
|
||||||
|
const workBufferPtr = sbrk(workBufferLength, heap);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const importObject = {
|
||||||
|
env: {
|
||||||
|
memory: memory
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
function loadWebAssembly(filename, imports) {
|
||||||
|
// Fetch the file and compile it
|
||||||
|
return fetch(filename)
|
||||||
|
.then(response => response.arrayBuffer())
|
||||||
|
.then(buffer => WebAssembly.compile(buffer))
|
||||||
|
.then(module => {
|
||||||
|
|
||||||
|
// Create the instance.
|
||||||
|
return new WebAssembly.Instance(module, importObject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
loadWebAssembly(path)
|
||||||
|
.then(wasmModule => {
|
||||||
|
response = {
|
||||||
|
nonce : wasmModule.exports.compute2(hashPtr, workBufferPtr, workBufferLength, difficulty),
|
||||||
|
chatBytesArray
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve()
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
return response
|
||||||
|
}
|
@ -7,6 +7,8 @@ const commonjs = require('@rollup/plugin-commonjs')
|
|||||||
const alias = require('@rollup/plugin-alias')
|
const alias = require('@rollup/plugin-alias')
|
||||||
const { terser } = require('rollup-plugin-terser')
|
const { terser } = require('rollup-plugin-terser')
|
||||||
const scss = require('rollup-plugin-scss')
|
const scss = require('rollup-plugin-scss')
|
||||||
|
const webWorkerLoader = require('rollup-plugin-web-worker-loader');
|
||||||
|
|
||||||
const generateES5BuildConfig = require('./generateES5BuildConfig')
|
const generateES5BuildConfig = require('./generateES5BuildConfig')
|
||||||
|
|
||||||
|
|
||||||
@ -61,6 +63,7 @@ const generateBuildConfig = ({ elementComponents, functionalComponents, otherOut
|
|||||||
commonjs(),
|
commonjs(),
|
||||||
globals(),
|
globals(),
|
||||||
progress(),
|
progress(),
|
||||||
|
webWorkerLoader(),
|
||||||
scss({
|
scss({
|
||||||
output: options.sassOutputDir
|
output: options.sassOutputDir
|
||||||
}),
|
}),
|
||||||
|
@ -6,6 +6,7 @@ const progress = require('rollup-plugin-progress');
|
|||||||
const { terser } = require("rollup-plugin-terser");
|
const { terser } = require("rollup-plugin-terser");
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const alias = require('@rollup/plugin-alias');
|
const alias = require('@rollup/plugin-alias');
|
||||||
|
const webWorkerLoader = require('rollup-plugin-web-worker-loader');
|
||||||
|
|
||||||
const generateRollupConfig = (file, { outputDir, aliases }) => {
|
const generateRollupConfig = (file, { outputDir, aliases }) => {
|
||||||
|
|
||||||
@ -36,6 +37,7 @@ const generateRollupConfig = (file, { outputDir, aliases }) => {
|
|||||||
}),
|
}),
|
||||||
commonjs(),
|
commonjs(),
|
||||||
progress(),
|
progress(),
|
||||||
|
webWorkerLoader(),
|
||||||
babel.babel({
|
babel.babel({
|
||||||
babelHelpers: 'bundled',
|
babelHelpers: 'bundled',
|
||||||
exclude: 'node_modules/**'
|
exclude: 'node_modules/**'
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
import ChatBase from "./chat/ChatBase.js"
|
import ChatBase from "./chat/ChatBase.js"
|
||||||
|
import { QORT_DECIMALS } from "../constants.js"
|
||||||
|
|
||||||
export default class PublicizeTransaction extends ChatBase {
|
export default class PublicizeTransaction extends ChatBase {
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -11,13 +12,17 @@ export default class PublicizeTransaction extends ChatBase {
|
|||||||
set proofOfWorkNonce(proofOfWorkNonce) {
|
set proofOfWorkNonce(proofOfWorkNonce) {
|
||||||
this._proofOfWorkNonce = this.constructor.utils.int32ToBytes(proofOfWorkNonce)
|
this._proofOfWorkNonce = this.constructor.utils.int32ToBytes(proofOfWorkNonce)
|
||||||
}
|
}
|
||||||
|
set fee(fee) {
|
||||||
|
this._fee = fee * QORT_DECIMALS
|
||||||
|
this._feeBytes = this.constructor.utils.int64ToBytes(this._fee)
|
||||||
|
}
|
||||||
get params() {
|
get params() {
|
||||||
const params = super.params;
|
const params = super.params;
|
||||||
params.push(
|
params.push(
|
||||||
this._proofOfWorkNonce,
|
this._proofOfWorkNonce,
|
||||||
this._feeBytes
|
this._feeBytes
|
||||||
)
|
)
|
||||||
|
console.log({params})
|
||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user