-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e09d01c
commit e138f28
Showing
1 changed file
with
26 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
object_id_into_uint8array = function(id, arr) { | ||
// passed array is exactly 16 bytes -- 12 for id, 3 blank, 1 for string pad length | ||
// initialize all of them so that we can assume they're initialized on the rust side | ||
const padding = id.length; | ||
let skip_bytes = 12 - Math.ceil(padding / 2); | ||
|
||
for (let byte = 0; byte < skip_bytes; byte++) { | ||
arr[byte] = 0; | ||
} | ||
|
||
const offset = padding % 2; | ||
// if there's an odd number of characters, grab one for the next byte | ||
if (offset === 1) { | ||
arr[skip_bytes] = parseInt(id.substr(0, 1), 16) | ||
skip_bytes++; | ||
} | ||
|
||
for (let byte = 0; byte < 12 - skip_bytes; byte++) { | ||
arr[byte + skip_bytes] = parseInt(id.substr(byte * 2 + offset, 2), 16) | ||
} | ||
|
||
arr[12] = 0; | ||
arr[13] = 0; | ||
arr[14] = 0; | ||
arr[15] = padding; | ||
} |