-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#266 - Added Map File to file structure.
1. Added Map File to file structure. 1. Increased code coverage of `TransactionProcessor` to 100%. 1. Some code refactoring.
- Loading branch information
1 parent
b4f059b
commit 170964a
Showing
17 changed files
with
294 additions
and
130 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
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
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
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,54 @@ | ||
import Compressor from './util/Compressor'; | ||
import ErrorCode from './ErrorCode'; | ||
import JsonAsync from './util/JsonAsync'; | ||
import MapFileModel from './models/MapFileModel'; | ||
import SidetreeError from '../../SidetreeError'; | ||
|
||
/** | ||
* Class containing Map File related operations. | ||
*/ | ||
export default class MapFile { | ||
/** | ||
* Parses and validates the given map file buffer. | ||
* @throws `SidetreeError` if failed parsing or validation. | ||
*/ | ||
public static async parseAndValidate (mapFileBuffer: Buffer): Promise<MapFileModel> { | ||
|
||
let decompressedBuffer; | ||
try { | ||
decompressedBuffer = await Compressor.decompress(mapFileBuffer); | ||
} catch (error) { | ||
throw SidetreeError.createFromError(ErrorCode.MapFileDecompressionFailure, error); | ||
} | ||
|
||
let mapFile; | ||
try { | ||
mapFile = await JsonAsync.parse(decompressedBuffer); | ||
} catch (error) { | ||
throw SidetreeError.createFromError(ErrorCode.MapFileNotJson, error); | ||
} | ||
|
||
const mapFileProperties = Object.keys(mapFile); | ||
if (mapFileProperties.length > 1) { | ||
throw new SidetreeError(ErrorCode.MapFileHasUnknownProperty); | ||
} | ||
|
||
if (typeof mapFile.batchFileHash !== 'string') { | ||
throw new SidetreeError(ErrorCode.MapFileBatchFileHashMissingOrIncorrectType); | ||
} | ||
|
||
return mapFile; | ||
} | ||
|
||
/** | ||
* Creates the Map File buffer. | ||
*/ | ||
public static async createBuffer (batchFileHash: string): Promise<Buffer> { | ||
const mapFileModel = { batchFileHash }; | ||
|
||
const rawData = JSON.stringify(mapFileModel); | ||
const compressedRawData = await Compressor.compress(Buffer.from(rawData)); | ||
|
||
return compressedRawData; | ||
} | ||
} |
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
Oops, something went wrong.