@typeberry/convert 0.1.2-b99f2bee → 0.1.2-ced735c9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +31 -148
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -26809,12 +26809,6 @@ async function runPvmTest(testContent) {
|
|
|
26809
26809
|
|
|
26810
26810
|
|
|
26811
26811
|
|
|
26812
|
-
function looseType(output) {
|
|
26813
|
-
return {
|
|
26814
|
-
value: output.value,
|
|
26815
|
-
encode: output.encode,
|
|
26816
|
-
};
|
|
26817
|
-
}
|
|
26818
26812
|
const SUPPORTED_TYPES = [
|
|
26819
26813
|
{
|
|
26820
26814
|
name: "block",
|
|
@@ -26832,12 +26826,8 @@ const SUPPORTED_TYPES = [
|
|
|
26832
26826
|
run(spec, data, option) {
|
|
26833
26827
|
const header = data;
|
|
26834
26828
|
if (option === "as-hash") {
|
|
26835
|
-
return
|
|
26836
|
-
value: hashBytes(encoder_Encoder.encodeObject(header_Header.Codec, header, spec)),
|
|
26837
|
-
encode: descriptors_codec.bytes(hash_HASH_SIZE),
|
|
26838
|
-
});
|
|
26829
|
+
return hashBytes(encoder_Encoder.encodeObject(header_Header.Codec, header, spec));
|
|
26839
26830
|
}
|
|
26840
|
-
throw new Error(`Invalid processing option: ${option}`);
|
|
26841
26831
|
},
|
|
26842
26832
|
},
|
|
26843
26833
|
},
|
|
@@ -26881,17 +26871,11 @@ const SUPPORTED_TYPES = [
|
|
|
26881
26871
|
run(spec, data, option) {
|
|
26882
26872
|
const state = data;
|
|
26883
26873
|
if (option === "as-entries") {
|
|
26884
|
-
return
|
|
26885
|
-
value: Object.fromEntries(state_entries_StateEntries.serializeInMemory(spec, state)),
|
|
26886
|
-
});
|
|
26874
|
+
return Object.fromEntries(state_entries_StateEntries.serializeInMemory(spec, state));
|
|
26887
26875
|
}
|
|
26888
26876
|
if (option === "as-root-hash") {
|
|
26889
|
-
return
|
|
26890
|
-
value: state_entries_StateEntries.serializeInMemory(spec, state).getRootHash(),
|
|
26891
|
-
encode: descriptors_codec.bytes(hash_HASH_SIZE),
|
|
26892
|
-
});
|
|
26877
|
+
return state_entries_StateEntries.serializeInMemory(spec, state).getRootHash();
|
|
26893
26878
|
}
|
|
26894
|
-
throw new Error(`Invalid processing option: ${option}`);
|
|
26895
26879
|
},
|
|
26896
26880
|
},
|
|
26897
26881
|
},
|
|
@@ -26901,39 +26885,19 @@ const SUPPORTED_TYPES = [
|
|
|
26901
26885
|
decode: StateTransitionGenesis.Codec,
|
|
26902
26886
|
json: () => StateTransitionGenesis.fromJson,
|
|
26903
26887
|
process: {
|
|
26904
|
-
options: ["as-state", "as-jip4"
|
|
26888
|
+
options: ["as-state", "as-jip4"],
|
|
26905
26889
|
run(spec, data, option) {
|
|
26906
26890
|
const test = data;
|
|
26907
26891
|
if (option === "as-state") {
|
|
26908
|
-
return
|
|
26909
|
-
value: stateFromKeyvals(spec, test.state),
|
|
26910
|
-
});
|
|
26892
|
+
return stateFromKeyvals(spec, test.state);
|
|
26911
26893
|
}
|
|
26912
26894
|
if (option === "as-jip4") {
|
|
26913
26895
|
const genesisState = new Map(test.state.keyvals.map((x) => [x.key, x.value]));
|
|
26914
|
-
return
|
|
26915
|
-
|
|
26916
|
-
|
|
26917
|
-
genesisState,
|
|
26918
|
-
}),
|
|
26896
|
+
return JipChainSpec.create({
|
|
26897
|
+
genesisHeader: encoder_Encoder.encodeObject(header_Header.Codec, test.header, spec),
|
|
26898
|
+
genesisState,
|
|
26919
26899
|
});
|
|
26920
26900
|
}
|
|
26921
|
-
if (option === "as-fuzz-message") {
|
|
26922
|
-
const init = Initialize.create({
|
|
26923
|
-
header: test.header,
|
|
26924
|
-
keyvals: test.state.keyvals,
|
|
26925
|
-
ancestry: [],
|
|
26926
|
-
});
|
|
26927
|
-
const msg = {
|
|
26928
|
-
type: types_MessageType.Initialize,
|
|
26929
|
-
value: init,
|
|
26930
|
-
};
|
|
26931
|
-
return looseType({
|
|
26932
|
-
value: msg,
|
|
26933
|
-
encode: types_messageCodec,
|
|
26934
|
-
});
|
|
26935
|
-
}
|
|
26936
|
-
throw new Error(`Invalid processing option: ${option}`);
|
|
26937
26901
|
},
|
|
26938
26902
|
},
|
|
26939
26903
|
},
|
|
@@ -26943,32 +26907,15 @@ const SUPPORTED_TYPES = [
|
|
|
26943
26907
|
decode: StateTransition.Codec,
|
|
26944
26908
|
json: () => StateTransition.fromJson,
|
|
26945
26909
|
process: {
|
|
26946
|
-
options: ["as-pre-state", "as-post-state"
|
|
26910
|
+
options: ["as-pre-state", "as-post-state"],
|
|
26947
26911
|
run(spec, data, option) {
|
|
26948
26912
|
const test = data;
|
|
26949
26913
|
if (option === "as-pre-state") {
|
|
26950
|
-
return
|
|
26951
|
-
value: stateFromKeyvals(spec, test.pre_state),
|
|
26952
|
-
});
|
|
26914
|
+
return stateFromKeyvals(spec, test.pre_state);
|
|
26953
26915
|
}
|
|
26954
26916
|
if (option === "as-post-state") {
|
|
26955
|
-
return
|
|
26956
|
-
value: stateFromKeyvals(spec, test.post_state),
|
|
26957
|
-
});
|
|
26958
|
-
}
|
|
26959
|
-
if (option === "as-fuzz-message") {
|
|
26960
|
-
const encoded = encoder_Encoder.encodeObject(block_Block.Codec, test.block, spec);
|
|
26961
|
-
const blockView = decoder_Decoder.decodeObject(block_Block.Codec.View, encoded, spec);
|
|
26962
|
-
const msg = {
|
|
26963
|
-
type: types_MessageType.ImportBlock,
|
|
26964
|
-
value: blockView,
|
|
26965
|
-
};
|
|
26966
|
-
return looseType({
|
|
26967
|
-
value: msg,
|
|
26968
|
-
encode: types_messageCodec,
|
|
26969
|
-
});
|
|
26917
|
+
return stateFromKeyvals(spec, test.post_state);
|
|
26970
26918
|
}
|
|
26971
|
-
throw new Error(`Invalid processing option: ${option}`);
|
|
26972
26919
|
},
|
|
26973
26920
|
},
|
|
26974
26921
|
},
|
|
@@ -26991,7 +26938,7 @@ const HELP = `
|
|
|
26991
26938
|
@typeberry/convert ${package_namespaceObject.rE} by Fluffy Labs.
|
|
26992
26939
|
|
|
26993
26940
|
Usage:
|
|
26994
|
-
@typeberry/convert [options] <bin-hex-or-json-input-file> <type> [process] [output-format]
|
|
26941
|
+
@typeberry/convert [options] <bin-hex-or-json-input-file> <type> [process] [output-format]
|
|
26995
26942
|
|
|
26996
26943
|
Attempts to read provided input file as 'type' and output in requested 'output-format'.
|
|
26997
26944
|
For some 'type's it's additionally possible to process the data before outputting it.
|
|
@@ -27000,7 +26947,6 @@ The input type is detected from file extension ('.bin', '.hex' or '.json').
|
|
|
27000
26947
|
Example usage:
|
|
27001
26948
|
@typeberry/convert ./genesis-header.json header to-hex
|
|
27002
26949
|
@typeberry/convert ./state-snapshot.json state-dump as-entries to-json
|
|
27003
|
-
@typeberry/convert ./state-snapshot.json stf-vector as-fuzz-message to-bin msg0.bin
|
|
27004
26950
|
|
|
27005
26951
|
Options:
|
|
27006
26952
|
--flavor - chain spec flavor, either 'full' or 'tiny'.
|
|
@@ -27010,7 +26956,6 @@ Output formats:
|
|
|
27010
26956
|
to-print - Print the object to the console
|
|
27011
26957
|
to-json - JSON format (when supported)
|
|
27012
26958
|
to-hex - JAM-codec hex-encoded string (when supported)
|
|
27013
|
-
to-bin - JAM-codec binary data (when supported)
|
|
27014
26959
|
to-repl - Start a JavaScript REPL with the data loaded into a variable
|
|
27015
26960
|
|
|
27016
26961
|
Input types:
|
|
@@ -27033,7 +26978,6 @@ var OutputFormat;
|
|
|
27033
26978
|
OutputFormat["Print"] = "to-print";
|
|
27034
26979
|
OutputFormat["Json"] = "to-json";
|
|
27035
26980
|
OutputFormat["Hex"] = "to-hex";
|
|
27036
|
-
OutputFormat["Bin"] = "to-bin";
|
|
27037
26981
|
OutputFormat["Repl"] = "to-repl";
|
|
27038
26982
|
})(OutputFormat || (OutputFormat = {}));
|
|
27039
26983
|
function parseArgs(cliInput, withRelPath) {
|
|
@@ -27055,16 +26999,14 @@ function parseArgs(cliInput, withRelPath) {
|
|
|
27055
26999
|
const type = parseType(args._.shift());
|
|
27056
27000
|
const maybeProcess = args._.shift();
|
|
27057
27001
|
const maybeOutputFormat = args._.shift();
|
|
27058
|
-
const maybeDestination = args._.shift();
|
|
27059
27002
|
assertNoMoreArgs(args);
|
|
27060
|
-
const { process, format
|
|
27003
|
+
const { process, format } = getProcessAndOutput(type, maybeProcess, maybeOutputFormat);
|
|
27061
27004
|
return {
|
|
27062
27005
|
flavor: chainSpec.flavor,
|
|
27063
27006
|
type,
|
|
27064
27007
|
process,
|
|
27065
27008
|
inputPath: withRelPath(input),
|
|
27066
27009
|
outputFormat: format,
|
|
27067
|
-
destination,
|
|
27068
27010
|
};
|
|
27069
27011
|
}
|
|
27070
27012
|
function parseType(type) {
|
|
@@ -27090,21 +27032,10 @@ function parseOutputFormat(output) {
|
|
|
27090
27032
|
return OutputFormat.Json;
|
|
27091
27033
|
case OutputFormat.Repl:
|
|
27092
27034
|
return OutputFormat.Repl;
|
|
27093
|
-
case OutputFormat.Bin:
|
|
27094
|
-
return OutputFormat.Bin;
|
|
27095
27035
|
default:
|
|
27096
27036
|
throw new Error(`Invalid output format: '${output}'.`);
|
|
27097
27037
|
}
|
|
27098
27038
|
}
|
|
27099
|
-
function parseProcess(processOptions, maybeProcess) {
|
|
27100
|
-
if (maybeProcess === undefined) {
|
|
27101
|
-
return null;
|
|
27102
|
-
}
|
|
27103
|
-
if (!processOptions.includes(maybeProcess)) {
|
|
27104
|
-
throw new Error(`Incorrect processing option: ${maybeProcess}. Expected one of: ${processOptions}.`);
|
|
27105
|
-
}
|
|
27106
|
-
return maybeProcess;
|
|
27107
|
-
}
|
|
27108
27039
|
// TODO [ToDr] Consider sharing that?
|
|
27109
27040
|
function parseOption(args, option, parser, defaultValue) {
|
|
27110
27041
|
if (args[option] === undefined) {
|
|
@@ -27140,64 +27071,33 @@ function assertNoMoreArgs(args) {
|
|
|
27140
27071
|
throw new Error(`Unrecognized options: '${keysLeft}'`);
|
|
27141
27072
|
}
|
|
27142
27073
|
}
|
|
27143
|
-
function
|
|
27144
|
-
const defaultProcess = "";
|
|
27074
|
+
function getProcessAndOutput(type, maybeProcess, maybeOutputFormat) {
|
|
27145
27075
|
const defaultFormat = parseOutputFormat(undefined);
|
|
27146
|
-
const
|
|
27147
|
-
// we have
|
|
27148
|
-
if (maybeProcess !== undefined && maybeOutputFormat !== undefined && maybeDestination !== undefined) {
|
|
27149
|
-
const format = parseOutputFormat(maybeOutputFormat);
|
|
27150
|
-
const process = parseProcess(processOptions, maybeProcess) ?? defaultProcess;
|
|
27151
|
-
const destination = maybeDestination;
|
|
27152
|
-
throwIfDumpNotSupported(format, destination);
|
|
27153
|
-
return { process, format, destination };
|
|
27154
|
-
}
|
|
27155
|
-
// we have either:
|
|
27156
|
-
// 1. process + format
|
|
27157
|
-
// 2. format + destination
|
|
27076
|
+
const options = type.process?.options ?? [];
|
|
27077
|
+
// we have both options, so we expect them in the right order.
|
|
27158
27078
|
if (maybeProcess !== undefined && maybeOutputFormat !== undefined) {
|
|
27159
|
-
|
|
27160
|
-
if (
|
|
27161
|
-
|
|
27162
|
-
|
|
27163
|
-
|
|
27164
|
-
}
|
|
27165
|
-
// first one has to be format then.
|
|
27166
|
-
const format = parseOutputFormat(maybeProcess);
|
|
27167
|
-
const destination = maybeOutputFormat;
|
|
27168
|
-
throwIfDumpNotSupported(format, destination);
|
|
27169
|
-
return { process: defaultProcess, format, destination };
|
|
27079
|
+
const format = parseOutputFormat(maybeOutputFormat);
|
|
27080
|
+
if (!options.includes(maybeProcess)) {
|
|
27081
|
+
throw new Error(`Incorrect processing option: ${maybeProcess}. Expected one of: ${options}.`);
|
|
27082
|
+
}
|
|
27083
|
+
return { process: maybeProcess, format };
|
|
27170
27084
|
}
|
|
27171
27085
|
// only one parameter, but it can be either output or processing.
|
|
27172
|
-
const destination = null;
|
|
27173
27086
|
if (maybeProcess !== undefined) {
|
|
27174
|
-
if (
|
|
27175
|
-
return { process: maybeProcess, format: defaultFormat
|
|
27087
|
+
if (options.includes(maybeProcess)) {
|
|
27088
|
+
return { process: maybeProcess, format: defaultFormat };
|
|
27176
27089
|
}
|
|
27177
27090
|
// now it should be output format, but we want to give a better error message,
|
|
27178
27091
|
// if user mispelled processing.
|
|
27179
27092
|
try {
|
|
27180
27093
|
const format = parseOutputFormat(maybeProcess);
|
|
27181
|
-
|
|
27182
|
-
return { process: defaultProcess, format, destination };
|
|
27094
|
+
return { process: "", format };
|
|
27183
27095
|
}
|
|
27184
27096
|
catch {
|
|
27185
27097
|
throw new Error(`'${maybeProcess}' is neither output format nor processing parameter.`);
|
|
27186
27098
|
}
|
|
27187
27099
|
}
|
|
27188
|
-
return { process:
|
|
27189
|
-
}
|
|
27190
|
-
function throwIfDumpNotSupported(format, destination) {
|
|
27191
|
-
if (destination !== null) {
|
|
27192
|
-
if (format === OutputFormat.Print || format === OutputFormat.Repl) {
|
|
27193
|
-
throw new Error(`Dumping to file is not supported for ${format}`);
|
|
27194
|
-
}
|
|
27195
|
-
}
|
|
27196
|
-
else {
|
|
27197
|
-
if (format === OutputFormat.Bin) {
|
|
27198
|
-
throw new Error(`${format} requires destination file`);
|
|
27199
|
-
}
|
|
27200
|
-
}
|
|
27100
|
+
return { process: "", format: defaultFormat };
|
|
27201
27101
|
}
|
|
27202
27102
|
|
|
27203
27103
|
// EXTERNAL MODULE: ./node_modules/json-bigint-patch/dist/index.js
|
|
@@ -27257,13 +27157,9 @@ function loadInputFile(file, withRelPath) {
|
|
|
27257
27157
|
throw new Error("Input file format unsupported.");
|
|
27258
27158
|
}
|
|
27259
27159
|
function dumpOutput(spec, data, type, outputFormat, args, withRelPath) {
|
|
27260
|
-
const { destination } = args;
|
|
27261
|
-
const dump = destination !== null
|
|
27262
|
-
? (v) => external_node_fs_default().writeFileSync(withRelPath(destination), v)
|
|
27263
|
-
: (v) => console.info(v);
|
|
27264
27160
|
switch (outputFormat) {
|
|
27265
27161
|
case OutputFormat.Print: {
|
|
27266
|
-
|
|
27162
|
+
console.info(`${debug_inspect(data)}`);
|
|
27267
27163
|
return;
|
|
27268
27164
|
}
|
|
27269
27165
|
case OutputFormat.Hex: {
|
|
@@ -27271,24 +27167,13 @@ function dumpOutput(spec, data, type, outputFormat, args, withRelPath) {
|
|
|
27271
27167
|
throw new Error(`${type.name} does not support encoding to JAM codec.`);
|
|
27272
27168
|
}
|
|
27273
27169
|
const encoded = encoder_Encoder.encodeObject(type.encode, data, spec);
|
|
27274
|
-
|
|
27275
|
-
return;
|
|
27276
|
-
}
|
|
27277
|
-
case OutputFormat.Bin: {
|
|
27278
|
-
if (type.encode === undefined) {
|
|
27279
|
-
throw new Error(`${type.name} does not support encoding to JAM codec.`);
|
|
27280
|
-
}
|
|
27281
|
-
if (destination === null) {
|
|
27282
|
-
throw new Error(`${OutputFormat.Bin} requires destination file.`);
|
|
27283
|
-
}
|
|
27284
|
-
const encoded = encoder_Encoder.encodeObject(type.encode, data, spec);
|
|
27285
|
-
dump(encoded.raw);
|
|
27170
|
+
console.info(`${encoded}`);
|
|
27286
27171
|
return;
|
|
27287
27172
|
}
|
|
27288
27173
|
case OutputFormat.Json: {
|
|
27289
27174
|
// TODO [ToDr] this will probably not work for all cases,
|
|
27290
27175
|
// but for now may be good enough.
|
|
27291
|
-
|
|
27176
|
+
console.info(toJson(data));
|
|
27292
27177
|
return;
|
|
27293
27178
|
}
|
|
27294
27179
|
case OutputFormat.Repl: {
|
|
@@ -27365,14 +27250,12 @@ function processOutput(spec, data, type, process) {
|
|
|
27365
27250
|
if (type.process === undefined || !type.process.options.includes(process)) {
|
|
27366
27251
|
throw new Error(`Unsupported processing: '${process}' for '${type.name}'`);
|
|
27367
27252
|
}
|
|
27368
|
-
const processed = type.process.run(spec, data, process);
|
|
27369
27253
|
return {
|
|
27370
|
-
processed:
|
|
27254
|
+
processed: type.process.run(spec, data, process),
|
|
27371
27255
|
type: {
|
|
27372
27256
|
...type,
|
|
27373
|
-
|
|
27374
|
-
|
|
27375
|
-
encode: processed.encode,
|
|
27257
|
+
// disable encoding, since it won't match
|
|
27258
|
+
encode: undefined,
|
|
27376
27259
|
},
|
|
27377
27260
|
};
|
|
27378
27261
|
}
|