mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
add helper function for setting custom path for contact
This commit is contained in:
parent
1a76e7a50e
commit
e12d87c6ba
2 changed files with 28 additions and 17 deletions
19
index.html
19
index.html
|
|
@ -373,24 +373,9 @@
|
|||
await this.connection.importContact(this.hexToBytes("120070b78b64782bffb918da2d6432204a149bd232dd66373415b5f7ba24733ba2efe843b0674dc86210127e259f417a24150af38953dd2eb597a6a75a5c0a86adea7e81dbfc7023e4cc12bfe1b628aeaf76303f456545e555ae6554344b8391c2f07e9b010381506f636b6574204e6f64652031"));
|
||||
},
|
||||
async setPath(contact) {
|
||||
|
||||
const newOutPath = [
|
||||
await this.connection.setContactPath(contact, [
|
||||
0xf4,
|
||||
];
|
||||
|
||||
// create out path
|
||||
const outPath = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
for(var i = 0; i < newOutPath.length; i++){
|
||||
outPath[i] = newOutPath[i];
|
||||
}
|
||||
|
||||
// update contact details
|
||||
contact.outPathLen = newOutPath.length;
|
||||
contact.outPath = outPath;
|
||||
|
||||
// update contact
|
||||
await this.connection.addOrUpdateContact(contact.publicKey, contact.type, contact.flags, contact.outPathLen, contact.outPath, contact.advName, contact.lastAdvert, contact.advLat, contact.advLon);
|
||||
|
||||
]);
|
||||
},
|
||||
bytesToHex(uint8Array) {
|
||||
return Array.from(uint8Array).map(byte => byte.toString(16).padStart(2, '0')).join('');
|
||||
|
|
|
|||
|
|
@ -881,6 +881,32 @@ class Connection extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
setContactPath(contact, path) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
|
||||
// create empty out path
|
||||
const maxPathLength = 64;
|
||||
const outPath = new Uint8Array(maxPathLength);
|
||||
|
||||
// fill out path with the provided path
|
||||
for(var i = 0; i < path.length && i < maxPathLength; i++){
|
||||
outPath[i] = path[i];
|
||||
}
|
||||
|
||||
// update contact details with new path and path length
|
||||
contact.outPathLen = path.length;
|
||||
contact.outPath = outPath;
|
||||
|
||||
// update contact
|
||||
return await this.addOrUpdateContact(contact.publicKey, contact.type, contact.flags, contact.outPathLen, contact.outPath, contact.advName, contact.lastAdvert, contact.advLat, contact.advLon);
|
||||
|
||||
} catch(e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
resetPath(pubKey) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue