@gnolang/tm2-js-client 1.0.0
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/LICENSE.md +201 -0
- package/README.md +78 -0
- package/bin/index.d.ts +4 -0
- package/bin/index.js +20 -0
- package/bin/proto/google/protobuf/any.d.ts +151 -0
- package/bin/proto/google/protobuf/any.js +124 -0
- package/bin/proto/index.d.ts +2 -0
- package/bin/proto/index.js +20 -0
- package/bin/proto/tm2/tx.d.ts +540 -0
- package/bin/proto/tm2/tx.js +365 -0
- package/bin/provider/endpoints.d.ts +28 -0
- package/bin/provider/endpoints.js +36 -0
- package/bin/provider/index.d.ts +6 -0
- package/bin/provider/index.js +22 -0
- package/bin/provider/jsonrpc/index.d.ts +1 -0
- package/bin/provider/jsonrpc/index.js +17 -0
- package/bin/provider/jsonrpc/jsonrpc.d.ts +27 -0
- package/bin/provider/jsonrpc/jsonrpc.js +219 -0
- package/bin/provider/jsonrpc/jsonrpc.test.d.ts +1 -0
- package/bin/provider/jsonrpc/jsonrpc.test.js +374 -0
- package/bin/provider/provider.d.ts +86 -0
- package/bin/provider/provider.js +2 -0
- package/bin/provider/types/abci.d.ts +39 -0
- package/bin/provider/types/abci.js +4 -0
- package/bin/provider/types/common.d.ts +154 -0
- package/bin/provider/types/common.js +2 -0
- package/bin/provider/types/index.d.ts +3 -0
- package/bin/provider/types/index.js +19 -0
- package/bin/provider/types/jsonrpc.d.ts +26 -0
- package/bin/provider/types/jsonrpc.js +2 -0
- package/bin/provider/utility/index.d.ts +2 -0
- package/bin/provider/utility/index.js +18 -0
- package/bin/provider/utility/provider.utility.d.ts +28 -0
- package/bin/provider/utility/provider.utility.js +194 -0
- package/bin/provider/utility/requests.utility.d.ts +33 -0
- package/bin/provider/utility/requests.utility.js +74 -0
- package/bin/provider/websocket/index.d.ts +1 -0
- package/bin/provider/websocket/index.js +17 -0
- package/bin/provider/websocket/ws.d.ts +55 -0
- package/bin/provider/websocket/ws.js +303 -0
- package/bin/provider/websocket/ws.test.d.ts +1 -0
- package/bin/provider/websocket/ws.test.js +535 -0
- package/bin/services/index.d.ts +2 -0
- package/bin/services/index.js +18 -0
- package/bin/services/rest/restService.d.ts +4 -0
- package/bin/services/rest/restService.js +72 -0
- package/bin/services/rest/restService.types.d.ts +6 -0
- package/bin/services/rest/restService.types.js +2 -0
- package/bin/wallet/index.d.ts +5 -0
- package/bin/wallet/index.js +21 -0
- package/bin/wallet/key/index.d.ts +1 -0
- package/bin/wallet/key/index.js +17 -0
- package/bin/wallet/key/key.d.ts +21 -0
- package/bin/wallet/key/key.js +102 -0
- package/bin/wallet/key/key.test.d.ts +1 -0
- package/bin/wallet/key/key.test.js +121 -0
- package/bin/wallet/ledger/index.d.ts +1 -0
- package/bin/wallet/ledger/index.js +17 -0
- package/bin/wallet/ledger/ledger.d.ts +22 -0
- package/bin/wallet/ledger/ledger.js +109 -0
- package/bin/wallet/signer.d.ts +29 -0
- package/bin/wallet/signer.js +2 -0
- package/bin/wallet/types/index.d.ts +2 -0
- package/bin/wallet/types/index.js +18 -0
- package/bin/wallet/types/sign.d.ts +17 -0
- package/bin/wallet/types/sign.js +4 -0
- package/bin/wallet/types/wallet.d.ts +5 -0
- package/bin/wallet/types/wallet.js +2 -0
- package/bin/wallet/utility/index.d.ts +1 -0
- package/bin/wallet/utility/index.js +17 -0
- package/bin/wallet/utility/utility.d.ts +30 -0
- package/bin/wallet/utility/utility.js +105 -0
- package/bin/wallet/wallet.d.ts +94 -0
- package/bin/wallet/wallet.js +340 -0
- package/bin/wallet/wallet.test.d.ts +1 -0
- package/bin/wallet/wallet.test.js +345 -0
- package/package.json +64 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface ABCIResponse {
|
|
2
|
+
response: {
|
|
3
|
+
ResponseBase: ABCIResponseBase;
|
|
4
|
+
Key: string | null;
|
|
5
|
+
Value: string | null;
|
|
6
|
+
Proof: MerkleProof | null;
|
|
7
|
+
Height: string;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export interface ABCIResponseBase {
|
|
11
|
+
Error: {
|
|
12
|
+
[key: string]: string;
|
|
13
|
+
} | null;
|
|
14
|
+
Data: string | null;
|
|
15
|
+
Events: string | null;
|
|
16
|
+
Log: string;
|
|
17
|
+
Info: string;
|
|
18
|
+
}
|
|
19
|
+
interface MerkleProof {
|
|
20
|
+
ops: {
|
|
21
|
+
type: string;
|
|
22
|
+
key: string | null;
|
|
23
|
+
data: string | null;
|
|
24
|
+
}[];
|
|
25
|
+
}
|
|
26
|
+
export interface ABCIAccount {
|
|
27
|
+
BaseAccount: {
|
|
28
|
+
address: string;
|
|
29
|
+
coins: string;
|
|
30
|
+
public_key: {
|
|
31
|
+
type: string;
|
|
32
|
+
value: string;
|
|
33
|
+
} | null;
|
|
34
|
+
account_number: string;
|
|
35
|
+
sequence: string;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export declare const ABCIErrorKey = "@type";
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { ABCIResponseBase } from './abci';
|
|
2
|
+
export interface NetworkInfo {
|
|
3
|
+
listening: boolean;
|
|
4
|
+
listeners: string[];
|
|
5
|
+
n_peers: string;
|
|
6
|
+
peers: string[];
|
|
7
|
+
}
|
|
8
|
+
export interface Status {
|
|
9
|
+
node_info: NodeInfo;
|
|
10
|
+
sync_info: SyncInfo;
|
|
11
|
+
validator_info: ValidatorInfo;
|
|
12
|
+
}
|
|
13
|
+
interface NodeInfo {
|
|
14
|
+
version_set: VersionInfo[];
|
|
15
|
+
net_address: string;
|
|
16
|
+
network: string;
|
|
17
|
+
software: string;
|
|
18
|
+
version: string;
|
|
19
|
+
channels: string;
|
|
20
|
+
monkier: string;
|
|
21
|
+
other: {
|
|
22
|
+
tx_index: string;
|
|
23
|
+
rpc_address: string;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
interface VersionInfo {
|
|
27
|
+
Name: string;
|
|
28
|
+
Version: string;
|
|
29
|
+
Optional: boolean;
|
|
30
|
+
}
|
|
31
|
+
interface SyncInfo {
|
|
32
|
+
latest_block_hash: string;
|
|
33
|
+
latest_app_hash: string;
|
|
34
|
+
latest_block_height: string;
|
|
35
|
+
latest_block_time: string;
|
|
36
|
+
catching_up: boolean;
|
|
37
|
+
}
|
|
38
|
+
interface ValidatorInfo {
|
|
39
|
+
address: string;
|
|
40
|
+
pub_key: PublicKey;
|
|
41
|
+
voting_power: string;
|
|
42
|
+
}
|
|
43
|
+
interface PublicKey {
|
|
44
|
+
type: string;
|
|
45
|
+
value: string;
|
|
46
|
+
}
|
|
47
|
+
export interface ConsensusParams {
|
|
48
|
+
block_height: string;
|
|
49
|
+
consensus_params: {
|
|
50
|
+
Block: {
|
|
51
|
+
MaxTxBytes: string;
|
|
52
|
+
MaxDataBytes: string;
|
|
53
|
+
MaxBlockBytes: string;
|
|
54
|
+
MaxGas: string;
|
|
55
|
+
TimeIotaMS: string;
|
|
56
|
+
};
|
|
57
|
+
Validator: {
|
|
58
|
+
PubKeyTypeURLs: string[];
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export interface ConsensusState {
|
|
63
|
+
round_state: {
|
|
64
|
+
[key: string]: string | null | object;
|
|
65
|
+
start_time: string;
|
|
66
|
+
proposal_block_hash: string | null;
|
|
67
|
+
locked_block_hash: string | null;
|
|
68
|
+
valid_block_hash: string | null;
|
|
69
|
+
height_vote_set: object;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
export interface BlockInfo {
|
|
73
|
+
block_meta: BlockMeta;
|
|
74
|
+
block: Block;
|
|
75
|
+
}
|
|
76
|
+
export interface BlockMeta {
|
|
77
|
+
block_id: BlockID;
|
|
78
|
+
header: BlockHeader;
|
|
79
|
+
}
|
|
80
|
+
export interface Block {
|
|
81
|
+
header: BlockHeader;
|
|
82
|
+
data: {
|
|
83
|
+
txs: string[] | null;
|
|
84
|
+
};
|
|
85
|
+
last_commit: {
|
|
86
|
+
block_id: BlockID;
|
|
87
|
+
precommits: PrecommitInfo[] | null;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export interface BlockHeader {
|
|
91
|
+
version: string;
|
|
92
|
+
chain_id: string;
|
|
93
|
+
height: string;
|
|
94
|
+
time: string;
|
|
95
|
+
num_txs: string;
|
|
96
|
+
total_txs: string;
|
|
97
|
+
app_version: string;
|
|
98
|
+
last_block_id: BlockID;
|
|
99
|
+
last_commit_hash: string | null;
|
|
100
|
+
data_hash: string | null;
|
|
101
|
+
validators_hash: string;
|
|
102
|
+
consensus_hash: string;
|
|
103
|
+
app_hash: string;
|
|
104
|
+
last_results_hash: string | null;
|
|
105
|
+
proposer_address: string;
|
|
106
|
+
}
|
|
107
|
+
export interface BlockID {
|
|
108
|
+
hash: string | null;
|
|
109
|
+
parts: {
|
|
110
|
+
total: string;
|
|
111
|
+
hash: string | null;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export interface PrecommitInfo {
|
|
115
|
+
type: number;
|
|
116
|
+
height: string;
|
|
117
|
+
round: string;
|
|
118
|
+
block_id: BlockID;
|
|
119
|
+
timestamp: string;
|
|
120
|
+
validator_address: string;
|
|
121
|
+
validator_index: string;
|
|
122
|
+
signature: string;
|
|
123
|
+
}
|
|
124
|
+
export interface BlockResult {
|
|
125
|
+
height: string;
|
|
126
|
+
results: {
|
|
127
|
+
deliver_tx: DeliverTx[] | null;
|
|
128
|
+
end_block: EndBlock;
|
|
129
|
+
begin_block: BeginBlock;
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
export interface DeliverTx {
|
|
133
|
+
ResponseBase: ABCIResponseBase;
|
|
134
|
+
GasWanted: string;
|
|
135
|
+
GasUsed: string;
|
|
136
|
+
}
|
|
137
|
+
export interface EndBlock {
|
|
138
|
+
ResponseBase: ABCIResponseBase;
|
|
139
|
+
ValidatorUpdates: string | null;
|
|
140
|
+
ConsensusParams: string | null;
|
|
141
|
+
Events: string | null;
|
|
142
|
+
}
|
|
143
|
+
export interface BeginBlock {
|
|
144
|
+
ResponseBase: ABCIResponseBase;
|
|
145
|
+
}
|
|
146
|
+
export interface BroadcastTxResult {
|
|
147
|
+
error: {
|
|
148
|
+
[key: string]: string;
|
|
149
|
+
} | null;
|
|
150
|
+
data: string | null;
|
|
151
|
+
Log: string;
|
|
152
|
+
hash: string;
|
|
153
|
+
}
|
|
154
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./abci"), exports);
|
|
18
|
+
__exportStar(require("./common"), exports);
|
|
19
|
+
__exportStar(require("./jsonrpc"), exports);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The base JSON-RPC 2.0 request
|
|
3
|
+
*/
|
|
4
|
+
export interface RPCRequest {
|
|
5
|
+
jsonrpc: string;
|
|
6
|
+
id: string | number;
|
|
7
|
+
method: string;
|
|
8
|
+
params?: any[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The base JSON-RPC 2.0 response
|
|
12
|
+
*/
|
|
13
|
+
export interface RPCResponse<Result> {
|
|
14
|
+
jsonrpc: string;
|
|
15
|
+
id: string | number;
|
|
16
|
+
result?: Result;
|
|
17
|
+
error?: RPCError;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The base JSON-RPC 2.0 typed response error
|
|
21
|
+
*/
|
|
22
|
+
export interface RPCError {
|
|
23
|
+
code: number;
|
|
24
|
+
message: string;
|
|
25
|
+
data?: any;
|
|
26
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./provider.utility"), exports);
|
|
18
|
+
__exportStar(require("./requests.utility"), exports);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Provider } from '../provider';
|
|
2
|
+
import { Tx } from '../../proto';
|
|
3
|
+
/**
|
|
4
|
+
* Extracts the specific balance denomination from the ABCI response
|
|
5
|
+
* @param {string | null} abciData the base64-encoded ABCI data
|
|
6
|
+
* @param {string} denomination the required denomination
|
|
7
|
+
*/
|
|
8
|
+
export declare const extractBalanceFromResponse: (abciData: string | null, denomination: string) => number;
|
|
9
|
+
/**
|
|
10
|
+
* Extracts the account sequence from the ABCI response
|
|
11
|
+
* @param {string | null} abciData the base64-encoded ABCI data
|
|
12
|
+
*/
|
|
13
|
+
export declare const extractSequenceFromResponse: (abciData: string | null) => number;
|
|
14
|
+
/**
|
|
15
|
+
* Extracts the account number from the ABCI response
|
|
16
|
+
* @param {string | null} abciData the base64-encoded ABCI data
|
|
17
|
+
*/
|
|
18
|
+
export declare const extractAccountNumberFromResponse: (abciData: string | null) => number;
|
|
19
|
+
/**
|
|
20
|
+
* Waits for the transaction to be committed to a block in the chain
|
|
21
|
+
* of the specified provider. This helper does a search for incoming blocks
|
|
22
|
+
* and checks if a transaction
|
|
23
|
+
* @param {Provider} provider the provider instance
|
|
24
|
+
* @param {string} hash the base64-encoded hash of the transaction
|
|
25
|
+
* @param {number} [fromHeight=latest] the starting height for the search. If omitted, it is the latest block in the chain
|
|
26
|
+
* @param {number} [timeout=15000] the timeout in MS for the search
|
|
27
|
+
*/
|
|
28
|
+
export declare const waitForTransaction: (provider: Provider, hash: string, fromHeight?: number, timeout?: number) => Promise<Tx>;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.waitForTransaction = exports.extractAccountNumberFromResponse = exports.extractSequenceFromResponse = exports.extractBalanceFromResponse = void 0;
|
|
40
|
+
var requests_utility_1 = require("./requests.utility");
|
|
41
|
+
var proto_1 = require("../../proto");
|
|
42
|
+
var crypto_1 = require("@cosmjs/crypto");
|
|
43
|
+
/**
|
|
44
|
+
* Extracts the specific balance denomination from the ABCI response
|
|
45
|
+
* @param {string | null} abciData the base64-encoded ABCI data
|
|
46
|
+
* @param {string} denomination the required denomination
|
|
47
|
+
*/
|
|
48
|
+
var extractBalanceFromResponse = function (abciData, denomination) {
|
|
49
|
+
// Make sure the response is initialized
|
|
50
|
+
if (!abciData) {
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
// Extract the balances
|
|
54
|
+
var balancesRaw = Buffer.from(abciData, 'base64')
|
|
55
|
+
.toString()
|
|
56
|
+
.replace(/"/gi, '');
|
|
57
|
+
// Find the correct balance denomination
|
|
58
|
+
var balances = balancesRaw.split(',');
|
|
59
|
+
if (balances.length < 1) {
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
62
|
+
// Find the correct denomination
|
|
63
|
+
var pattern = new RegExp("^(\\d+)".concat(denomination, "$"));
|
|
64
|
+
for (var _i = 0, balances_1 = balances; _i < balances_1.length; _i++) {
|
|
65
|
+
var balance = balances_1[_i];
|
|
66
|
+
var match = balance.match(pattern);
|
|
67
|
+
if (match) {
|
|
68
|
+
return parseInt(match[1], 10);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return 0;
|
|
72
|
+
};
|
|
73
|
+
exports.extractBalanceFromResponse = extractBalanceFromResponse;
|
|
74
|
+
/**
|
|
75
|
+
* Extracts the account sequence from the ABCI response
|
|
76
|
+
* @param {string | null} abciData the base64-encoded ABCI data
|
|
77
|
+
*/
|
|
78
|
+
var extractSequenceFromResponse = function (abciData) {
|
|
79
|
+
// Make sure the response is initialized
|
|
80
|
+
if (!abciData) {
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
// Parse the account
|
|
85
|
+
var account = (0, requests_utility_1.parseABCI)(abciData);
|
|
86
|
+
return parseInt(account.BaseAccount.sequence, 10);
|
|
87
|
+
}
|
|
88
|
+
catch (e) { }
|
|
89
|
+
// Account not initialized,
|
|
90
|
+
// return default value (0)
|
|
91
|
+
return 0;
|
|
92
|
+
};
|
|
93
|
+
exports.extractSequenceFromResponse = extractSequenceFromResponse;
|
|
94
|
+
/**
|
|
95
|
+
* Extracts the account number from the ABCI response
|
|
96
|
+
* @param {string | null} abciData the base64-encoded ABCI data
|
|
97
|
+
*/
|
|
98
|
+
var extractAccountNumberFromResponse = function (abciData) {
|
|
99
|
+
// Make sure the response is initialized
|
|
100
|
+
if (!abciData) {
|
|
101
|
+
throw new Error('account is not initialized');
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
// Parse the account
|
|
105
|
+
var account = (0, requests_utility_1.parseABCI)(abciData);
|
|
106
|
+
return parseInt(account.BaseAccount.account_number, 10);
|
|
107
|
+
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
throw new Error('account is not initialized');
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
exports.extractAccountNumberFromResponse = extractAccountNumberFromResponse;
|
|
113
|
+
/**
|
|
114
|
+
* Waits for the transaction to be committed to a block in the chain
|
|
115
|
+
* of the specified provider. This helper does a search for incoming blocks
|
|
116
|
+
* and checks if a transaction
|
|
117
|
+
* @param {Provider} provider the provider instance
|
|
118
|
+
* @param {string} hash the base64-encoded hash of the transaction
|
|
119
|
+
* @param {number} [fromHeight=latest] the starting height for the search. If omitted, it is the latest block in the chain
|
|
120
|
+
* @param {number} [timeout=15000] the timeout in MS for the search
|
|
121
|
+
*/
|
|
122
|
+
var waitForTransaction = function (provider, hash, fromHeight, timeout) { return __awaiter(void 0, void 0, void 0, function () {
|
|
123
|
+
return __generator(this, function (_a) {
|
|
124
|
+
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () {
|
|
125
|
+
var currentHeight, _a, exitTimeout, fetchInterval;
|
|
126
|
+
return __generator(this, function (_b) {
|
|
127
|
+
switch (_b.label) {
|
|
128
|
+
case 0:
|
|
129
|
+
if (!fromHeight) return [3 /*break*/, 1];
|
|
130
|
+
_a = fromHeight;
|
|
131
|
+
return [3 /*break*/, 3];
|
|
132
|
+
case 1: return [4 /*yield*/, provider.getBlockNumber()];
|
|
133
|
+
case 2:
|
|
134
|
+
_a = _b.sent();
|
|
135
|
+
_b.label = 3;
|
|
136
|
+
case 3:
|
|
137
|
+
currentHeight = _a;
|
|
138
|
+
exitTimeout = timeout ? timeout : 15000;
|
|
139
|
+
fetchInterval = setInterval(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
140
|
+
var latestHeight, blockNum, block, _i, _a, tx, txRaw, txHash;
|
|
141
|
+
return __generator(this, function (_b) {
|
|
142
|
+
switch (_b.label) {
|
|
143
|
+
case 0: return [4 /*yield*/, provider.getBlockNumber()];
|
|
144
|
+
case 1:
|
|
145
|
+
latestHeight = _b.sent();
|
|
146
|
+
if (latestHeight < currentHeight) {
|
|
147
|
+
// No need to parse older blocks
|
|
148
|
+
return [2 /*return*/];
|
|
149
|
+
}
|
|
150
|
+
blockNum = currentHeight;
|
|
151
|
+
_b.label = 2;
|
|
152
|
+
case 2:
|
|
153
|
+
if (!(blockNum <= latestHeight)) return [3 /*break*/, 5];
|
|
154
|
+
return [4 /*yield*/, provider.getBlock(blockNum)];
|
|
155
|
+
case 3:
|
|
156
|
+
block = _b.sent();
|
|
157
|
+
// Check if there are any transactions at all in the block
|
|
158
|
+
if (!block.block.data.txs || block.block.data.txs.length == 0) {
|
|
159
|
+
return [3 /*break*/, 4];
|
|
160
|
+
}
|
|
161
|
+
// Find the transaction among the block transactions
|
|
162
|
+
for (_i = 0, _a = block.block.data.txs; _i < _a.length; _i++) {
|
|
163
|
+
tx = _a[_i];
|
|
164
|
+
txRaw = (0, requests_utility_1.base64ToUint8Array)(tx);
|
|
165
|
+
txHash = (0, crypto_1.sha256)(txRaw);
|
|
166
|
+
if ((0, requests_utility_1.uint8ArrayToBase64)(txHash) == hash) {
|
|
167
|
+
// Clear the interval
|
|
168
|
+
clearInterval(fetchInterval);
|
|
169
|
+
// Decode the transaction from amino
|
|
170
|
+
resolve(proto_1.Tx.decode(txRaw));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
_b.label = 4;
|
|
174
|
+
case 4:
|
|
175
|
+
blockNum++;
|
|
176
|
+
return [3 /*break*/, 2];
|
|
177
|
+
case 5:
|
|
178
|
+
currentHeight = latestHeight + 1;
|
|
179
|
+
return [2 /*return*/];
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}); }, 1000);
|
|
183
|
+
setTimeout(function () {
|
|
184
|
+
// Clear the fetch interval
|
|
185
|
+
clearInterval(fetchInterval);
|
|
186
|
+
reject('transaction fetch timeout');
|
|
187
|
+
}, exitTimeout);
|
|
188
|
+
return [2 /*return*/];
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}); })];
|
|
192
|
+
});
|
|
193
|
+
}); };
|
|
194
|
+
exports.waitForTransaction = waitForTransaction;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { RPCError, RPCRequest, RPCResponse } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a new JSON-RPC 2.0 request
|
|
4
|
+
* @param {string} method the requested method
|
|
5
|
+
* @param {string[]} [params] the requested params, if any
|
|
6
|
+
*/
|
|
7
|
+
export declare const newRequest: (method: string, params?: any[]) => RPCRequest;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new JSON-RPC 2.0 response
|
|
10
|
+
* @param {Result} result the response result, if any
|
|
11
|
+
* @param {RPCError} error the response error, if any
|
|
12
|
+
*/
|
|
13
|
+
export declare const newResponse: <Result>(result?: Result | undefined, error?: RPCError) => RPCResponse<Result>;
|
|
14
|
+
/**
|
|
15
|
+
* Parses the base64 encoded ABCI JSON into a concrete type
|
|
16
|
+
* @param {string} data the base64-encoded JSON
|
|
17
|
+
*/
|
|
18
|
+
export declare const parseABCI: <Result>(data: string) => Result;
|
|
19
|
+
/**
|
|
20
|
+
* Converts a string into base64 representation
|
|
21
|
+
* @param {string} str the raw string
|
|
22
|
+
*/
|
|
23
|
+
export declare const stringToBase64: (str: string) => string;
|
|
24
|
+
/**
|
|
25
|
+
* Converts a base64 string into a Uint8Array representation
|
|
26
|
+
* @param {string} str the base64-encoded string
|
|
27
|
+
*/
|
|
28
|
+
export declare const base64ToUint8Array: (str: string) => Uint8Array;
|
|
29
|
+
/**
|
|
30
|
+
* Converts a Uint8Array into base64 representation
|
|
31
|
+
* @param {Uint8Array} data the Uint8Array to be encoded
|
|
32
|
+
*/
|
|
33
|
+
export declare const uint8ArrayToBase64: (data: Uint8Array) => string;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uint8ArrayToBase64 = exports.base64ToUint8Array = exports.stringToBase64 = exports.parseABCI = exports.newResponse = exports.newRequest = void 0;
|
|
4
|
+
// The version of the supported JSON-RPC protocol
|
|
5
|
+
var standardVersion = '2.0';
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new JSON-RPC 2.0 request
|
|
8
|
+
* @param {string} method the requested method
|
|
9
|
+
* @param {string[]} [params] the requested params, if any
|
|
10
|
+
*/
|
|
11
|
+
var newRequest = function (method, params) {
|
|
12
|
+
return {
|
|
13
|
+
// the ID of the request is not that relevant for this helper method;
|
|
14
|
+
// for finer ID control, instantiate the request object directly
|
|
15
|
+
id: Date.now(),
|
|
16
|
+
jsonrpc: standardVersion,
|
|
17
|
+
method: method,
|
|
18
|
+
params: params,
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
exports.newRequest = newRequest;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new JSON-RPC 2.0 response
|
|
24
|
+
* @param {Result} result the response result, if any
|
|
25
|
+
* @param {RPCError} error the response error, if any
|
|
26
|
+
*/
|
|
27
|
+
var newResponse = function (result, error) {
|
|
28
|
+
return {
|
|
29
|
+
id: Date.now(),
|
|
30
|
+
jsonrpc: standardVersion,
|
|
31
|
+
result: result,
|
|
32
|
+
error: error,
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
exports.newResponse = newResponse;
|
|
36
|
+
/**
|
|
37
|
+
* Parses the base64 encoded ABCI JSON into a concrete type
|
|
38
|
+
* @param {string} data the base64-encoded JSON
|
|
39
|
+
*/
|
|
40
|
+
var parseABCI = function (data) {
|
|
41
|
+
var jsonData = Buffer.from(data, 'base64').toString();
|
|
42
|
+
var parsedData = JSON.parse(jsonData);
|
|
43
|
+
if (!parsedData) {
|
|
44
|
+
throw new Error('unable to parse JSON response');
|
|
45
|
+
}
|
|
46
|
+
return parsedData;
|
|
47
|
+
};
|
|
48
|
+
exports.parseABCI = parseABCI;
|
|
49
|
+
/**
|
|
50
|
+
* Converts a string into base64 representation
|
|
51
|
+
* @param {string} str the raw string
|
|
52
|
+
*/
|
|
53
|
+
var stringToBase64 = function (str) {
|
|
54
|
+
var buffer = Buffer.from(str, 'utf-8');
|
|
55
|
+
return buffer.toString('base64');
|
|
56
|
+
};
|
|
57
|
+
exports.stringToBase64 = stringToBase64;
|
|
58
|
+
/**
|
|
59
|
+
* Converts a base64 string into a Uint8Array representation
|
|
60
|
+
* @param {string} str the base64-encoded string
|
|
61
|
+
*/
|
|
62
|
+
var base64ToUint8Array = function (str) {
|
|
63
|
+
var buffer = Buffer.from(str, 'base64');
|
|
64
|
+
return new Uint8Array(buffer);
|
|
65
|
+
};
|
|
66
|
+
exports.base64ToUint8Array = base64ToUint8Array;
|
|
67
|
+
/**
|
|
68
|
+
* Converts a Uint8Array into base64 representation
|
|
69
|
+
* @param {Uint8Array} data the Uint8Array to be encoded
|
|
70
|
+
*/
|
|
71
|
+
var uint8ArrayToBase64 = function (data) {
|
|
72
|
+
return Buffer.from(data).toString('base64');
|
|
73
|
+
};
|
|
74
|
+
exports.uint8ArrayToBase64 = uint8ArrayToBase64;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ws';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./ws"), exports);
|