|
| 1 | +import { |
| 2 | + flattenBinArray, |
| 3 | + formatError, |
| 4 | + numberToBinUint32LE, |
| 5 | + readMultiple |
| 6 | +} from "../lib.js"; |
| 7 | +import { |
| 8 | + type MaybeReadResult, |
| 9 | + type ReadPosition, |
| 10 | +} from "../lib.js" |
| 11 | +import { |
| 12 | + readBytes, |
| 13 | + readUint32LE, |
| 14 | +} from "./read-components.js"; |
| 15 | + |
| 16 | +const SHA256HASHLEN = 32; |
| 17 | + |
| 18 | +export enum HeaderDecodingError { |
| 19 | + version = "Error reading version.", |
| 20 | + previousBlock = "Error reading previous block.", |
| 21 | + merkleRootHash = "Error reading merkle root hash", |
| 22 | + time = "Error reading time", |
| 23 | + difficultyTarget = "Error reading difficulty target", |
| 24 | + nonce = "Error reading nonce", |
| 25 | + generic = "Error reading header.", |
| 26 | + endsWithUnexpectedBytes = "Error decoding header: the provided header includes unexpected bytes.", |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Represents the header of a block in a blockchain. |
| 31 | + */ |
| 32 | +export type BlockHeader = { |
| 33 | + /** |
| 34 | + * The version of the block. |
| 35 | + */ |
| 36 | + version: number; |
| 37 | + |
| 38 | + /** |
| 39 | + * The hash of the previous block in the blockchain. |
| 40 | + */ |
| 41 | + previousBlockHash: Uint8Array; |
| 42 | + |
| 43 | + /** |
| 44 | + * The hash of the Merkle root of the transactions in the block. |
| 45 | + */ |
| 46 | + merkleRootHash: Uint8Array; |
| 47 | + |
| 48 | + /** |
| 49 | + * The Unix epoch time at which the block was created. |
| 50 | + */ |
| 51 | + time: number; |
| 52 | + |
| 53 | + /** |
| 54 | + * The target value for the block's proof-of-work. |
| 55 | + */ |
| 56 | + difficultyTarget: number; |
| 57 | + |
| 58 | + /** |
| 59 | + * A random value used in the proof-of-work calculation. |
| 60 | + */ |
| 61 | + nonce: number; |
| 62 | +}; |
| 63 | + |
| 64 | +/** |
| 65 | + * Attempts to read a BlockHeader from the provided binary data at the given position. |
| 66 | + * |
| 67 | + * @param {ReadPosition} position - The position in the binary data from which to start reading. |
| 68 | + * @returns {MaybeReadResult<BlockHeader>} A parsed BlockHeader object if successful, or an error message if not. |
| 69 | + */ |
| 70 | +export const readHeader = ( |
| 71 | + position: ReadPosition, |
| 72 | +): MaybeReadResult<BlockHeader> => { |
| 73 | + const headerRead = readMultiple(position, [ |
| 74 | + readUint32LE, |
| 75 | + readBytes(SHA256HASHLEN), // previous block hash |
| 76 | + readBytes(SHA256HASHLEN), // merkle root |
| 77 | + readUint32LE, // Unix epoch time |
| 78 | + readUint32LE, // target difficulty A.K.A bits |
| 79 | + readUint32LE, // nonce |
| 80 | + ]); |
| 81 | + if (typeof headerRead === "string") { |
| 82 | + return formatError(HeaderDecodingError.generic, headerRead); |
| 83 | + } |
| 84 | + const { |
| 85 | + position: nextPosition, |
| 86 | + result: [ |
| 87 | + version, |
| 88 | + previousBlockHash, |
| 89 | + merkleRootHash, |
| 90 | + time, |
| 91 | + difficultyTarget, |
| 92 | + nonce, |
| 93 | + ], |
| 94 | + } = headerRead; |
| 95 | + return { |
| 96 | + position: nextPosition, |
| 97 | + result: { |
| 98 | + version, |
| 99 | + previousBlockHash: previousBlockHash.reverse(), |
| 100 | + merkleRootHash: merkleRootHash.reverse(), |
| 101 | + time, |
| 102 | + difficultyTarget, |
| 103 | + nonce, |
| 104 | + }, |
| 105 | + }; |
| 106 | +}; |
| 107 | + |
| 108 | +/** |
| 109 | + * Decodes a BlockHeader from a given Uint8Array containing its binary representation. |
| 110 | + * |
| 111 | + * @param {Uint8Array} bin - The binary data containing the encoded BlockHeader. |
| 112 | + * @returns {BlockHeader | string} A parsed BlockHeader object if successful, or an error message if not. |
| 113 | + */ |
| 114 | +export const decodeHeader = (bin: Uint8Array): BlockHeader | string => { |
| 115 | + const headerRead = readHeader({ bin, index: 0 }); |
| 116 | + if (typeof headerRead === "string") { |
| 117 | + return headerRead; |
| 118 | + } |
| 119 | + if (headerRead.position.index !== bin.length) { |
| 120 | + return formatError( |
| 121 | + HeaderDecodingError.endsWithUnexpectedBytes, |
| 122 | + `Encoded header ends at index ${headerRead.position.index - 1}, leaving ${bin.length - headerRead.position.index |
| 123 | + } remaining bytes.`, |
| 124 | + ); |
| 125 | + } |
| 126 | + return headerRead.result; |
| 127 | +}; |
| 128 | + |
| 129 | +/** |
| 130 | + * Encodes a BlockHeader object into its binary representation. |
| 131 | + * |
| 132 | + * This function takes a `BlockHeader` object and returns a new `Uint8Array` containing its |
| 133 | + * serialized form. The encoding process follows the little-endian convention for all numerical |
| 134 | + * values (version, time, difficultyTarget, and nonce). |
| 135 | + * |
| 136 | + * @param {BlockHeader} header - The BlockHeader object to encode. |
| 137 | + * @returns {Uint8Array} A new Uint8Array containing the binary representation of the BlockHeader. |
| 138 | + */ |
| 139 | +export const encodeHeader = (header: BlockHeader) => |
| 140 | + flattenBinArray([ |
| 141 | + numberToBinUint32LE(header.version), |
| 142 | + header.previousBlockHash.reverse(), |
| 143 | + header.merkleRootHash.reverse(), |
| 144 | + numberToBinUint32LE(header.time), |
| 145 | + numberToBinUint32LE(header.difficultyTarget), |
| 146 | + numberToBinUint32LE(header.nonce), |
| 147 | + ]); |
0 commit comments