From fdc83c2f78b33e889f46bb402e658738eaf77bee Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sat, 15 Feb 2025 22:31:56 +1300 Subject: [PATCH] add helper function for exporting contact --- index.html | 7 ++++++- src/connection/connection.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index 8bbde55..d570979 100644 --- a/index.html +++ b/index.html @@ -362,7 +362,12 @@ await this.connection.sendCommandShareContact(contact.publicKey); }, async exportContact(contact) { - await this.connection.sendCommandExportContact(contact?.publicKey); + try { + const response = await this.connection.exportContact(contact?.publicKey); + console.log(response); + } catch(e) { + alert("Failed to export contact!"); + } }, async importContact() { await this.connection.importContact(this.hexToBytes("120070b78b64782bffb918da2d6432204a149bd232dd66373415b5f7ba24733ba2efe843b0674dc86210127e259f417a24150af38953dd2eb597a6a75a5c0a86adea7e81dbfc7023e4cc12bfe1b628aeaf76303f456545e555ae6554344b8391c2f07e9b010381506f636b6574204e6f64652031")); diff --git a/src/connection/connection.js b/src/connection/connection.js index 18a7c94..ccb9b58 100644 --- a/src/connection/connection.js +++ b/src/connection/connection.js @@ -757,6 +757,37 @@ class Connection extends EventEmitter { }); } + exportContact(pubKey = null) { + return new Promise(async (resolve, reject) => { + try { + + // resolve promise when we receive export contact response + const onExportContact = (response) => { + this.off(Constants.ResponseCodes.ExportContact, onExportContact); + this.off(Constants.ResponseCodes.Err, onErr); + resolve(response); + } + + // reject promise when we receive err + const onErr = () => { + this.off(Constants.ResponseCodes.ExportContact, onExportContact); + this.off(Constants.ResponseCodes.Err, onErr); + reject(); + } + + // listen for events + this.once(Constants.ResponseCodes.ExportContact, onExportContact); + this.once(Constants.ResponseCodes.Err, onErr); + + // export contact + await this.sendCommandExportContact(pubKey); + + } catch(e) { + reject(e); + } + }); + } + } export default Connection;