-
Notifications
You must be signed in to change notification settings - Fork 55
/
ScriptAll.ts
128 lines (104 loc) · 3.48 KB
/
ScriptAll.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
import * as Cardano from '../../../Cardano';
import { CborReader, CborReaderState, CborWriter } from '../../CBOR';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
import { NativeScript } from './NativeScript';
const EMBEDDED_GROUP_SIZE = 2;
/**
* This script evaluates to true if all the sub-scripts evaluate to true.
*
* If the list of sub-scripts is empty, this script evaluates to true.
*/
export class ScriptAll {
#nativeScripts: Array<NativeScript>;
#originalBytes: HexBlob | undefined = undefined;
/**
* Initializes a new instance of the ScriptAll class.
*
* @param nativeScripts The list of sub-scripts.
*/
constructor(nativeScripts: Array<NativeScript>) {
this.#nativeScripts = nativeScripts;
}
/**
* Serializes a ScriptAll into CBOR format.
*
* @returns The ScriptAll in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (this.#originalBytes) return this.#originalBytes;
// CDDL
// script_all = (1, [ * native_script ])
writer.writeStartArray(EMBEDDED_GROUP_SIZE);
writer.writeInt(Cardano.NativeScriptKind.RequireAllOf);
writer.writeStartArray(this.#nativeScripts.length);
for (const nativeScript of this.#nativeScripts) writer.writeEncodedValue(Buffer.from(nativeScript.toCbor(), 'hex'));
return writer.encodeAsHex();
}
/**
* Deserializes the ScriptAll from a CBOR byte array.
*
* @param cbor The CBOR encoded ScriptAll object.
* @returns The new ScriptAll instance.
*/
static fromCbor(cbor: HexBlob): ScriptAll {
const reader = new CborReader(cbor);
const length = reader.readStartArray();
if (length !== EMBEDDED_GROUP_SIZE)
throw new InvalidArgumentError(
'cbor',
`Expected an array of two elements, but got an array of ${length} elements`
);
const kind = Number(reader.readInt());
if (kind !== Cardano.NativeScriptKind.RequireAllOf)
throw new InvalidArgumentError(
'cbor',
`Expected kind ${Cardano.NativeScriptKind.RequireAllOf}, but got kind ${kind}`
);
const scripts = new Array<NativeScript>();
reader.readStartArray();
while (reader.peekState() !== CborReaderState.EndArray)
scripts.push(NativeScript.fromCbor(HexBlob.fromBytes(reader.readEncodedValue())));
reader.readEndArray();
const script = new ScriptAll(scripts);
script.#originalBytes = cbor;
return script;
}
/**
* Creates a Core RequireAllOfScript object from the current ScriptAll object.
*
* @returns The Core RequireAllOfScript object.
*/
toCore(): Cardano.RequireAllOfScript {
return {
__type: Cardano.ScriptType.Native,
kind: Cardano.NativeScriptKind.RequireAllOf,
scripts: this.#nativeScripts.map((script) => script.toCore())
};
}
/**
* Creates a ScriptAll object from the given Core RequireAllOfScript object.
*
* @param script The core RequireAllOfScript object.
*/
static fromCore(script: Cardano.RequireAllOfScript) {
return new ScriptAll(script.scripts.map((nativeScript) => NativeScript.fromCore(nativeScript)));
}
/**
* Gets the list of sub-scripts.
*
* @returns The list of sub-scripts.
*/
nativeScripts(): Array<NativeScript> {
return this.#nativeScripts;
}
/**
* Sets the list of sub-scripts.
*
* @param nativeScripts The list of sub-scripts.
*/
setNativeScripts(nativeScripts: Array<NativeScript>) {
this.#nativeScripts = nativeScripts;
this.#originalBytes = undefined;
}
}