-
Notifications
You must be signed in to change notification settings - Fork 55
/
UpdateDelegateRepresentative.ts
159 lines (131 loc) · 4.5 KB
/
UpdateDelegateRepresentative.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 { Anchor } from '../Common';
import { CborReader, CborReaderState, CborWriter } from '../CBOR';
import { CertificateKind } from './CertificateKind';
import { CertificateType } from '../../Cardano';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
import { hexToBytes } from '../../util/misc';
const EMBEDDED_GROUP_SIZE = 2;
/** Updates the DRep anchored metadata. */
export class UpdateDelegateRepresentative {
#drepCredential: Cardano.Credential;
#anchor: Anchor | undefined;
#originalBytes: HexBlob | undefined = undefined;
/**
* Initializes a new instance of the UpdateDelegateRepresentative class.
*
* @param drepCredential The DRep credential.
* @param anchor The anchor.
*/
constructor(drepCredential: Cardano.Credential, anchor?: Anchor) {
this.#drepCredential = drepCredential;
this.#anchor = anchor;
}
/**
* Serializes a UpdateDelegateRepresentative into CBOR format.
*
* @returns The UpdateDelegateRepresentative in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (this.#originalBytes) return this.#originalBytes;
// CDDL
// update_drep_cert = (18, drep_credential, anchor / null)
writer.writeStartArray(3);
writer.writeInt(CertificateKind.UpdateDrep);
// CDDL
// stake_credential =
// [ 0, addr_keyhash
// // 1, scripthash
// ]
writer.writeStartArray(EMBEDDED_GROUP_SIZE);
writer.writeInt(this.#drepCredential.type);
writer.writeByteString(Buffer.from(this.#drepCredential.hash, 'hex'));
if (this.#anchor) {
writer.writeEncodedValue(hexToBytes(this.#anchor.toCbor()));
} else {
writer.writeNull();
}
return writer.encodeAsHex();
}
/**
* Deserializes the UpdateDelegateRepresentative from a CBOR byte array.
*
* @param cbor The CBOR encoded UpdateDelegateRepresentative object.
* @returns The new UpdateDelegateRepresentative instance.
*/
static fromCbor(cbor: HexBlob): UpdateDelegateRepresentative {
const reader = new CborReader(cbor);
const length = reader.readStartArray();
if (length !== 3)
throw new InvalidArgumentError(
'cbor',
`Expected an array of ${EMBEDDED_GROUP_SIZE} elements, but got an array of ${length} elements`
);
const kind = Number(reader.readInt());
if (kind !== CertificateKind.UpdateDrep)
throw new InvalidArgumentError(
'cbor',
`Expected certificate kind ${CertificateKind.UpdateDrep}, 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();
let anchor;
if (reader.peekState() === CborReaderState.Null) {
reader.readNull();
} else {
anchor = Anchor.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
}
reader.readEndArray();
const cert = new UpdateDelegateRepresentative({ hash, type }, anchor);
cert.#originalBytes = cbor;
return cert;
}
/**
* Creates a Core UpdateDelegateRepresentativeCertificate object from the current UpdateDelegateRepresentative object.
*
* @returns The Core UpdateDelegateRepresentativeCertificate object.
*/
toCore(): Cardano.UpdateDelegateRepresentativeCertificate {
return {
__typename: CertificateType.UpdateDelegateRepresentative,
anchor: this.#anchor ? this.#anchor.toCore() : null,
dRepCredential: this.#drepCredential
};
}
/**
* Creates a UpdateDelegateRepresentative object from the given Core UpdateDelegateRepresentativeCertificate object.
*
* @param cert core UpdateDelegateRepresentativeCertificate object.
*/
static fromCore(cert: Cardano.UpdateDelegateRepresentativeCertificate) {
return new UpdateDelegateRepresentative(
cert.dRepCredential,
cert.anchor ? Anchor.fromCore(cert.anchor) : undefined
);
}
/**
* Gets DRep credential.
*
* @returns The DRep credential.
*/
credential(): Cardano.Credential {
return this.#drepCredential;
}
/**
* Gets the anchor.
*
* @returns The anchor.
*/
anchor(): Anchor | undefined {
return this.#anchor;
}
}