-
Notifications
You must be signed in to change notification settings - Fork 55
/
VotingProcedure.ts
141 lines (123 loc) · 3.43 KB
/
VotingProcedure.ts
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
import * as Cardano from '../../../Cardano';
import { Anchor } from '../../Common/Anchor';
import { CborReader, CborReaderState, CborWriter } from '../../CBOR';
import { HexBlob } from '@cardano-sdk/util';
import { Vote } from '../../../Cardano';
import { hexToBytes } from '../../../util/misc';
const EMBEDDED_GROUP_SIZE = 2;
/**
* A voting procedure is a pair of:
*
* - a vote.
* - an anchor, it links the vote to arbitrary off-chain JSON payload of metadata.
*/
export class VotingProcedure {
#vote: Vote;
#anchor: Anchor | undefined;
#originalBytes: HexBlob | undefined = undefined;
/**
* Initialize a new instance of the VotingProcedure class.
*
* @param vote The vote (Yes, No or Abstain).
* @param anchor The vote anchor (or undefined if none).
*/
constructor(vote: Vote, anchor?: Anchor) {
this.#vote = vote;
this.#anchor = anchor;
}
/**
* Serializes a VotingProcedure into CBOR format.
*
* @returns The VotingProcedure in CBOR format.
*/
toCbor(): HexBlob {
if (this.#originalBytes) return this.#originalBytes;
const writer = new CborWriter();
// CDDL
// voting_procedure =
// [ vote
// , anchor / null
// ]
writer.writeStartArray(EMBEDDED_GROUP_SIZE);
writer.writeInt(this.#vote);
this.#anchor ? writer.writeEncodedValue(hexToBytes(this.#anchor.toCbor())) : writer.writeNull();
return writer.encodeAsHex();
}
/**
* Deserializes the VotingProcedure from a CBOR byte array.
*
* @param cbor The CBOR encoded VotingProcedure object.
* @returns The new VotingProcedure instance.
*/
static fromCbor(cbor: HexBlob): VotingProcedure {
const reader = new CborReader(cbor);
reader.readStartArray();
const vote = Number(reader.readInt());
let anchor;
if (reader.peekState() === CborReaderState.Null) {
reader.readNull();
anchor = undefined;
} else {
anchor = Anchor.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
}
const votingProcedure = new VotingProcedure(vote, anchor);
votingProcedure.#originalBytes = cbor;
return votingProcedure;
}
/**
* Creates a Core VotingProcedure object from the current VotingProcedure object.
*
* @returns The Core VotingProcedure object.
*/
toCore(): Cardano.VotingProcedure {
return {
anchor: this.#anchor ? this.#anchor.toCore() : null,
vote: this.#vote
};
}
/**
* Creates a VotingProcedure object from the given Core VotingProcedure object.
*
* @param votingProcedure The core VotingProcedure object.
*/
static fromCore(votingProcedure: Cardano.VotingProcedure): VotingProcedure {
return new VotingProcedure(
votingProcedure.vote,
votingProcedure.anchor ? Anchor.fromCore(votingProcedure.anchor) : undefined
);
}
/**
* Gets the vote.
*
* @returns The vote.
*/
vote(): Vote {
return this.#vote;
}
/**
* Sets the vote.
*
* @param vote The vote.
*/
setVote(vote: Vote) {
this.#vote = vote;
this.#originalBytes = undefined;
}
/**
* Gets the anchor, a link to arbitrary off-chain JSON payload of metadata.
*
* @returns The anchor.
*/
anchor(): Anchor | undefined {
return this.#anchor;
}
/**
* Sets the anchor, a link to arbitrary off-chain JSON payload of metadata.
*
* @param anchor The anchor.
*/
setAnchor(anchor: Anchor | undefined) {
this.#anchor = anchor;
this.#originalBytes = undefined;
}
}