| 471 | } |
| 472 | |
| 473 | _toNumber(buffer, significantBytes) { |
| 474 | // Convert a token's byte array to a number in order to perform computations. |
| 475 | // This depends on the number of significant bytes that is used to normalize all tokens |
| 476 | // to the same size. For example if the token is 0x01 but significant bytes is 2, the |
| 477 | // result is 0x0100. |
| 478 | let target = buffer; |
| 479 | if(buffer.length !== significantBytes) { |
| 480 | target = Buffer.alloc(significantBytes); |
| 481 | buffer.copy(target); |
| 482 | } |
| 483 | |
| 484 | // similar to Integer.fromBuffer except we force the sign to 0. |
| 485 | const bits = new Array(Math.ceil(target.length / 4)); |
| 486 | for (let i = 0; i < bits.length; i++) { |
| 487 | let offset = target.length - ((i + 1) * 4); |
| 488 | let value; |
| 489 | if (offset < 0) { |
| 490 | //The buffer length is not multiple of 4 |
| 491 | offset = offset + 4; |
| 492 | value = 0; |
| 493 | for (let j = 0; j < offset; j++) { |
| 494 | const byte = target[j]; |
| 495 | value = value | (byte << (offset - j - 1) * 8); |
| 496 | } |
| 497 | } |
| 498 | else { |
| 499 | value = target.readInt32BE(offset); |
| 500 | } |
| 501 | bits[i] = value; |
| 502 | } |
| 503 | return new Integer(bits, 0); |
| 504 | } |
| 505 | |
| 506 | _toBuffer(number, significantBytes) { |
| 507 | // Convert numeric representation back to a buffer. |