Skip to content

Commit

Permalink
fix: possible incorrect decoding
Browse files Browse the repository at this point in the history
fix #12
  • Loading branch information
akiver committed Jan 26, 2019
1 parent 41bde84 commit 277509a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ function bytesToHex(bytes: number[]) {
}

/**
* Convert a hexadecimal string into a byte array
* Convert a BigNumber into a byte array.
*/
function hexToBytes(str: string) {
let array = []
for (var i = 0, j = 0; i < str.length; i += 2, j++) {
array[j] = parseInt('0x' + str.substr(i, 2))
function bigNumberToByteArray(big: BigNumber) {
let str = big.toString(16)
if (str.length % 2 > 0) {
str = '0' + str
}
const bytes = []
for (let i = 0; i < str.length; i += 2) {
bytes.push(parseInt(str.slice(i, i + 2), 16))
}

return array
return bytes
}

/**
Expand Down Expand Up @@ -125,7 +129,7 @@ const decode = (shareCode: string): Sharecode => {
big = big.multipliedBy(DICTIONARY_LENGTH).plus(DICTIONARY.indexOf(chars[i]))
}

const bytes = hexToBytes(big.toString(16))
const bytes = bigNumberToByteArray(big)

return {
matchId: {
Expand Down

0 comments on commit 277509a

Please sign in to comment.