Compare commits
No commits in common. "e0e186f4f4e42897ddf39c82d214ee3c238fd56a" and "c422881cd13f51dad8f027890288047ebcecfb85" have entirely different histories.
e0e186f4f4
...
c422881cd1
@ -251,20 +251,15 @@ export async function registerAllListeners() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log('🚀 Starting pairing process');
|
console.log('🚀 Starting pairing process');
|
||||||
const myAddress = services.getDeviceAddress();
|
await prepareAndSendPairingTx();
|
||||||
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 services.confirmPairing();
|
await services.confirmPairing();
|
||||||
|
|
||||||
|
const pairingId = services.getPairingProcessId();
|
||||||
|
|
||||||
|
if (!pairingId) {
|
||||||
|
throw new Error('Failed to get pairing process id');
|
||||||
|
}
|
||||||
|
|
||||||
// Send success response
|
// Send success response
|
||||||
const successMsg = {
|
const successMsg = {
|
||||||
type: MessageType.PAIRING_CREATED,
|
type: MessageType.PAIRING_CREATED,
|
||||||
|
@ -702,16 +702,33 @@ export default class Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async confirmPairing() {
|
public async confirmPairing() {
|
||||||
try {
|
if (!this.processId || !this.stateId) {
|
||||||
// Is the wasm paired?
|
console.error('Missing process and/or state ID');
|
||||||
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');
|
|
||||||
return;
|
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> {
|
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 {
|
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) {
|
} catch (e) {
|
||||||
throw new Error(`Failed to pair device: ${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];
|
const storages = [STORAGEURL];
|
||||||
|
|
||||||
return await retrieveData(storages, hash);
|
return await retrieveData(storages, hash);
|
||||||
|
@ -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> {
|
export async function storeData(servers: string[], key: string, value: Blob, ttl: number | null): Promise<AxiosResponse | null> {
|
||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
try {
|
try {
|
||||||
// Handle relative paths (for development proxy) vs absolute URLs (for production)
|
// Append key and ttl as query parameters
|
||||||
let url: string;
|
const url = new URL(`${server}/store`);
|
||||||
if (server.startsWith('/')) {
|
url.searchParams.append('key', key);
|
||||||
// Relative path - construct manually for proxy
|
|
||||||
url = `${server}/store?key=${encodeURIComponent(key)}`;
|
|
||||||
if (ttl !== null) {
|
if (ttl !== null) {
|
||||||
url += `&ttl=${ttl}`;
|
url.searchParams.append('ttl', ttl.toString());
|
||||||
}
|
|
||||||
} 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the encrypted ArrayBuffer as the raw request body.
|
// 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: {
|
headers: {
|
||||||
'Content-Type': 'application/octet-stream'
|
'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> {
|
export async function retrieveData(servers: string[], key: string): Promise<ArrayBuffer | null> {
|
||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
try {
|
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:
|
// When fetching the data from the server:
|
||||||
const response = await axios.get(url, {
|
const response = await axios.get(`${server}/retrieve/${key}`, {
|
||||||
responseType: 'arraybuffer'
|
responseType: 'arraybuffer'
|
||||||
});
|
});
|
||||||
|
if (response.status !== 200) {
|
||||||
if (response.status === 200) {
|
console.error('Received response status', response.status);
|
||||||
// Validate that we received an ArrayBuffer
|
continue;
|
||||||
if (response.data instanceof ArrayBuffer) {
|
}
|
||||||
|
// console.log('Retrieved data:', response.data);
|
||||||
return response.data;
|
return response.data;
|
||||||
} else {
|
|
||||||
console.error('Server returned non-ArrayBuffer data:', typeof response.data);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.error(`Server ${server} returned status ${response.status}`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (axios.isAxiosError(error)) {
|
console.error('Error retrieving data:', 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return null
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TestResponse {
|
interface TestResponse {
|
||||||
@ -100,12 +62,7 @@ export async function testData(servers: string[], key: string): Promise<Record<s
|
|||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
res[server] = null;
|
res[server] = null;
|
||||||
try {
|
try {
|
||||||
// Handle relative paths (for development proxy) vs absolute URLs (for production)
|
const response = await axios.get(`${server}/test/${key}`);
|
||||||
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);
|
|
||||||
if (response.status !== 200) {
|
if (response.status !== 200) {
|
||||||
console.error(`${server}: Test response status: ${response.status}`);
|
console.error(`${server}: Test response status: ${response.status}`);
|
||||||
continue;
|
continue;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user