Skip to content

Commit

Permalink
Merge pull request #127 from xmtp/jazz/contact-bundle-error-handling
Browse files Browse the repository at this point in the history
fix: return clearer error on ContactBundle decode faliure
  • Loading branch information
jazzz authored Jul 6, 2022
2 parents 8d5476f + 666e95e commit a77c7fa
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/ContactBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export default class ContactBundle implements proto.ContactBundleV1 {
}

toBytes(): Uint8Array {
return proto.ContactBundleV1.encode({
keyBundle: this.keyBundle,
}).finish()
return this.keyBundle.toBytes()
}

static fromBytes(bytes: Uint8Array): ContactBundle {
Expand Down Expand Up @@ -49,11 +47,13 @@ export default class ContactBundle implements proto.ContactBundleV1 {
e instanceof RangeError ||
(e instanceof Error && e.message.startsWith('invalid wire type'))
) {
// Adds a default fallback for older versions of the proto
const legacyBundle = proto.ContactBundleV1.decode(bytes)
return legacyBundle.keyBundle
// Adds a default fallback for older versions of the proto (Which may also fail)
try {
return proto.PublicKeyBundle.decode(bytes)
} catch (e) {
throw new Error("Couldn't decode contact bundle: " + e)
}
}
throw new Error("Couldn't decode contact bundle:" + e)
}
}
}

0 comments on commit a77c7fa

Please sign in to comment.