Compare commits

..

No commits in common. "e0e186f4f4e42897ddf39c82d214ee3c238fd56a" and "c422881cd13f51dad8f027890288047ebcecfb85" have entirely different histories.

3 changed files with 82 additions and 81 deletions

View File

@ -251,20 +251,15 @@ export async function registerAllListeners() {
}
console.log('🚀 Starting pairing process');
const myAddress = services.getDeviceAddress();
const createPairingProcessReturn = await services.createPairingProcess('', [myAddress]);
const pairingId = createPairingProcessReturn.updated_process?.process_id;
const stateId = createPairingProcessReturn.updated_process?.current_process?.states[0]?.state_id as string;
services.pairDevice(pairingId, [myAddress]);
await services.handleApiReturn(createPairingProcessReturn);
const createPrdUpdateReturn = await services.createPrdUpdate(pairingId, stateId);
await services.handleApiReturn(createPrdUpdateReturn);
const approveChangeReturn = await services.approveChange(pairingId, stateId);
await services.handleApiReturn(approveChangeReturn);
await prepareAndSendPairingTx();
await services.confirmPairing();
const pairingId = services.getPairingProcessId();
if (!pairingId) {
throw new Error('Failed to get pairing process id');
}
// Send success response
const successMsg = {
type: MessageType.PAIRING_CREATED,

View File

@ -702,16 +702,33 @@ export default class Services {
}
public async confirmPairing() {
try {
// Is the wasm paired?
const pairingId = this.getPairingProcessId();
// TODO confirm that the pairing process id is known, commited
const newDevice = this.dumpDeviceFromMemory();
await this.saveDeviceInDatabase(newDevice);
} catch (e) {
console.error('Failed to confirm pairing');
if (!this.processId || !this.stateId) {
console.error('Missing process and/or state ID');
return;
}
let createPrdUpdateReturn;
try {
createPrdUpdateReturn = await this.createPrdUpdate(this.processId, this.stateId);
} catch (e) {
throw new Error(`createPrdUpdate failed: ${e}`);
}
await this.handleApiReturn(createPrdUpdateReturn);
let approveChangeReturn;
try {
approveChangeReturn = await this.approveChange(this.processId, this.stateId);
} catch (e) {
throw new Error(`approveChange failed: ${e}`);
}
await this.handleApiReturn(approveChangeReturn);
await this.pairDevice();
this.processId = null;
this.stateId = null;
const newDevice = this.dumpDeviceFromMemory();
await this.saveDeviceInDatabase(newDevice);
await navigate('account');
}
public async updateDevice(): Promise<void> {
@ -749,9 +766,41 @@ export default class Services {
}
}
public pairDevice(processId: string, spAddressList: string[]): void {
public async pairDevice() {
if (!this.processId) {
console.error('No processId set');
return;
}
const process = await this.getProcess(this.processId);
if (!process) {
console.error('Unknown process');
return;
}
let spAddressList: string[] = [];
try {
this.sdkClient.pair_device(processId, spAddressList);
let encodedSpAddressList: number[] = [];
if (this.stateId) {
const state = process.states.find(state => state.state_id === this.stateId);
if (state) {
encodedSpAddressList = state.public_data['pairedAddresses'];
}
} else {
// We assume it's the last commited state
const lastCommitedState = this.getLastCommitedState(process);
if (lastCommitedState) {
encodedSpAddressList = lastCommitedState.public_data['pairedAddresses'];
}
}
spAddressList = this.sdkClient.decode_value(encodedSpAddressList);
if (!spAddressList || spAddressList.length == 0) {
throw new Error('Empty pairedAddresses');
}
} catch (e) {
throw new Error(`Failed to get pairedAddresses from process: ${e}`);
}
try {
this.sdkClient.pair_device(this.processId, spAddressList);
} catch (e) {
throw new Error(`Failed to pair device: ${e}`);
}
@ -1027,7 +1076,7 @@ export default class Services {
}
}
public async fetchValueFromStorage(hash: string): Promise<ArrayBuffer | null> {
public async fetchValueFromStorage(hash: string): Promise<any | null> {
const storages = [STORAGEURL];
return await retrieveData(storages, hash);

View File

@ -3,26 +3,15 @@ import axios, { AxiosResponse } from 'axios';
export async function storeData(servers: string[], key: string, value: Blob, ttl: number | null): Promise<AxiosResponse | null> {
for (const server of servers) {
try {
// Handle relative paths (for development proxy) vs absolute URLs (for production)
let url: string;
if (server.startsWith('/')) {
// Relative path - construct manually for proxy
url = `${server}/store?key=${encodeURIComponent(key)}`;
if (ttl !== null) {
url += `&ttl=${ttl}`;
}
} else {
// Absolute URL - use URL constructor
const urlObj = new URL(`${server}/store`);
urlObj.searchParams.append('key', key);
if (ttl !== null) {
urlObj.searchParams.append('ttl', ttl.toString());
}
url = urlObj.toString();
// Append key and ttl as query parameters
const url = new URL(`${server}/store`);
url.searchParams.append('key', key);
if (ttl !== null) {
url.searchParams.append('ttl', ttl.toString());
}
// Send the encrypted ArrayBuffer as the raw request body.
const response = await axios.post(url, value, {
const response = await axios.post(url.toString(), value, {
headers: {
'Content-Type': 'application/octet-stream'
},
@ -46,48 +35,21 @@ export async function storeData(servers: string[], key: string, value: Blob, ttl
export async function retrieveData(servers: string[], key: string): Promise<ArrayBuffer | null> {
for (const server of servers) {
try {
// Handle relative paths (for development proxy) vs absolute URLs (for production)
const url = server.startsWith('/')
? `${server}/retrieve/${key}` // Relative path - use as-is for proxy
: new URL(`${server}/retrieve/${key}`).toString(); // Absolute URL - construct properly
console.log('Retrieving data', key,' from:', url);
// When fetching the data from the server:
const response = await axios.get(url, {
const response = await axios.get(`${server}/retrieve/${key}`, {
responseType: 'arraybuffer'
});
if (response.status === 200) {
// Validate that we received an ArrayBuffer
if (response.data instanceof ArrayBuffer) {
return response.data;
} else {
console.error('Server returned non-ArrayBuffer data:', typeof response.data);
if (response.status !== 200) {
console.error('Received response status', response.status);
continue;
}
} else {
console.error(`Server ${server} returned status ${response.status}`);
continue;
}
// console.log('Retrieved data:', response.data);
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
if (error.response?.status === 404) {
console.log(`Data not found on server ${server} for key ${key}`);
continue; // Try next server
} else if (error.response?.status) {
console.error(`Server ${server} error ${error.response.status}:`, error.response.statusText);
continue;
} else {
console.error(`Network error connecting to ${server}:`, error.message);
continue;
}
} else {
console.error(`Unexpected error retrieving data from ${server}:`, error);
continue;
}
console.error('Error retrieving data:', error);
}
}
return null;
return null
}
interface TestResponse {
@ -100,12 +62,7 @@ export async function testData(servers: string[], key: string): Promise<Record<s
for (const server of servers) {
res[server] = null;
try {
// Handle relative paths (for development proxy) vs absolute URLs (for production)
const url = server.startsWith('/')
? `${server}/test/${key}` // Relative path - use as-is for proxy
: new URL(`${server}/test/${key}`).toString(); // Absolute URL - construct properly
const response = await axios.get(url);
const response = await axios.get(`${server}/test/${key}`);
if (response.status !== 200) {
console.error(`${server}: Test response status: ${response.status}`);
continue;