-
Notifications
You must be signed in to change notification settings - Fork 344
/
responses.ts
866 lines (757 loc) · 25.8 KB
/
responses.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
/* eslint-disable @typescript-eslint/naming-convention */
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
import { assert } from "@cosmjs/utils";
import { DateWithNanoseconds, fromRfc3339WithNanoseconds } from "../../dates";
import { SubscriptionEvent } from "../../rpcclients";
import { BlockIdFlag, CommitSignature, ValidatorPubkey } from "../../types";
import {
assertArray,
assertBoolean,
assertNotEmpty,
assertNumber,
assertObject,
assertSet,
assertString,
dictionaryToStringMap,
Integer,
may,
optional,
} from "../encodings";
import { hashTx } from "../hasher";
import * as responses from "../responses";
interface AbciInfoResult {
readonly response: RpcAbciInfoResponse;
}
interface RpcAbciInfoResponse {
readonly data?: string;
readonly last_block_height?: string;
/** base64 encoded */
readonly last_block_app_hash?: string;
}
function decodeAbciInfo(data: RpcAbciInfoResponse): responses.AbciInfoResponse {
return {
data: data.data,
lastBlockHeight: may(Integer.parse, data.last_block_height),
lastBlockAppHash: may(fromBase64, data.last_block_app_hash),
};
}
interface AbciQueryResult {
readonly response: RpcAbciQueryResponse;
}
export interface RpcProofOp {
readonly type: string;
/** base64 encoded */
readonly key: string;
/** base64 encoded */
readonly data: string;
}
export interface RpcQueryProof {
readonly ops: readonly RpcProofOp[];
}
function decodeQueryProof(data: RpcQueryProof): responses.QueryProof {
return {
ops: data.ops.map((op) => ({
type: op.type,
key: fromBase64(op.key),
data: fromBase64(op.data),
})),
};
}
interface RpcAbciQueryResponse {
/** base64 encoded */
readonly key: string;
/** base64 encoded */
readonly value?: string;
readonly proofOps?: RpcQueryProof;
readonly height?: string;
readonly index?: string;
readonly code?: string; // only for errors
readonly log?: string;
}
function decodeAbciQuery(data: RpcAbciQueryResponse): responses.AbciQueryResponse {
return {
key: fromBase64(optional(data.key, "")),
value: fromBase64(optional(data.value, "")),
proof: may(decodeQueryProof, data.proofOps),
height: may(Integer.parse, data.height),
code: may(Integer.parse, data.code),
index: may(Integer.parse, data.index),
log: data.log,
};
}
interface RpcAttribute {
/** base64 encoded */
readonly key: string;
/** base64 encoded */
readonly value: string;
}
function decodeAttribute(attribute: RpcAttribute): responses.Attribute {
return {
key: fromBase64(assertNotEmpty(attribute.key)),
value: fromBase64(optional(attribute.value, "")),
};
}
function decodeAttributes(attributes: readonly RpcAttribute[]): responses.Attribute[] {
return assertArray(attributes).map(decodeAttribute);
}
interface RpcEvent {
readonly type: string;
readonly attributes: readonly RpcAttribute[];
}
function decodeEvent(event: RpcEvent): responses.Event {
return {
type: event.type,
attributes: decodeAttributes(event.attributes),
};
}
function decodeEvents(events: readonly RpcEvent[]): readonly responses.Event[] {
return assertArray(events).map(decodeEvent);
}
interface RpcTxData {
readonly codespace?: string;
readonly code?: number;
readonly log?: string;
/** base64 encoded */
readonly data?: string;
readonly events?: readonly RpcEvent[];
readonly gas_wanted?: string;
readonly gas_used?: string;
}
function decodeTxData(data: RpcTxData): responses.TxData {
return {
code: Integer.parse(assertNumber(optional<number>(data.code, 0))),
codeSpace: data.codespace,
log: data.log,
data: may(fromBase64, data.data),
events: data.events ? decodeEvents(data.events) : [],
gasWanted: Integer.parse(optional<string>(data.gas_wanted, "0")),
gasUsed: Integer.parse(optional<string>(data.gas_used, "0")),
};
}
// yes, a different format for status and dump consensus state
interface RpcPubkey {
readonly type: string;
/** base64 encoded */
readonly value: string;
}
function decodePubkey(data: RpcPubkey): ValidatorPubkey {
switch (data.type) {
// go-amino special code
case "tendermint/PubKeyEd25519":
return {
algorithm: "ed25519",
data: fromBase64(assertNotEmpty(data.value)),
};
case "tendermint/PubKeySecp256k1":
return {
algorithm: "secp256k1",
data: fromBase64(assertNotEmpty(data.value)),
};
default:
throw new Error(`unknown pubkey type: ${data.type}`);
}
}
// for evidence, block results, etc.
interface RpcValidatorUpdate {
/** hex encoded */
readonly address: string;
readonly pub_key: RpcPubkey;
readonly voting_power: string;
readonly proposer_priority: string;
}
function decodeValidatorUpdate(data: RpcValidatorUpdate): responses.Validator {
return {
pubkey: decodePubkey(assertObject(data.pub_key)),
votingPower: Integer.parse(assertNotEmpty(data.voting_power)),
address: fromHex(assertNotEmpty(data.address)),
proposerPriority: Integer.parse(data.proposer_priority),
};
}
interface RpcBlockParams {
readonly max_bytes: string;
readonly max_gas: string;
}
/**
* Note: we do not parse block.time_iota_ms for now because of this CHANGELOG entry
*
* > Add time_iota_ms to block's consensus parameters (not exposed to the application)
* https://github.com/tendermint/tendermint/blob/master/CHANGELOG.md#v0310
*/
function decodeBlockParams(data: RpcBlockParams): responses.BlockParams {
return {
maxBytes: Integer.parse(assertNotEmpty(data.max_bytes)),
maxGas: Integer.parse(assertNotEmpty(data.max_gas)),
};
}
interface RpcEvidenceParams {
readonly max_age_num_blocks: string;
readonly max_age_duration: string;
}
function decodeEvidenceParams(data: RpcEvidenceParams): responses.EvidenceParams {
return {
maxAgeNumBlocks: Integer.parse(assertNotEmpty(data.max_age_num_blocks)),
maxAgeDuration: Integer.parse(assertNotEmpty(data.max_age_duration)),
};
}
/**
* Example data:
* {
* "block": {
* "max_bytes": "22020096",
* "max_gas": "-1",
* "time_iota_ms": "1000"
* },
* "evidence": {
* "max_age_num_blocks": "100000",
* "max_age_duration": "172800000000000"
* },
* "validator": {
* "pub_key_types": [
* "ed25519"
* ]
* }
* }
*/
interface RpcConsensusParams {
readonly block: RpcBlockParams;
readonly evidence: RpcEvidenceParams;
}
function decodeConsensusParams(data: RpcConsensusParams): responses.ConsensusParams {
return {
block: decodeBlockParams(assertObject(data.block)),
evidence: decodeEvidenceParams(assertObject(data.evidence)),
};
}
interface RpcBlockResultsResponse {
readonly height: string;
readonly txs_results: readonly RpcTxData[] | null;
readonly begin_block_events: readonly RpcEvent[] | null;
readonly end_block_events: readonly RpcEvent[] | null;
readonly validator_updates: readonly RpcValidatorUpdate[] | null;
readonly consensus_param_updates: RpcConsensusParams | null;
}
function decodeBlockResults(data: RpcBlockResultsResponse): responses.BlockResultsResponse {
return {
height: Integer.parse(assertNotEmpty(data.height)),
results: (data.txs_results || []).map(decodeTxData),
validatorUpdates: (data.validator_updates || []).map(decodeValidatorUpdate),
consensusUpdates: may(decodeConsensusParams, data.consensus_param_updates),
beginBlockEvents: decodeEvents(data.begin_block_events || []),
endBlockEvents: decodeEvents(data.end_block_events || []),
};
}
interface RpcBlockId {
/** hex encoded */
readonly hash: string;
readonly parts: {
readonly total: number;
/** hex encoded */
readonly hash: string;
};
}
function decodeBlockId(data: RpcBlockId): responses.BlockId {
return {
hash: fromHex(assertNotEmpty(data.hash)),
parts: {
total: assertNotEmpty(data.parts.total),
hash: fromHex(assertNotEmpty(data.parts.hash)),
},
};
}
interface RpcBlockVersion {
readonly block: string;
readonly app?: string;
}
function decodeBlockVersion(data: RpcBlockVersion): responses.Version {
return {
block: Integer.parse(data.block),
app: Integer.parse(data.app ?? 0),
};
}
interface RpcHeader {
readonly version: RpcBlockVersion;
readonly chain_id: string;
readonly height: string;
readonly time: string;
readonly last_block_id: RpcBlockId;
/** hex encoded */
readonly last_commit_hash: string;
/** hex encoded */
readonly data_hash: string;
/** hex encoded */
readonly validators_hash: string;
/** hex encoded */
readonly next_validators_hash: string;
/** hex encoded */
readonly consensus_hash: string;
/** hex encoded */
readonly app_hash: string;
/** hex encoded */
readonly last_results_hash: string;
/** hex encoded */
readonly evidence_hash: string;
/** hex encoded */
readonly proposer_address: string;
}
function decodeHeader(data: RpcHeader): responses.Header {
return {
version: decodeBlockVersion(data.version),
chainId: assertNotEmpty(data.chain_id),
height: Integer.parse(assertNotEmpty(data.height)),
time: fromRfc3339WithNanoseconds(assertNotEmpty(data.time)),
// When there is no last block ID (i.e. this block's height is 1), we get an empty structure like this:
// { hash: '', parts: { total: 0, hash: '' } }
lastBlockId: data.last_block_id.hash ? decodeBlockId(data.last_block_id) : null,
lastCommitHash: fromHex(assertSet(data.last_commit_hash)),
dataHash: fromHex(assertSet(data.data_hash)),
validatorsHash: fromHex(assertSet(data.validators_hash)),
nextValidatorsHash: fromHex(assertSet(data.next_validators_hash)),
consensusHash: fromHex(assertSet(data.consensus_hash)),
appHash: fromHex(assertSet(data.app_hash)),
lastResultsHash: fromHex(assertSet(data.last_results_hash)),
evidenceHash: fromHex(assertSet(data.evidence_hash)),
proposerAddress: fromHex(assertNotEmpty(data.proposer_address)),
};
}
interface RpcBlockMeta {
readonly block_id: RpcBlockId;
readonly block_size: string;
readonly header: RpcHeader;
readonly num_txs: string;
}
function decodeBlockMeta(data: RpcBlockMeta): responses.BlockMeta {
return {
blockId: decodeBlockId(data.block_id),
blockSize: Integer.parse(assertNotEmpty(data.block_size)),
header: decodeHeader(data.header),
numTxs: Integer.parse(assertNotEmpty(data.num_txs)),
};
}
interface RpcBlockchainResponse {
readonly last_height: string;
readonly block_metas: readonly RpcBlockMeta[];
}
function decodeBlockchain(data: RpcBlockchainResponse): responses.BlockchainResponse {
return {
lastHeight: Integer.parse(assertNotEmpty(data.last_height)),
blockMetas: assertArray(data.block_metas).map(decodeBlockMeta),
};
}
interface RpcBroadcastTxSyncResponse extends RpcTxData {
/** hex encoded */
readonly hash: string;
}
function decodeBroadcastTxSync(data: RpcBroadcastTxSyncResponse): responses.BroadcastTxSyncResponse {
return {
...decodeTxData(data),
hash: fromHex(assertNotEmpty(data.hash)),
};
}
interface RpcBroadcastTxCommitResponse {
readonly height: string;
/** hex encoded */
readonly hash: string;
readonly check_tx: RpcTxData;
readonly deliver_tx?: RpcTxData;
}
function decodeBroadcastTxCommit(data: RpcBroadcastTxCommitResponse): responses.BroadcastTxCommitResponse {
return {
height: Integer.parse(data.height),
hash: fromHex(assertNotEmpty(data.hash)),
checkTx: decodeTxData(assertObject(data.check_tx)),
deliverTx: may(decodeTxData, data.deliver_tx),
};
}
function decodeBlockIdFlag(blockIdFlag: number): BlockIdFlag {
assert(blockIdFlag in BlockIdFlag);
return blockIdFlag;
}
type RpcSignature = {
readonly block_id_flag: number;
/** hex encoded */
readonly validator_address: string;
readonly timestamp: string;
/**
* Base64 encoded signature.
* There are cases when this is not set, see https://github.com/cosmos/cosmjs/issues/704#issuecomment-797122415.
*/
readonly signature: string | null;
};
/**
* In some cases a timestamp is optional and set to the value 0 in Go.
* This can lead to strings like "0001-01-01T00:00:00Z" (see https://github.com/cosmos/cosmjs/issues/704#issuecomment-797122415).
* This decoder tries to clean up such encoding from the API and turn them
* into undefined values.
*/
function decodeOptionalTime(timestamp: string): DateWithNanoseconds | undefined {
const nonZeroTime = timestamp && !timestamp.startsWith("0001-01-01");
return nonZeroTime ? fromRfc3339WithNanoseconds(timestamp) : undefined;
}
function decodeCommitSignature(data: RpcSignature): CommitSignature {
return {
blockIdFlag: decodeBlockIdFlag(data.block_id_flag),
validatorAddress: data.validator_address ? fromHex(data.validator_address) : undefined,
timestamp: decodeOptionalTime(data.timestamp),
signature: data.signature ? fromBase64(data.signature) : undefined,
};
}
interface RpcCommit {
readonly block_id: RpcBlockId;
readonly height: string;
readonly round: string;
readonly signatures: readonly RpcSignature[];
}
function decodeCommit(data: RpcCommit): responses.Commit {
return {
blockId: decodeBlockId(assertObject(data.block_id)),
height: Integer.parse(assertNotEmpty(data.height)),
round: Integer.parse(data.round),
signatures: assertArray(data.signatures).map(decodeCommitSignature),
};
}
interface RpcCommitResponse {
readonly signed_header: {
readonly header: RpcHeader;
readonly commit: RpcCommit;
};
readonly canonical: boolean;
}
function decodeCommitResponse(data: RpcCommitResponse): responses.CommitResponse {
return {
canonical: assertBoolean(data.canonical),
header: decodeHeader(data.signed_header.header),
commit: decodeCommit(data.signed_header.commit),
};
}
interface RpcValidatorGenesis {
/** hex-encoded */
readonly address: string;
readonly pub_key: RpcPubkey;
readonly power: string;
readonly name?: string;
}
function decodeValidatorGenesis(data: RpcValidatorGenesis): responses.Validator {
return {
address: fromHex(assertNotEmpty(data.address)),
pubkey: decodePubkey(assertObject(data.pub_key)),
votingPower: Integer.parse(assertNotEmpty(data.power)),
};
}
interface RpcGenesisResponse {
readonly genesis_time: string;
readonly chain_id: string;
readonly consensus_params: RpcConsensusParams;
// The validators key is used to specify a set of validators for testnets or PoA blockchains.
// PoS blockchains use the app_state.genutil.gentxs field to stake and bond a number of validators in the first block.
readonly validators?: readonly RpcValidatorGenesis[];
/** hex encoded */
readonly app_hash: string;
readonly app_state: Record<string, unknown> | undefined;
}
interface GenesisResult {
readonly genesis: RpcGenesisResponse;
}
function decodeGenesis(data: RpcGenesisResponse): responses.GenesisResponse {
return {
genesisTime: fromRfc3339WithNanoseconds(assertNotEmpty(data.genesis_time)),
chainId: assertNotEmpty(data.chain_id),
consensusParams: decodeConsensusParams(data.consensus_params),
validators: data.validators ? assertArray(data.validators).map(decodeValidatorGenesis) : [],
appHash: fromHex(assertSet(data.app_hash)), // empty string in kvstore app
appState: data.app_state,
};
}
// this is in status
interface RpcValidatorInfo {
/** hex encoded */
readonly address: string;
readonly pub_key: RpcPubkey;
readonly voting_power: string;
}
function decodeValidatorInfo(data: RpcValidatorInfo): responses.Validator {
return {
pubkey: decodePubkey(assertObject(data.pub_key)),
votingPower: Integer.parse(assertNotEmpty(data.voting_power)),
address: fromHex(assertNotEmpty(data.address)),
};
}
interface RpcNodeInfo {
/** hex encoded */
readonly id: string;
/** IP and port */
readonly listen_addr: string;
readonly network: string;
readonly version: string;
readonly channels: string; // ???
readonly moniker: string;
readonly protocol_version: {
readonly p2p: string;
readonly block: string;
readonly app: string;
};
/**
* Additional information. E.g.
* {
* "tx_index": "on",
* "rpc_address":"tcp://0.0.0.0:26657"
* }
*/
readonly other: Record<string, unknown>;
}
function decodeNodeInfo(data: RpcNodeInfo): responses.NodeInfo {
return {
id: fromHex(assertNotEmpty(data.id)),
listenAddr: assertNotEmpty(data.listen_addr),
network: assertNotEmpty(data.network),
version: assertString(data.version), // Can be empty (https://github.com/cosmos/cosmos-sdk/issues/7963)
channels: assertNotEmpty(data.channels),
moniker: assertNotEmpty(data.moniker),
other: dictionaryToStringMap(data.other),
protocolVersion: {
app: Integer.parse(assertNotEmpty(data.protocol_version.app)),
block: Integer.parse(assertNotEmpty(data.protocol_version.block)),
p2p: Integer.parse(assertNotEmpty(data.protocol_version.p2p)),
},
};
}
interface RpcSyncInfo {
/** hex encoded */
readonly latest_block_hash: string;
/** hex encoded */
readonly latest_app_hash: string;
readonly latest_block_height: string;
readonly latest_block_time: string;
readonly catching_up: boolean;
}
function decodeSyncInfo(data: RpcSyncInfo): responses.SyncInfo {
return {
latestBlockHash: fromHex(assertNotEmpty(data.latest_block_hash)),
latestAppHash: fromHex(assertNotEmpty(data.latest_app_hash)),
latestBlockTime: fromRfc3339WithNanoseconds(assertNotEmpty(data.latest_block_time)),
latestBlockHeight: Integer.parse(assertNotEmpty(data.latest_block_height)),
catchingUp: assertBoolean(data.catching_up),
};
}
interface RpcStatusResponse {
readonly node_info: RpcNodeInfo;
readonly sync_info: RpcSyncInfo;
readonly validator_info: RpcValidatorInfo;
}
function decodeStatus(data: RpcStatusResponse): responses.StatusResponse {
return {
nodeInfo: decodeNodeInfo(data.node_info),
syncInfo: decodeSyncInfo(data.sync_info),
validatorInfo: decodeValidatorInfo(data.validator_info),
};
}
/**
* Example data:
* {
* "root_hash": "10A1A17D5F818099B5CAB5B91733A3CC27C0DB6CE2D571AC27FB970C314308BB",
* "data": "ZVlERVhDV2lVNEUwPXhTUjc4Tmp2QkNVSg==",
* "proof": {
* "total": "1",
* "index": "0",
* "leaf_hash": "EKGhfV+BgJm1yrW5FzOjzCfA22zi1XGsJ/uXDDFDCLs=",
* "aunts": []
* }
* }
*/
interface RpcTxProof {
/** base64 encoded */
readonly data: string;
/** hex encoded */
readonly root_hash: string;
readonly proof: {
readonly total: string;
readonly index: string;
/** base64 encoded */
readonly leaf_hash: string;
/** base64 encoded */
readonly aunts: readonly string[];
};
}
function decodeTxProof(data: RpcTxProof): responses.TxProof {
return {
data: fromBase64(assertNotEmpty(data.data)),
rootHash: fromHex(assertNotEmpty(data.root_hash)),
proof: {
total: Integer.parse(assertNotEmpty(data.proof.total)),
index: Integer.parse(assertNotEmpty(data.proof.index)),
leafHash: fromBase64(assertNotEmpty(data.proof.leaf_hash)),
aunts: assertArray(data.proof.aunts).map(fromBase64),
},
};
}
interface RpcTxResponse {
/** Raw tx bytes, base64 encoded */
readonly tx: string;
readonly tx_result: RpcTxData;
readonly height: string;
readonly index: number;
/** hex encoded */
readonly hash: string;
readonly proof?: RpcTxProof;
}
function decodeTxResponse(data: RpcTxResponse): responses.TxResponse {
return {
tx: fromBase64(assertNotEmpty(data.tx)),
result: decodeTxData(assertObject(data.tx_result)),
height: Integer.parse(assertNotEmpty(data.height)),
index: Integer.parse(assertNumber(data.index)),
hash: fromHex(assertNotEmpty(data.hash)),
proof: may(decodeTxProof, data.proof),
};
}
interface RpcTxSearchResponse {
readonly txs: readonly RpcTxResponse[];
readonly total_count: string;
}
function decodeTxSearch(data: RpcTxSearchResponse): responses.TxSearchResponse {
return {
totalCount: Integer.parse(assertNotEmpty(data.total_count)),
txs: assertArray(data.txs).map(decodeTxResponse),
};
}
interface RpcTxEvent {
/** Raw tx bytes, base64 encoded */
readonly tx: string;
readonly result: RpcTxData;
readonly height: string;
/** Not set since Tendermint 0.34 */
readonly index?: number;
}
function decodeTxEvent(data: RpcTxEvent): responses.TxEvent {
const tx = fromBase64(assertNotEmpty(data.tx));
return {
tx: tx,
hash: hashTx(tx),
result: decodeTxData(data.result),
height: Integer.parse(assertNotEmpty(data.height)),
index: may(Integer.parse, data.index),
};
}
interface RpcValidatorsResponse {
readonly block_height: string;
readonly validators: readonly RpcValidatorUpdate[];
readonly count: string;
readonly total: string;
}
function decodeValidators(data: RpcValidatorsResponse): responses.ValidatorsResponse {
return {
blockHeight: Integer.parse(assertNotEmpty(data.block_height)),
validators: assertArray(data.validators).map(decodeValidatorUpdate),
count: Integer.parse(assertNotEmpty(data.count)),
total: Integer.parse(assertNotEmpty(data.total)),
};
}
// We lost track on how the evidence structure actually looks like.
// This is any now and passed to the caller untouched.
type RpcEvidence = any;
interface RpcBlock {
readonly header: RpcHeader;
readonly last_commit: RpcCommit;
readonly data: {
/** Raw tx bytes, base64 encoded */
readonly txs?: readonly string[];
};
// It's currently unclear why the deep nesting is requied.
// See https://github.com/tendermint/tendermint/issues/7697.
readonly evidence?: {
readonly evidence?: readonly RpcEvidence[];
};
}
function decodeBlock(data: RpcBlock): responses.Block {
return {
header: decodeHeader(assertObject(data.header)),
// For the block at height 1, last commit is not set. This is represented in an empty object like this:
// { height: '0', round: 0, block_id: { hash: '', parts: [Object] }, signatures: [] }
lastCommit: data.last_commit.block_id.hash ? decodeCommit(assertObject(data.last_commit)) : null,
txs: data.data.txs ? assertArray(data.data.txs).map(fromBase64) : [],
// Lift up .evidence.evidence to just .evidence
// See https://github.com/tendermint/tendermint/issues/7697
evidence: data.evidence?.evidence ?? [],
};
}
interface RpcBlockResponse {
readonly block_id: RpcBlockId;
readonly block: RpcBlock;
}
function decodeBlockResponse(data: RpcBlockResponse): responses.BlockResponse {
return {
blockId: decodeBlockId(data.block_id),
block: decodeBlock(data.block),
};
}
interface RpcBlockSearchResponse {
readonly blocks: readonly RpcBlockResponse[];
readonly total_count: string;
}
function decodeBlockSearch(data: RpcBlockSearchResponse): responses.BlockSearchResponse {
return {
totalCount: Integer.parse(assertNotEmpty(data.total_count)),
blocks: assertArray(data.blocks).map(decodeBlockResponse),
};
}
export class Responses {
public static decodeAbciInfo(response: JsonRpcSuccessResponse): responses.AbciInfoResponse {
return decodeAbciInfo(assertObject((response.result as AbciInfoResult).response));
}
public static decodeAbciQuery(response: JsonRpcSuccessResponse): responses.AbciQueryResponse {
return decodeAbciQuery(assertObject((response.result as AbciQueryResult).response));
}
public static decodeBlock(response: JsonRpcSuccessResponse): responses.BlockResponse {
return decodeBlockResponse(response.result as RpcBlockResponse);
}
public static decodeBlockResults(response: JsonRpcSuccessResponse): responses.BlockResultsResponse {
return decodeBlockResults(response.result as RpcBlockResultsResponse);
}
public static decodeBlockSearch(response: JsonRpcSuccessResponse): responses.BlockSearchResponse {
return decodeBlockSearch(response.result as RpcBlockSearchResponse);
}
public static decodeBlockchain(response: JsonRpcSuccessResponse): responses.BlockchainResponse {
return decodeBlockchain(response.result as RpcBlockchainResponse);
}
public static decodeBroadcastTxSync(response: JsonRpcSuccessResponse): responses.BroadcastTxSyncResponse {
return decodeBroadcastTxSync(response.result as RpcBroadcastTxSyncResponse);
}
public static decodeBroadcastTxAsync(response: JsonRpcSuccessResponse): responses.BroadcastTxAsyncResponse {
return Responses.decodeBroadcastTxSync(response);
}
public static decodeBroadcastTxCommit(
response: JsonRpcSuccessResponse,
): responses.BroadcastTxCommitResponse {
return decodeBroadcastTxCommit(response.result as RpcBroadcastTxCommitResponse);
}
public static decodeCommit(response: JsonRpcSuccessResponse): responses.CommitResponse {
return decodeCommitResponse(response.result as RpcCommitResponse);
}
public static decodeGenesis(response: JsonRpcSuccessResponse): responses.GenesisResponse {
return decodeGenesis(assertObject((response.result as GenesisResult).genesis));
}
public static decodeHealth(): responses.HealthResponse {
return null;
}
public static decodeStatus(response: JsonRpcSuccessResponse): responses.StatusResponse {
return decodeStatus(response.result as RpcStatusResponse);
}
public static decodeNewBlockEvent(event: SubscriptionEvent): responses.NewBlockEvent {
return decodeBlock(event.data.value.block as RpcBlock);
}
public static decodeNewBlockHeaderEvent(event: SubscriptionEvent): responses.NewBlockHeaderEvent {
return decodeHeader(event.data.value.header as RpcHeader);
}
public static decodeTxEvent(event: SubscriptionEvent): responses.TxEvent {
return decodeTxEvent(event.data.value.TxResult as RpcTxEvent);
}
public static decodeTx(response: JsonRpcSuccessResponse): responses.TxResponse {
return decodeTxResponse(response.result as RpcTxResponse);
}
public static decodeTxSearch(response: JsonRpcSuccessResponse): responses.TxSearchResponse {
return decodeTxSearch(response.result as RpcTxSearchResponse);
}
public static decodeValidators(response: JsonRpcSuccessResponse): responses.ValidatorsResponse {
return decodeValidators(response.result as RpcValidatorsResponse);
}
}