/
normalizers.js
268 lines (249 loc) · 7.57 KB
/
normalizers.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// This package provides normalizer functions. Even though CKB uses molecule
// as the serialization layer. There is still CKB specific knowledge that does
// not belong in molecule. For example, all numbers in CKB protocols are
// serialized using little endian format. This package tries to encode such
// knowledge. The goal here, is that you are free to use whatever types that
// makes most sense to represent the values. As long as you pass your object
// through the normalizers here, molecule should be able to serialize the values
// into correct formats required by CKB.
//
// Note this is only used when you need to deal with CKB structures in molecule
// format. If you are using RPCs or GraphQL to interact with CKB, chances are you
// will not need this package.
import JSBI from "jsbi";
import { Reader } from "./reader";
import { BigIntToHexString } from "./rpc";
function normalizeHexNumber(length) {
return function(debugPath, value) {
if (!(value instanceof ArrayBuffer)) {
let intValue = BigIntToHexString(JSBI.BigInt(value)).substr(2);
if (intValue.length % 2 !== 0) {
intValue = "0" + intValue;
}
if (intValue.length / 2 > length) {
throw new Error(
`${debugPath} is ${intValue.length /
2} bytes long, expected length is ${length}!`
);
}
const view = new DataView(new ArrayBuffer(length));
for (let i = 0; i < intValue.length / 2; i++) {
const start = intValue.length - (i + 1) * 2;
view.setUint8(i, parseInt(intValue.substr(start, 2), 16));
}
value = view.buffer;
}
if (value.byteLength < length) {
const array = new Uint8Array(length);
array.set(new Uint8Array(value), 0);
value = array.buffer;
}
return value;
};
}
function normalizeRawData(length) {
return function(debugPath, value) {
value = new Reader(value).toArrayBuffer();
if (length > 0 && value.byteLength !== length) {
throw new Error(
`${debugPath} has invalid length ${value.byteLength}, required: ${length}`
);
}
return value;
};
}
function normalizeObject(debugPath, object, keys) {
const result = {};
for (const [key, f] of Object.entries(keys)) {
const value = object[key];
if (!value) {
throw new Error(`${debugPath} is missing ${key}!`);
}
result[key] = f(`${debugPath}.${key}`, value);
}
return result;
}
export function NormalizeScript(script, { debugPath = "script" } = {}) {
return normalizeObject(debugPath, script, {
code_hash: normalizeRawData(32),
hash_type: function(debugPath, value) {
switch (value) {
case "data":
return 0;
case "type":
return 1;
case 0:
return value;
case 1:
return value;
default:
throw new Error(`${debugPath}.hash_type has invalid value: ${value}`);
}
},
args: normalizeRawData(-1)
});
}
export function NormalizeOutPoint(outPoint, { debugPath = "out_point" } = {}) {
return normalizeObject(debugPath, outPoint, {
tx_hash: normalizeRawData(32),
index: normalizeHexNumber(4)
});
}
function toNormalize(normalize) {
return function(debugPath, value) {
return normalize(value, {
debugPath
});
};
}
export function NormalizeCellInput(
cellInput,
{ debugPath = "cell_input" } = {}
) {
return normalizeObject(debugPath, cellInput, {
since: normalizeHexNumber(8),
previous_output: toNormalize(NormalizeOutPoint)
});
}
export function NormalizeCellOutput(
cellOutput,
{ debugPath = "cell_output" } = {}
) {
const result = normalizeObject(debugPath, cellOutput, {
capacity: normalizeHexNumber(8),
lock: toNormalize(NormalizeScript)
});
if (cellOutput.type) {
result.type_ = NormalizeScript(cellOutput.type, {
debugPath: `${debugPath}.type`
});
}
return result;
}
export function NormalizeCellDep(cellDep, { debugPath = "cell_dep" } = {}) {
return normalizeObject(debugPath, cellDep, {
out_point: toNormalize(NormalizeOutPoint),
dep_type: function(debugPath, value) {
switch (value) {
case "code":
return 0;
case "dep_group":
return 1;
case 0:
return value;
case 1:
return value;
default:
throw new Error(`${debugPath}.dep_type has invalid value: ${value}`);
}
}
});
}
function toNormalizeArray(normalizeFunction) {
return function(debugPath, array) {
return array.map((item, i) => {
return normalizeFunction(`${debugPath}[${i}]`, item);
});
};
}
export function NormalizeRawTransaction(
rawTransaction,
{ debugPath = "raw_transaction" } = {}
) {
return normalizeObject(debugPath, rawTransaction, {
version: normalizeHexNumber(4),
cell_deps: toNormalizeArray(toNormalize(NormalizeCellDep)),
header_deps: toNormalizeArray(normalizeRawData(32)),
inputs: toNormalizeArray(toNormalize(NormalizeCellInput)),
outputs: toNormalizeArray(toNormalize(NormalizeCellOutput)),
outputs_data: toNormalizeArray(normalizeRawData(-1))
});
}
export function NormalizeTransaction(
transaction,
{ debugPath = "transaction" } = {}
) {
const rawTransaction = NormalizeRawTransaction(transaction, {
debugPath: `(raw)${debugPath}`
});
const result = normalizeObject(debugPath, transaction, {
witnesses: toNormalizeArray(normalizeRawData(-1))
});
result.raw = rawTransaction;
return result;
}
export function NormalizeRawHeader(
rawHeader,
{ debugPath = "raw_header" } = {}
) {
return normalizeObject(debugPath, rawHeader, {
version: normalizeHexNumber(4),
compact_target: normalizeHexNumber(4),
timestamp: normalizeHexNumber(8),
number: normalizeHexNumber(8),
epoch: normalizeHexNumber(8),
parent_hash: normalizeRawData(32),
transactions_root: normalizeRawData(32),
proposals_hash: normalizeRawData(32),
uncles_hash: normalizeRawData(32),
dao: normalizeRawData(32)
});
}
export function NormalizeHeader(header, { debugPath = "header" } = {}) {
const rawHeader = NormalizeRawHeader(header, {
debugPath: `(raw)${debugPath}`
});
const result = normalizeObject(debugPath, header, {
nonce: normalizeHexNumber(16)
});
result.raw = rawHeader;
return result;
}
export function NormalizeUncleBlock(
uncleBlock,
{ debugPath = "uncle_block" } = {}
) {
return normalizeObject(debugPath, uncleBlock, {
header: toNormalize(NormalizeHeader),
proposals: toNormalizeArray(normalizeRawData(10))
});
}
export function NormalizeBlock(block, { debugPath = "block" } = {}) {
return normalizeObject(debugPath, block, {
header: toNormalize(NormalizeHeader),
uncles: toNormalizeArray(toNormalize(NormalizeUncleBlock)),
transactions: toNormalizeArray(toNormalize(NormalizeTransaction)),
proposals: toNormalizeArray(normalizeRawData(10))
});
}
export function NormalizeCellbaseWitness(
cellbaseWitness,
{ debugPath = "cellbase_witness" } = {}
) {
return normalizeObject(debugPath, cellbaseWitness, {
lock: toNormalize(NormalizeScript),
message: normalizeRawData(-1)
});
}
export function NormalizeWitnessArgs(
witnessArgs,
{ debugPath = "witness_args" } = {}
) {
const result = {};
if (witnessArgs.lock) {
result.lock = normalizeRawData(-1)(`${debugPath}.lock`, witnessArgs.lock);
}
if (witnessArgs.input_type) {
result.input_type = normalizeRawData(-1)(
`${debugPath}.input_type`,
witnessArgs.input_type
);
}
if (witnessArgs.output_type) {
result.output_type = normalizeRawData(-1)(
`${debugPath}.output_type`,
witnessArgs.output_type
);
}
return result;
}