services various improvements
This commit is contained in:
parent
2f4cd4fb9b
commit
a251458f04
@ -1,4 +1,4 @@
|
|||||||
import { createUserReturn, User, Process, createNotificationTransactionReturn, parse_network_msg, outputs_list, parseNetworkMsgReturn, FaucetMessage, AnkFlag, NewTxMessage, encryptWithNewKeyResult, AnkSharedSecret, NetworkMessage } from '../dist/pkg/sdk_client';
|
import { createUserReturn, User, Process, createTransactionReturn, parse_network_msg, outputs_list, FaucetMessage, AnkFlag, NewTxMessage, encryptWithNewKeyResult, AnkSharedSecret, CachedMessage } from '../dist/pkg/sdk_client';
|
||||||
import IndexedDB from './database'
|
import IndexedDB from './database'
|
||||||
import { WebSocketClient } from './websockets';
|
import { WebSocketClient } from './websockets';
|
||||||
|
|
||||||
@ -110,38 +110,31 @@ class Services {
|
|||||||
let notificationInfo = await services.notify_address_for_message(recipientSpAddress, msg_payload);
|
let notificationInfo = await services.notify_address_for_message(recipientSpAddress, msg_payload);
|
||||||
if (notificationInfo) {
|
if (notificationInfo) {
|
||||||
let networkMsg = notificationInfo.new_network_msg;
|
let networkMsg = notificationInfo.new_network_msg;
|
||||||
const msgId = notificationInfo.new_network_msg.id;
|
|
||||||
let shared_secret = '';
|
let shared_secret = '';
|
||||||
if (networkMsg.shared_secret) {
|
if (networkMsg.shared_secret) {
|
||||||
shared_secret = networkMsg.shared_secret;
|
shared_secret = networkMsg.shared_secret;
|
||||||
} else {
|
} else {
|
||||||
throw 'no shared_secret';
|
throw 'no shared_secret';
|
||||||
}
|
}
|
||||||
let ciphers: string[] = [];
|
|
||||||
console.info('Successfully sent notification transaction');
|
console.info('Successfully sent notification transaction');
|
||||||
// Save the secret to db
|
|
||||||
const indexedDb = await IndexedDB.getInstance();
|
const connection = await services.pickWebsocketConnectionRandom();
|
||||||
const db = await indexedDb.getDb();
|
const flag: AnkFlag = "Unknown";
|
||||||
|
|
||||||
// encrypt the message(s)
|
// encrypt the message(s)
|
||||||
|
// TODO we'd rather do that in the wasm as part of notify_address_for_message
|
||||||
try {
|
try {
|
||||||
const cipher = await services.encryptData(msg_payload, shared_secret);
|
const cipher = await services.encryptData(msg_payload, shared_secret);
|
||||||
let updated_msg = notificationInfo.new_network_msg;
|
let updated_msg = notificationInfo.new_network_msg;
|
||||||
updated_msg.plaintext = msg_payload;
|
updated_msg.plaintext = msg_payload;
|
||||||
updated_msg.ciphertext = cipher;
|
updated_msg.ciphertext = cipher;
|
||||||
await indexedDb.writeObject(db, indexedDb.getStoreList().AnkMessages, updated_msg, null);
|
await services.updateMessages(updated_msg);
|
||||||
ciphers.push(cipher);
|
connection?.sendMessage(flag, cipher);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
const connection = await services.pickWebsocketConnectionRandom();
|
|
||||||
const flag: AnkFlag = "Unknown";
|
|
||||||
// add peers list
|
// add peers list
|
||||||
// add processes list
|
// add processes list
|
||||||
// send message (transaction in envelope)
|
// send message (transaction in envelope)
|
||||||
for (const payload of ciphers) {
|
|
||||||
connection?.sendMessage(flag, payload);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -322,10 +315,10 @@ class Services {
|
|||||||
services.attachSubmitListener("form4nk", services.updateAnId);
|
services.attachSubmitListener("form4nk", services.updateAnId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async parseNetworkMessage(raw: string, feeRate: number): Promise<parseNetworkMsgReturn> {
|
public async parseNetworkMessage(raw: string, feeRate: number): Promise<CachedMessage> {
|
||||||
const services = await Services.getInstance();
|
const services = await Services.getInstance();
|
||||||
try {
|
try {
|
||||||
const msg: parseNetworkMsgReturn = services.sdkClient.parse_network_msg(raw, feeRate);
|
const msg: CachedMessage = services.sdkClient.parse_network_msg(raw, feeRate);
|
||||||
return msg;
|
return msg;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
@ -443,7 +436,7 @@ class Services {
|
|||||||
return process;
|
return process;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateMessages(message: NetworkMessage): Promise<void> {
|
public async updateMessages(message: CachedMessage): Promise<void> {
|
||||||
const indexedDb = await IndexedDB.getInstance();
|
const indexedDb = await IndexedDB.getInstance();
|
||||||
const db = await indexedDb.getDb();
|
const db = await indexedDb.getDb();
|
||||||
|
|
||||||
@ -454,6 +447,17 @@ class Services {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async removeMessage(id: number): Promise<void> {
|
||||||
|
const indexedDb = await IndexedDB.getInstance();
|
||||||
|
const db = await indexedDb.getDb();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await indexedDb.rmObject(db, indexedDb.getStoreList().AnkMessages, id);
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async updateProcesses(): Promise<void> {
|
public async updateProcesses(): Promise<void> {
|
||||||
const services = await Services.getInstance();
|
const services = await Services.getInstance();
|
||||||
const processList: Process[] = services.sdkClient.get_processes();
|
const processList: Process[] = services.sdkClient.get_processes();
|
||||||
@ -768,6 +772,7 @@ class Services {
|
|||||||
const flag: AnkFlag = 'Faucet';
|
const flag: AnkFlag = 'Faucet';
|
||||||
const faucetMsg = services.sdkClient.create_faucet_msg();
|
const faucetMsg = services.sdkClient.create_faucet_msg();
|
||||||
connection.sendMessage(flag, JSON.stringify(faucetMsg));
|
connection.sendMessage(flag, JSON.stringify(faucetMsg));
|
||||||
|
await services.updateMessages(faucetMsg);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to obtain tokens with relay ", connection.getUrl());
|
console.error("Failed to obtain tokens with relay ", connection.getUrl());
|
||||||
return null;
|
return null;
|
||||||
@ -806,7 +811,7 @@ class Services {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async confirm_sender_address(sp_address: string): Promise<void> {
|
public async confirm_sender_address(msg: CachedMessage): Promise<void> {
|
||||||
const services = await Services.getInstance();
|
const services = await Services.getInstance();
|
||||||
const connection = await services.pickWebsocketConnectionRandom();
|
const connection = await services.pickWebsocketConnectionRandom();
|
||||||
if (!connection) {
|
if (!connection) {
|
||||||
@ -824,10 +829,10 @@ class Services {
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
let notificationInfo: createNotificationTransactionReturn;
|
let notificationInfo: createTransactionReturn;
|
||||||
try {
|
try {
|
||||||
const feeRate = 1;
|
const feeRate = 1;
|
||||||
notificationInfo = services.sdkClient.create_notification_transaction(sp_address, null, feeRate);
|
notificationInfo = services.sdkClient.answer_confirmation_transaction(msg, feeRate);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(`Failed to create confirmation transaction: ${error}`);
|
throw new Error(`Failed to create confirmation transaction: ${error}`);
|
||||||
}
|
}
|
||||||
@ -840,7 +845,7 @@ class Services {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async notify_address_for_message(sp_address: string, message: string): Promise<createNotificationTransactionReturn> {
|
public async notify_address_for_message(sp_address: string, message: string): Promise<createTransactionReturn> {
|
||||||
const services = await Services.getInstance();
|
const services = await Services.getInstance();
|
||||||
const connection = await services.pickWebsocketConnectionRandom();
|
const connection = await services.pickWebsocketConnectionRandom();
|
||||||
if (!connection) {
|
if (!connection) {
|
||||||
@ -850,7 +855,7 @@ class Services {
|
|||||||
try {
|
try {
|
||||||
const feeRate = 1;
|
const feeRate = 1;
|
||||||
const commitment = services.sdkClient.create_commitment(message);
|
const commitment = services.sdkClient.create_commitment(message);
|
||||||
let notificationInfo: createNotificationTransactionReturn = services.sdkClient.create_notification_transaction(sp_address, commitment, feeRate);
|
let notificationInfo: createTransactionReturn = services.sdkClient.create_notification_transaction(sp_address, commitment, feeRate);
|
||||||
const flag: AnkFlag = "NewTx";
|
const flag: AnkFlag = "NewTx";
|
||||||
const newTxMsg: NewTxMessage = {
|
const newTxMsg: NewTxMessage = {
|
||||||
'transaction': notificationInfo.transaction,
|
'transaction': notificationInfo.transaction,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user