-
Notifications
You must be signed in to change notification settings - Fork 55
/
Unregistration.ts
169 lines (141 loc) · 4.67 KB
/
Unregistration.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
160
161
162
163
164
165
166
167
168
169
import * as Cardano from '../../Cardano';
import * as Crypto from '@cardano-sdk/crypto';
import { CborReader, CborWriter } from '../CBOR';
import { CertificateKind } from './CertificateKind';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
const EMBEDDED_GROUP_SIZE = 2;
/**
* This certificate is used when a stakeholder no longer wants to participate in
* staking. It revokes the stake Unregistration and the associated stake is no
* longer counted when calculating stake pool rewards.
*
* Deposit must match the expected deposit amount specified by `ppKeyDepositL` in
* the protocol parameters.
*
* Remark: Replaces the deprecated `StakeDeregistration` in after Conway era.
*/
export class Unregistration {
#credential: Cardano.Credential;
#deposit: Cardano.Lovelace;
#originalBytes: HexBlob | undefined = undefined;
/**
* Initializes a new instance of the Unregistration class.
*
* @param credential The stake credential.
* @param deposit Must match the expected deposit amount specified by `ppKeyDepositL` in
* the protocol parameters.
*/
constructor(credential: Cardano.Credential, deposit: Cardano.Lovelace) {
this.#credential = credential;
this.#deposit = deposit;
}
/**
* Serializes a Unregistration into CBOR format.
*
* @returns The Unregistration in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (this.#originalBytes) return this.#originalBytes;
// CDDL
// unreg_cert = (8, stake_credential, coin)
writer.writeStartArray(3);
writer.writeInt(CertificateKind.Unregistration);
// 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.writeInt(this.#deposit);
return writer.encodeAsHex();
}
/**
* Deserializes the Unregistration from a CBOR byte array.
*
* @param cbor The CBOR encoded Unregistration object.
* @returns The new Unregistration instance.
*/
static fromCbor(cbor: HexBlob): Unregistration {
const reader = new CborReader(cbor);
const length = reader.readStartArray();
if (length !== 3)
throw new InvalidArgumentError('cbor', `Expected an array of 3 elements, but got an array of ${length} elements`);
const kind = Number(reader.readInt());
if (kind !== CertificateKind.Unregistration)
throw new InvalidArgumentError(
'cbor',
`Expected certificate kind ${CertificateKind.Unregistration}, 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 deposit = reader.readInt();
reader.readEndArray();
const cert = new Unregistration({ hash, type }, deposit);
cert.#originalBytes = cbor;
return cert;
}
/**
* Creates a Core NewStakeAddressCertificate object from the current Unregistration object.
*
* @returns The Core NewStakeAddressCertificate object.
*/
toCore(): Cardano.NewStakeAddressCertificate {
return {
__typename: Cardano.CertificateType.Unregistration,
deposit: this.#deposit,
stakeCredential: this.#credential
};
}
/**
* Creates a Unregistration object from the given Core StakeAddressCertificate object.
*
* @param cert core StakeAddressCertificate object.
*/
static fromCore(cert: Cardano.NewStakeAddressCertificate) {
return new Unregistration(cert.stakeCredential, cert.deposit);
}
/**
* Gets the stake credential from this certificate.
*
* @returns The stake credential.
*/
stakeCredential(): Cardano.Credential {
return this.#credential;
}
/**
* Sets the stake credential from this certificate.
*
* @param credential The stake credential.
*/
setStakeCredential(credential: Cardano.Credential): void {
this.#credential = credential;
this.#originalBytes = undefined;
}
/**
* Gets the deposit from this certificate.
*
* @returns The stake credential.
*/
deposit(): Cardano.Lovelace {
return this.#deposit;
}
/**
* Sets the deposit from this certificate.
*
* @param deposit The deposit.
*/
setDeposit(deposit: Cardano.Lovelace): void {
this.#deposit = deposit;
this.#originalBytes = undefined;
}
}