Minor improvements and bug fixes

This commit is contained in:
Sosthene 2024-10-14 13:22:27 +02:00
parent 1eab5efab8
commit 1f4153619a
2 changed files with 60 additions and 44 deletions

View File

@ -69,7 +69,7 @@ export default class Routing {
window.onclick = (event) => { window.onclick = (event) => {
const modal = document.getElementById('modal'); const modal = document.getElementById('modal');
if (event.target === modal) { if (event.target === modal) {
this.confirm(this.currentPrd || pcd, outpointCommitment || this.currentOutpoint); this.confirm(pcd, outpointCommitment);
} }
} }
} }
@ -84,12 +84,12 @@ export default class Routing {
if (modal) modal.style.display = 'none'; if (modal) modal.style.display = 'none';
} }
async confirm(prd?: any, outpointCommitment?: string) { async confirm(prd: any, outpointCommitment: string) {
const service = await Services.getInstance() const service = await Services.getInstance()
const modal = document.getElementById('modal') const modal = document.getElementById('modal')
console.log("🚀 ~ Routing ~ confirm ~ prd:", this.currentPrd || prd, outpointCommitment) console.log("🚀 ~ Routing ~ confirm ~ prd:", prd, outpointCommitment)
if (modal) modal.style.display = 'none'; if (modal) modal.style.display = 'none';
if(this.currentPrd || prd) service.pairDevice(this.currentPrd || prd, outpointCommitment || this.currentOutpoint as string) await service.pairDevice(prd, outpointCommitment);
} }
async closeConfirmationModal() { async closeConfirmationModal() {
const modal = document.getElementById('modal') const modal = document.getElementById('modal')

View File

@ -103,10 +103,10 @@ export default class Services {
} }
} }
async prepareProcessTx(myAddress: string, recipientAddress: string) { private prepareProcessTx(myAddress: string, recipientAddress: string) {
const initial_session_privkey = new Uint8Array(32); const initial_session_privkey = new Uint8Array(32);
const initial_session_pubkey = new Uint8Array(32); const initial_session_pubkey = new Uint8Array(32);
const paringTemplate = { const pairingTemplate = {
"description": "AliceBob", "description": "AliceBob",
"roles": { "roles": {
"owner": { "owner": {
@ -134,27 +134,33 @@ export default class Services {
} }
const apiReturn = await this.sdkClient.create_update_transaction(undefined, JSON.stringify(paringTemplate), 1) const apiReturn = this.sdkClient.create_update_transaction(undefined, JSON.stringify(pairingTemplate), 1)
return apiReturn return apiReturn
} }
public async sendPairingTx(spAddress: string): Promise<void> { public async sendPairingTx(spAddress: string): Promise<void> {
// const services = await Services.getInstance(); const amount = this.sdkClient.get_available_amount();
const amount = await this.getAmount() as any
console.log("🚀 ~ Services ~ sendPairingTx ~ amount:", amount) if (amount === 0n) {
const faucetMsg = this.sdkClient.create_faucet_msg();
await this.sendFaucetMessage(faucetMsg);
}
const localAddress = await this.getDeviceAddress() as any const localAddress = this.sdkClient.get_address();
const emptyTxid = '0'.repeat(64) const emptyTxid = '0'.repeat(64)
try { try {
let commitmentOutpoint = `${emptyTxid}:${U32_MAX}`; let commitmentOutpoint = `${emptyTxid}:${U32_MAX}`;
await this.sdkClient.pair_device(commitmentOutpoint, [spAddress]) this.sdkClient.pair_device(commitmentOutpoint, [spAddress])
} catch (e) { } catch (e) {
console.error("Services ~ Error:", e); console.error("Services ~ Error:", e);
return return
} }
const apiReturn = await this.prepareProcessTx(localAddress, spAddress)
this.handleApiReturn(apiReturn); setTimeout( async () => {
const apiReturn = this.prepareProcessTx(localAddress, spAddress)
await this.handleApiReturn(apiReturn);
}, 100)
return return
} }
@ -211,7 +217,7 @@ export default class Services {
async parseNewTx(tx: string) { async parseNewTx(tx: string) {
try { try {
// console.log('==============> sending txxxxxxx parser', tx) // console.log('==============> sending txxxxxxx parser', tx)
const parsedTx = await this.sdkClient.parse_new_tx(tx, 0, 0.01) const parsedTx = await this.sdkClient.parse_new_tx(tx, 0, 0.0001)
if(parsedTx) { if(parsedTx) {
console.log("🚀 ~ Services ~ parseNewTx ~ parsedTx:", parsedTx) console.log("🚀 ~ Services ~ parseNewTx ~ parsedTx:", parsedTx)
try { try {
@ -223,40 +229,52 @@ export default class Services {
} }
} }
} catch(e) { } catch(e) {
console.trace('Not our transaction'); console.trace(e);
} }
} }
private async handleApiReturn(apiReturn: ApiReturn) { private async handleApiReturn(apiReturn: ApiReturn) {
if(apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length) { if (apiReturn.ciphers_to_send && apiReturn.ciphers_to_send.length) {
await this.sendCipherMessages(apiReturn.ciphers_to_send) await this.sendCipherMessages(apiReturn.ciphers_to_send)
} }
if(apiReturn.new_tx_to_send) { setTimeout(async () => {
await this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send)) if (apiReturn.updated_process && apiReturn.updated_process.length) {
} const [processCommitment, process] = apiReturn.updated_process;
console.debug('Updated Process Commitment:', processCommitment);
console.debug('Process Details:', process);
if(apiReturn.updated_process && apiReturn.updated_process.length) { // Save process to storage
const [processCommitment, process] = apiReturn.updated_process; localStorage.setItem(processCommitment, JSON.stringify(process));
console.debug('Updated Process Commitment:', processCommitment); // Check if the newly updated process reveals some new information
console.debug('Process Details:', process); try {
// Save process to storage const proposals: string[] = this.sdkClient.get_update_proposals(processCommitment);
localStorage.setItem(processCommitment, JSON.stringify(process)); const actual_proposal = JSON.parse(proposals[0]); // just hacky way to solve redundant info for now
// router.openConfirmationModal(impendingRequest, outpointCommitment) console.info(actual_proposal);
} let router = await Routing.getInstance();
router.openConfirmationModal(actual_proposal, processCommitment);
} catch (e) {
console.error(e);
}
}
if(apiReturn.updated_cached_msg && apiReturn.updated_cached_msg.length) { if(apiReturn.updated_cached_msg && apiReturn.updated_cached_msg.length) {
apiReturn.updated_cached_msg.forEach((msg, index) => { apiReturn.updated_cached_msg.forEach((msg, index) => {
console.debug(`CachedMessage ${index}:`, msg); console.debug(`CachedMessage ${index}:`, msg);
// Save the message to local storage // Save the message to local storage
localStorage.setItem(msg.id.toString(), JSON.stringify(msg)); localStorage.setItem(msg.id.toString(), JSON.stringify(msg));
}); });
} }
if (apiReturn.commit_to_send) { if (apiReturn.commit_to_send) {
const commit = apiReturn.commit_to_send; const commit = apiReturn.commit_to_send;
this.sendCommitMessage(JSON.stringify(commit)); await this.sendCommitMessage(JSON.stringify(commit));
} }
if (apiReturn.new_tx_to_send) {
await this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send))
}
}, 0)
} }
async pairDevice(prd: any, outpointCommitment: string) { async pairDevice(prd: any, outpointCommitment: string) {
@ -327,9 +345,7 @@ export default class Services {
} }
async getDeviceAddress() { async getDeviceAddress() {
const address = await this.sdkClient.get_address() return await this.sdkClient.get_address();
console.log("🚀 ~ Services ~ getDeviceAddress ~ address:", address)
return address
} }
async dumpDevice() { async dumpDevice() {
@ -481,7 +497,7 @@ export default class Services {
} }
} }
private cleanSubsciptions(): void { private cleanSubscriptions(): void {
for (const sub of this.subscriptions) { for (const sub of this.subscriptions) {
const el = sub.element; const el = sub.element;
const eventHandler = sub.eventHandler; const eventHandler = sub.eventHandler;
@ -498,7 +514,7 @@ export default class Services {
return; return;
} }
this.cleanSubsciptions() this.cleanSubscriptions()
// const user = services.sdkClient.create_user('Test', 'test', 10, 1, 'Messaging') // const user = services.sdkClient.create_user('Test', 'test', 10, 1, 'Messaging')
// console.log("🚀 ~ Services ~ injectProcessListPage ~ user:", user) // console.log("🚀 ~ Services ~ injectProcessListPage ~ user:", user)