add helper function for exporting contact

This commit is contained in:
liamcottle 2025-02-15 22:31:56 +13:00
parent 54a3d00d7e
commit fdc83c2f78
2 changed files with 37 additions and 1 deletions

View file

@ -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"));

View file

@ -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;