-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.js
160 lines (160 loc) · 5.32 KB
/
registry.js
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
"use strict";
/* eslint-disable */
/**
* This file and any referenced files were automatically generated by @cosmology/[email protected]
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or yarn proto command to regenerate this bundle.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.GlobalDecoderRegistry = void 0;
const binary_1 = require("./binary");
const any_1 = require("./google/protobuf/any");
class GlobalDecoderRegistry {
static registerAminoProtoMapping(aminoType, typeUrl) {
GlobalDecoderRegistry.aminoProtoMapping[aminoType] = typeUrl;
}
static register(key, decoder) {
GlobalDecoderRegistry.registry[key] = decoder;
}
static getDecoder(key) {
return GlobalDecoderRegistry.registry[key];
}
static getDecoderByInstance(obj) {
if (obj === undefined || obj === null) {
return null;
}
const protoType = obj;
if (protoType.$typeUrl) {
return GlobalDecoderRegistry.getDecoder(protoType.$typeUrl);
}
for (const key in GlobalDecoderRegistry.registry) {
if (Object.prototype.hasOwnProperty.call(GlobalDecoderRegistry.registry, key)) {
const element = GlobalDecoderRegistry.registry[key];
if (element.is(obj)) {
return element;
}
if (element.isSDK && element.isSDK(obj)) {
return element;
}
if (element.isAmino && element.isAmino(obj)) {
return element;
}
}
}
return null;
}
static getDecoderByAminoType(type) {
if (type === undefined || type === null) {
return null;
}
const typeUrl = GlobalDecoderRegistry.aminoProtoMapping[type];
if (!typeUrl) {
return null;
}
return GlobalDecoderRegistry.getDecoder(typeUrl);
}
static wrapAny(obj) {
if (any_1.Any.is(obj)) {
return obj;
}
const decoder = getDecoderByInstance(obj);
return {
typeUrl: decoder.typeUrl,
value: decoder.encode(obj).finish(),
};
}
static unwrapAny(input) {
let data;
if (any_1.Any.is(input)) {
data = input;
}
else {
const reader = input instanceof binary_1.BinaryReader ? input : new binary_1.BinaryReader(input);
data = any_1.Any.decode(reader, reader.uint32());
}
const decoder = GlobalDecoderRegistry.getDecoder(data.typeUrl);
if (!decoder) {
return data;
}
return decoder.decode(data.value);
}
static fromJSON(object) {
const decoder = getDecoderByInstance(object);
return decoder.fromJSON(object);
}
static toJSON(message) {
const decoder = getDecoderByInstance(message);
return decoder.toJSON(message);
}
static fromPartial(object) {
const decoder = getDecoderByInstance(object);
return decoder ? decoder.fromPartial(object) : object;
}
static fromSDK(object) {
const decoder = getDecoderByInstance(object);
return decoder.fromSDK(object);
}
static fromSDKJSON(object) {
const decoder = getDecoderByInstance(object);
return decoder.fromSDKJSON(object);
}
static toSDK(object) {
const decoder = getDecoderByInstance(object);
return decoder.toSDK(object);
}
static fromAmino(object) {
const decoder = getDecoderByInstance(object);
return decoder.fromAmino(object);
}
static fromAminoMsg(object) {
const decoder = GlobalDecoderRegistry.getDecoderByAminoType(object.type);
if (!decoder) {
throw new Error(`There's no decoder for the amino type ${object.type}`);
}
return decoder.fromAminoMsg(object);
}
static toAmino(object) {
let data;
let decoder;
if (any_1.Any.is(object)) {
data = GlobalDecoderRegistry.unwrapAny(object);
decoder = GlobalDecoderRegistry.getDecoder(object.typeUrl);
if (!decoder) {
decoder = any_1.Any;
}
}
else {
data = object;
decoder = getDecoderByInstance(object);
}
return decoder.toAmino(data);
}
static toAminoMsg(object) {
let data;
let decoder;
if (any_1.Any.is(object)) {
data = GlobalDecoderRegistry.unwrapAny(object);
decoder = GlobalDecoderRegistry.getDecoder(object.typeUrl);
if (!decoder) {
decoder = any_1.Any;
}
}
else {
data = object;
decoder = getDecoderByInstance(object);
}
return decoder.toAminoMsg(data);
}
}
exports.GlobalDecoderRegistry = GlobalDecoderRegistry;
GlobalDecoderRegistry.registry = {};
GlobalDecoderRegistry.aminoProtoMapping = {};
function getDecoderByInstance(obj) {
const decoder = GlobalDecoderRegistry.getDecoderByInstance(obj);
if (!decoder) {
throw new Error(`There's no decoder for the instance ${JSON.stringify(obj)}`);
}
return decoder;
}
GlobalDecoderRegistry.register(any_1.Any.typeUrl, any_1.Any);
//# sourceMappingURL=registry.js.map