Minor improvements

This commit is contained in:
Sosthene 2024-10-12 15:09:34 +02:00
parent 8d745693b7
commit 22ff8a7880
2 changed files with 18 additions and 21 deletions

View File

@ -140,20 +140,16 @@ export default class Services {
} }
public async sendPairingTx(spAddress: string): Promise<void> { public async sendPairingTx(spAddress: string): Promise<void> {
const services = await Services.getInstance(); // const services = await Services.getInstance();
const amount = await this.getAmount() as any const amount = await this.getAmount() as any
console.log("🚀 ~ Services ~ sendPairingTx ~ amount:", amount) console.log("🚀 ~ Services ~ sendPairingTx ~ amount:", amount)
if(amount === 0n) {
const faucetMessage = await services.createFaucetMessage()
console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage)
services.websocketConnection?.sendNormalMessage(faucetMessage)
}
const localAddress = await this.getDeviceAddress() as any const localAddress = await this.getDeviceAddress() as any
const emptyTxid = '0'.repeat(64) const emptyTxid = '0'.repeat(64)
try { try {
await services.sdkClient.pair_device(`${emptyTxid}:${U32_MAX}`, [spAddress]) let commitmentOutpoint = `${emptyTxid}:${U32_MAX}`;
await this.sdkClient.pair_device(commitmentOutpoint, [spAddress])
} catch (e) { } catch (e) {
console.error("Services ~ Error:", e); console.error("Services ~ Error:", e);
return return
@ -171,14 +167,17 @@ export default class Services {
async sendNewTxMessage(message: string) { async sendNewTxMessage(message: string) {
const services = await Services.getInstance(); const services = await Services.getInstance();
console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ newTxMessage:", message) if (!services.websocketConnection) {
services.websocketConnection?.sendMessage('NewTx', message) throw new Error('No websocket connection');
}
// console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ newTxMessage:", message)
await services.websocketConnection.sendMessage('NewTx', message)
} }
async sendCommitMessage(message: string) { async sendCommitMessage(message: string) {
const services = await Services.getInstance(); const services = await Services.getInstance();
console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ CommitMessage:", message) // console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ CommitMessage:", message)
services.websocketConnection?.sendMessage('Commit', message) await services.websocketConnection?.sendMessage('Commit', message)
} }
async sendCipherMessages(ciphers: string[]) { async sendCipherMessages(ciphers: string[]) {
@ -189,11 +188,10 @@ export default class Services {
} }
} }
async sendFaucetMessage(): Promise<void> { async sendFaucetMessage(message: string): Promise<void> {
const services = await Services.getInstance(); const services = await Services.getInstance();
const faucetMessage = await services.createFaucetMessage() // console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", message)
console.log("🚀 ~ WebSocketClient ~ this.ws.onopen= ~ faucetMessage:", faucetMessage) await services.websocketConnection?.sendMessage('Faucet', message);
services.websocketConnection?.sendNormalMessage(faucetMessage)
} }
async parseCipher(message: string) { async parseCipher(message: string) {
@ -237,7 +235,6 @@ export default class Services {
} }
private async handleApiReturn(apiReturn: ApiReturn) { private async handleApiReturn(apiReturn: ApiReturn) {
// const service = await Services.getInstance()
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)
} }
@ -347,12 +344,12 @@ export default class Services {
async dumpDevice() { async dumpDevice() {
const services = await Services.getInstance(); const services = await Services.getInstance();
const device = await services.sdkClient.dump_device() const device = await services.sdkClient.dump_device()
console.log("🚀 ~ Services ~ dumpDevice ~ device:", device) // console.log("🚀 ~ Services ~ dumpDevice ~ device:", device)
return device return device
} }
async saveDevice(device: any): Promise<any> { async saveDevice(device: any): Promise<any> {
console.log("🚀 ~ Services ~ saveDevice ~ device:", device) // console.log("🚀 ~ Services ~ saveDevice ~ device:", device)
localStorage.setItem('wallet', device); localStorage.setItem('wallet', device);
} }

View File

@ -19,8 +19,8 @@ class WebSocketClient {
const amount = await service.getAmount() as bigint const amount = await service.getAmount() as bigint
if(amount === 0n) { if(amount === 0n) {
const faucetMessage = await services.createFaucetMessage() const faucetMessage = await services.createFaucetMessage()
this.sendNormalMessage(faucetMessage) this.sendNormalMessage(faucetMessage)
} }
while (this.messageQueue.length > 0) { while (this.messageQueue.length > 0) {
@ -118,7 +118,7 @@ class WebSocketClient {
console.log("Sending message:", JSON.stringify(networkMessage)); console.log("Sending message:", JSON.stringify(networkMessage));
this.ws.send(JSON.stringify(networkMessage)); this.ws.send(JSON.stringify(networkMessage));
} else { } else {
console.warn('WebSocket is not open. ReadyState:', this.ws.readyState); console.error('WebSocket is not open. ReadyState:', this.ws.readyState);
this.messageQueue.push(message); this.messageQueue.push(message);
} }
} }