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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | 1x 1x 1x 1x 1x 1x 58x 58x 58x 56x 2x 1x 1x 56x 56x 56x 56x 56x 56x 168x 56x 57x 57x 3x 9x 28x 28x 28x 28x 28x 84x 84x 28x 28x 28x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 5x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { BufferReader, BufferWriter } from '@node-dlc/bufio';
import { MessageType } from '../MessageType';
import { getTlv } from '../serialize/getTlv';
import { IDlcMessage } from './DlcMessage';
import { PayoutFunction, PayoutFunctionV0JSON } from './PayoutFunction';
import {
IRoundingIntervalsV0JSON,
RoundingIntervalsV0,
} from './RoundingIntervalsV0';
export abstract class ContractDescriptor {
public static deserialize(
buf: Buffer,
): ContractDescriptorV0 | ContractDescriptorV1 {
const reader = new BufferReader(buf);
const type = Number(reader.readBigSize());
switch (type) {
case MessageType.ContractDescriptorV0:
return ContractDescriptorV0.deserialize(buf);
case MessageType.ContractDescriptorV1:
return ContractDescriptorV1.deserialize(buf);
default:
throw new Error(
`Contract Descriptor TLV type must be ContractDescriptorV0 or ContractDescriptorV1`,
);
}
}
public abstract type: number;
public abstract length: bigint;
public abstract toJSON(): ContractDescriptorV0JSON | ContractDescriptorV1JSON;
public abstract serialize(): Buffer;
}
/**
* ContractDescriptor V0 contains information about a contract's outcomes
* and their corresponding payouts.
*/
export class ContractDescriptorV0
extends ContractDescriptor
implements IDlcMessage {
public static type = MessageType.ContractDescriptorV0;
/**
* Deserializes an contract_descriptor_v0 message
* @param buf
*/
public static deserialize(buf: Buffer): ContractDescriptorV0 {
const instance = new ContractDescriptorV0();
const reader = new BufferReader(buf);
reader.readBigSize(); // read type
instance.length = reader.readBigSize();
reader.readBigSize(); // num_outcomes
while (!reader.eof) {
instance.outcomes.push({
outcome: reader.readBytes(32),
localPayout: reader.readUInt64BE(),
});
}
return instance;
}
/**
* The type for contract_descriptor_v0 message. contract_descriptor_v0 = 42768
*/
public type = ContractDescriptorV0.type;
public length: bigint;
public outcomes: IOutcome[] = [];
/**
* Converts contract_descriptor_v0 to JSON
*/
public toJSON(): ContractDescriptorV0JSON {
return {
type: this.type,
outcomes: this.outcomes.map((outcome) => {
return {
outcome: outcome.outcome.toString('hex'),
localPayout: Number(outcome.localPayout),
};
}),
};
}
/**
* Serializes the contract_descriptor_v0 message into a Buffer
*/
public serialize(): Buffer {
const writer = new BufferWriter();
writer.writeBigSize(this.type);
const dataWriter = new BufferWriter();
dataWriter.writeBigSize(this.outcomes.length);
for (const outcome of this.outcomes) {
dataWriter.writeBytes(outcome.outcome);
dataWriter.writeUInt64BE(outcome.localPayout);
}
writer.writeBigSize(dataWriter.size);
writer.writeBytes(dataWriter.toBuffer());
return writer.toBuffer();
}
}
/**
* ContractDescriptor V1 contains information about a contract's outcomes
* and their corresponding payouts.
*/
export class ContractDescriptorV1
extends ContractDescriptor
implements IDlcMessage {
public static type = MessageType.ContractDescriptorV1;
/**
* Deserializes an contract_descriptor_v1 message
* @param buf
*/
public static deserialize(buf: Buffer): ContractDescriptorV1 {
const instance = new ContractDescriptorV1();
const reader = new BufferReader(buf);
reader.readBigSize(); // read type
instance.length = reader.readBigSize();
instance.numDigits = reader.readUInt16BE(); // num_digits
instance.payoutFunction = PayoutFunction.deserialize(getTlv(reader));
instance.roundingIntervals = RoundingIntervalsV0.deserialize(
getTlv(reader),
);
return instance;
}
/**
* The type for contract_descriptor_v1 message. contract_descriptor_v1 = 42784
*/
public type = ContractDescriptorV1.type;
public length: bigint;
public numDigits: number;
public payoutFunction: PayoutFunction;
public roundingIntervals: RoundingIntervalsV0;
/**
* Validates correctness of all fields in the message
* https://github.com/discreetlogcontracts/dlcspecs/blob/master/Messaging.md#the-contract_descriptor-type
* @throws Will throw an error if validation fails
*/
public validate(): void {
this.roundingIntervals.validate();
}
/**
* Converts contract_descriptor_v1 to JSON
*/
public toJSON(): ContractDescriptorV1JSON {
return {
type: this.type,
numDigits: this.numDigits,
payoutFunction: this.payoutFunction.toJSON(),
roundingIntervals: this.roundingIntervals.toJSON(),
};
}
/**
* Serializes the contract_descriptor_v1 message into a Buffer
*/
public serialize(): Buffer {
const writer = new BufferWriter();
writer.writeBigSize(this.type);
const dataWriter = new BufferWriter();
dataWriter.writeUInt16BE(this.numDigits);
dataWriter.writeBytes(this.payoutFunction.serialize());
dataWriter.writeBytes(this.roundingIntervals.serialize());
writer.writeBigSize(dataWriter.size);
writer.writeBytes(dataWriter.toBuffer());
return writer.toBuffer();
}
}
interface IOutcome {
outcome: Buffer;
localPayout: bigint;
}
interface IOutcomeJSON {
outcome: string;
localPayout: number;
}
export interface ContractDescriptorV0JSON {
type: number;
outcomes: IOutcomeJSON[];
}
export interface ContractDescriptorV1JSON {
type: number;
numDigits: number;
payoutFunction: PayoutFunctionV0JSON;
roundingIntervals: IRoundingIntervalsV0JSON;
}
|