-
Notifications
You must be signed in to change notification settings - Fork 54
/
ScriptAny.ts
129 lines (105 loc) · 3.52 KB
/
ScriptAny.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
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 any the sub-scripts evaluate to true. That is, if one
* or more evaluate to true.
*
* If the list of sub-scripts is empty, this script evaluates to false.
*/
export class ScriptAny {
#nativeScripts: Array<NativeScript>;
#originalBytes: HexBlob | undefined = undefined;
/**
* Initializes a new instance of the ScriptAny class.
*
* @param nativeScripts The list of sub-scripts.
*/
constructor(nativeScripts: Array<NativeScript>) {
this.#nativeScripts = nativeScripts;
}
/**
* Serializes a ScriptAny into CBOR format.
*
* @returns The ScriptAny in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (this.#originalBytes) return this.#originalBytes;
// CDDL
// script_any = (2, [ * native_script ])
writer.writeStartArray(EMBEDDED_GROUP_SIZE);
writer.writeInt(Cardano.NativeScriptKind.RequireAnyOf);
writer.writeStartArray(this.#nativeScripts.length);
for (const nativeScript of this.#nativeScripts) writer.writeEncodedValue(Buffer.from(nativeScript.toCbor(), 'hex'));
return writer.encodeAsHex();
}
/**
* Deserializes the ScriptAny from a CBOR byte array.
*
* @param cbor The CBOR encoded ScriptAny object.
* @returns The new ScriptAny instance.
*/
static fromCbor(cbor: HexBlob): ScriptAny {
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.RequireAnyOf)
throw new InvalidArgumentError(
'cbor',
`Expected kind ${Cardano.NativeScriptKind.RequireAnyOf}, 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 ScriptAny(scripts);
script.#originalBytes = cbor;
return script;
}
/**
* Creates a Core RequireAnyOfScript object from the current ScriptAny object.
*
* @returns The Core RequireAnyOfScript object.
*/
toCore(): Cardano.RequireAnyOfScript {
return {
__type: Cardano.ScriptType.Native,
kind: Cardano.NativeScriptKind.RequireAnyOf,
scripts: this.#nativeScripts.map((script) => script.toCore())
};
}
/**
* Creates a ScriptAny object from the given Core RequireAnyOfScript object.
*
* @param script The core RequireAnyOfScript object.
*/
static fromCore(script: Cardano.RequireAnyOfScript) {
return new ScriptAny(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;
}
}