Remove unnecessary async
(cherry picked from commit 19da9676053cb4e0bfa6ec5c648f98b156becbca)
This commit is contained in:
parent
a7e9043ae4
commit
a41328db28
@ -1,7 +1,7 @@
|
||||
import { INotification } from '~/models/notification.model';
|
||||
import { IProcess } from '~/models/process.model';
|
||||
import { initWebsocket, sendMessage } from '../websockets';
|
||||
import type { ApiReturn, Device, HandshakeMessage, Member, MerkleProofResult, NewTxMessage, OutPointProcessMap, Process, ProcessState, RoleDefinition, SecretsStore, UserDiff } from '../../pkg/sdk_client';
|
||||
import { ApiReturn, Device, HandshakeMessage, Member, MerkleProofResult, NewTxMessage, OutPointProcessMap, Process, ProcessState, RoleDefinition, SecretsStore, UserDiff } from '../../pkg/sdk_client';
|
||||
import ModalService from './modal.service';
|
||||
import Database from './database.service';
|
||||
import { navigate } from '../router';
|
||||
@ -12,8 +12,8 @@ export const U32_MAX = 4294967295;
|
||||
|
||||
const BASEURL = `http://localhost`;
|
||||
const BOOTSTRAPURL = [`${BASEURL}:8090`];
|
||||
const STORAGEURL = `${BASEURL}:8081`;
|
||||
const BLINDBITURL = `${BASEURL}:8000`;
|
||||
const STORAGEURL = `${BASEURL}:8081`
|
||||
const BLINDBITURL = `${BASEURL}:8000`
|
||||
const DEFAULTAMOUNT = 1000n;
|
||||
const EMPTY32BYTES = String('').padStart(64, '0');
|
||||
|
||||
@ -60,14 +60,6 @@ export default class Services {
|
||||
this.notifications = this.getNotifications();
|
||||
this.sdkClient = await import('../../pkg/sdk_client');
|
||||
this.sdkClient.setup();
|
||||
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const isE2E = params.has('e2e');
|
||||
if (isE2E) {
|
||||
// Mode E2E: ne pas tenter de connexion aux relays
|
||||
return;
|
||||
}
|
||||
|
||||
for (const wsurl of Object.values(BOOTSTRAPURL)) {
|
||||
this.updateRelay(wsurl, '');
|
||||
}
|
||||
@ -153,7 +145,7 @@ export default class Services {
|
||||
* Print all key/value pairs for debugging.
|
||||
*/
|
||||
public printAllRelays(): void {
|
||||
console.log('Current relay addresses:');
|
||||
console.log("Current relay addresses:");
|
||||
for (const [wsurl, spAddress] of Object.entries(this.relayAddresses)) {
|
||||
console.log(`${wsurl} -> ${spAddress}`);
|
||||
}
|
||||
@ -307,18 +299,27 @@ export default class Services {
|
||||
min_sig_member: 1.0,
|
||||
},
|
||||
],
|
||||
storages: [STORAGEURL],
|
||||
storages: [STORAGEURL]
|
||||
},
|
||||
};
|
||||
try {
|
||||
return this.createProcess(privateData, publicData, roles);
|
||||
return this.createProcess(
|
||||
privateData,
|
||||
publicData,
|
||||
roles
|
||||
);
|
||||
} catch (e) {
|
||||
throw new Error(`Creating process failed:, ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
private isFileBlob(value: any): value is { type: string; data: Uint8Array } {
|
||||
return typeof value === 'object' && value !== null && typeof value.type === 'string' && value.data instanceof Uint8Array;
|
||||
private isFileBlob(value: any): value is { type: string, data: Uint8Array } {
|
||||
return (
|
||||
typeof value === 'object' &&
|
||||
value !== null &&
|
||||
typeof value.type === 'string' &&
|
||||
value.data instanceof Uint8Array
|
||||
);
|
||||
}
|
||||
|
||||
private splitData(obj: Record<string, any>) {
|
||||
@ -336,7 +337,11 @@ export default class Services {
|
||||
return { jsonCompatibleData, binaryData };
|
||||
}
|
||||
|
||||
public async createProcess(privateData: Record<string, any>, publicData: Record<string, any>, roles: Record<string, RoleDefinition>): Promise<ApiReturn> {
|
||||
public async createProcess(
|
||||
privateData: Record<string, any>,
|
||||
publicData: Record<string, any>,
|
||||
roles: Record<string, RoleDefinition>,
|
||||
): Promise<ApiReturn> {
|
||||
let relayAddress = this.getAllRelays()[0]?.spAddress;
|
||||
|
||||
if (!relayAddress || relayAddress === '') {
|
||||
@ -359,11 +364,11 @@ export default class Services {
|
||||
const publicSplitData = this.splitData(publicData);
|
||||
const encodedPrivateData = {
|
||||
...this.sdkClient.encode_json(privateSplitData.jsonCompatibleData),
|
||||
...this.sdkClient.encode_binary(privateSplitData.binaryData),
|
||||
...this.sdkClient.encode_binary(privateSplitData.binaryData)
|
||||
};
|
||||
const encodedPublicData = {
|
||||
...this.sdkClient.encode_json(publicSplitData.jsonCompatibleData),
|
||||
...this.sdkClient.encode_binary(publicSplitData.binaryData),
|
||||
...this.sdkClient.encode_binary(publicSplitData.binaryData)
|
||||
};
|
||||
|
||||
console.log('encoded data:', encodedPrivateData);
|
||||
@ -382,9 +387,16 @@ export default class Services {
|
||||
console.log('members:', members);
|
||||
await this.checkConnections([...members]);
|
||||
|
||||
const result = this.sdkClient.create_new_process(encodedPrivateData, roles, encodedPublicData, relayAddress, feeRate, this.getAllMembers());
|
||||
const result = this.sdkClient.create_new_process (
|
||||
encodedPrivateData,
|
||||
roles,
|
||||
encodedPublicData,
|
||||
relayAddress,
|
||||
feeRate,
|
||||
this.getAllMembers()
|
||||
);
|
||||
|
||||
return result;
|
||||
return(result);
|
||||
}
|
||||
|
||||
public async updateProcess(process: Process, privateData: Record<string, any>, publicData: Record<string, any>, roles: Record<string, RoleDefinition> | null): Promise<ApiReturn> {
|
||||
@ -398,7 +410,7 @@ export default class Services {
|
||||
let members: Set<Member> = new Set();
|
||||
for (const role of Object.values(roles!)) {
|
||||
for (const member of role.members) {
|
||||
members.add(member);
|
||||
members.add(member)
|
||||
}
|
||||
}
|
||||
if (members.size === 0) {
|
||||
@ -419,11 +431,11 @@ export default class Services {
|
||||
const publicSplitData = this.splitData(publicData);
|
||||
const encodedPrivateData = {
|
||||
...this.sdkClient.encode_json(privateSplitData.jsonCompatibleData),
|
||||
...this.sdkClient.encode_binary(privateSplitData.binaryData),
|
||||
...this.sdkClient.encode_binary(privateSplitData.binaryData)
|
||||
};
|
||||
const encodedPublicData = {
|
||||
...this.sdkClient.encode_json(publicSplitData.jsonCompatibleData),
|
||||
...this.sdkClient.encode_binary(publicSplitData.binaryData),
|
||||
...this.sdkClient.encode_binary(publicSplitData.binaryData)
|
||||
};
|
||||
try {
|
||||
return this.sdkClient.update_process(process, encodedPrivateData, roles, encodedPublicData, this.getAllMembers());
|
||||
@ -524,6 +536,7 @@ export default class Services {
|
||||
if (waitingModal) {
|
||||
this.device2Ready = true;
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(`Parsed cipher with error: ${e}`);
|
||||
}
|
||||
@ -591,7 +604,7 @@ export default class Services {
|
||||
|
||||
if (apiReturn.new_tx_to_send && apiReturn.new_tx_to_send.transaction.length != 0) {
|
||||
this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send));
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
}
|
||||
|
||||
if (apiReturn.secrets) {
|
||||
@ -768,7 +781,7 @@ export default class Services {
|
||||
try {
|
||||
let encodedSpAddressList: number[] = [];
|
||||
if (this.stateId) {
|
||||
const state = process.states.find((state) => state.state_id === this.stateId);
|
||||
const state = process.states.find(state => state.state_id === this.stateId);
|
||||
if (state) {
|
||||
encodedSpAddressList = state.public_data['pairedAddresses'];
|
||||
}
|
||||
@ -837,7 +850,7 @@ export default class Services {
|
||||
try {
|
||||
const prevDevice = await this.getDeviceFromDatabase();
|
||||
if (prevDevice) {
|
||||
await db.deleteObject(walletStore, '1');
|
||||
await db.deleteObject(walletStore, "1");
|
||||
}
|
||||
await db.addObject({
|
||||
storeName: walletStore,
|
||||
@ -1214,7 +1227,7 @@ export default class Services {
|
||||
let hasAccess = false;
|
||||
// If we're not supposed to have access to this attribute, ignore
|
||||
for (const role of Object.values(roles)) {
|
||||
for (const rule of Object.values(role.validation_rules) as any[]) {
|
||||
for (const rule of Object.values(role.validation_rules)) {
|
||||
if (rule.fields.includes(attribute)) {
|
||||
if (role.members.includes(pairingProcessId)) {
|
||||
// We have access to this attribute
|
||||
@ -1235,7 +1248,7 @@ export default class Services {
|
||||
let retries = 0;
|
||||
|
||||
while ((!hash || !key) && retries < maxRetries) {
|
||||
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
||||
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
||||
// Re-read hash and key after waiting
|
||||
hash = state.pcd_commitment[attribute];
|
||||
key = state.keys[attribute];
|
||||
@ -1352,6 +1365,7 @@ export default class Services {
|
||||
this.device2Ready = false;
|
||||
}
|
||||
|
||||
|
||||
// Handle the handshake message
|
||||
public async handleHandshakeMsg(url: string, parsedMsg: any) {
|
||||
try {
|
||||
@ -1389,12 +1403,10 @@ export default class Services {
|
||||
const existing = await this.getProcess(processId);
|
||||
if (existing) {
|
||||
// Look for state id we don't know yet
|
||||
let new_states: string[] = [];
|
||||
let roles: Record<string, RoleDefinition>[] = [];
|
||||
let new_states = [];
|
||||
let roles = [];
|
||||
for (const state of process.states) {
|
||||
if (!state.state_id || state.state_id === EMPTY32BYTES) {
|
||||
continue;
|
||||
}
|
||||
if (!state.state_id || state.state_id === EMPTY32BYTES) { continue; }
|
||||
if (!this.lookForStateId(existing, state.state_id)) {
|
||||
if (this.rolesContainsUs(state.roles)) {
|
||||
new_states.push(state.state_id);
|
||||
@ -1442,7 +1454,7 @@ export default class Services {
|
||||
|
||||
await this.batchSaveProcessesToDb(toSave);
|
||||
}
|
||||
}, 500);
|
||||
}, 500)
|
||||
} catch (e) {
|
||||
console.error('Failed to parse init message:', e);
|
||||
}
|
||||
@ -1495,7 +1507,9 @@ export default class Services {
|
||||
* @returns Un tableau contenant tous les membres
|
||||
*/
|
||||
public getAllMembersSorted(): Record<string, Member> {
|
||||
return Object.fromEntries(Object.entries(this.membersList).sort(([keyA], [keyB]) => keyA.localeCompare(keyB)));
|
||||
return Object.fromEntries(
|
||||
Object.entries(this.membersList).sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
|
||||
);
|
||||
}
|
||||
|
||||
public getAllMembers(): Record<string, Member> {
|
||||
@ -1511,14 +1525,10 @@ export default class Services {
|
||||
}
|
||||
|
||||
public compareMembers(memberA: string[], memberB: string[]): boolean {
|
||||
if (!memberA || !memberB) {
|
||||
return false;
|
||||
}
|
||||
if (memberA.length !== memberB.length) {
|
||||
return false;
|
||||
}
|
||||
if (!memberA || !memberB) { return false }
|
||||
if (memberA.length !== memberB.length) { return false }
|
||||
|
||||
const res = memberA.every((item) => memberB.includes(item)) && memberB.every((item) => memberA.includes(item));
|
||||
const res = memberA.every(item => memberB.includes(item)) && memberB.every(item => memberA.includes(item));
|
||||
|
||||
return res;
|
||||
}
|
||||
@ -1527,14 +1537,16 @@ export default class Services {
|
||||
const content = JSON.parse(response);
|
||||
const error = content.error;
|
||||
const errorMsg = error['GenericError'];
|
||||
const dontRetry = ['State is identical to the previous state', 'Not enough valid proofs', 'Not enough members to validate'];
|
||||
if (dontRetry.includes(errorMsg)) {
|
||||
return;
|
||||
}
|
||||
const dontRetry = [
|
||||
'State is identical to the previous state',
|
||||
'Not enough valid proofs',
|
||||
'Not enough members to validate',
|
||||
];
|
||||
if (dontRetry.includes(errorMsg)) { return; }
|
||||
// Wait and retry
|
||||
setTimeout(async () => {
|
||||
this.sendCommitMessage(JSON.stringify(content));
|
||||
}, 1000);
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
public getRoles(process: Process): Record<string, RoleDefinition> | null {
|
||||
@ -1567,11 +1579,8 @@ export default class Services {
|
||||
const lastCommitedState = this.getLastCommitedState(process);
|
||||
if (lastCommitedState && lastCommitedState.public_data) {
|
||||
const processName = lastCommitedState!.public_data['processName'];
|
||||
if (processName) {
|
||||
return this.decodeValue(processName);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
if (processName) { return this.decodeValue(processName) }
|
||||
else { return null }
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@ -1613,7 +1622,7 @@ export default class Services {
|
||||
this.myProcesses = newMyProcesses; // atomic update
|
||||
return Array.from(this.myProcesses);
|
||||
} catch (e) {
|
||||
console.error('Failed to get processes:', e);
|
||||
console.error("Failed to get processes:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -1631,16 +1640,13 @@ export default class Services {
|
||||
|
||||
public hexToBlob(hexString: string): Blob {
|
||||
const uint8Array = this.hexToUInt8Array(hexString);
|
||||
// Crée un ArrayBuffer standard et copie les données pour éviter ArrayBufferLike/SharedArrayBuffer
|
||||
const ab = new ArrayBuffer(uint8Array.length);
|
||||
const view = new Uint8Array(ab);
|
||||
view.set(uint8Array);
|
||||
return new Blob([ab], { type: 'application/octet-stream' });
|
||||
|
||||
return new Blob([uint8Array], { type: "application/octet-stream" });
|
||||
}
|
||||
|
||||
public hexToUInt8Array(hexString: string): Uint8Array {
|
||||
if (hexString.length % 2 !== 0) {
|
||||
throw new Error('Invalid hex string: length must be even');
|
||||
throw new Error("Invalid hex string: length must be even");
|
||||
}
|
||||
const uint8Array = new Uint8Array(hexString.length / 2);
|
||||
for (let i = 0; i < hexString.length; i += 2) {
|
||||
@ -1654,7 +1660,7 @@ export default class Services {
|
||||
const buffer = await blob.arrayBuffer();
|
||||
const bytes = new Uint8Array(buffer);
|
||||
return Array.from(bytes)
|
||||
.map((byte) => byte.toString(16).padStart(2, '0'))
|
||||
.map(byte => byte.toString(16).padStart(2, '0'))
|
||||
.join('');
|
||||
}
|
||||
|
||||
@ -1677,7 +1683,7 @@ export default class Services {
|
||||
public getLastCommitedState(process: Process): ProcessState | null {
|
||||
if (process.states.length === 0) return null;
|
||||
const processTip = process.states[process.states.length - 1].commited_in;
|
||||
const lastCommitedState = process.states.findLast((state: ProcessState) => state.commited_in !== processTip);
|
||||
const lastCommitedState = process.states.findLast(state => state.commited_in !== processTip);
|
||||
if (lastCommitedState) {
|
||||
return lastCommitedState;
|
||||
} else {
|
||||
@ -1699,13 +1705,13 @@ export default class Services {
|
||||
public getUncommitedStates(process: Process): ProcessState[] {
|
||||
if (process.states.length === 0) return [];
|
||||
const processTip = process.states[process.states.length - 1].commited_in;
|
||||
const res = process.states.filter((state: ProcessState) => state.commited_in === processTip);
|
||||
return res.filter((state: ProcessState) => state.state_id !== EMPTY32BYTES);
|
||||
const res = process.states.filter(state => state.commited_in === processTip);
|
||||
return res.filter(state => state.state_id !== EMPTY32BYTES);
|
||||
}
|
||||
|
||||
public getStateFromId(process: Process, stateId: string): ProcessState | null {
|
||||
if (process.states.length === 0) return null;
|
||||
const state = process.states.find((state: ProcessState) => state.state_id === stateId);
|
||||
const state = process.states.find(state => state.state_id === stateId);
|
||||
if (state) {
|
||||
return state;
|
||||
} else {
|
||||
@ -1716,7 +1722,7 @@ export default class Services {
|
||||
public getNextStateAfterId(process: Process, stateId: string): ProcessState | null {
|
||||
if (process.states.length === 0) return null;
|
||||
|
||||
const index = process.states.findIndex((state: ProcessState) => state.state_id === stateId);
|
||||
const index = process.states.findIndex(state => state.state_id === stateId);
|
||||
|
||||
if (index !== -1 && index < process.states.length - 1) {
|
||||
return process.states[index + 1];
|
||||
@ -1726,9 +1732,7 @@ export default class Services {
|
||||
}
|
||||
|
||||
public isPairingProcess(roles: Record<string, RoleDefinition>): boolean {
|
||||
if (Object.keys(roles).length != 1) {
|
||||
return false;
|
||||
}
|
||||
if (Object.keys(roles).length != 1) { return false }
|
||||
const pairingRole = roles['pairing'];
|
||||
if (pairingRole) {
|
||||
// For now that's enough, we should probably test more things
|
||||
@ -1740,7 +1744,7 @@ export default class Services {
|
||||
|
||||
public async updateMemberPublicName(process: Process, newName: string): Promise<ApiReturn> {
|
||||
const publicData = {
|
||||
memberPublicName: newName,
|
||||
'memberPublicName': newName
|
||||
};
|
||||
|
||||
return await this.updateProcess(process, {}, publicData, null);
|
||||
|
Loading…
x
Reference in New Issue
Block a user