@gnolang/tm2-js-client
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -7,1 +7,2 @@ export * from './jsonrpc'; | ||
export * from './provider'; | ||
export * from './errors'; |
@@ -23,1 +23,2 @@ "use strict"; | ||
__exportStar(require("./provider"), exports); | ||
__exportStar(require("./errors"), exports); |
import { Provider } from '../provider'; | ||
import { BlockInfo, BlockResult, ConsensusParams, NetworkInfo, Status } from '../types'; | ||
import { TransactionEndpoint } from '../endpoints'; | ||
import { Tx } from '../../proto'; | ||
@@ -25,4 +26,6 @@ /** | ||
getStatus(): Promise<Status>; | ||
sendTransaction(tx: string): Promise<string>; | ||
sendTransaction(tx: string, endpoint?: TransactionEndpoint.BROADCAST_TX_SYNC | TransactionEndpoint.BROADCAST_TX_COMMIT): Promise<string>; | ||
private broadcastTxSync; | ||
private broadcastTxCommit; | ||
waitForTransaction(hash: string, fromHeight?: number, timeout?: number): Promise<Tx>; | ||
} |
@@ -43,2 +43,3 @@ "use strict"; | ||
var endpoints_1 = require("../endpoints"); | ||
var errors_utility_1 = require("../utility/errors.utility"); | ||
/** | ||
@@ -196,12 +197,36 @@ * Provider based on JSON-RPC HTTP requests | ||
}; | ||
JSONRPCProvider.prototype.sendTransaction = function (tx) { | ||
JSONRPCProvider.prototype.sendTransaction = function (tx, endpoint) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response; | ||
var queryEndpoint, request; | ||
return __generator(this, function (_a) { | ||
queryEndpoint = endpoint | ||
? endpoint | ||
: endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC; | ||
request = (0, utility_1.newRequest)(queryEndpoint, [tx]); | ||
if (queryEndpoint == endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC) { | ||
return [2 /*return*/, this.broadcastTxSync(request)]; | ||
} | ||
// The endpoint is a commit broadcast | ||
// (it waits for the transaction to be committed) to the chain before returning | ||
return [2 /*return*/, this.broadcastTxCommit(request)]; | ||
}); | ||
}); | ||
}; | ||
JSONRPCProvider.prototype.broadcastTxSync = function (request) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response, errType, log; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, { | ||
request: (0, utility_1.newRequest)(endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC, [tx]), | ||
request: request, | ||
})]; | ||
case 1: | ||
response = _a.sent(); | ||
// Check if there is an immediate tx-broadcast error | ||
// (originating from basic transaction checks like CheckTx) | ||
if (response.error) { | ||
errType = response.error.ABCIErrorKey; | ||
log = response.Log; | ||
throw (0, errors_utility_1.constructRequestError)(errType, log); | ||
} | ||
return [2 /*return*/, response.hash]; | ||
@@ -212,2 +237,30 @@ } | ||
}; | ||
JSONRPCProvider.prototype.broadcastTxCommit = function (request) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response, check_tx, deliver_tx, errType, log, errType, log; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, services_1.RestService.post(this.baseURL, { | ||
request: request, | ||
})]; | ||
case 1: | ||
response = _a.sent(); | ||
check_tx = response.check_tx, deliver_tx = response.deliver_tx; | ||
// Check if there is an immediate tx-broadcast error (in CheckTx) | ||
if (check_tx.ResponseBase.Error) { | ||
errType = check_tx.ResponseBase.Error.ABCIErrorKey; | ||
log = check_tx.ResponseBase.Log; | ||
throw (0, errors_utility_1.constructRequestError)(errType, log); | ||
} | ||
// Check if there is a parsing error with the transaction (in DeliverTx) | ||
if (deliver_tx.ResponseBase.Error) { | ||
errType = deliver_tx.ResponseBase.Error.ABCIErrorKey; | ||
log = deliver_tx.ResponseBase.Log; | ||
throw (0, errors_utility_1.constructRequestError)(errType, log); | ||
} | ||
return [2 /*return*/, response.hash]; | ||
} | ||
}); | ||
}); | ||
}; | ||
JSONRPCProvider.prototype.waitForTransaction = function (hash, fromHeight, timeout) { | ||
@@ -214,0 +267,0 @@ return __awaiter(this, void 0, void 0, function () { |
@@ -49,2 +49,3 @@ "use strict"; | ||
var endpoints_1 = require("../endpoints"); | ||
var messages_1 = require("../errors/messages"); | ||
jest.mock('axios'); | ||
@@ -112,22 +113,53 @@ var mockedAxios = axios_1.default; | ||
}); }); | ||
test('sendTransaction', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var mockResult, provider, hash; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
mockResult = (0, jest_mock_extended_1.mock)(); | ||
mockResult.hash = 'hash123'; | ||
mockedAxios.post.mockResolvedValue({ | ||
data: (0, utility_1.newResponse)(mockResult), | ||
}); | ||
provider = new jsonrpc_1.JSONRPCProvider(mockURL); | ||
return [4 /*yield*/, provider.sendTransaction('encoded tx')]; | ||
case 1: | ||
hash = _a.sent(); | ||
expect(axios_1.default.post).toHaveBeenCalled(); | ||
expect(hash).toEqual(mockResult.hash); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
describe('sendTransaction', function () { | ||
var validResult = { | ||
error: null, | ||
data: null, | ||
Log: '', | ||
hash: 'hash123', | ||
}; | ||
var mockError = '/std.UnauthorizedError'; | ||
var mockLog = 'random error message'; | ||
var invalidResult = { | ||
error: { | ||
ABCIErrorKey: mockError, | ||
}, | ||
data: null, | ||
Log: mockLog, | ||
hash: '', | ||
}; | ||
test.each([ | ||
[validResult, validResult.hash, '', ''], | ||
[invalidResult, invalidResult.hash, messages_1.UnauthorizedErrorMessage, mockLog], // error out | ||
])('case %#', function (response, expectedHash, expectedErr, expectedLog) { return __awaiter(void 0, void 0, void 0, function () { | ||
var provider, hash, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
mockedAxios.post.mockResolvedValue({ | ||
data: (0, utility_1.newResponse)(response), | ||
}); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
provider = new jsonrpc_1.JSONRPCProvider(mockURL); | ||
return [4 /*yield*/, provider.sendTransaction('encoded tx')]; | ||
case 2: | ||
hash = _a.sent(); | ||
expect(axios_1.default.post).toHaveBeenCalled(); | ||
expect(hash).toEqual(expectedHash); | ||
if (expectedErr != '') { | ||
fail('expected error'); | ||
} | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
expect(e_1.message).toBe(expectedErr); | ||
expect(e_1.log).toBe(expectedLog); | ||
return [3 /*break*/, 4]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
}); | ||
test('waitForTransaction', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -342,3 +374,3 @@ var emptyBlock, tx, encodedTx, txHash, filledBlock, latestBlock, startBlock, mockStatus, responseMap, provider, receivedTx; | ||
])('case %#', function (response, expected) { return __awaiter(void 0, void 0, void 0, function () { | ||
var mockABCIResponse, provider, accountNumber, e_1; | ||
var mockABCIResponse, provider, accountNumber, e_2; | ||
return __generator(this, function (_a) { | ||
@@ -369,4 +401,4 @@ switch (_a.label) { | ||
case 3: | ||
e_1 = _a.sent(); | ||
expect(e_1.message).toContain('account is not initialized'); | ||
e_2 = _a.sent(); | ||
expect(e_2.message).toContain('account is not initialized'); | ||
return [3 /*break*/, 4]; | ||
@@ -373,0 +405,0 @@ case 4: return [2 /*return*/]; |
import { BlockInfo, BlockResult, ConsensusParams, NetworkInfo, Status } from './types'; | ||
import { Tx } from '../proto'; | ||
import { TransactionEndpoint } from './endpoints'; | ||
/** | ||
@@ -71,4 +72,6 @@ * Read-only abstraction for accessing blockchain data | ||
* @param {string} tx the base64-encoded signed transaction | ||
* @param {TransactionEndpoint} endpoint the transaction broadcast type (sync / commit). | ||
* Defaults to broadcast_sync | ||
*/ | ||
sendTransaction(tx: string): Promise<string>; | ||
sendTransaction(tx: string, endpoint?: TransactionEndpoint.BROADCAST_TX_SYNC | TransactionEndpoint.BROADCAST_TX_COMMIT): Promise<string>; | ||
/** | ||
@@ -75,0 +78,0 @@ * Waits for the transaction to be committed on the chain. |
@@ -146,3 +146,3 @@ import { ABCIResponseBase } from './abci'; | ||
} | ||
export interface BroadcastTxResult { | ||
export interface BroadcastTxSyncResult { | ||
error: { | ||
@@ -155,2 +155,8 @@ [key: string]: string; | ||
} | ||
export interface BroadcastTxCommitResult { | ||
check_tx: DeliverTx; | ||
deliver_tx: DeliverTx; | ||
hash: string; | ||
height: string; | ||
} | ||
export {}; |
/// <reference types="node" /> | ||
import { Provider } from '../provider'; | ||
import { BlockInfo, BlockResult, ConsensusParams, NetworkInfo, RPCRequest, RPCResponse, Status } from '../types'; | ||
import { TransactionEndpoint } from '../endpoints'; | ||
import { Tx } from '../../proto'; | ||
@@ -53,4 +54,6 @@ /** | ||
getStatus(): Promise<Status>; | ||
sendTransaction(tx: string): Promise<string>; | ||
sendTransaction(tx: string, endpoint?: TransactionEndpoint.BROADCAST_TX_SYNC | TransactionEndpoint.BROADCAST_TX_COMMIT): Promise<string>; | ||
private broadcastTxSync; | ||
private broadcastTxCommit; | ||
waitForTransaction(hash: string, fromHeight?: number, timeout?: number): Promise<Tx>; | ||
} |
@@ -42,2 +42,3 @@ "use strict"; | ||
var endpoints_1 = require("../endpoints"); | ||
var errors_utility_1 = require("../utility/errors.utility"); | ||
/** | ||
@@ -286,11 +287,36 @@ * Provider based on WS JSON-RPC HTTP requests | ||
}; | ||
WSProvider.prototype.sendTransaction = function (tx) { | ||
WSProvider.prototype.sendTransaction = function (tx, endpoint) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response; | ||
var queryEndpoint, request; | ||
return __generator(this, function (_a) { | ||
queryEndpoint = endpoint | ||
? endpoint | ||
: endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC; | ||
request = (0, utility_1.newRequest)(queryEndpoint, [tx]); | ||
if (queryEndpoint == endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC) { | ||
return [2 /*return*/, this.broadcastTxSync(request)]; | ||
} | ||
// The endpoint is a commit broadcast | ||
// (it waits for the transaction to be committed) to the chain before returning | ||
return [2 /*return*/, this.broadcastTxCommit(request)]; | ||
}); | ||
}); | ||
}; | ||
WSProvider.prototype.broadcastTxSync = function (request) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response, broadcastResponse, errType, log; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.sendRequest((0, utility_1.newRequest)(endpoints_1.TransactionEndpoint.BROADCAST_TX_SYNC, [tx]))]; | ||
case 0: return [4 /*yield*/, this.sendRequest(request)]; | ||
case 1: | ||
response = _a.sent(); | ||
return [2 /*return*/, this.parseResponse(response).hash]; | ||
broadcastResponse = this.parseResponse(response); | ||
// Check if there is an immediate tx-broadcast error | ||
// (originating from basic transaction checks like CheckTx) | ||
if (broadcastResponse.error) { | ||
errType = broadcastResponse.error.ABCIErrorKey; | ||
log = broadcastResponse.Log; | ||
throw (0, errors_utility_1.constructRequestError)(errType, log); | ||
} | ||
return [2 /*return*/, broadcastResponse.hash]; | ||
} | ||
@@ -300,2 +326,29 @@ }); | ||
}; | ||
WSProvider.prototype.broadcastTxCommit = function (request) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response, broadcastResponse, check_tx, deliver_tx, errType, log, errType, log; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.sendRequest(request)]; | ||
case 1: | ||
response = _a.sent(); | ||
broadcastResponse = this.parseResponse(response); | ||
check_tx = broadcastResponse.check_tx, deliver_tx = broadcastResponse.deliver_tx; | ||
// Check if there is an immediate tx-broadcast error (in CheckTx) | ||
if (check_tx.ResponseBase.Error) { | ||
errType = check_tx.ResponseBase.Error.ABCIErrorKey; | ||
log = check_tx.ResponseBase.Log; | ||
throw (0, errors_utility_1.constructRequestError)(errType, log); | ||
} | ||
// Check if there is a parsing error with the transaction (in DeliverTx) | ||
if (deliver_tx.ResponseBase.Error) { | ||
errType = deliver_tx.ResponseBase.Error.ABCIErrorKey; | ||
log = deliver_tx.ResponseBase.Log; | ||
throw (0, errors_utility_1.constructRequestError)(errType, log); | ||
} | ||
return [2 /*return*/, broadcastResponse.hash]; | ||
} | ||
}); | ||
}); | ||
}; | ||
WSProvider.prototype.waitForTransaction = function (hash, fromHeight, timeout) { | ||
@@ -302,0 +355,0 @@ return (0, utility_1.waitForTransaction)(this, hash, fromHeight, timeout); |
@@ -48,2 +48,3 @@ "use strict"; | ||
var endpoints_1 = require("../endpoints"); | ||
var messages_1 = require("../errors/messages"); | ||
describe('WS Provider', function () { | ||
@@ -340,26 +341,50 @@ var wsPort = 8545; | ||
}); }); | ||
test('sendTransaction', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var mockResult, hash; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
mockResult = { | ||
error: null, | ||
data: null, | ||
Log: '', | ||
hash: 'hash123', | ||
}; | ||
// Set the response | ||
return [4 /*yield*/, setHandler(mockResult)]; | ||
case 1: | ||
// Set the response | ||
_a.sent(); | ||
return [4 /*yield*/, wsProvider.sendTransaction('encoded tx')]; | ||
case 2: | ||
hash = _a.sent(); | ||
expect(hash).toBe(mockResult.hash); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
describe('sendTransaction', function () { | ||
var validResult = { | ||
error: null, | ||
data: null, | ||
Log: '', | ||
hash: 'hash123', | ||
}; | ||
var mockError = '/std.UnauthorizedError'; | ||
var mockLog = 'random error message'; | ||
var invalidResult = { | ||
error: { | ||
ABCIErrorKey: mockError, | ||
}, | ||
data: null, | ||
Log: mockLog, | ||
hash: '', | ||
}; | ||
test.each([ | ||
[validResult, validResult.hash, '', ''], | ||
[invalidResult, invalidResult.hash, messages_1.UnauthorizedErrorMessage, mockLog], // error out | ||
])('case %#', function (response, expectedHash, expectedErr, expectedLog) { return __awaiter(void 0, void 0, void 0, function () { | ||
var hash, e_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, setHandler(response)]; | ||
case 1: | ||
_a.sent(); | ||
_a.label = 2; | ||
case 2: | ||
_a.trys.push([2, 4, , 5]); | ||
return [4 /*yield*/, wsProvider.sendTransaction('encoded tx')]; | ||
case 3: | ||
hash = _a.sent(); | ||
expect(hash).toEqual(expectedHash); | ||
if (expectedErr != '') { | ||
fail('expected error'); | ||
} | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
e_2 = _a.sent(); | ||
expect(e_2.message).toBe(expectedErr); | ||
expect(e_2.log).toBe(expectedLog); | ||
return [3 /*break*/, 5]; | ||
case 5: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
}); | ||
var getEmptyBlockInfo = function () { | ||
@@ -366,0 +391,0 @@ var emptyHeader = { |
@@ -63,4 +63,4 @@ "use strict"; | ||
function KeySigner(privateKey, publicKey, addressPrefix) { | ||
var _this = this; | ||
if (addressPrefix === void 0) { addressPrefix = utility_1.defaultAddressPrefix; } | ||
var _this = this; | ||
this.getAddress = function () { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -67,0 +67,0 @@ return __generator(this, function (_a) { |
@@ -54,4 +54,4 @@ "use strict"; | ||
function LedgerSigner(connector, accountIndex, addressPrefix) { | ||
var _this = this; | ||
if (addressPrefix === void 0) { addressPrefix = utility_1.defaultAddressPrefix; } | ||
var _this = this; | ||
this.getAddress = function () { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -58,0 +58,0 @@ var compressedPubKey; |
@@ -73,3 +73,3 @@ "use strict"; | ||
*/ | ||
var Wallet = exports.Wallet = /** @class */ (function () { | ||
var Wallet = /** @class */ (function () { | ||
function Wallet() { | ||
@@ -279,4 +279,3 @@ var _this = this; | ||
Wallet.createRandom = function (options) { return __awaiter(void 0, void 0, void 0, function () { | ||
var publicKey, privateKey, wallet; | ||
var _b; | ||
var _b, publicKey, privateKey, wallet; | ||
return __generator(_a, function (_c) { | ||
@@ -286,4 +285,4 @@ switch (_c.label) { | ||
case 1: | ||
publicKey = (_b = _c.sent(), _b.publicKey), privateKey = _b.privateKey; | ||
wallet = new Wallet(); | ||
_b = _c.sent(), publicKey = _b.publicKey, privateKey = _b.privateKey; | ||
wallet = new _a(); | ||
wallet.signer = new key_1.KeySigner(privateKey, crypto_1.Secp256k1.compressPubkey(publicKey), options === null || options === void 0 ? void 0 : options.addressPrefix); | ||
@@ -300,4 +299,3 @@ return [2 /*return*/, wallet]; | ||
Wallet.fromMnemonic = function (mnemonic, options) { return __awaiter(void 0, void 0, void 0, function () { | ||
var publicKey, privateKey, wallet; | ||
var _b; | ||
var _b, publicKey, privateKey, wallet; | ||
return __generator(_a, function (_c) { | ||
@@ -307,4 +305,4 @@ switch (_c.label) { | ||
case 1: | ||
publicKey = (_b = _c.sent(), _b.publicKey), privateKey = _b.privateKey; | ||
wallet = new Wallet(); | ||
_b = _c.sent(), publicKey = _b.publicKey, privateKey = _b.privateKey; | ||
wallet = new _a(); | ||
wallet.signer = new key_1.KeySigner(privateKey, crypto_1.Secp256k1.compressPubkey(publicKey), options === null || options === void 0 ? void 0 : options.addressPrefix); | ||
@@ -327,3 +325,3 @@ return [2 /*return*/, wallet]; | ||
publicKey = (_b.sent()).pubkey; | ||
wallet = new Wallet(); | ||
wallet = new _a(); | ||
wallet.signer = new key_1.KeySigner(privateKey, crypto_1.Secp256k1.compressPubkey(publicKey), options === null || options === void 0 ? void 0 : options.addressPrefix); | ||
@@ -341,3 +339,3 @@ return [2 /*return*/, wallet]; | ||
var _b; | ||
var wallet = new Wallet(); | ||
var wallet = new _a(); | ||
wallet.signer = new ledger_1.LedgerSigner(connector, (_b = options === null || options === void 0 ? void 0 : options.accountIndex) !== null && _b !== void 0 ? _b : 0, options === null || options === void 0 ? void 0 : options.addressPrefix); | ||
@@ -348,1 +346,2 @@ return wallet; | ||
}()); | ||
exports.Wallet = Wallet; |
{ | ||
"name": "@gnolang/tm2-js-client", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Tendermint2 JS / TS Client", | ||
@@ -36,8 +36,8 @@ "main": "./bin/index.js", | ||
"eslint": "^8.40.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-prettier": "5.0.0", | ||
"jest": "^29.5.0", | ||
"jest-mock-extended": "^3.0.4", | ||
"jest-websocket-mock": "^2.4.0", | ||
"prettier": "^2.8.8", | ||
"prettier": "^3.0.0", | ||
"ts-jest": "^29.1.0", | ||
@@ -44,0 +44,0 @@ "ts-proto": "^1.147.2", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
267569
85
5670