Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 2x 4x 8x 8x 21x 21x 21x 21x 3x | import { BigNumber } from "ethers";
import { formatUnits, hexlify, toUtf8String } from "ethers/lib/utils";
import {
DATA_FEED_ID_BS,
DATA_PACKAGES_COUNT_BS,
DATA_POINTS_COUNT_BS,
DATA_POINT_VALUE_BYTE_SIZE_BS,
DEFAULT_NUM_VALUE_DECIMALS,
REDSTONE_MARKER_BS,
REDSTONE_MARKER_HEX,
SIGNATURE_BS,
TIMESTAMP_BS,
UNSIGNED_METADATA_BYTE_SIZE_BS,
} from "../common/redstone-constants";
import { convertBytesToNumber } from "../common/utils";
import { DataPackage } from "../data-package/DataPackage";
import { SignedDataPackage } from "../data-package/SignedDataPackage";
import { DataPoint } from "../data-point/DataPoint";
import { NumericDataPoint } from "../data-point/NumericDataPoint";
export interface RedstonePayloadParsingResult {
signedDataPackages: SignedDataPackage[];
unsignedMetadata: Uint8Array;
remainderPrefix: Uint8Array;
}
type SliceConfig = { negativeOffset: number; length: number };
export class RedstonePayloadParser {
// Last bytes of bytesData must contain valid redstone payload
constructor(private bytesData: Uint8Array) {}
parse(): RedstonePayloadParsingResult {
this.assertValidRedstoneMarker();
const unsignedMetadata = this.extractUnsignedMetadata();
let negativeOffset =
unsignedMetadata.length +
UNSIGNED_METADATA_BYTE_SIZE_BS +
REDSTONE_MARKER_BS;
const numberOfDataPackages = this.extractNumber({
negativeOffset,
length: DATA_PACKAGES_COUNT_BS,
});
negativeOffset += DATA_PACKAGES_COUNT_BS;
// Extracting all data packages
const signedDataPackages: SignedDataPackage[] = [];
for (let i = 0; i < numberOfDataPackages; i++) {
const signedDataPackage = this.extractSignedDataPackage(negativeOffset);
signedDataPackages.push(signedDataPackage);
negativeOffset += signedDataPackage.toBytes().length;
}
// Preparing remainder prefix bytes
const remainderPrefix = this.slice({
negativeOffset,
length: this.bytesData.length - negativeOffset,
});
signedDataPackages.reverse(); // reversing, because we read from the end
return {
signedDataPackages,
unsignedMetadata,
remainderPrefix,
};
}
extractUnsignedMetadata(): Uint8Array {
const unsignedMetadataSize = this.extractNumber({
negativeOffset: REDSTONE_MARKER_BS,
length: UNSIGNED_METADATA_BYTE_SIZE_BS,
});
return this.slice({
negativeOffset: REDSTONE_MARKER_BS + UNSIGNED_METADATA_BYTE_SIZE_BS,
length: unsignedMetadataSize,
});
}
assertValidRedstoneMarker() {
const redstoneMarker = this.slice({
negativeOffset: 0,
length: REDSTONE_MARKER_BS,
});
const redstoneMarkerHex = hexlify(redstoneMarker);
Iif (redstoneMarkerHex !== REDSTONE_MARKER_HEX) {
throw new Error(`Received invalid redstone marker: ${redstoneMarkerHex}`);
}
}
extractSignedDataPackage(initialNegativeOffset: number): SignedDataPackage {
// Extracting signature
let negativeOffset = initialNegativeOffset;
const signature = this.slice({
negativeOffset,
length: SIGNATURE_BS,
});
// Extracting number of data points
negativeOffset += SIGNATURE_BS;
const dataPointsCount = this.extractNumber({
negativeOffset,
length: DATA_POINTS_COUNT_BS,
});
// Extracting data points value byte size
negativeOffset += DATA_POINTS_COUNT_BS;
const dataPointsValueSize = this.extractNumber({
negativeOffset,
length: DATA_POINT_VALUE_BYTE_SIZE_BS,
});
// Extracting timestamp
negativeOffset += DATA_POINT_VALUE_BYTE_SIZE_BS;
const timestamp = this.extractNumber({
negativeOffset,
length: TIMESTAMP_BS,
});
// Extracting all data points
negativeOffset += TIMESTAMP_BS;
const dataPoints: DataPoint[] = [];
for (let i = 0; i < dataPointsCount; i++) {
// Extracting data point value
const dataPointValue = this.slice({
negativeOffset,
length: dataPointsValueSize,
});
// Extracting data feed id for data point
negativeOffset += dataPointsValueSize;
const dataFeedId = this.slice({
negativeOffset,
length: DATA_FEED_ID_BS,
});
// Shifting negative offset
negativeOffset += DATA_FEED_ID_BS;
// Building a data point
const dataPoint = RedstonePayloadParser.createDataPoint(
dataFeedId,
dataPointValue
);
// Collecting data point
// Using `unshift` instead of `push` because we read from the end
dataPoints.unshift(dataPoint);
}
return new SignedDataPackage(
new DataPackage(dataPoints, timestamp),
hexlify(signature)
);
}
private static createDataPoint(
dataFeedId: Uint8Array,
dataPointValue: Uint8Array
): DataPoint {
return new DataPoint(
toUtf8String(dataFeedId).replaceAll("\x00", ""),
dataPointValue
);
}
private extractNumber(sliceConfig: SliceConfig): number {
const bytesArr = this.slice(sliceConfig);
return convertBytesToNumber(bytesArr);
}
private slice(sliceConfig: SliceConfig): Uint8Array {
const { negativeOffset, length } = sliceConfig;
const end = this.bytesData.length - negativeOffset;
const start = end - length;
return this.bytesData.slice(start, end);
}
}
export const convertDataPointToNumericDataPoint = (
dataPoint: DataPoint,
decimals?: number
) =>
new NumericDataPoint({
value: Number(
formatUnits(
BigNumber.from(dataPoint.value),
decimals ?? DEFAULT_NUM_VALUE_DECIMALS
)
),
dataFeedId: dataPoint.dataFeedId,
decimals,
});
|