-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Identity serialization/deserialization
After the format of keeping the identity on the server was changed we have to update how we serialize and deserialize Identity values
- Loading branch information
Showing
3 changed files
with
49 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,57 @@ | ||
// Helper function convert from string to Uint8Array | ||
function hexStringToUint8Array(str: string): Uint8Array { | ||
let matches = str.match(/.{1,2}/g) || []; | ||
let data = Uint8Array.from(matches.map((byte: string) => parseInt(byte, 16))); | ||
return data; | ||
} | ||
|
||
// Helper function for converting Uint8Array to hex string | ||
function uint8ArrayToHexString(array: Uint8Array): string { | ||
return Array.prototype.map | ||
.call(array, (x) => ("00" + x.toString(16)).slice(-2)) | ||
.join(""); | ||
} | ||
|
||
/** | ||
* A unique public identifier for a client connected to a database. | ||
*/ | ||
export class Identity { | ||
private data: Uint8Array; | ||
private data: string; | ||
|
||
/** | ||
* Creates a new `Identity`. | ||
*/ | ||
constructor(data: Uint8Array) { | ||
this.data = data; | ||
constructor(data: { __identity_bytes: string } | Uint8Array) { | ||
// we get a JSON with __identity_bytes when getting a token with a JSON API | ||
// and an Uint8Array when using BSATN | ||
this.data = | ||
data.constructor === Uint8Array | ||
? uint8ArrayToHexString(data as Uint8Array) | ||
: (data as { __identity_bytes: string }).__identity_bytes; | ||
} | ||
|
||
/** | ||
* Compare two identities for equality. | ||
*/ | ||
isEqual(other: Identity): boolean { | ||
if (this.data.length !== other.data.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < this.data.length; i++) { | ||
if (this.data[i] !== other.data[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
return this.toHexString() === other.toHexString(); | ||
} | ||
|
||
/** | ||
* Print the identity as a hexadecimal string. | ||
*/ | ||
toHexString(): string { | ||
return Array.prototype.map | ||
.call(this.data, (x) => ("00" + x.toString(16)).slice(-2)) | ||
.join(""); | ||
return this.data; | ||
} | ||
|
||
toUint8Array(): Uint8Array { | ||
return this.data; | ||
return hexStringToUint8Array(this.toHexString()); | ||
} | ||
|
||
/** | ||
* Parse an Identity from a hexadecimal string. | ||
*/ | ||
static fromString(str: string): Identity { | ||
let matches = str.match(/.{1,2}/g) || []; | ||
let data = Uint8Array.from( | ||
matches.map((byte: string) => parseInt(byte, 16)) | ||
); | ||
return new Identity(data); | ||
return new Identity({ __identity_bytes: str }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters