-
Notifications
You must be signed in to change notification settings - Fork 55
/
InfoAction.ts
78 lines (64 loc) · 2.07 KB
/
InfoAction.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
import * as Cardano from '../../../Cardano';
import { CborReader, CborWriter } from '../../CBOR';
import { GovernanceActionKind } from './GovernanceActionKind';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
const EMBEDDED_GROUP_SIZE = 1;
/**
* Represents an action that has no direct effect on the blockchain,
* but serves as an on-chain record or informative notice.
*/
export class InfoAction {
#originalBytes: HexBlob | undefined = undefined;
/**
* Serializes a InfoAction into CBOR format.
*
* @returns The InfoAction in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (this.#originalBytes) return this.#originalBytes;
// CDDL
// info_action = 6
writer.writeStartArray(EMBEDDED_GROUP_SIZE);
writer.writeInt(GovernanceActionKind.Info);
return writer.encodeAsHex();
}
/**
* Deserializes the InfoAction from a CBOR byte array.
*
* @param cbor The CBOR encoded InfoAction object.
* @returns The new InfoAction instance.
*/
static fromCbor(cbor: HexBlob): InfoAction {
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.readUInt());
if (kind !== GovernanceActionKind.Info)
throw new InvalidArgumentError(
'cbor',
`Expected action kind, expected ${GovernanceActionKind.Info} but got ${kind}`
);
const action = new InfoAction();
action.#originalBytes = cbor;
return action;
}
/**
* Creates a Core InfoAction object from the current InfoAction object.
*
* @returns The Core InfoAction object.
*/
toCore(): Cardano.InfoAction {
return {
__typename: Cardano.GovernanceActionType.info_action
};
}
/** Creates a InfoAction object from the given Core InfoAction object. */
static fromCore(_: Cardano.InfoAction) {
return new InfoAction();
}
}