-
Notifications
You must be signed in to change notification settings - Fork 17
/
Parameter.ts
243 lines (221 loc) · 7.82 KB
/
Parameter.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { Buffer } from 'buffer/index.js';
import { checkParameterLength } from '../contractHelpers.js';
import { SchemaType, serializeSchemaType } from '../schemaTypes.js';
import { deserializeTypeValue, serializeTypeValue } from '../schema.js';
import type {
Base64String,
HexString,
SmartContractTypeValues,
} from '../types.js';
import type * as Proto from '../grpc-api/v2/concordium/types.js';
import {
TypedJson,
TypedJsonDiscriminator,
makeFromTypedJson,
} from './util.js';
/**
* The {@linkcode TypedJsonDiscriminator} discriminator associated with {@linkcode Type} type.
* @deprecated
*/
export const JSON_DISCRIMINATOR = TypedJsonDiscriminator.Parameter;
/**
* @deprecated
*/
export type Serializable = HexString;
/** Parameter for a smart contract entrypoint. */
class Parameter {
/** Having a private field prevents similar structured objects to be considered the same type (similar to nominal typing). */
private __type = JSON_DISCRIMINATOR;
constructor(
/** Internal buffer of bytes representing the parameter. */
public readonly buffer: Uint8Array
) {}
/**
* Get a string representation of the parameter.
* @returns {string} The string representation.
*/
public toString(): string {
return toHexString(this);
}
/**
* Get a JSON-serializable representation of the parameter.
* @returns {HexString} The JSON-serializable representation.
*/
public toJSON(): HexString {
return toHexString(this);
}
}
/**
* Converts a {@linkcode HexString} to a parameter.
* @param {HexString} json The JSON representation of the parameter.
* @returns {Parameter} The parameter.
*/
export function fromJSON(json: HexString): Parameter {
return fromHexString(json);
}
/**
* Unwraps {@linkcode Type} value
* @deprecated Use the {@linkcode Parameter.toJSON} method instead.
* @param value value to unwrap.
* @returns the unwrapped {@linkcode Serializable} value
*/
export function toUnwrappedJSON(value: Type): Serializable {
return toHexString(value);
}
/** Parameter for a smart contract entrypoint. */
export type Type = Parameter;
/**
* Type predicate for {@linkcode Type}
*
* @param value value to check.
* @returns whether `value` is of type {@linkcode Type}
*/
export function instanceOf(value: unknown): value is Parameter {
return value instanceof Parameter;
}
/**
* Create an empty parameter.
* @returns {Parameter} An empty parameter.
*/
export function empty(): Parameter {
return fromBufferUnchecked(new ArrayBuffer(0));
}
/**
* Create a parameter for a smart contract entrypoint.
* Ensuring the buffer does not exceed the maximum number of bytes supported for a smart contract parameter.
* @param {ArrayBuffer} buffer The buffer of bytes representing the parameter.
* @throws If the provided buffer exceed the supported number of bytes for a smart contract.
* @returns {Parameter}
*/
export function fromBuffer(buffer: ArrayBuffer): Parameter {
checkParameterLength(buffer);
return fromBufferUnchecked(buffer);
}
/**
* Create an unchecked parameter for a smart contract entrypoint.
* It is up to the caller to ensure the buffer does not exceed the maximum number of bytes supported for a smart contract parameter.
* @param {ArrayBuffer} buffer The buffer of bytes representing the parameter.
* @returns {Parameter}
*/
export function fromBufferUnchecked(buffer: ArrayBuffer): Parameter {
return new Parameter(new Uint8Array(buffer));
}
/**
* Create a parameter for a smart contract entrypoint from a hex string.
* Ensuring the parameter does not exceed the maximum number of bytes supported for a smart contract parameter.
* @param {HexString} hex String with hex encoding of the parameter.
* @throws If the provided parameter exceed the supported number of bytes for a smart contract.
* @returns {Parameter}
*/
export function fromHexString(hex: HexString): Parameter {
return fromBuffer(Buffer.from(hex, 'hex'));
}
/**
* Convert a parameter into a hex string.
* @param {Parameter} parameter The parameter to encode in a hex string.
* @returns {HexString}
*/
export function toHexString(parameter: Parameter): HexString {
return Buffer.from(parameter.buffer).toString('hex');
}
/**
* Convert a parameter into a buffer.
* @param {Parameter} parameter The parameter to get the buffer from.
* @returns {Uint8Array}
*/
export function toBuffer(parameter: Parameter): Uint8Array {
return parameter.buffer;
}
/**
* Create a parameter from a schema type and the corresponding schema value.
* @param {SchemaType} schemaType The schema type for some parameter.
* @param {unknown} value The parameter value fitting the schema type.
* @returns {Parameter} A parameter of the provided value encoded using the schema type.
*/
export function fromSchemaType(
schemaType: SchemaType,
value: unknown
): Parameter {
const schemaBytes = serializeSchemaType(schemaType);
return serializeTypeValue(value, schemaBytes);
}
/**
* Create a parameter from a schema type and the corresponding schema value.
* @param {Base64String} schemaBase64 The schema type for some parameter in base64.
* @param {unknown} value The parameter value fitting the schema type.
* @returns {Parameter} A parameter of the provided value encoded using the schema type.
*/
export function fromBase64SchemaType(
schemaBase64: Base64String,
value: unknown
): Parameter {
const schemaBytes = Buffer.from(schemaBase64, 'base64');
return serializeTypeValue(value, schemaBytes);
}
/**
* Parse a contract parameter using a schema type.
* @param {Parameter} parameter The parameter.
* @param {SchemaType} schemaType The schema type for the parameter.
* @returns {SmartContractTypeValues}
*/
export function parseWithSchemaType(
parameter: Parameter,
schemaType: SchemaType
): SmartContractTypeValues {
const schemaBytes = serializeSchemaType(schemaType);
return deserializeTypeValue(toBuffer(parameter), schemaBytes);
}
/**
* Parse a contract parameter using a schema type.
* @param {Parameter} parameter The parameter to parse.
* @param {Base64String} schemaBase64 The schema type for the parameter encoded as Base64.
* @returns {SmartContractTypeValues}
*/
export function parseWithSchemaTypeBase64(
parameter: Parameter,
schemaBase64: Base64String
): SmartContractTypeValues {
const schemaBytes = Buffer.from(schemaBase64, 'base64');
return deserializeTypeValue(toBuffer(parameter), schemaBytes);
}
/**
* Convert a smart contract parameter from its protobuf encoding.
* @param {Proto.Parameter} parameter The parameter in protobuf.
* @returns {Parameter} The parameter.
*/
export function fromProto(parameter: Proto.Parameter): Parameter {
return fromBuffer(parameter.value);
}
/**
* Convert a parameter into its protobuf encoding.
* @param {Parameter} parameter The parameter.
* @returns {Proto.Parameter} The protobuf encoding.
*/
export function toProto(parameter: Parameter): Proto.Parameter {
return {
value: parameter.buffer,
};
}
/**
* Takes an {@linkcode Type} and transforms it to a {@linkcode TypedJson} format.
* @deprecated Use the {@linkcode Parameter.toJSON} method instead.
* @param {Type} value - The parameter to transform.
* @returns {TypedJson} The transformed object.
*/
export function toTypedJSON(value: Parameter): TypedJson<Serializable> {
return {
['@type']: JSON_DISCRIMINATOR,
value: toHexString(value),
};
}
/**
* Takes a {@linkcode TypedJson} object and converts it to instance of type {@linkcode Type}.
* @deprecated Use the {@linkcode fromJSON} function instead.
* @param {TypedJson} json - The typed JSON to convert.
* @throws {TypedJsonParseError} - If unexpected JSON string is passed.
* @returns {Type} The parsed instance.
*/
export const fromTypedJSON = /*#__PURE__*/ makeFromTypedJson(
JSON_DISCRIMINATOR,
fromHexString
);