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 | 1x 1x 1x 1x 1x 1x 67x 67x 67x 67x 67x 67x 123x 67x 67x 67x 67x 67x 67x 72x 72x 31x 1x 30x 5x 5x 5x 2x 5x 45x 62x 62x 62x 62x 62x 132x 62x 62x 62x 62x 62x 62x 62x | import { BufferReader, BufferWriter } from '@node-dlc/bufio';
import { MessageType } from '../MessageType';
import { getTlv } from '../serialize/getTlv';
import { IDlcMessage } from './DlcMessage';
import {
DigitDecompositionEventDescriptorV0,
EventDescriptor,
IDigitDecompositionEventDescriptorV0JSON,
IEnumEventDescriptorV0JSON,
} from './EventDescriptor';
/**
* For users to be able to create DLCs based on a given event, they also
* need to obtain information about the oracle and the time at which it
* plans on releasing a signature over the event outcome. OracleEvent
* mesages contain such information, which includes:
* - the nonce(s) that will be used to sign the event outcome(s)
* - the earliest time (UTC) at which it plans on releasing a signature
* over the event outcome, in epoch seconds
* - the event descriptor
* - the event ID which can be a name or categorization associated with
* the event by the oracle
*/
export class OracleEventV0 implements IDlcMessage {
public static type = MessageType.OracleEventV0;
/**
* Deserializes an oracle_event message
* @param buf
*/
public static deserialize(buf: Buffer): OracleEventV0 {
const instance = new OracleEventV0();
const reader = new BufferReader(buf);
reader.readBigSize(); // read type
instance.length = reader.readBigSize();
const nonceCount = reader.readUInt16BE();
for (let i = 0; i < nonceCount; i++) {
instance.oracleNonces.push(reader.readBytes(32));
}
instance.eventMaturityEpoch = reader.readUInt32BE();
instance.eventDescriptor = EventDescriptor.deserialize(getTlv(reader));
const eventIdLength = reader.readBigSize();
const eventIdBuf = reader.readBytes(Number(eventIdLength));
instance.eventId = eventIdBuf.toString();
return instance;
}
/**
* The type for oracle_event message. oracle_event = 55330
*/
public type = OracleEventV0.type;
public length: bigint;
public oracleNonces: Buffer[] = [];
public eventMaturityEpoch: number;
public eventDescriptor: EventDescriptor;
public eventId: string;
/**
* Validates correctness of all fields in the message
* https://github.com/discreetlogcontracts/dlcspecs/blob/master/Oracle.md
* @throws Will throw an error if validation fails
*/
public validate(): void {
if (this.eventMaturityEpoch < 0) {
throw new Error('eventMaturityEpoch must be greater than or equal to 0');
}
if (
this.eventDescriptor.type === DigitDecompositionEventDescriptorV0.type
) {
const eventDescriptor = this
.eventDescriptor as DigitDecompositionEventDescriptorV0;
eventDescriptor.validate();
if (eventDescriptor.nbDigits !== this.oracleNonces.length) {
throw Error(
'OracleEvent oracleNonces length must match DigitDecompositionEventDescriptor nbDigits',
);
}
}
}
/**
* Converts oracle_event to JSON
*/
public toJSON(): IOracleEventV0JSON {
return {
type: this.type,
oracleNonces: this.oracleNonces.map((oracle) => oracle.toString('hex')),
eventMaturityEpoch: this.eventMaturityEpoch,
eventDescriptor: this.eventDescriptor.toJSON(),
eventId: this.eventId,
};
}
/**
* Serializes the oracle_event message into a Buffer
*/
public serialize(): Buffer {
const writer = new BufferWriter();
writer.writeBigSize(this.type);
const dataWriter = new BufferWriter();
dataWriter.writeUInt16BE(this.oracleNonces.length);
for (const nonce of this.oracleNonces) {
dataWriter.writeBytes(nonce);
}
dataWriter.writeUInt32BE(this.eventMaturityEpoch);
dataWriter.writeBytes(this.eventDescriptor.serialize());
dataWriter.writeBigSize(this.eventId.length);
dataWriter.writeBytes(Buffer.from(this.eventId));
writer.writeBigSize(dataWriter.size);
writer.writeBytes(dataWriter.toBuffer());
return writer.toBuffer();
}
}
export interface IOracleEventV0JSON {
type: number;
oracleNonces: string[];
eventMaturityEpoch: number;
eventDescriptor:
| IEnumEventDescriptorV0JSON
| IDigitDecompositionEventDescriptorV0JSON;
eventId: string;
}
|