-
Notifications
You must be signed in to change notification settings - Fork 55
/
GenesisKeyDelegation.ts
185 lines (159 loc) · 6.45 KB
/
GenesisKeyDelegation.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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 = 4;
/**
* This certificate is used to delegate from a Genesis key to a set of keys. This was primarily used in the early
* phases of the Cardano network's existence during the transition from the Byron to the Shelley era.
*/
export class GenesisKeyDelegation {
#genesisHash: Crypto.Hash28ByteBase16;
#genesisDelegateHash: Crypto.Hash28ByteBase16;
#vrfKeyHash: Crypto.Hash32ByteBase16;
#originalBytes: HexBlob | undefined = undefined;
/**
* Initializes a new instance of the GenesisKeyDelegation class.
*
* @param genesisHash The genesis block of the particular blockchain network, which is
* a unique identifier for that chain. This is used to confirm that the delegation is
* occurring on the intended blockchain.
* @param genesisDelegateHash This is the public key hash of the delegate to whom the
* power of the genesis key is being delegated. In the transitional phase from the Byron
* era to the Shelley era, the holders of the genesis keys used a GenesisKeyDelegation
* certificate to delegate their rights to produce blocks to a specific set of new keys (genesis delegates).
* @param vrfKeyHash This is the hash of the Verifiable Random Function (VRF) key of the delegate.
*/
constructor(
genesisHash: Crypto.Hash28ByteBase16,
genesisDelegateHash: Crypto.Hash28ByteBase16,
vrfKeyHash: Crypto.Hash32ByteBase16
) {
this.#genesisHash = genesisHash;
this.#genesisDelegateHash = genesisDelegateHash;
this.#vrfKeyHash = vrfKeyHash;
}
/**
* Serializes a GenesisKeyDelegation into CBOR format.
*
* @returns The GenesisKeyDelegation in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (this.#originalBytes) return this.#originalBytes;
// CDDL
// genesis_key_delegation = (5, genesishash, genesis_delegate_hash, vrf_keyhash)
writer.writeStartArray(EMBEDDED_GROUP_SIZE);
writer.writeInt(CertificateKind.GenesisKeyDelegation);
writer.writeByteString(Buffer.from(this.#genesisHash, 'hex'));
writer.writeByteString(Buffer.from(this.#genesisDelegateHash, 'hex'));
writer.writeByteString(Buffer.from(this.#vrfKeyHash, 'hex'));
return writer.encodeAsHex();
}
/**
* Deserializes the GenesisKeyDelegation from a CBOR byte array.
*
* @param cbor The CBOR encoded GenesisKeyDelegation object.
* @returns The new GenesisKeyDelegation instance.
*/
static fromCbor(cbor: HexBlob): GenesisKeyDelegation {
const reader = new CborReader(cbor);
const length = reader.readStartArray();
if (length !== EMBEDDED_GROUP_SIZE)
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.GenesisKeyDelegation)
throw new InvalidArgumentError(
'cbor',
`Expected certificate kind ${CertificateKind.GenesisKeyDelegation}, but got ${kind}`
);
const genesisHash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
const genesisDelegateHash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
const vrfKeyHash = Crypto.Hash32ByteBase16(HexBlob.fromBytes(reader.readByteString()));
reader.readEndArray();
const cert = new GenesisKeyDelegation(genesisHash, genesisDelegateHash, vrfKeyHash);
cert.#originalBytes = cbor;
return cert;
}
/**
* Creates a Core GenesisKeyDelegationCertificate object from the current GenesisKeyDelegation object.
*
* @returns The Core GenesisKeyDelegationCertificate object.
*/
toCore(): Cardano.GenesisKeyDelegationCertificate {
return {
__typename: Cardano.CertificateType.GenesisKeyDelegation,
genesisDelegateHash: this.#genesisDelegateHash,
genesisHash: this.#genesisHash,
vrfKeyHash: this.#vrfKeyHash
};
}
/**
* Creates a GenesisKeyDelegation object from the given Core GenesisKeyDelegationCertificate object.
*
* @param cert core GenesisKeyDelegationCertificate object.
*/
static fromCore(cert: Cardano.GenesisKeyDelegationCertificate) {
return new GenesisKeyDelegation(cert.genesisHash, cert.genesisDelegateHash, cert.vrfKeyHash);
}
/**
* Gets the genesis block of the particular blockchain network, which is
* a unique identifier for that chain. This is used to confirm that the delegation is
* occurring on the intended blockchain.
*
* @returns The genesis hash.
*/
genesisHash(): Crypto.Hash28ByteBase16 {
return this.#genesisHash;
}
/**
* Sets the genesis block of the particular blockchain network.
*
* @param genesisHash The genesis hash.
*/
setGenesisHash(genesisHash: Crypto.Hash28ByteBase16): void {
this.#genesisHash = genesisHash;
this.#originalBytes = undefined;
}
/**
* Gets the public key hash of the delegate to whom the power of the genesis key is being delegated.
* In the transitional phase from the Byron era to the Shelley era, the holders of the genesis keys
* used a GenesisKeyDelegation certificate to delegate their rights to produce blocks to a specific
* set of new keys (genesis delegates).
*
* @returns The public key hash of the delegate.
*/
genesisDelegateHash(): Crypto.Hash28ByteBase16 {
return this.#genesisDelegateHash;
}
/**
* Sets the public key hash of the delegate to whom the power of the genesis key is being delegated.
*
* @param genesisDelegateHash The public key hash of the delegate.
*/
setGenesisDelegateHash(genesisDelegateHash: Crypto.Hash28ByteBase16): void {
this.#genesisDelegateHash = genesisDelegateHash;
this.#originalBytes = undefined;
}
/**
* Gets the hash of the Verifiable Random Function (VRF) key of the delegate.
*
* @returns The hash of the Verifiable Random Function (VRF) key.
*/
vrfKeyHash(): Crypto.Hash32ByteBase16 {
return this.#vrfKeyHash;
}
/**
* Sets the hash of the Verifiable Random Function (VRF) key of the delegate.
*
* @param vrfKeyHash The hash of the Verifiable Random Function (VRF) key.
*/
setVrfKeyHash(vrfKeyHash: Crypto.Hash32ByteBase16): void {
this.#vrfKeyHash = vrfKeyHash;
this.#originalBytes = undefined;
}
}