- Added Hash128 edge case

This commit is contained in:
AlexanderBuzz
2023-01-31 20:41:25 +01:00
parent 47f27d94fb
commit 6af6309b82
2 changed files with 16 additions and 5 deletions

View File

@@ -3,8 +3,7 @@
// Organize imports
const fs = require("fs")
const parseArgs = require('minimist')
const TxSerializer = require('./tx-serializer') // Main serialization logic can be found in this file
const TxSerializer = require('./tx-serializer')
function main(rawJson, verbose) {
const json = JSON.parse(rawJson)
@@ -37,7 +36,7 @@ const args = parseArgs(process.argv.slice(2), {
let rawJson
if (args.json) {
rawJson = args.json
main(rawJson)
main(rawJson, args.verbose)
} else if (args.stdin) {
const stdin = process.openStdin();
@@ -48,7 +47,7 @@ if (args.json) {
});
stdin.on('end', function() {
main(data)
main(data, args.verbose)
});
} else {
rawJson = fs.readFileSync(args.filename, 'utf8')

View File

@@ -373,6 +373,11 @@ class TxSerializer {
* @returns {string}
*/
hash128ToBytes(contents) {
if (/^0+$/.exec(contents)) {
// Edge case, an all-zero bytes input returns an empty string
return ""
}
const buffer = this.hashToBytes(contents)
if(buffer.length !== 16) {
// 16 bytes = 128 bits
@@ -585,11 +590,11 @@ class TxSerializer {
this._logger("Serializing field " + fieldName + " of type " + fieldType)
const idPrefix = this.fieldId(fieldName)
this._logger("ID Prefix is: " + idPrefix.toUpperCase())
// Special case: convert from string to UInt16
if (fieldName === "TransactionType") {
const fieldBytes = this.txTypeToBytes(fieldValue)
this._logger("ID Prefix is: " + idPrefix.toUpperCase())
this._logger(fieldName + ' : ' + fieldBytes.toUpperCase())
return idPrefix + fieldBytes
@@ -612,6 +617,13 @@ class TxSerializer {
const fieldBytes = dispatch[fieldType](fieldValue)
if (fieldBytes.length === 0) {
this._logger('Unset field: ' + fieldName)
return ''
}
this._logger("ID Prefix is: " + idPrefix.toUpperCase())
this._logger(fieldName + ': ' + fieldBytes.toUpperCase())
return idPrefix.toString("hex") + fieldBytes