-
Notifications
You must be signed in to change notification settings - Fork 55
/
VoteRegistrationDelegation.ts
159 lines (131 loc) · 4.7 KB
/
VoteRegistrationDelegation.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as Cardano from '../../Cardano';
import * as Crypto from '@cardano-sdk/crypto';
import { CborReader, CborWriter } from '../CBOR';
import { CertificateKind } from './CertificateKind';
import { CertificateType } from '../../Cardano';
import { DRep } from './DRep';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
import { hexToBytes } from '../../util/misc';
const EMBEDDED_GROUP_SIZE = 2;
/** This certificate Register the stake key and delegate with a single certificate to a DRep. */
export class VoteRegistrationDelegation {
#credential: Cardano.Credential;
#dRep: DRep;
#deposit: Cardano.Lovelace;
#originalBytes: HexBlob | undefined = undefined;
/**
* Initializes a new instance of the VoteRegistrationDelegation class.
*
* @param stakeCredential The stake credential to delegate.
* @param deposit Must match the expected deposit amount specified by `ppKeyDepositL` in
* the protocol parameters.
* @param dRep The DRep to delegate to.
*/
constructor(stakeCredential: Cardano.Credential, deposit: Cardano.Lovelace, dRep: DRep) {
this.#credential = stakeCredential;
this.#deposit = deposit;
this.#dRep = dRep;
}
/**
* Serializes a VoteRegistrationDelegation into CBOR format.
*
* @returns The VoteRegistrationDelegation in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (this.#originalBytes) return this.#originalBytes;
// CDDL
// vote_reg_deleg_cert = (12, stake_credential, drep, coin)
writer.writeStartArray(4);
writer.writeInt(CertificateKind.VoteRegistrationDelegation);
// CDDL
// stake_credential =
// [ 0, addr_keyhash
// // 1, scripthash
// ]
writer.writeStartArray(EMBEDDED_GROUP_SIZE);
writer.writeInt(this.#credential.type);
writer.writeByteString(Buffer.from(this.#credential.hash, 'hex'));
writer.writeEncodedValue(hexToBytes(this.#dRep.toCbor()));
writer.writeInt(this.#deposit);
return writer.encodeAsHex();
}
/**
* Deserializes the VoteRegistrationDelegation from a CBOR byte array.
*
* @param cbor The CBOR encoded VoteRegistrationDelegation object.
* @returns The new VoteRegistrationDelegation instance.
*/
static fromCbor(cbor: HexBlob): VoteRegistrationDelegation {
const reader = new CborReader(cbor);
const length = reader.readStartArray();
if (length !== 4)
throw new InvalidArgumentError('cbor', `Expected an array of 4 elements, but got an array of ${length} elements`);
const kind = Number(reader.readInt());
if (kind !== CertificateKind.VoteRegistrationDelegation)
throw new InvalidArgumentError(
'cbor',
`Expected certificate kind ${CertificateKind.VoteRegistrationDelegation}, but got ${kind}`
);
const credLength = reader.readStartArray();
if (credLength !== EMBEDDED_GROUP_SIZE)
throw new InvalidArgumentError(
'cbor',
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
);
const type = Number(reader.readInt()) as Cardano.CredentialType;
const hash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
reader.readEndArray();
const dRep = DRep.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
const deposit = reader.readInt();
reader.readEndArray();
const cert = new VoteRegistrationDelegation({ hash, type }, deposit, dRep);
cert.#originalBytes = cbor;
return cert;
}
/**
* Creates a Core VoteRegistrationDelegationCertificate object from the current VoteRegistrationDelegation object.
*
* @returns The Core VoteRegistrationDelegationCertificate object.
*/
toCore(): Cardano.VoteRegistrationDelegationCertificate {
return {
__typename: CertificateType.VoteRegistrationDelegation,
dRep: this.#dRep.toCore(),
deposit: this.#deposit,
stakeCredential: this.#credential
};
}
/**
* Creates a VoteRegistrationDelegation object from the given Core VoteRegistrationDelegationCertificate object.
*
* @param deleg core VoteRegistrationDelegationCertificate object.
*/
static fromCore(deleg: Cardano.VoteRegistrationDelegationCertificate) {
return new VoteRegistrationDelegation(deleg.stakeCredential, deleg.deposit, DRep.fromCore(deleg.dRep));
}
/**
* Gets the stake credential being delegated.
*
* @returns The stake credential.
*/
stakeCredential(): Cardano.Credential {
return this.#credential;
}
/**
* Gets the deposit.
*
* @returns The deposit.
*/
deposit(): Cardano.Lovelace {
return this.#deposit;
}
/**
* Gets the DRep being delegated to.
*
* @returns The DRep.
*/
dRep(): DRep {
return this.#dRep;
}
}