From 9812cf7a3c9ade98763bace5de61e070238311b4 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 19 May 2026 10:23:31 -0500 Subject: [PATCH 1/2] =?UTF-8?q?chore(dapi-client,dapi-grpc):=20cleanup=20?= =?UTF-8?q?=E2=80=94=20drop=20unused=20deps,=20inline=20winston/fetch/prom?= =?UTF-8?q?isify=20shims?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Non-breaking cleanup pass; package stays CJS, no public API changes, no consumer changes required. dapi-client: replace winston with a minimal console-backed logger that preserves the same API (.error/.warn/.info/.verbose/.debug/.silly/.getForId). Drop node-fetch and the lib/test/bootstrap setimmediate shim — Node 18+ has both globally. Drop the https.Agent self-signed-TLS branch from requestJsonRpc (was Node-only; consumers wanting this must configure NODE_TLS_REJECT_UNAUTHORIZED at the app layer). Inline lodash/sample in ListDAPIAddressProvider. Add engines.node >=18.18. Remove dependencies: winston, node-fetch, lodash, bs58 (unused), node-inspect-extracted (unused). Remove devDeps: setimmediate. dapi-grpc: inline the promisify shim in core/v0/web/CorePromiseClient.js and platform/v0/web/PlatformPromiseClient.js so the browser bundle no longer requires Node's util module. Both files document the shim so a future codegen regen does not silently reintroduce require('util'). --- .pnp.cjs | 15 +-- .../clients/core/v0/web/CorePromiseClient.js | 9 +- .../platform/v0/web/PlatformPromiseClient.js | 10 +- .../ListDAPIAddressProvider.js | 2 +- packages/js-dapi-client/lib/index.js | 2 - packages/js-dapi-client/lib/logger/index.js | 105 +++++------------- packages/js-dapi-client/lib/test/bootstrap.js | 3 - .../JsonRpcTransport/requestJsonRpc.js | 12 -- packages/js-dapi-client/package.json | 11 +- .../polyfills/fetch-polyfill.js | 10 -- yarn.lock | 5 - 11 files changed, 56 insertions(+), 128 deletions(-) delete mode 100644 packages/js-dapi-client/polyfills/fetch-polyfill.js diff --git a/.pnp.cjs b/.pnp.cjs index b2a860bb236..4da943bc044 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -2576,7 +2576,6 @@ const RAW_RUNTIME_STATE = ["assert-browserify", "npm:2.0.0"],\ ["babel-loader", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:9.1.3"],\ ["browserify-zlib", "npm:0.2.0"],\ - ["bs58", "npm:4.0.1"],\ ["buffer", "npm:6.0.3"],\ ["cbor", "npm:8.1.0"],\ ["chai", "npm:4.3.10"],\ @@ -2595,10 +2594,7 @@ const RAW_RUNTIME_STATE = ["karma-mocha", "npm:2.0.1"],\ ["karma-mocha-reporter", "virtual:e2d057e7cc143d3cb9bec864f4a2d862441b5a09f81f8e6c46e7a098cbc89e4d07017cc6e2e2142d5704bb55da853cbec2d025ebc0b30e8696c31380c00f2c7d#npm:2.2.5"],\ ["karma-webpack", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:5.0.0"],\ - ["lodash", "npm:4.17.23"],\ ["mocha", "npm:11.1.0"],\ - ["node-fetch", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:2.6.7"],\ - ["node-inspect-extracted", "npm:1.0.8"],\ ["nyc", "npm:15.1.0"],\ ["os-browserify", "npm:0.3.0"],\ ["path-browserify", "npm:1.0.1"],\ @@ -2612,8 +2608,7 @@ const RAW_RUNTIME_STATE = ["util", "npm:0.12.4"],\ ["wasm-x11-hash", "npm:0.0.2"],\ ["webpack", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:5.105.0"],\ - ["webpack-cli", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:4.9.1"],\ - ["winston", "npm:3.3.3"]\ + ["webpack-cli", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:4.9.1"]\ ],\ "linkType": "SOFT"\ }]\ @@ -4422,7 +4417,7 @@ const RAW_RUNTIME_STATE = ["@octokit/request-error", "npm:2.1.0"],\ ["@octokit/types", "npm:6.34.0"],\ ["is-plain-object", "npm:5.0.0"],\ - ["node-fetch", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:2.6.7"],\ + ["node-fetch", "virtual:25a5f5382d53dbf298bf7a1191760bc2e0a523a619eeb0e667b99a8649e8ad183f9e2e0b45f6fb831b92f4078b61622aa567cf79565f6aa5af9597e3c84864f6#npm:2.6.7"],\ ["universal-user-agent", "npm:6.0.0"]\ ],\ "linkType": "HARD"\ @@ -16640,12 +16635,12 @@ const RAW_RUNTIME_STATE = ],\ "linkType": "SOFT"\ }],\ - ["virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:2.6.7", {\ - "packageLocation": "./.yarn/__virtual__/node-fetch-virtual-a3f0ba2944/0/cache/node-fetch-npm-2.6.7-777aa2a6df-4bc9245383.zip/node_modules/node-fetch/",\ + ["virtual:25a5f5382d53dbf298bf7a1191760bc2e0a523a619eeb0e667b99a8649e8ad183f9e2e0b45f6fb831b92f4078b61622aa567cf79565f6aa5af9597e3c84864f6#npm:2.6.7", {\ + "packageLocation": "./.yarn/__virtual__/node-fetch-virtual-d3846f8e12/0/cache/node-fetch-npm-2.6.7-777aa2a6df-4bc9245383.zip/node_modules/node-fetch/",\ "packageDependencies": [\ ["@types/encoding", null],\ ["encoding", null],\ - ["node-fetch", "virtual:8f25fc90e0fb5fd89843707863857591fa8c52f9f33eadced4bf404b1871d91959f7bb86948ae0e1b53ee94d491ef8fde9c0b58b39c9490c0d0fa6c931945f97#npm:2.6.7"],\ + ["node-fetch", "virtual:25a5f5382d53dbf298bf7a1191760bc2e0a523a619eeb0e667b99a8649e8ad183f9e2e0b45f6fb831b92f4078b61622aa567cf79565f6aa5af9597e3c84864f6#npm:2.6.7"],\ ["whatwg-url", "npm:5.0.0"]\ ],\ "packagePeers": [\ diff --git a/packages/dapi-grpc/clients/core/v0/web/CorePromiseClient.js b/packages/dapi-grpc/clients/core/v0/web/CorePromiseClient.js index 3f1549d8058..818ef24e84a 100644 --- a/packages/dapi-grpc/clients/core/v0/web/CorePromiseClient.js +++ b/packages/dapi-grpc/clients/core/v0/web/CorePromiseClient.js @@ -1,4 +1,11 @@ -const { promisify } = require('util'); +// Inline promisify shim — avoids requiring Node's `util` module so this file +// can be bundled for browsers without a polyfill. If the codegen template +// is regenerated, restore this shim. +function promisify(fn) { + return (...args) => new Promise((resolve, reject) => { + fn(...args, (err, result) => (err ? reject(err) : resolve(result))); + }); +} const GrpcError = require('@dashevo/grpc-common/lib/server/error/GrpcError'); const { CoreClient } = require('./core_pb_service'); diff --git a/packages/dapi-grpc/clients/platform/v0/web/PlatformPromiseClient.js b/packages/dapi-grpc/clients/platform/v0/web/PlatformPromiseClient.js index 13be28a2d50..fbf39888b2e 100644 --- a/packages/dapi-grpc/clients/platform/v0/web/PlatformPromiseClient.js +++ b/packages/dapi-grpc/clients/platform/v0/web/PlatformPromiseClient.js @@ -1,5 +1,13 @@ const { PlatformClient } = require('./platform_pb_service'); -const { promisify } = require('util'); + +// Inline promisify shim — avoids requiring Node's `util` module so this file +// can be bundled for browsers without a polyfill. If the codegen template +// is regenerated, restore this shim. +function promisify(fn) { + return (...args) => new Promise((resolve, reject) => { + fn(...args, (err, result) => (err ? reject(err) : resolve(result))); + }); +} class PlatformPromiseClient { /** diff --git a/packages/js-dapi-client/lib/dapiAddressProvider/ListDAPIAddressProvider.js b/packages/js-dapi-client/lib/dapiAddressProvider/ListDAPIAddressProvider.js index d9133a1ad4c..c13217e98d4 100644 --- a/packages/js-dapi-client/lib/dapiAddressProvider/ListDAPIAddressProvider.js +++ b/packages/js-dapi-client/lib/dapiAddressProvider/ListDAPIAddressProvider.js @@ -1,4 +1,4 @@ -const sample = require('lodash/sample'); +const sample = (arr) => arr[Math.floor(Math.random() * arr.length)]; const networks = require('@dashevo/dashcore-lib/lib/networks'); class ListDAPIAddressProvider { diff --git a/packages/js-dapi-client/lib/index.js b/packages/js-dapi-client/lib/index.js index f10beb47695..65ad0f4deda 100644 --- a/packages/js-dapi-client/lib/index.js +++ b/packages/js-dapi-client/lib/index.js @@ -1,5 +1,3 @@ -require('../polyfills/fetch-polyfill'); - const DAPIClient = require('./DAPIClient'); const NotFoundError = require('./transport/GrpcTransport/errors/NotFoundError'); diff --git a/packages/js-dapi-client/lib/logger/index.js b/packages/js-dapi-client/lib/logger/index.js index a06b34c145d..ffca12f534b 100644 --- a/packages/js-dapi-client/lib/logger/index.js +++ b/packages/js-dapi-client/lib/logger/index.js @@ -1,80 +1,33 @@ -const util = require('util'); -const winston = require('winston'); +const LOG_LEVEL = (typeof process !== 'undefined' && process.env && process.env.LOG_LEVEL) || 'silent'; -// TODO: Refactor to use params instead on envs - -const LOG_LEVEL = process.env.LOG_LEVEL || 'silent'; -const LOG_TO_FILE = process.env.LOG_WALLET_TO_FILE || 'false'; - -// Log levels: -// error 0 -// warn 1 -// info 2 (default) -// verbose 3 -// debug 4 -// silly 5 - -const loggers = {}; - -const createLogger = (formats = [], id = '') => { - const format = winston.format.combine( - { - transform: (info) => { - const args = info[Symbol.for('splat')]; - const result = { ...info }; - if (args) { - result.message = util.format(info.message, ...args); - } - return result; - }, - }, - ...formats, - winston.format.colorize(), - winston.format.printf(({ - level, message, - }) => `${level}: ${message}`), - ); - - const transports = [ - new winston.transports.Console({ - format, - silent: LOG_LEVEL === 'silent', - }), - ]; - - if (LOG_TO_FILE === 'true' && typeof window === 'undefined') { - transports.push( - new winston.transports.File({ - filename: `wallet${id !== '' ? `_${id}` : ''}`, - format, - silent: LOG_LEVEL === 'silent', - }), - ); - } - - return winston.createLogger({ - level: LOG_LEVEL, - transports, - }); +const LEVELS = { + silent: -1, error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5, }; -const logger = createLogger(); - -logger.getForId = (id) => { - if (!loggers[id]) { - const format = { - transform: (info) => { - const message = `[DAPIClient: ${id}] ${info.message}`; - return { ...info, message }; - }, - }; - - loggers[id] = createLogger([format], id); - } - - return loggers[id]; -}; - -logger.verbose(`Logger uses "${LOG_LEVEL}" level`, { level: LOG_LEVEL }); +const cache = {}; + +function build(level = LOG_LEVEL, prefix = '') { + const threshold = LEVELS[level] != null ? LEVELS[level] : LEVELS.silent; + const noop = () => {}; + const fmt = prefix ? (...a) => [prefix, ...a] : (...a) => a; + + const logger = { + error: threshold >= 0 ? (...a) => console.error(...fmt(...a)) : noop, + warn: threshold >= 1 ? (...a) => console.warn(...fmt(...a)) : noop, + info: threshold >= 2 ? (...a) => console.info(...fmt(...a)) : noop, + verbose: threshold >= 3 ? (...a) => console.debug(...fmt(...a)) : noop, + debug: threshold >= 4 ? (...a) => console.debug(...fmt(...a)) : noop, + silly: threshold >= 5 ? (...a) => console.debug(...fmt(...a)) : noop, + getForId(id, overrideLevel) { + const effective = overrideLevel || level; + const key = `${id}\0${effective}`; + if (!cache[key]) { + cache[key] = build(effective, `[DAPIClient: ${id}]`); + } + return cache[key]; + }, + }; + return logger; +} -module.exports = logger; +module.exports = build(); diff --git a/packages/js-dapi-client/lib/test/bootstrap.js b/packages/js-dapi-client/lib/test/bootstrap.js index 473d16fb1d1..85d7947d666 100644 --- a/packages/js-dapi-client/lib/test/bootstrap.js +++ b/packages/js-dapi-client/lib/test/bootstrap.js @@ -1,6 +1,3 @@ -require('../../polyfills/fetch-polyfill'); -require('setimmediate'); - const { expect, use } = require('chai'); const sinon = require('sinon'); const sinonChai = require('sinon-chai'); diff --git a/packages/js-dapi-client/lib/transport/JsonRpcTransport/requestJsonRpc.js b/packages/js-dapi-client/lib/transport/JsonRpcTransport/requestJsonRpc.js index ddbdf797088..8ccfabe29fe 100644 --- a/packages/js-dapi-client/lib/transport/JsonRpcTransport/requestJsonRpc.js +++ b/packages/js-dapi-client/lib/transport/JsonRpcTransport/requestJsonRpc.js @@ -1,4 +1,3 @@ -const https = require('https'); const JsonRpcError = require('./errors/JsonRpcError'); const WrongHttpCodeError = require('./errors/WrongHttpCodeError'); /** @@ -47,17 +46,6 @@ async function requestJsonRpc(protocol, host, port, selfSigned, method, params, Object.assign(requestOptions, { signal: controller.signal }); } - // For NodeJS Client - if (typeof process !== 'undefined' - && process.versions != null - && process.versions.node != null - && protocol === 'https' - && selfSigned) { - requestOptions.agent = new https.Agent({ - rejectUnauthorized: false, - }); - } - // eslint-disable-next-line const response = await fetch(url, requestOptions); if (typeof requestTimeoutId !== 'undefined') { diff --git a/packages/js-dapi-client/package.json b/packages/js-dapi-client/package.json index 2bccc23c235..ede6b739dc8 100644 --- a/packages/js-dapi-client/package.json +++ b/packages/js-dapi-client/package.json @@ -31,14 +31,9 @@ "@dashevo/dashcore-lib": "~0.22.0", "@dashevo/grpc-common": "workspace:*", "@dashevo/wasm-dpp": "workspace:*", - "bs58": "^4.0.1", "cbor": "^8.0.0", "google-protobuf": "^3.12.2", - "lodash": "^4.17.23", - "node-fetch": "^2.6.7", - "node-inspect-extracted": "^1.0.8", - "wasm-x11-hash": "~0.0.2", - "winston": "^3.2.1" + "wasm-x11-hash": "~0.0.2" }, "devDependencies": { "@babel/core": "^7.26.10", @@ -76,10 +71,12 @@ "webpack": "^5.104.0", "webpack-cli": "^4.9.1" }, + "engines": { + "node": ">=18.18" + }, "files": [ "docs", "lib", - "polyfills", "dist" ], "scripts": { diff --git a/packages/js-dapi-client/polyfills/fetch-polyfill.js b/packages/js-dapi-client/polyfills/fetch-polyfill.js deleted file mode 100644 index 8fa8ac2ffce..00000000000 --- a/packages/js-dapi-client/polyfills/fetch-polyfill.js +++ /dev/null @@ -1,10 +0,0 @@ -/* eslint-disable */ - -const { default: fetch, Headers, Request, Response } = require('node-fetch'); - -if (typeof window === 'undefined') { - globalThis.fetch = fetch; - globalThis.Headers = Headers; - globalThis.Request = Request; - globalThis.Response = Response; -} diff --git a/yarn.lock b/yarn.lock index 57d28cca8c9..f6864af8205 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1523,7 +1523,6 @@ __metadata: assert-browserify: "npm:^2.0.0" babel-loader: "npm:^9.1.3" browserify-zlib: "npm:^0.2.0" - bs58: "npm:^4.0.1" buffer: "npm:^6.0.3" cbor: "npm:^8.0.0" chai: "npm:^4.3.10" @@ -1542,10 +1541,7 @@ __metadata: karma-mocha: "npm:^2.0.1" karma-mocha-reporter: "npm:^2.2.5" karma-webpack: "npm:^5.0.0" - lodash: "npm:^4.17.23" mocha: "npm:^11.1.0" - node-fetch: "npm:^2.6.7" - node-inspect-extracted: "npm:^1.0.8" nyc: "npm:^15.1.0" os-browserify: "npm:^0.3.0" path-browserify: "npm:^1.0.1" @@ -1560,7 +1556,6 @@ __metadata: wasm-x11-hash: "npm:~0.0.2" webpack: "npm:^5.104.0" webpack-cli: "npm:^4.9.1" - winston: "npm:^3.2.1" languageName: unknown linkType: soft From 0b8c68ebbab18c3484ce22b65575736b40595b76 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 19 May 2026 11:02:12 -0500 Subject: [PATCH 2/2] refactor(dapi-client)!: expose Uint8Array instead of Buffer in public API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds lib/utils/bytes.js helper (hexToBytes/bytesToHex/base64ToBytes/bytesToBase64/concatBytes/bytesEqual) and converts all Buffer.* call sites in dapi-client lib/ to Uint8Array, with corresponding test updates. Package stays CJS. Production exceptions where Buffer is retained: BlockHeadersReader passes Buffer to dashcore-lib's BlockHeader (its BufferReader needs .readInt32LE), and GetIdentitiesContractKeysResponse passes Buffer to wasm-dpp's Identifier.from (it explicitly requires Node Buffer). createGrpcTransportError now handles both raw bytes (grpc-js path) and base64 strings (grpc-web path) for drive-error-data-bin, stack-bin, and dash-serialized-consensus-error-bin metadata fields, restoring the dual-format behavior that Buffer.from(x, 'base64') used to provide implicitly. Test updates: spec files that construct expected protobuf requests now wrap .toBuffer() with new Uint8Array(...) to match production's normalization (sinon calledOnceWithExactly distinguishes Buffer from plain Uint8Array). Breaking change for direct consumers: response object byte fields are now Uint8Array. Callers that do response.field.toString('hex') will fail — use bytesToHex(response.field) from lib/utils/bytes instead. Buffer.isBuffer(response.field) now returns false; use response.field instanceof Uint8Array. Test results: dapi-client 315/315, wallet-lib 377/377, js-dash-sdk 60/60 — downstream consumers continue passing without modification (they exercise dapi-client mostly via mocks). --- .../SimplifiedMasternodeListProvider.js | 9 +-- .../lib/methods/core/getBlockByHashFactory.js | 4 +- .../methods/core/getBlockByHeightFactory.js | 4 +- .../core/getBlockchainStatusFactory.js | 6 +- .../core/getMasternodeStatusFactory.js | 3 +- .../getTransaction/GetTransactionResponse.js | 12 ++-- ...ribeToBlockHeadersWithChainLocksFactory.js | 3 +- ...ubscribeToTransactionsWithProofsFactory.js | 3 +- .../GetDataContractResponse.js | 6 +- .../getDataContract/getDataContractFactory.js | 8 +-- .../getDataContractHistoryFactory.js | 8 +-- .../getDocuments/GetDocumentsResponse.js | 6 +- .../getDocuments/getDocumentsFactory.js | 11 ++-- .../GetIdentitiesContractKeysResponse.js | 1 + .../getIdentitiesContractKeysFactory.js | 13 ++-- .../getIdentity/GetIdentityResponse.js | 6 +- .../getIdentity/getIdentityFactory.js | 8 +-- .../getIdentityBalanceFactory.js | 8 +-- .../GetIdentityByPublicKeyHashResponse.js | 6 +- .../getIdentityContractNonceFactory.js | 12 ++-- .../getIdentityKeys/getIdentityKeysFactory.js | 6 +- .../getIdentityNonceFactory.js | 6 +- ...rotocolVersionUpgradeVoteStatusResponse.js | 3 +- ...ProtocolVersionUpgradeVoteStatusFactory.js | 3 +- .../platform/getStatus/GetStatusResponse.js | 13 ++-- .../lib/methods/platform/response/Proof.js | 18 +++--- .../WaitForStateTransitionResultResponse.js | 2 +- .../lib/test/fixtures/getProofFixture.js | 14 +++-- .../lib/test/fixtures/getStatusFixture.js | 14 +++-- .../lib/test/mocks/mockHeadersChain.js | 5 +- .../GrpcTransport/createGrpcTransportError.js | 19 ++++-- packages/js-dapi-client/lib/utils/bytes.js | 63 +++++++++++++++++++ .../methods/core/CoreMethodsFacade.spec.js | 10 +-- .../platform/PlatformMethodsFacade.spec.js | 19 +++--- .../core/broadcastTransactionFactory.spec.js | 2 +- .../core/getBlockByHashFactory.spec.js | 4 +- .../core/getBlockByHeightFactory.spec.js | 4 +- .../core/getBlockchainStatusFactory.spec.js | 6 +- .../core/getMasternodeStatusFactory.spec.js | 2 +- .../GetTransactionResponse.spec.js | 8 +-- .../getTransactionFactory.spec.js | 4 +- ...oBlockHeadersWithChainLocksFactory.spec.js | 3 +- ...ibeToTransactionsWithProofsFactory.spec.js | 3 +- .../GetDataContractResponse.spec.js | 6 +- .../getDataContractFactory.spec.js | 4 +- .../getDataContractHistoryFactory.spec.js | 2 +- .../getDocuments/GetDocumentsResponse.spec.js | 2 +- .../getDocuments/getDocumentsFactory.spec.js | 8 +-- .../getIdentitiesContractKeysFactory.spec.js | 4 +- .../getIdentity/GetIdentityResponse.spec.js | 6 +- .../getIdentity/getIdentityFactory.spec.js | 8 +-- .../getIdentityBalanceFactory.spec.js | 2 +- ...GetIdentityByPublicKeyHashResponse.spec.js | 2 +- .../getIdentityByPublicKeyHashFactory.spec.js | 2 +- .../GetIdentityContractNonce.spec.js | 4 +- .../getIdentityContractNonceFactory.spec.js | 4 +- .../getIdentityKeys/GetIdentityKeys.spec.js | 6 +- .../getIdentityKeysFactory.spec.js | 4 +- .../getIdentityNonce/GetIdentityNonce.spec.js | 4 +- .../getIdentityNonceFactory.spec.js | 2 +- ...olVersionUpgradeVoteStatusResponse.spec.js | 11 ++-- ...colVersionUpgradeVoteStatusFactory.spec.js | 19 +++--- .../getStatus/GetStatusResponse.spec.js | 37 +++++------ .../getStatus/getStatusFactory.spec.js | 23 +++---- ...aitForStateTransitionResultFactory.spec.js | 24 +++---- 65 files changed, 324 insertions(+), 228 deletions(-) create mode 100644 packages/js-dapi-client/lib/utils/bytes.js diff --git a/packages/js-dapi-client/lib/SimplifiedMasternodeListProvider/SimplifiedMasternodeListProvider.js b/packages/js-dapi-client/lib/SimplifiedMasternodeListProvider/SimplifiedMasternodeListProvider.js index 3c1c6e20c6b..1916b48cc1a 100644 --- a/packages/js-dapi-client/lib/SimplifiedMasternodeListProvider/SimplifiedMasternodeListProvider.js +++ b/packages/js-dapi-client/lib/SimplifiedMasternodeListProvider/SimplifiedMasternodeListProvider.js @@ -3,6 +3,7 @@ const SimplifiedMNListDiff = require('@dashevo/dashcore-lib/lib/deterministicmnl const cbor = require('cbor'); const logger = require('../logger'); +const { bytesToHex } = require('../utils/bytes'); class SimplifiedMasternodeListProvider { /** @@ -100,11 +101,11 @@ class SimplifiedMasternodeListProvider { let simplifiedMNListDiff; let simplifiedMNListDiffObject; - let simplifiedMNListDiffBuffer; + let simplifiedMNListDiffBytes; try { - simplifiedMNListDiffBuffer = Buffer.from(response.getMasternodeListDiff_asU8()); + simplifiedMNListDiffBytes = new Uint8Array(response.getMasternodeListDiff_asU8()); - simplifiedMNListDiffObject = cbor.decodeFirstSync(simplifiedMNListDiffBuffer); + simplifiedMNListDiffObject = cbor.decodeFirstSync(simplifiedMNListDiffBytes); simplifiedMNListDiff = new SimplifiedMNListDiff( simplifiedMNListDiffObject, @@ -118,7 +119,7 @@ class SimplifiedMasternodeListProvider { network: this.options.network, error: e, simplifiedMNListDiffObject, - simplifiedMNListDiffBytes: simplifiedMNListDiffBuffer.toString('hex'), + simplifiedMNListDiffBytes: bytesToHex(simplifiedMNListDiffBytes), }, ); diff --git a/packages/js-dapi-client/lib/methods/core/getBlockByHashFactory.js b/packages/js-dapi-client/lib/methods/core/getBlockByHashFactory.js index 72de31354be..d007018bd2b 100644 --- a/packages/js-dapi-client/lib/methods/core/getBlockByHashFactory.js +++ b/packages/js-dapi-client/lib/methods/core/getBlockByHashFactory.js @@ -15,7 +15,7 @@ function getBlockByHashFactory(grpcTransport) { * @typedef {getBlockByHash} * @param {string} hash * @param {DAPIClientOptions} [options] - * @returns {Promise} + * @returns {Promise} */ async function getBlockByHash(hash, options = {}) { const getBlockRequest = new GetBlockRequest(); @@ -29,7 +29,7 @@ function getBlockByHashFactory(grpcTransport) { ); const blockBinaryArray = response.getBlock(); - return Buffer.from(blockBinaryArray); + return new Uint8Array(blockBinaryArray); } return getBlockByHash; diff --git a/packages/js-dapi-client/lib/methods/core/getBlockByHeightFactory.js b/packages/js-dapi-client/lib/methods/core/getBlockByHeightFactory.js index e95a58aeccc..88fdf0d357a 100644 --- a/packages/js-dapi-client/lib/methods/core/getBlockByHeightFactory.js +++ b/packages/js-dapi-client/lib/methods/core/getBlockByHeightFactory.js @@ -15,7 +15,7 @@ function getBlockByHeightFactory(grpcTransport) { * @typedef {getBlockByHeight} * @param {number} height * @param {DAPIClientOptions} [options] - * @returns {Promise} + * @returns {Promise} */ async function getBlockByHeight(height, options = {}) { const getBlockRequest = new GetBlockRequest(); @@ -30,7 +30,7 @@ function getBlockByHeightFactory(grpcTransport) { const blockBinaryArray = response.getBlock(); - return Buffer.from(blockBinaryArray); + return new Uint8Array(blockBinaryArray); } return getBlockByHeight; diff --git a/packages/js-dapi-client/lib/methods/core/getBlockchainStatusFactory.js b/packages/js-dapi-client/lib/methods/core/getBlockchainStatusFactory.js index 22b1f88e573..486637c4530 100644 --- a/packages/js-dapi-client/lib/methods/core/getBlockchainStatusFactory.js +++ b/packages/js-dapi-client/lib/methods/core/getBlockchainStatusFactory.js @@ -29,18 +29,18 @@ function getBlockchainStatusFactory(grpcTransport) { const responseObject = response.toObject(); - // Respond with Buffers instead of base64 for binary fields + // Respond with Uint8Arrays instead of base64 for binary fields if (response.getChain()) { if (response.getChain() .getBestBlockHash()) { - responseObject.chain.bestBlockHash = Buffer.from(response.getChain() + responseObject.chain.bestBlockHash = new Uint8Array(response.getChain() .getBestBlockHash()); } if (response.getChain() .getChainWork()) { - responseObject.chain.chainWork = Buffer.from(response.getChain() + responseObject.chain.chainWork = new Uint8Array(response.getChain() .getChainWork()); } } diff --git a/packages/js-dapi-client/lib/methods/core/getMasternodeStatusFactory.js b/packages/js-dapi-client/lib/methods/core/getMasternodeStatusFactory.js index c0da5feef98..9f63a28ffb1 100644 --- a/packages/js-dapi-client/lib/methods/core/getMasternodeStatusFactory.js +++ b/packages/js-dapi-client/lib/methods/core/getMasternodeStatusFactory.js @@ -5,6 +5,7 @@ const { CorePromiseClient, }, } = require('@dashevo/dapi-grpc'); +const { base64ToBytes } = require('../../utils/bytes'); /** * @param {GrpcTransport} grpcTransport @@ -34,7 +35,7 @@ function getMasternodeStatusFactory(grpcTransport) { responseObject.status = Object.keys(GetMasternodeStatusResponse.Status) .find((key) => GetMasternodeStatusResponse.Status[key] === responseObject.status); - responseObject.proTxHash = Buffer.from(responseObject.proTxHash, 'base64'); + responseObject.proTxHash = base64ToBytes(responseObject.proTxHash); return responseObject; } diff --git a/packages/js-dapi-client/lib/methods/core/getTransaction/GetTransactionResponse.js b/packages/js-dapi-client/lib/methods/core/getTransaction/GetTransactionResponse.js index 9f65b503c39..97aeeae2c99 100644 --- a/packages/js-dapi-client/lib/methods/core/getTransaction/GetTransactionResponse.js +++ b/packages/js-dapi-client/lib/methods/core/getTransaction/GetTransactionResponse.js @@ -4,8 +4,8 @@ class GetTransactionResponse { /** * * @param {object} properties - * @param {Buffer} properties.transaction - * @param {Buffer} properties.blockHash + * @param {Uint8Array} properties.transaction + * @param {Uint8Array} properties.blockHash * @param {number} properties.height * @param {number} properties.confirmations * @param {boolean} properties.isInstantLocked @@ -22,7 +22,7 @@ class GetTransactionResponse { /** * Get transaction - * @returns {Buffer} + * @returns {Uint8Array} */ getTransaction() { return this.transaction; @@ -30,7 +30,7 @@ class GetTransactionResponse { /** * Get block hash - * @returns {Buffer} + * @returns {Uint8Array} */ getBlockHash() { return this.blockHash; @@ -75,8 +75,8 @@ class GetTransactionResponse { } return new GetTransactionResponse({ - transaction: Buffer.from(transactionBinaryArray), - blockHash: Buffer.from(proto.getBlockHash()), + transaction: new Uint8Array(transactionBinaryArray), + blockHash: new Uint8Array(proto.getBlockHash()), height: proto.getHeight(), confirmations: proto.getConfirmations(), isInstantLocked: proto.getIsInstantLocked(), diff --git a/packages/js-dapi-client/lib/methods/core/subscribeToBlockHeadersWithChainLocksFactory.js b/packages/js-dapi-client/lib/methods/core/subscribeToBlockHeadersWithChainLocksFactory.js index 397b3ffd939..8fe67b0c05b 100644 --- a/packages/js-dapi-client/lib/methods/core/subscribeToBlockHeadersWithChainLocksFactory.js +++ b/packages/js-dapi-client/lib/methods/core/subscribeToBlockHeadersWithChainLocksFactory.js @@ -6,6 +6,7 @@ const { } = require('@dashevo/dapi-grpc'); const DAPIClientError = require('../../errors/DAPIClientError'); +const { hexToBytes } = require('../../utils/bytes'); /** * @param {GrpcTransport} grpcTransport @@ -41,7 +42,7 @@ function subscribeToBlockHeadersWithChainLocksFactory(grpcTransport) { if (options.fromBlockHash) { request.setFromBlockHash( - Buffer.from(options.fromBlockHash, 'hex'), + hexToBytes(options.fromBlockHash), ); } diff --git a/packages/js-dapi-client/lib/methods/core/subscribeToTransactionsWithProofsFactory.js b/packages/js-dapi-client/lib/methods/core/subscribeToTransactionsWithProofsFactory.js index bf1784291eb..45d54ca3776 100644 --- a/packages/js-dapi-client/lib/methods/core/subscribeToTransactionsWithProofsFactory.js +++ b/packages/js-dapi-client/lib/methods/core/subscribeToTransactionsWithProofsFactory.js @@ -7,6 +7,7 @@ const { } = require('@dashevo/dapi-grpc'); const DAPIClientError = require('../../errors/DAPIClientError'); +const { hexToBytes } = require('../../utils/bytes'); /** * @param {GrpcTransport} grpcTransport @@ -65,7 +66,7 @@ function subscribeToTransactionsWithProofsFactory(grpcTransport) { if (options.fromBlockHash) { request.setFromBlockHash( - Buffer.from(options.fromBlockHash, 'hex'), + hexToBytes(options.fromBlockHash), ); } diff --git a/packages/js-dapi-client/lib/methods/platform/getDataContract/GetDataContractResponse.js b/packages/js-dapi-client/lib/methods/platform/getDataContract/GetDataContractResponse.js index e1be4b3506e..1ff6735b3dd 100644 --- a/packages/js-dapi-client/lib/methods/platform/getDataContract/GetDataContractResponse.js +++ b/packages/js-dapi-client/lib/methods/platform/getDataContract/GetDataContractResponse.js @@ -3,7 +3,7 @@ const InvalidResponseError = require('../response/errors/InvalidResponseError'); class GetDataContractResponse extends AbstractResponse { /** - * @param {Buffer} dataContract + * @param {Uint8Array} dataContract * @param {Metadata} metadata * @param {Proof} [proof] */ @@ -14,7 +14,7 @@ class GetDataContractResponse extends AbstractResponse { } /** - * @returns {Buffer} + * @returns {Uint8Array} */ getDataContract() { return this.dataContract; @@ -33,7 +33,7 @@ class GetDataContractResponse extends AbstractResponse { } return new GetDataContractResponse( - Buffer.from(dataContract), + new Uint8Array(dataContract), metadata, proof, ); diff --git a/packages/js-dapi-client/lib/methods/platform/getDataContract/getDataContractFactory.js b/packages/js-dapi-client/lib/methods/platform/getDataContract/getDataContractFactory.js index 0966fd05923..ed1a5548445 100644 --- a/packages/js-dapi-client/lib/methods/platform/getDataContract/getDataContractFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getDataContract/getDataContractFactory.js @@ -16,7 +16,7 @@ function getDataContractFactory(grpcTransport) { /** * Fetch Data Contract by id * @typedef {getDataContract} - * @param {Buffer} contractId + * @param {Uint8Array} contractId * @param {DAPIClientOptions & {prove: boolean}} [options] * @returns {Promise} */ @@ -24,12 +24,12 @@ function getDataContractFactory(grpcTransport) { const { GetDataContractRequestV0 } = GetDataContractRequest; const getDataContractRequest = new GetDataContractRequest(); - // need to convert objects inherited from Buffer to pure buffer as google protobuf + // need to convert objects inherited from Uint8Array to pure Uint8Array as google protobuf // doesn't support extended buffers // https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049 - if (Buffer.isBuffer(contractId)) { + if (contractId instanceof Uint8Array) { // eslint-disable-next-line no-param-reassign - contractId = Buffer.from(contractId); + contractId = new Uint8Array(contractId); } getDataContractRequest.setV0( diff --git a/packages/js-dapi-client/lib/methods/platform/getDataContractHistory/getDataContractHistoryFactory.js b/packages/js-dapi-client/lib/methods/platform/getDataContractHistory/getDataContractHistoryFactory.js index 1059c74bd58..183bcd3e57e 100644 --- a/packages/js-dapi-client/lib/methods/platform/getDataContractHistory/getDataContractHistoryFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getDataContractHistory/getDataContractHistoryFactory.js @@ -17,7 +17,7 @@ function getDataContractHistoryFactory(grpcTransport) { /** * Fetch Data Contract by id * @typedef {getDataContractHistory} - * @param {Buffer} contractId + * @param {Uint8Array} contractId * @param {bigint} [startAtMs] * @param {number} [limit] * @param {number} [offset] @@ -34,12 +34,12 @@ function getDataContractHistoryFactory(grpcTransport) { const { GetDataContractHistoryRequestV0 } = GetDataContractHistoryRequest; const getDataContractHistoryRequest = new GetDataContractHistoryRequest(); - // need to convert objects inherited from Buffer to pure buffer as google protobuf + // need to convert objects inherited from Uint8Array to pure Uint8Array as google protobuf // doesn't support extended buffers // https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049 - if (Buffer.isBuffer(contractId)) { + if (contractId instanceof Uint8Array) { // eslint-disable-next-line no-param-reassign - contractId = Buffer.from(contractId); + contractId = new Uint8Array(contractId); } getDataContractHistoryRequest.setV0( diff --git a/packages/js-dapi-client/lib/methods/platform/getDocuments/GetDocumentsResponse.js b/packages/js-dapi-client/lib/methods/platform/getDocuments/GetDocumentsResponse.js index 5340a5d3b0d..0f65018a1d3 100644 --- a/packages/js-dapi-client/lib/methods/platform/getDocuments/GetDocumentsResponse.js +++ b/packages/js-dapi-client/lib/methods/platform/getDocuments/GetDocumentsResponse.js @@ -2,7 +2,7 @@ const AbstractResponse = require('../response/AbstractResponse'); class GetDocumentsResponse extends AbstractResponse { /** - * @param {Buffer[]} documents + * @param {Uint8Array[]} documents * @param {Metadata} metadata * @param {Proof} [proof] */ @@ -13,7 +13,7 @@ class GetDocumentsResponse extends AbstractResponse { } /** - * @returns {Buffer[]} + * @returns {Uint8Array[]} */ getDocuments() { return this.documents; @@ -30,7 +30,7 @@ class GetDocumentsResponse extends AbstractResponse { return new GetDocumentsResponse( documents !== undefined - ? documents.getDocumentsList().map((document) => Buffer.from(document)) : [], + ? documents.getDocumentsList().map((document) => new Uint8Array(document)) : [], metadata, proof, ); diff --git a/packages/js-dapi-client/lib/methods/platform/getDocuments/getDocumentsFactory.js b/packages/js-dapi-client/lib/methods/platform/getDocuments/getDocumentsFactory.js index 4c086b21f78..7af4c1b3d20 100644 --- a/packages/js-dapi-client/lib/methods/platform/getDocuments/getDocumentsFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getDocuments/getDocumentsFactory.js @@ -18,7 +18,7 @@ function getDocumentsFactory(grpcTransport) { /** * Fetch Documents from Drive * @typedef {getDocuments} - * @param {Buffer} contractId - Data Contract ID + * @param {Uint8Array} contractId - Data Contract ID * @param {string} type - Document type * @param {DAPIClientOptions & getDocumentsOptions & {prove: boolean}} [options] * @returns {Promise} @@ -44,15 +44,16 @@ function getDocumentsFactory(grpcTransport) { const { GetDocumentsRequestV0 } = GetDocumentsRequest; const getDocumentsRequest = new GetDocumentsRequest(); - // need to convert Identifier to pure buffer as google protobuf doesn't support extended buffers + // need to convert Identifier to pure Uint8Array as google protobuf doesn't support + // extended buffers // https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049 - // need to convert objects inherited from Buffer to pure buffer as google protobuf + // need to convert objects inherited from Uint8Array to pure Uint8Array as google protobuf // doesn't support extended buffers // https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049 - if (Buffer.isBuffer(contractId)) { + if (contractId instanceof Uint8Array) { // eslint-disable-next-line no-param-reassign - contractId = Buffer.from(contractId); + contractId = new Uint8Array(contractId); } getDocumentsRequest.setV0( diff --git a/packages/js-dapi-client/lib/methods/platform/getIdentitiesContractKeys/GetIdentitiesContractKeysResponse.js b/packages/js-dapi-client/lib/methods/platform/getIdentitiesContractKeys/GetIdentitiesContractKeysResponse.js index 4fd843f6f92..15c72b156e5 100644 --- a/packages/js-dapi-client/lib/methods/platform/getIdentitiesContractKeys/GetIdentitiesContractKeysResponse.js +++ b/packages/js-dapi-client/lib/methods/platform/getIdentitiesContractKeys/GetIdentitiesContractKeysResponse.js @@ -34,6 +34,7 @@ class GetIdentitiesContractKeysResponse extends AbstractResponse { const keysEntries = identitiesKeys.getEntriesList(); identitiesKeysMap = keysEntries.reduce((acc, entry) => { + // wasm-dpp Identifier.from requires Node Buffer (not plain Uint8Array) const identityId = Identifier.from(Buffer.from(entry.getIdentityId())).toString(); if (!acc[identityId]) { acc[identityId] = {}; diff --git a/packages/js-dapi-client/lib/methods/platform/getIdentitiesContractKeys/getIdentitiesContractKeysFactory.js b/packages/js-dapi-client/lib/methods/platform/getIdentitiesContractKeys/getIdentitiesContractKeysFactory.js index 47140bfe0cc..2297531bf1e 100644 --- a/packages/js-dapi-client/lib/methods/platform/getIdentitiesContractKeys/getIdentitiesContractKeysFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getIdentitiesContractKeys/getIdentitiesContractKeysFactory.js @@ -17,8 +17,8 @@ function getIdentitiesContractKeysFactory(grpcTransport) { /** * Fetch the identities by public key hashes * @typedef {getIdentitiesContractKeys} - * @param {Buffer[]} identitiesIds - * @param {Buffer} contractId + * @param {Uint8Array[]} identitiesIds + * @param {Uint8Array} contractId * @param {IdentityPublicKey.PURPOSES[]} keyPurposes * @param {string | null} documentTypeName * @param {DAPIClientOptions & {prove: boolean}} [options] @@ -36,17 +36,16 @@ function getIdentitiesContractKeysFactory(grpcTransport) { // eslint-disable-next-line no-param-reassign identitiesIds = identitiesIds.map((id) => { - if (Buffer.isBuffer(id)) { - // eslint-disable-next-line no-param-reassign - id = Buffer.from(id); + if (id instanceof Uint8Array) { + return new Uint8Array(id); } return id; }); - if (Buffer.isBuffer(contractId)) { + if (contractId instanceof Uint8Array) { // eslint-disable-next-line no-param-reassign - contractId = Buffer.from(contractId); + contractId = new Uint8Array(contractId); } getIdentitiesContractKeysRequest.setV0( diff --git a/packages/js-dapi-client/lib/methods/platform/getIdentity/GetIdentityResponse.js b/packages/js-dapi-client/lib/methods/platform/getIdentity/GetIdentityResponse.js index 718633fb569..a23864d959c 100644 --- a/packages/js-dapi-client/lib/methods/platform/getIdentity/GetIdentityResponse.js +++ b/packages/js-dapi-client/lib/methods/platform/getIdentity/GetIdentityResponse.js @@ -3,7 +3,7 @@ const InvalidResponseError = require('../response/errors/InvalidResponseError'); class GetIdentityResponse extends AbstractResponse { /** - * @param {Buffer} identity + * @param {Uint8Array} identity * @param {Metadata} metadata * @param {Proof} [proof] */ @@ -14,7 +14,7 @@ class GetIdentityResponse extends AbstractResponse { } /** - * @returns {Buffer} + * @returns {Uint8Array} */ getIdentity() { return this.identity; @@ -33,7 +33,7 @@ class GetIdentityResponse extends AbstractResponse { } return new GetIdentityResponse( - Buffer.from(proto.getV0().getIdentity()), + new Uint8Array(proto.getV0().getIdentity()), metadata, proof, ); diff --git a/packages/js-dapi-client/lib/methods/platform/getIdentity/getIdentityFactory.js b/packages/js-dapi-client/lib/methods/platform/getIdentity/getIdentityFactory.js index 6179fe0233e..abafd51d699 100644 --- a/packages/js-dapi-client/lib/methods/platform/getIdentity/getIdentityFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getIdentity/getIdentityFactory.js @@ -16,19 +16,19 @@ function getIdentityFactory(grpcTransport) { /** * Fetch the identity by id * @typedef {getIdentity} - * @param {Buffer} id + * @param {Uint8Array} id * @param {DAPIClientOptions & {prove: boolean}} [options] * @returns {Promise} */ async function getIdentity(id, options = {}) { const { GetIdentityRequestV0 } = GetIdentityRequest; const getIdentityRequest = new GetIdentityRequest(); - // need to convert objects inherited from Buffer to pure buffer as google protobuf + // need to convert objects inherited from Uint8Array to pure Uint8Array as google protobuf // doesn't support extended buffers // https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049 - if (Buffer.isBuffer(id)) { + if (id instanceof Uint8Array) { // eslint-disable-next-line no-param-reassign - id = Buffer.from(id); + id = new Uint8Array(id); } getIdentityRequest.setV0( diff --git a/packages/js-dapi-client/lib/methods/platform/getIdentityBalance/getIdentityBalanceFactory.js b/packages/js-dapi-client/lib/methods/platform/getIdentityBalance/getIdentityBalanceFactory.js index 93862ccff9a..e7141239cde 100644 --- a/packages/js-dapi-client/lib/methods/platform/getIdentityBalance/getIdentityBalanceFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getIdentityBalance/getIdentityBalanceFactory.js @@ -16,19 +16,19 @@ function getIdentityBalanceFactory(grpcTransport) { /** * Fetch the identity balance by id * @typedef {getIdentityBalance} - * @param {Buffer} id + * @param {Uint8Array} id * @param {DAPIClientOptions & {prove: boolean}} [options] * @returns {Promise} */ async function getIdentityBalance(id, options = {}) { const { GetIdentityBalanceRequestV0 } = GetIdentityBalanceRequest; const getIdentityBalanceRequest = new GetIdentityBalanceRequest(); - // need to convert objects inherited from Buffer to pure buffer as google protobuf + // need to convert objects inherited from Uint8Array to pure Uint8Array as google protobuf // doesn't support extended buffers // https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049 - if (Buffer.isBuffer(id)) { + if (id instanceof Uint8Array) { // eslint-disable-next-line no-param-reassign - id = Buffer.from(id); + id = new Uint8Array(id); } getIdentityBalanceRequest.setV0( diff --git a/packages/js-dapi-client/lib/methods/platform/getIdentityByPublicKeyHash/GetIdentityByPublicKeyHashResponse.js b/packages/js-dapi-client/lib/methods/platform/getIdentityByPublicKeyHash/GetIdentityByPublicKeyHashResponse.js index 86f97aecfe7..b67fa3d6cdf 100644 --- a/packages/js-dapi-client/lib/methods/platform/getIdentityByPublicKeyHash/GetIdentityByPublicKeyHashResponse.js +++ b/packages/js-dapi-client/lib/methods/platform/getIdentityByPublicKeyHash/GetIdentityByPublicKeyHashResponse.js @@ -2,7 +2,7 @@ const AbstractResponse = require('../response/AbstractResponse'); class GetIdentityByPublicKeyHashResponse extends AbstractResponse { /** - * @param {Buffer} identities + * @param {Uint8Array} identities * @param identity * @param {Metadata} metadata * @param {Proof} [proof] @@ -14,7 +14,7 @@ class GetIdentityByPublicKeyHashResponse extends AbstractResponse { } /** - * @returns {Buffer[]} + * @returns {Uint8Array[]} */ getIdentity() { return this.identity; @@ -30,7 +30,7 @@ class GetIdentityByPublicKeyHashResponse extends AbstractResponse { const identity = proto.getV0().getIdentity(); return new GetIdentityByPublicKeyHashResponse( - Buffer.from(identity), + new Uint8Array(identity), metadata, proof, ); diff --git a/packages/js-dapi-client/lib/methods/platform/getIdentityContractNonce/getIdentityContractNonceFactory.js b/packages/js-dapi-client/lib/methods/platform/getIdentityContractNonce/getIdentityContractNonceFactory.js index 59aec51dc87..683a01dc465 100644 --- a/packages/js-dapi-client/lib/methods/platform/getIdentityContractNonce/getIdentityContractNonceFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getIdentityContractNonce/getIdentityContractNonceFactory.js @@ -16,8 +16,8 @@ function getIdentityContractNonceFactory(grpcTransport) { /** * Fetch the version upgrade votes status * @typedef {getIdentityContractNonce} - * @param {Buffer} identityId - * @param {Buffer} contractId + * @param {Uint8Array} identityId + * @param {Uint8Array} contractId * @param {DAPIClientOptions & {prove: boolean}} [options] * @returns {Promise} */ @@ -29,14 +29,14 @@ function getIdentityContractNonceFactory(grpcTransport) { // eslint-disable-next-line max-len const getIdentityContractNonceRequest = new GetIdentityContractNonceRequest(); - if (Buffer.isBuffer(identityId)) { + if (identityId instanceof Uint8Array) { // eslint-disable-next-line no-param-reassign - identityId = Buffer.from(identityId); + identityId = new Uint8Array(identityId); } - if (Buffer.isBuffer(contractId)) { + if (contractId instanceof Uint8Array) { // eslint-disable-next-line no-param-reassign - contractId = Buffer.from(contractId); + contractId = new Uint8Array(contractId); } getIdentityContractNonceRequest.setV0( diff --git a/packages/js-dapi-client/lib/methods/platform/getIdentityKeys/getIdentityKeysFactory.js b/packages/js-dapi-client/lib/methods/platform/getIdentityKeys/getIdentityKeysFactory.js index 4bc32a03a65..772e6678f3c 100644 --- a/packages/js-dapi-client/lib/methods/platform/getIdentityKeys/getIdentityKeysFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getIdentityKeys/getIdentityKeysFactory.js @@ -23,16 +23,16 @@ function getIdentityKeysFactory(grpcTransport) { /** * Fetch the version upgrade votes status * @typedef {getIdentityKeys} - * @param {Buffer} identityId + * @param {Uint8Array} identityId * @param {number[]=} keyIds * @param {number} limit * @param {DAPIClientOptions & {prove: boolean}} [options] * @returns {Promise} */ async function getIdentityKeys(identityId, keyIds, limit = 100, options = {}) { - if (Buffer.isBuffer(identityId)) { + if (identityId instanceof Uint8Array) { // eslint-disable-next-line no-param-reassign - identityId = Buffer.from(identityId); + identityId = new Uint8Array(identityId); } const getIdentityKeysRequest = new GetIdentityKeysRequest(); diff --git a/packages/js-dapi-client/lib/methods/platform/getIdentityNonce/getIdentityNonceFactory.js b/packages/js-dapi-client/lib/methods/platform/getIdentityNonce/getIdentityNonceFactory.js index 217cfe48c34..a0a62dc2fa0 100644 --- a/packages/js-dapi-client/lib/methods/platform/getIdentityNonce/getIdentityNonceFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getIdentityNonce/getIdentityNonceFactory.js @@ -16,7 +16,7 @@ function getIdentityNonceFactory(grpcTransport) { /** * Fetch the version upgrade votes status * @typedef {getIdentityNonce} - * @param {Buffer} identityId + * @param {Uint8Array} identityId * @param {DAPIClientOptions & {prove: boolean}} [options] * @returns {Promise} */ @@ -28,9 +28,9 @@ function getIdentityNonceFactory(grpcTransport) { // eslint-disable-next-line max-len const getIdentityNonceRequest = new GetIdentityNonceRequest(); - if (Buffer.isBuffer(identityId)) { + if (identityId instanceof Uint8Array) { // eslint-disable-next-line no-param-reassign - identityId = Buffer.from(identityId); + identityId = new Uint8Array(identityId); } getIdentityNonceRequest.setV0( diff --git a/packages/js-dapi-client/lib/methods/platform/getProtocolVersionUpgradeVoteStatus/GetProtocolVersionUpgradeVoteStatusResponse.js b/packages/js-dapi-client/lib/methods/platform/getProtocolVersionUpgradeVoteStatus/GetProtocolVersionUpgradeVoteStatusResponse.js index a28ec23e6c1..e072443c09c 100644 --- a/packages/js-dapi-client/lib/methods/platform/getProtocolVersionUpgradeVoteStatus/GetProtocolVersionUpgradeVoteStatusResponse.js +++ b/packages/js-dapi-client/lib/methods/platform/getProtocolVersionUpgradeVoteStatus/GetProtocolVersionUpgradeVoteStatusResponse.js @@ -1,6 +1,7 @@ const AbstractResponse = require('../response/AbstractResponse'); const VersionSignal = require('./VersionSignal'); const InvalidResponseError = require('../response/errors/InvalidResponseError'); +const { bytesToHex } = require('../../../utils/bytes'); class GetProtocolVersionUpgradeVoteStatusResponse extends AbstractResponse { /** @@ -40,7 +41,7 @@ class GetProtocolVersionUpgradeVoteStatusResponse extends AbstractResponse { const versionSignalsList = versions && versions.getVersionSignalsList(); if (versionSignalsList) { versionSignals = versionSignalsList.map((versionSignal) => new VersionSignal( - Buffer.from(versionSignal.getProTxHash()).toString('hex'), + bytesToHex(new Uint8Array(versionSignal.getProTxHash())), versionSignal.getVersion(), )); } diff --git a/packages/js-dapi-client/lib/methods/platform/getProtocolVersionUpgradeVoteStatus/getProtocolVersionUpgradeVoteStatusFactory.js b/packages/js-dapi-client/lib/methods/platform/getProtocolVersionUpgradeVoteStatus/getProtocolVersionUpgradeVoteStatusFactory.js index bc79c54315c..e7ecf26c02d 100644 --- a/packages/js-dapi-client/lib/methods/platform/getProtocolVersionUpgradeVoteStatus/getProtocolVersionUpgradeVoteStatusFactory.js +++ b/packages/js-dapi-client/lib/methods/platform/getProtocolVersionUpgradeVoteStatus/getProtocolVersionUpgradeVoteStatusFactory.js @@ -7,6 +7,7 @@ const { const GetProtocolVersionUpgradeVoteStatusResponse = require('./GetProtocolVersionUpgradeVoteStatusResponse'); const InvalidResponseError = require('../response/errors/InvalidResponseError'); +const { hexToBytes } = require('../../../utils/bytes'); /** * @param {GrpcTransport} grpcTransport @@ -31,7 +32,7 @@ function getProtocolVersionUpgradeVoteStatusFactory(grpcTransport) { getProtocolVersionUpgradeVoteStatusRequest.setV0( new GetProtocolVersionUpgradeVoteStatusRequestV0() - .setStartProTxHash(Buffer.from(startProTxHash, 'hex')) + .setStartProTxHash(hexToBytes(startProTxHash)) .setCount(count) .setProve(!!options.prove), ); diff --git a/packages/js-dapi-client/lib/methods/platform/getStatus/GetStatusResponse.js b/packages/js-dapi-client/lib/methods/platform/getStatus/GetStatusResponse.js index 1509b69b64a..ff465a82d00 100644 --- a/packages/js-dapi-client/lib/methods/platform/getStatus/GetStatusResponse.js +++ b/packages/js-dapi-client/lib/methods/platform/getStatus/GetStatusResponse.js @@ -4,6 +4,7 @@ const ChainStatus = require('./ChainStatus'); const TimeStatus = require('./TimeStatus'); const StateSyncStatus = require('./StateSyncStatus'); const NetworkStatus = require('./NetworkStatus'); +const { bytesToHex } = require('../../../utils/bytes'); class GetStatusResponse { /** @@ -92,17 +93,17 @@ class GetStatusResponse { driveNextEpochProtocol, ); - const nodeId = Buffer.from(v0.getNode().getId()).toString('hex'); - const proTxHash = Buffer.from(v0.getNode().getProTxHash()).toString('hex'); + const nodeId = bytesToHex(new Uint8Array(v0.getNode().getId())); + const proTxHash = bytesToHex(new Uint8Array(v0.getNode().getProTxHash())); const node = new NodeStatus(nodeId, proTxHash); const catchingUp = v0.getChain().getCatchingUp(); - const latestBlockHash = Buffer.from(v0.getChain().getLatestBlockHash()).toString('hex'); - const latestAppHash = Buffer.from(v0.getChain().getLatestAppHash()).toString('hex'); + const latestBlockHash = bytesToHex(new Uint8Array(v0.getChain().getLatestBlockHash())); + const latestAppHash = bytesToHex(new Uint8Array(v0.getChain().getLatestAppHash())); const latestBlockHeight = BigInt(v0.getChain().getLatestBlockHeight()); - const earliestBlockHash = Buffer.from(v0.getChain().getEarliestBlockHash()).toString('hex'); - const earliestAppHash = Buffer.from(v0.getChain().getEarliestAppHash()).toString('hex'); + const earliestBlockHash = bytesToHex(new Uint8Array(v0.getChain().getEarliestBlockHash())); + const earliestAppHash = bytesToHex(new Uint8Array(v0.getChain().getEarliestAppHash())); const earliestBlockHeight = BigInt(v0.getChain().getEarliestBlockHeight()); const maxPeerBlockHeight = BigInt(v0.getChain().getMaxPeerBlockHeight()); const coreChainLockedHeight = v0.getChain().getCoreChainLockedHeight(); diff --git a/packages/js-dapi-client/lib/methods/platform/response/Proof.js b/packages/js-dapi-client/lib/methods/platform/response/Proof.js index 56111ac5458..9d284ea969a 100644 --- a/packages/js-dapi-client/lib/methods/platform/response/Proof.js +++ b/packages/js-dapi-client/lib/methods/platform/response/Proof.js @@ -1,9 +1,9 @@ class Proof { /** * @param {object} properties - * @param {Buffer} properties.merkleProof - * @param {Buffer} properties.quorumHash - * @param {Buffer} properties.signature + * @param {Uint8Array} properties.merkleProof + * @param {Uint8Array} properties.quorumHash + * @param {Uint8Array} properties.signature * @param {number} properties.round */ constructor(properties) { @@ -14,21 +14,21 @@ class Proof { } /** - * @returns {Buffer} + * @returns {Uint8Array} */ getGrovedbProof() { return this.merkleProof; } /** - * @returns {Buffer} + * @returns {Uint8Array} */ getQuorumHash() { return this.quorumHash; } /** - * @returns {Buffer} + * @returns {Uint8Array} */ getSignature() { return this.signature; @@ -48,9 +48,9 @@ class Proof { */ static createFromProto(proofProto) { return new Proof({ - merkleProof: Buffer.from(proofProto.getGrovedbProof()), - quorumHash: Buffer.from(proofProto.getQuorumHash()), - signature: Buffer.from(proofProto.getSignature()), + merkleProof: new Uint8Array(proofProto.getGrovedbProof()), + quorumHash: new Uint8Array(proofProto.getQuorumHash()), + signature: new Uint8Array(proofProto.getSignature()), round: proofProto.getRound(), }); } diff --git a/packages/js-dapi-client/lib/methods/platform/waitForStateTransitionResult/WaitForStateTransitionResultResponse.js b/packages/js-dapi-client/lib/methods/platform/waitForStateTransitionResult/WaitForStateTransitionResultResponse.js index 836987a2376..fad4c5cbc2f 100644 --- a/packages/js-dapi-client/lib/methods/platform/waitForStateTransitionResult/WaitForStateTransitionResultResponse.js +++ b/packages/js-dapi-client/lib/methods/platform/waitForStateTransitionResult/WaitForStateTransitionResultResponse.js @@ -38,7 +38,7 @@ class WaitForStateTransitionResultResponse extends AbstractResponse { let data; if (proto.getV0().getError().getData()) { - data = Buffer.from(proto.getV0().getError().getData()); + data = new Uint8Array(proto.getV0().getError().getData()); } error = new ErrorResult( diff --git a/packages/js-dapi-client/lib/test/fixtures/getProofFixture.js b/packages/js-dapi-client/lib/test/fixtures/getProofFixture.js index 274b9f8a70f..a5710ea1ee3 100644 --- a/packages/js-dapi-client/lib/test/fixtures/getProofFixture.js +++ b/packages/js-dapi-client/lib/test/fixtures/getProofFixture.js @@ -1,15 +1,17 @@ +const { hexToBytes, base64ToBytes } = require('../../utils/bytes'); + /** * @returns {{ - * merkleProof: Buffer, - * signature: Buffer, - * quorumHash: Buffer + * merkleProof: Uint8Array, + * signature: Uint8Array, + * quorumHash: Uint8Array * }} */ function getProofFixture() { return { - quorumHash: Buffer.from('AQEBAQEBAQEBAQEB', 'base64'), - signature: Buffer.from('AgICAgICAgICAgIC', 'base64'), - merkleProof: Buffer.from('0100000001f0faf5f55674905a68eba1be2f946e667c1cb5010101', 'hex'), + quorumHash: base64ToBytes('AQEBAQEBAQEBAQEB'), + signature: base64ToBytes('AgICAgICAgICAgIC'), + merkleProof: hexToBytes('0100000001f0faf5f55674905a68eba1be2f946e667c1cb5010101'), round: 42, }; } diff --git a/packages/js-dapi-client/lib/test/fixtures/getStatusFixture.js b/packages/js-dapi-client/lib/test/fixtures/getStatusFixture.js index 1336e117372..49dbf60621b 100644 --- a/packages/js-dapi-client/lib/test/fixtures/getStatusFixture.js +++ b/packages/js-dapi-client/lib/test/fixtures/getStatusFixture.js @@ -1,3 +1,5 @@ +const { base64ToBytes } = require('../../utils/bytes'); + /** * */ @@ -22,16 +24,16 @@ function getStatusFixture() { }, }, node: { - id: new Uint8Array(Buffer.from('QbMI9zfKnjn2e1UxWJAxmKiMUW4=', 'base64')), - proTxHash: new Uint8Array(Buffer.from('s7V0hXG2D+mtEScV1qUXJdblpSqcOvX9NqFyTPUNhi8=', 'base64')), + id: base64ToBytes('QbMI9zfKnjn2e1UxWJAxmKiMUW4='), + proTxHash: base64ToBytes('s7V0hXG2D+mtEScV1qUXJdblpSqcOvX9NqFyTPUNhi8='), }, chain: { catchingUp: false, - latestBlockHash: new Uint8Array(Buffer.from('mVDwGtY2oJSaLLgv3WpLp2dFDyFEtqhD4z1gl2OJceY=', 'base64')), - latestAppHash: new Uint8Array(Buffer.from('jHgEBK8aZ74TUKcUGN58EFzUNvNsLboOgYe6eH/JetU=', 'base64')), + latestBlockHash: base64ToBytes('mVDwGtY2oJSaLLgv3WpLp2dFDyFEtqhD4z1gl2OJceY='), + latestAppHash: base64ToBytes('jHgEBK8aZ74TUKcUGN58EFzUNvNsLboOgYe6eH/JetU='), latestBlockHeight: '94461', - earliestBlockHash: new Uint8Array(Buffer.from('CPoCwn7AOQujAeT8fj1+rbNQyBk+PmKgk2iXBuOiC/o=', 'base64')), - earliestAppHash: new Uint8Array(Buffer.from('vwzLnKBxugGubmegwJD5eAPSbVbWddzVExeBy8rI7I8=', 'base64')), + earliestBlockHash: base64ToBytes('CPoCwn7AOQujAeT8fj1+rbNQyBk+PmKgk2iXBuOiC/o='), + earliestAppHash: base64ToBytes('vwzLnKBxugGubmegwJD5eAPSbVbWddzVExeBy8rI7I8='), earliestBlockHeight: '1', maxPeerBlockHeight: '94461', coreChainLockedHeight: 1187358, diff --git a/packages/js-dapi-client/lib/test/mocks/mockHeadersChain.js b/packages/js-dapi-client/lib/test/mocks/mockHeadersChain.js index 2ae0a911a27..3c7a04e3752 100644 --- a/packages/js-dapi-client/lib/test/mocks/mockHeadersChain.js +++ b/packages/js-dapi-client/lib/test/mocks/mockHeadersChain.js @@ -1,6 +1,7 @@ const X11 = require('wasm-x11-hash'); const { BlockHeader, configure } = require('@dashevo/dashcore-lib'); const { genesis } = require('@dashevo/dash-spv'); +const { hexToBytes } = require('../../utils/bytes'); const getRoot = (network) => { switch (network) { @@ -38,8 +39,8 @@ const mockHeadersChain = async (network, length, root) => { for (let i = 0; i < length - 1; i += 1) { const header = new BlockHeader({ version: prevHeader.version, - prevHash: Buffer.from(prevHeader.hash, 'hex').reverse(), - merkleRoot: Buffer.alloc(32), + prevHash: hexToBytes(prevHeader.hash).reverse(), + merkleRoot: new Uint8Array(32), time: prevHeader.time + BLOCK_TIME, bits: prevHeader.bits, nonce: 3861367235, diff --git a/packages/js-dapi-client/lib/transport/GrpcTransport/createGrpcTransportError.js b/packages/js-dapi-client/lib/transport/GrpcTransport/createGrpcTransportError.js index e5cae56b753..2fdc053c8b1 100644 --- a/packages/js-dapi-client/lib/transport/GrpcTransport/createGrpcTransportError.js +++ b/packages/js-dapi-client/lib/transport/GrpcTransport/createGrpcTransportError.js @@ -14,6 +14,7 @@ const ServerError = require('../errors/response/ServerError'); const InvalidRequestError = require('../errors/response/InvalidRequestError'); const InvalidRequestDPPError = require('../errors/response/InvalidRequestDPPError'); const InternalServerError = require('./errors/InternalServerError'); +const { base64ToBytes } = require('../../utils/bytes'); const INVALID_REQUEST_CODES = [ GrpcErrorCodes.INVALID_ARGUMENT, @@ -57,10 +58,13 @@ async function createGrpcTransportError(grpcError, dapiAddress) { const metadata = parseMetadata(grpcError.metadata) || {}; - // Error data + // Error data — grpc-js passes raw bytes; grpc-web passes a base64 string. + // Original Buffer.from(x, 'base64') silently passed through bytes; reproduce that. const driveErrorData = metadata['drive-error-data-bin']; if (driveErrorData) { - const encodedData = Buffer.from(driveErrorData, 'base64'); + const encodedData = typeof driveErrorData === 'string' + ? base64ToBytes(driveErrorData) + : new Uint8Array(driveErrorData); data = cbor.decode(encodedData); } @@ -70,10 +74,12 @@ async function createGrpcTransportError(grpcError, dapiAddress) { code = Number(driveErrorCode); } - // Error stack + // Error stack — same dual-format handling as driveErrorData above. const driveErrorStack = metadata['stack-bin']; if (driveErrorStack) { - const encodedStack = Buffer.from(driveErrorStack, 'base64'); + const encodedStack = typeof driveErrorStack === 'string' + ? base64ToBytes(driveErrorStack) + : new Uint8Array(driveErrorStack); data.stack = cbor.decode(encodedStack); } @@ -124,7 +130,10 @@ async function createGrpcTransportError(grpcError, dapiAddress) { throw new Error(`Can't deserialize consensus error ${code}: serialized data is missing`); } - const consensusErrorBytes = Buffer.from(consensusErrorString, 'base64'); + // grpc-js passes raw bytes; grpc-web passes a base64 string. Handle both. + const consensusErrorBytes = typeof consensusErrorString === 'string' + ? base64ToBytes(consensusErrorString) + : new Uint8Array(consensusErrorString); const consensusError = deserializeConsensusError(consensusErrorBytes); delete data.serializedError; diff --git a/packages/js-dapi-client/lib/utils/bytes.js b/packages/js-dapi-client/lib/utils/bytes.js new file mode 100644 index 00000000000..bed5176b2d0 --- /dev/null +++ b/packages/js-dapi-client/lib/utils/bytes.js @@ -0,0 +1,63 @@ +function hexToBytes(hex) { + if (typeof hex !== 'string') { + throw new TypeError('hex must be a string'); + } + if (hex.length % 2 !== 0) { + throw new Error('hex must have even length'); + } + const out = new Uint8Array(hex.length / 2); + for (let i = 0; i < out.length; i += 1) { + out[i] = parseInt(hex.substr(i * 2, 2), 16); + } + return out; +} + +function bytesToHex(bytes) { + return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join(''); +} + +function base64ToBytes(b64) { + const bin = atob(b64); + const out = new Uint8Array(bin.length); + for (let i = 0; i < bin.length; i += 1) { + out[i] = bin.charCodeAt(i); + } + return out; +} + +function bytesToBase64(bytes) { + let bin = ''; + for (let i = 0; i < bytes.length; i += 1) { + bin += String.fromCharCode(bytes[i]); + } + return btoa(bin); +} + +function concatBytes(arrays) { + let total = 0; + for (const a of arrays) total += a.length; + const out = new Uint8Array(total); + let offset = 0; + for (const a of arrays) { + out.set(a, offset); + offset += a.length; + } + return out; +} + +function bytesEqual(a, b) { + if (a.length !== b.length) return false; + for (let i = 0; i < a.length; i += 1) { + if (a[i] !== b[i]) return false; + } + return true; +} + +module.exports = { + hexToBytes, + bytesToHex, + base64ToBytes, + bytesToBase64, + concatBytes, + bytesEqual, +}; diff --git a/packages/js-dapi-client/test/integration/methods/core/CoreMethodsFacade.spec.js b/packages/js-dapi-client/test/integration/methods/core/CoreMethodsFacade.spec.js index d7eeecc6875..81a67152bc5 100644 --- a/packages/js-dapi-client/test/integration/methods/core/CoreMethodsFacade.spec.js +++ b/packages/js-dapi-client/test/integration/methods/core/CoreMethodsFacade.spec.js @@ -36,7 +36,7 @@ describe('CoreMethodsFacade', () => { response.setTransactionId('4f46066bd50cc2684484407696b7949e82bd906ea92c040f59a97cba47ed8176'); grpcTransportMock.request.resolves(response); - const transaction = Buffer.from('transaction'); + const transaction = new TextEncoder().encode('transaction'); await coreMethods.broadcastTransaction(transaction); expect(grpcTransportMock.request).to.be.calledOnce(); @@ -57,7 +57,7 @@ describe('CoreMethodsFacade', () => { describe('#getBlockByHash', () => { it('should get block by hash', async () => { - const block = Buffer.from('block'); + const block = new TextEncoder().encode('block'); const response = new GetBlockResponse(); response.setBlock(block); grpcTransportMock.request.resolves(response); @@ -70,7 +70,7 @@ describe('CoreMethodsFacade', () => { describe('#getBlockByHeight', () => { it('should get block by height', async () => { - const block = Buffer.from('block'); + const block = new TextEncoder().encode('block'); const response = new GetBlockResponse(); response.setBlock(block); grpcTransportMock.request.resolves(response); @@ -124,10 +124,10 @@ describe('CoreMethodsFacade', () => { describe('#getTransaction', () => { it('should get transaction', async () => { - const transaction = Buffer.from('transaction'); + const transaction = new TextEncoder().encode('transaction'); const response = new GetTransactionResponse(); response.setTransaction(transaction); - response.setBlockHash(Buffer.from('blockHash')); + response.setBlockHash(new TextEncoder().encode('blockHash')); response.setHeight(1); response.setConfirmations(2); diff --git a/packages/js-dapi-client/test/integration/methods/platform/PlatformMethodsFacade.spec.js b/packages/js-dapi-client/test/integration/methods/platform/PlatformMethodsFacade.spec.js index fbb1f32ed5c..a5288db5d52 100644 --- a/packages/js-dapi-client/test/integration/methods/platform/PlatformMethodsFacade.spec.js +++ b/packages/js-dapi-client/test/integration/methods/platform/PlatformMethodsFacade.spec.js @@ -25,6 +25,7 @@ const getDataContractFixture = require('@dashevo/wasm-dpp/lib/test/fixtures/getD const getIdentityFixture = require('@dashevo/wasm-dpp/lib/test/fixtures/getIdentityFixture'); const PlatformMethodsFacade = require('../../../../lib/methods/platform/PlatformMethodsFacade'); +const { hexToBytes, bytesToHex } = require('../../../../lib/utils/bytes'); const { WaitForStateTransitionResultResponseV0 } = WaitForStateTransitionResultResponse; const { GetIdentityResponseV0 } = GetIdentityResponse; @@ -164,8 +165,8 @@ describe('PlatformMethodsFacade', () => { grpcTransportMock.request.resolves(response); await platformMethods.getIdentitiesContractKeys([ - Buffer.alloc(32).fill(1), - ], Buffer.alloc(32).fill(2), [KeyPurpose.ENCRYPTION]); + new Uint8Array(32).fill(1), + ], new Uint8Array(32).fill(2), [KeyPurpose.ENCRYPTION]); expect(grpcTransportMock.request).to.be.calledOnce(); }); @@ -182,7 +183,7 @@ describe('PlatformMethodsFacade', () => { grpcTransportMock.request.resolves(response); await platformMethods.waitForStateTransitionResult( - Buffer.from('6f49655a2906852a38e473dd47574fb70b8b7c4e5cee9ea8e7da3f07b970c421', 'hex'), + hexToBytes('6f49655a2906852a38e473dd47574fb70b8b7c4e5cee9ea8e7da3f07b970c421'), false, ); @@ -218,8 +219,8 @@ describe('PlatformMethodsFacade', () => { describe('#getProtocolVersionUpgradeVoteStatus', () => { it('should get version upgrade votes status', async () => { - const startProTxHash = Buffer.alloc(32).fill('a').toString('hex'); - const proTxHash = Buffer.alloc(32).fill('b').toString('hex'); + const startProTxHash = bytesToHex(new Uint8Array(32).fill(0x61)); + const proTxHash = bytesToHex(new Uint8Array(32).fill(0x62)); const { VersionSignal, VersionSignals } = GetProtocolVersionUpgradeVoteStatusResponseV0; @@ -277,7 +278,7 @@ describe('PlatformMethodsFacade', () => { grpcTransportMock.request.resolves(response); - await platformMethods.getIdentityContractNonce(Buffer.alloc(32), Buffer.alloc(32)); + await platformMethods.getIdentityContractNonce(new Uint8Array(32), new Uint8Array(32)); expect(grpcTransportMock.request).to.be.calledOnce(); }); @@ -295,7 +296,7 @@ describe('PlatformMethodsFacade', () => { grpcTransportMock.request.resolves(response); - await platformMethods.getIdentityNonce(Buffer.alloc(32), Buffer.alloc(32)); + await platformMethods.getIdentityNonce(new Uint8Array(32), new Uint8Array(32)); expect(grpcTransportMock.request).to.be.calledOnce(); }); @@ -309,13 +310,13 @@ describe('PlatformMethodsFacade', () => { response.setV0( new GetIdentityKeysResponseV0() - .setKeys(new Keys().setKeysBytesList([Buffer.alloc(41), Buffer.alloc(46)])) + .setKeys(new Keys().setKeysBytesList([new Uint8Array(41), new Uint8Array(46)])) .setMetadata(new ResponseMetadata()), ); grpcTransportMock.request.resolves(response); - await platformMethods.getIdentityKeys(Buffer.alloc(32), [0, 1], 100, {}); + await platformMethods.getIdentityKeys(new Uint8Array(32), [0, 1], 100, {}); expect(grpcTransportMock.request).to.be.calledOnce(); }); diff --git a/packages/js-dapi-client/test/unit/methods/core/broadcastTransactionFactory.spec.js b/packages/js-dapi-client/test/unit/methods/core/broadcastTransactionFactory.spec.js index 1b7ef038185..d7cba51b8f8 100644 --- a/packages/js-dapi-client/test/unit/methods/core/broadcastTransactionFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/core/broadcastTransactionFactory.spec.js @@ -25,7 +25,7 @@ describe('broadcastTransactionFactory', () => { grpcTransportMock, ); - transaction = Buffer.from('transaction'); + transaction = new TextEncoder().encode('transaction'); id = '4f46066bd50cc2684484407696b7949e82bd906ea92c040f59a97cba47ed8176'; }); diff --git a/packages/js-dapi-client/test/unit/methods/core/getBlockByHashFactory.spec.js b/packages/js-dapi-client/test/unit/methods/core/getBlockByHashFactory.spec.js index d9548cf6b69..e16e60130d1 100644 --- a/packages/js-dapi-client/test/unit/methods/core/getBlockByHashFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/core/getBlockByHashFactory.spec.js @@ -14,7 +14,7 @@ describe('getBlockByHashFactory', () => { let block; beforeEach(function beforeEach() { - block = Buffer.from('block'); + block = new TextEncoder().encode('block'); const response = new GetBlockResponse(); response.setBlock(block); @@ -36,7 +36,7 @@ describe('getBlockByHashFactory', () => { const request = new GetBlockRequest(); request.setHash(hash); - expect(result).to.be.instanceof(Buffer); + expect(result).to.be.instanceof(Uint8Array); expect(result).to.deep.equal(block); expect(grpcTransportMock.request).to.be.calledOnceWithExactly( CorePromiseClient, diff --git a/packages/js-dapi-client/test/unit/methods/core/getBlockByHeightFactory.spec.js b/packages/js-dapi-client/test/unit/methods/core/getBlockByHeightFactory.spec.js index 5a6ad896648..1ea27f50887 100644 --- a/packages/js-dapi-client/test/unit/methods/core/getBlockByHeightFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/core/getBlockByHeightFactory.spec.js @@ -14,7 +14,7 @@ describe('getBlockByHeightFactory', () => { let block; beforeEach(function beforeEach() { - block = Buffer.from('block'); + block = new TextEncoder().encode('block'); const response = new GetBlockResponse(); response.setBlock(block); @@ -36,7 +36,7 @@ describe('getBlockByHeightFactory', () => { const request = new GetBlockRequest(); request.setHeight(height); - expect(result).to.be.instanceof(Buffer); + expect(result).to.be.instanceof(Uint8Array); expect(result).to.deep.equal(block); expect(grpcTransportMock.request).to.be.calledOnceWithExactly( CorePromiseClient, diff --git a/packages/js-dapi-client/test/unit/methods/core/getBlockchainStatusFactory.spec.js b/packages/js-dapi-client/test/unit/methods/core/getBlockchainStatusFactory.spec.js index bceb20a0ec6..f2fca4360d2 100644 --- a/packages/js-dapi-client/test/unit/methods/core/getBlockchainStatusFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/core/getBlockchainStatusFactory.spec.js @@ -7,6 +7,7 @@ const { } = require('@dashevo/dapi-grpc'); const getBlockchainStatusFactory = require('../../../../lib/methods/core/getBlockchainStatusFactory'); +const { base64ToBytes } = require('../../../../lib/utils/bytes'); describe('getBlockchainStatusFactory', () => { let getBlockchainStatus; @@ -25,7 +26,7 @@ describe('getBlockchainStatusFactory', () => { response.setStatus(GetBlockchainStatusResponse.Status.READY); const chain = new GetBlockchainStatusResponse.Chain(); - chain.setBestBlockHash(Buffer.from('bestBlockHash')); + chain.setBestBlockHash(new TextEncoder().encode('bestBlockHash')); response.setChain(chain); @@ -53,7 +54,8 @@ describe('getBlockchainStatusFactory', () => { status: 'READY', }; - expectedResult.chain.bestBlockHash = Buffer.from(expectedResult.chain.bestBlockHash, 'base64'); + // toObject returns bestBlockHash as base64 string; production code converts to Uint8Array + expectedResult.chain.bestBlockHash = base64ToBytes(expectedResult.chain.bestBlockHash); expect(result).to.deep.equal(expectedResult); }); diff --git a/packages/js-dapi-client/test/unit/methods/core/getMasternodeStatusFactory.spec.js b/packages/js-dapi-client/test/unit/methods/core/getMasternodeStatusFactory.spec.js index fdd1b569e53..2a74dac3ba7 100644 --- a/packages/js-dapi-client/test/unit/methods/core/getMasternodeStatusFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/core/getMasternodeStatusFactory.spec.js @@ -45,7 +45,7 @@ describe('getMasternodeStatusFactory', () => { const expectedResult = { ...response.toObject(), - proTxHash: Buffer.alloc(0), + proTxHash: new Uint8Array(0), status: 'READY', }; diff --git a/packages/js-dapi-client/test/unit/methods/core/getTransaction/GetTransactionResponse.spec.js b/packages/js-dapi-client/test/unit/methods/core/getTransaction/GetTransactionResponse.spec.js index 294e9a6d253..6185d629db1 100644 --- a/packages/js-dapi-client/test/unit/methods/core/getTransaction/GetTransactionResponse.spec.js +++ b/packages/js-dapi-client/test/unit/methods/core/getTransaction/GetTransactionResponse.spec.js @@ -13,8 +13,8 @@ describe('GetTransactionResponse', () => { beforeEach(() => { response = { - transaction: Buffer.from('transaction'), - blockHash: Buffer.from('blockHash'), + transaction: new TextEncoder().encode('transaction'), + blockHash: new TextEncoder().encode('blockHash'), height: 10, confirmations: 42, instantLocked: true, @@ -72,8 +72,8 @@ describe('GetTransactionResponse', () => { const instance = GetTransactionResponse.createFromProto(proto); expect(instance).to.be.an.instanceOf(GetTransactionResponse); - expect(instance.transaction).to.deep.equal(Buffer.from(proto.getTransaction())); - expect(instance.blockHash).to.deep.equal(Buffer.from(proto.getBlockHash())); + expect(instance.transaction).to.deep.equal(new Uint8Array(proto.getTransaction())); + expect(instance.blockHash).to.deep.equal(new Uint8Array(proto.getBlockHash())); expect(instance.height).to.deep.equal(proto.getHeight()); expect(instance.confirmations).to.deep.equal(proto.getConfirmations()); expect(instance.instantLocked).to.deep.equal(proto.getIsInstantLocked()); diff --git a/packages/js-dapi-client/test/unit/methods/core/getTransaction/getTransactionFactory.spec.js b/packages/js-dapi-client/test/unit/methods/core/getTransaction/getTransactionFactory.spec.js index cbbe9312186..b7c9895e9b2 100644 --- a/packages/js-dapi-client/test/unit/methods/core/getTransaction/getTransactionFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/core/getTransaction/getTransactionFactory.spec.js @@ -20,8 +20,8 @@ describe('getTransactionFactory', () => { let isInstantLocked; beforeEach(function beforeEach() { - transaction = Buffer.from('transaction'); - blockHash = Buffer.from('blockHash'); + transaction = new TextEncoder().encode('transaction'); + blockHash = new TextEncoder().encode('blockHash'); height = 42; confirmations = 3; isChainLocked = true; diff --git a/packages/js-dapi-client/test/unit/methods/core/subscribeToBlockHeadersWithChainLocksFactory.spec.js b/packages/js-dapi-client/test/unit/methods/core/subscribeToBlockHeadersWithChainLocksFactory.spec.js index 32ce15e4a45..6132eccb95f 100644 --- a/packages/js-dapi-client/test/unit/methods/core/subscribeToBlockHeadersWithChainLocksFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/core/subscribeToBlockHeadersWithChainLocksFactory.spec.js @@ -9,6 +9,7 @@ const { EventEmitter } = require('events'); const subscribeToBlockHeadersWithChainLocksFactory = require('../../../../lib/methods/core/subscribeToBlockHeadersWithChainLocksFactory'); const DAPIClientError = require('../../../../lib/errors/DAPIClientError'); +const { hexToBytes } = require('../../../../lib/utils/bytes'); describe('subscribeToBlockHeadersWithChainLocks', () => { let subscribeToBlockHeadersWithChainLocks; @@ -50,7 +51,7 @@ describe('subscribeToBlockHeadersWithChainLocks', () => { const request = new BlockHeadersWithChainLocksRequest(); request.setFromBlockHeight(1); - request.setFromBlockHash(Buffer.from(options.fromBlockHash, 'hex')); + request.setFromBlockHash(hexToBytes(options.fromBlockHash)); request.setCount(options.count); expect(grpcTransportMock.request).to.be.calledWith( diff --git a/packages/js-dapi-client/test/unit/methods/core/subscribeToTransactionsWithProofsFactory.spec.js b/packages/js-dapi-client/test/unit/methods/core/subscribeToTransactionsWithProofsFactory.spec.js index 7fb33af14a8..f77c6d3c658 100644 --- a/packages/js-dapi-client/test/unit/methods/core/subscribeToTransactionsWithProofsFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/core/subscribeToTransactionsWithProofsFactory.spec.js @@ -13,6 +13,7 @@ const { EventEmitter } = require('events'); const subscribeToTransactionsWithProofsFactory = require('../../../../lib/methods/core/subscribeToTransactionsWithProofsFactory'); const DAPIClientError = require('../../../../lib/errors/DAPIClientError'); +const { hexToBytes } = require('../../../../lib/utils/bytes'); describe('subscribeToTransactionsWithProofsFactory', () => { let subscribeToTransactionsWithProofs; @@ -54,7 +55,7 @@ describe('subscribeToTransactionsWithProofsFactory', () => { request.setBloomFilter(bloomFilterMessage); request.setFromBlockHeight(options.fromBlockHeight); request.setCount(options.count); - request.setFromBlockHash(Buffer.from(options.fromBlockHash, 'hex')); + request.setFromBlockHash(hexToBytes(options.fromBlockHash)); expect(grpcTransportMock.request).to.be.calledOnceWithExactly( CorePromiseClient, diff --git a/packages/js-dapi-client/test/unit/methods/platform/getDataContract/GetDataContractResponse.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getDataContract/GetDataContractResponse.spec.js index 7ac39250489..2249c9e7dd9 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getDataContract/GetDataContractResponse.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getDataContract/GetDataContractResponse.spec.js @@ -41,7 +41,7 @@ describe('GetDataContractResponse', () => { it('should return proof', () => { getDataContractResponse = new GetDataContractResponseClass( - Buffer.alloc(0), + new Uint8Array(0), new Metadata(metadataFixture), new Proof(proofFixture), ); @@ -49,7 +49,7 @@ describe('GetDataContractResponse', () => { const dataContract = getDataContractResponse.getDataContract(); const proof = getDataContractResponse.getProof(); - expect(dataContract).to.deep.equal(Buffer.alloc(0)); + expect(dataContract).to.deep.equal(new Uint8Array(0)); expect(proof).to.be.an.instanceOf(Proof); expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); @@ -113,7 +113,7 @@ describe('GetDataContractResponse', () => { getDataContractResponse = GetDataContractResponseClass.createFromProto(proto); expect(getDataContractResponse).to.be.an.instanceOf(GetDataContractResponseClass); - expect(getDataContractResponse.getDataContract()).to.deep.equal(Buffer.alloc(0)); + expect(getDataContractResponse.getDataContract()).to.deep.equal(new Uint8Array(0)); expect(getDataContractResponse.getMetadata().getHeight()) .to.deep.equal(BigInt(metadataFixture.height)); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getDataContract/getDataContractFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getDataContract/getDataContractFactory.spec.js index 4d725d0256f..18bf80a492d 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getDataContract/getDataContractFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getDataContract/getDataContractFactory.spec.js @@ -118,7 +118,7 @@ describe('getDataContractFactory', () => { options, ]); - expect(result.getDataContract()).to.deep.equal(Buffer.alloc(0)); + expect(result.getDataContract()).to.deep.equal(new Uint8Array(0)); expect(result.getProof()).to.be.an.instanceOf(ProofClass); expect(result.getProof().getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); @@ -146,7 +146,7 @@ describe('getDataContractFactory', () => { const request = new GetDataContractRequest(); request.setV0( new GetDataContractRequestV0() - .setId(contractId.toBuffer()) + .setId(new Uint8Array(contractId.toBuffer())) .setProve(false), ); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getDataContractHistory/getDataContractHistoryFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getDataContractHistory/getDataContractHistoryFactory.spec.js index b8597e45c22..5bd2c30006b 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getDataContractHistory/getDataContractHistoryFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getDataContractHistory/getDataContractHistoryFactory.spec.js @@ -190,7 +190,7 @@ describe('getDataContractHistoryFactory', () => { const request = new GetDataContractHistoryRequest(); request.setV0( new GetDataContractHistoryRequestV0() - .setId(contractId.toBuffer()) + .setId(new Uint8Array(contractId.toBuffer())) .setLimit(new UInt32Value([10])) .setOffset(new UInt32Value([0])) .setStartAtMs('0') diff --git a/packages/js-dapi-client/test/unit/methods/platform/getDocuments/GetDocumentsResponse.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getDocuments/GetDocumentsResponse.spec.js index 777ceb112ff..afa2b6e92f9 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getDocuments/GetDocumentsResponse.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getDocuments/GetDocumentsResponse.spec.js @@ -28,7 +28,7 @@ describe('GetDocumentsResponse', () => { proofFixture = getProofFixture(); serializedDocuments = documentsFixture - .map((document) => Buffer.from(JSON.stringify(document))); + .map((document) => new TextEncoder().encode(JSON.stringify(document))); const { GetDocumentsResponseV0 } = GetDocumentsResponse; proto = new GetDocumentsResponse(); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getDocuments/getDocumentsFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getDocuments/getDocumentsFactory.spec.js index 02f99ae7fca..1486f0080b9 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getDocuments/getDocumentsFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getDocuments/getDocumentsFactory.spec.js @@ -53,7 +53,7 @@ describe('getDocumentsFactory', () => { documentsFixture = await getDocumentsFixture(); serializedDocuments = documentsFixture - .map((document) => Buffer.from(JSON.stringify(document))); + .map((document) => new TextEncoder().encode(JSON.stringify(document))); const metadata = new ResponseMetadata(); metadata.setHeight(metadataFixture.height); @@ -91,7 +91,7 @@ describe('getDocumentsFactory', () => { const request = new GetDocumentsRequest(); request.setV0( new GetDocumentsRequestV0() - .setDataContractId(contractIdBuffer) + .setDataContractId(new Uint8Array(contractIdBuffer)) .setDocumentType(type) .setLimit(options.limit) .setWhere(cbor.encode(options.where)) @@ -132,7 +132,7 @@ describe('getDocumentsFactory', () => { const request = new GetDocumentsRequest(); request.setV0( new GetDocumentsRequestV0() - .setDataContractId(contractIdBuffer) + .setDataContractId(new Uint8Array(contractIdBuffer)) .setDocumentType(type) .setLimit(options.limit) .setWhere(cbor.encode(options.where)) @@ -174,7 +174,7 @@ describe('getDocumentsFactory', () => { const request = new GetDocumentsRequest(); request.setV0( new GetDocumentsRequestV0() - .setDataContractId(contractIdBuffer) + .setDataContractId(new Uint8Array(contractIdBuffer)) .setDocumentType(type) .setLimit(options.limit) .setWhere(cbor.encode(options.where)) diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentitiesContractKeys/getIdentitiesContractKeysFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentitiesContractKeys/getIdentitiesContractKeysFactory.spec.js index 1dbb6f6be1a..13628f3277b 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentitiesContractKeys/getIdentitiesContractKeysFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentitiesContractKeys/getIdentitiesContractKeysFactory.spec.js @@ -113,9 +113,9 @@ describe('getIdentitiesContractKeysFactory', () => { new GetIdentitiesContractKeysRequestV0() .setProve(!!options.prove) .setIdentitiesIdsList( - [Buffer.from(identityFixtureA.getId()), Buffer.from(identityFixtureB.getId())], + [new Uint8Array(identityFixtureA.getId()), new Uint8Array(identityFixtureB.getId())], ) - .setContractId(Buffer.from(contractId)) + .setContractId(new Uint8Array(contractId)) .setPurposesList([KeyPurpose.ENCRYPTION, KeyPurpose.DECRYPTION]) .setDocumentTypeName('contactRequest'), ); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentity/GetIdentityResponse.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentity/GetIdentityResponse.spec.js index 788184965fa..8ed386e2a6d 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentity/GetIdentityResponse.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentity/GetIdentityResponse.spec.js @@ -58,7 +58,7 @@ describe('GetIdentityResponse', () => { it('should return proof', () => { getIdentityResponse = new GetIdentityResponseClass( - Buffer.alloc(0), + new Uint8Array(0), new Metadata(metadataFixture), new Proof(proofFixture), ); @@ -66,7 +66,7 @@ describe('GetIdentityResponse', () => { const identity = getIdentityResponse.getIdentity(); const proof = getIdentityResponse.getProof(); - expect(identity).to.deep.equal(Buffer.alloc(0)); + expect(identity).to.deep.equal(new Uint8Array(0)); expect(proof).to.be.an.instanceOf(Proof); expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); @@ -104,7 +104,7 @@ describe('GetIdentityResponse', () => { getIdentityResponse = GetIdentityResponseClass.createFromProto(proto); - expect(getIdentityResponse.getIdentity()).to.deep.equal(Buffer.alloc(0)); + expect(getIdentityResponse.getIdentity()).to.deep.equal(new Uint8Array(0)); expect(getIdentityResponse.getMetadata().getHeight()) .to.deep.equal(BigInt(metadataFixture.height)); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentity/getIdentityFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentity/getIdentityFactory.spec.js index 711ff8b16c3..f14e0644f14 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentity/getIdentityFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentity/getIdentityFactory.spec.js @@ -72,7 +72,7 @@ describe('getIdentityFactory', () => { const request = new GetIdentityRequest(); request.setV0( new GetIdentityRequestV0() - .setId(identityId.toBuffer()) + .setId(new Uint8Array(identityId.toBuffer())) .setProve(false), ); @@ -107,7 +107,7 @@ describe('getIdentityFactory', () => { const request = new GetIdentityRequest(); request.setV0( new GetIdentityRequestV0() - .setId(identityId.toBuffer()) + .setId(new Uint8Array(identityId.toBuffer())) .setProve(true), ); @@ -118,7 +118,7 @@ describe('getIdentityFactory', () => { options, ); - expect(result.getIdentity()).to.deep.equal(Buffer.alloc(0)); + expect(result.getIdentity()).to.deep.equal(new Uint8Array(0)); expect(result.getMetadata().getHeight()) .to.deep.equal(BigInt(metadataFixture.height)); @@ -145,7 +145,7 @@ describe('getIdentityFactory', () => { const request = new GetIdentityRequest(); request.setV0( new GetIdentityRequestV0() - .setId(identityId.toBuffer()) + .setId(new Uint8Array(identityId.toBuffer())) .setProve(false), ); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentityBalance/getIdentityBalanceFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentityBalance/getIdentityBalanceFactory.spec.js index daffa9f3cbc..c8c47a47891 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentityBalance/getIdentityBalanceFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentityBalance/getIdentityBalanceFactory.spec.js @@ -29,7 +29,7 @@ describe('getIdentityBalanceFactory', () => { beforeEach(async function beforeEach() { balance = BigInt(1337); - identityId = Buffer.alloc(32).fill(0); + identityId = new Uint8Array(32); metadataFixture = getMetadataFixture(); proofFixture = getProofFixture(); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentityByPublicKeyHash/GetIdentityByPublicKeyHashResponse.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentityByPublicKeyHash/GetIdentityByPublicKeyHashResponse.spec.js index 18545b0f54c..8b87598ab59 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentityByPublicKeyHash/GetIdentityByPublicKeyHashResponse.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentityByPublicKeyHash/GetIdentityByPublicKeyHashResponse.spec.js @@ -118,7 +118,7 @@ describe('GetIdentityByPublicKeyHashResponse', () => { expect(getIdentityResponse).to.be.an.instanceOf( GetIdentityByPublicKeyHashResponseClass, ); - expect(getIdentityResponse.getIdentity()).to.deep.equal(Buffer.alloc(0)); + expect(getIdentityResponse.getIdentity()).to.deep.equal(new Uint8Array(0)); expect(getIdentityResponse.getMetadata().getHeight()) .to.deep.equal(BigInt(metadataFixture.height)); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentityByPublicKeyHash/getIdentityByPublicKeyHashFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentityByPublicKeyHash/getIdentityByPublicKeyHashFactory.spec.js index 1a8514b375c..f38147513b6 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentityByPublicKeyHash/getIdentityByPublicKeyHashFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentityByPublicKeyHash/getIdentityByPublicKeyHashFactory.spec.js @@ -121,7 +121,7 @@ describe('getIdentityByPublicKeyHashFactory', () => { request, options, ); - expect(result.getIdentity()).to.deep.equal(Buffer.alloc(0)); + expect(result.getIdentity()).to.deep.equal(new Uint8Array(0)); expect(result.getMetadata().getHeight()) .to.deep.equal(BigInt(metadataFixture.height)); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentityContractNonce/GetIdentityContractNonce.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentityContractNonce/GetIdentityContractNonce.spec.js index 1d8065f2875..84e1b20a6ef 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentityContractNonce/GetIdentityContractNonce.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentityContractNonce/GetIdentityContractNonce.spec.js @@ -56,7 +56,7 @@ describe('GetIdentityContractNonceResponse', () => { it('should return proof', () => { getIdentityContractNonceResponse = new GetIdentityContractNonceResponseClass( - Buffer.alloc(0), + new Uint8Array(0), new Metadata(metadataFixture), new Proof(proofFixture), ); @@ -64,7 +64,7 @@ describe('GetIdentityContractNonceResponse', () => { const identityContractNonce = getIdentityContractNonceResponse.getIdentityContractNonce(); const proof = getIdentityContractNonceResponse.getProof(); - expect(identityContractNonce).to.deep.equal(Buffer.alloc(0)); + expect(identityContractNonce).to.deep.equal(new Uint8Array(0)); expect(proof).to.be.an.instanceOf(Proof); expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentityContractNonce/getIdentityContractNonceFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentityContractNonce/getIdentityContractNonceFactory.spec.js index 966820157a5..f5a82d8def7 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentityContractNonce/getIdentityContractNonceFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentityContractNonce/getIdentityContractNonceFactory.spec.js @@ -27,8 +27,8 @@ describe('getIdentityContractNonceFactory', () => { beforeEach(async function beforeEach() { nonce = BigInt(1); - identityId = Buffer.alloc(32).fill(0); - contractId = Buffer.alloc(32).fill(1); + identityId = new Uint8Array(32); + contractId = new Uint8Array(32).fill(1); metadataFixture = getMetadataFixture(); proofFixture = getProofFixture(); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentityKeys/GetIdentityKeys.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentityKeys/GetIdentityKeys.spec.js index 84e88aee243..f4e4dc0b3af 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentityKeys/GetIdentityKeys.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentityKeys/GetIdentityKeys.spec.js @@ -22,7 +22,7 @@ describe('GetIdentityKeysResponse', () => { beforeEach(async () => { metadataFixture = getMetadataFixture(); - keys = [Buffer.alloc(41).fill(1), Buffer.alloc(48).fill(2), Buffer.alloc(55).fill(3)]; + keys = [new Uint8Array(41).fill(1), new Uint8Array(48).fill(2), new Uint8Array(55).fill(3)]; proofFixture = getProofFixture(); const { GetIdentityKeysResponseV0 } = GetIdentityKeysResponse; @@ -58,7 +58,7 @@ describe('GetIdentityKeysResponse', () => { it('should return proof', () => { getIdentityKeysResponse = new GetIdentityKeysResponseClass( - Buffer.alloc(0), + new Uint8Array(0), new Metadata(metadataFixture), new Proof(proofFixture), ); @@ -66,7 +66,7 @@ describe('GetIdentityKeysResponse', () => { const identityKeys = getIdentityKeysResponse.getIdentityKeys(); const proof = getIdentityKeysResponse.getProof(); - expect(identityKeys).to.deep.equal(Buffer.alloc(0)); + expect(identityKeys).to.deep.equal(new Uint8Array(0)); expect(proof).to.be.an.instanceOf(Proof); expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentityKeys/getIdentityKeysFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentityKeys/getIdentityKeysFactory.spec.js index f9873c97a62..d56fcf3b965 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentityKeys/getIdentityKeysFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentityKeys/getIdentityKeysFactory.spec.js @@ -35,11 +35,11 @@ describe('getIdentityKeysFactory', () => { let proofResponse; beforeEach(async function beforeEach() { - keys = [Buffer.alloc(41), Buffer.alloc(46)]; + keys = [new Uint8Array(41), new Uint8Array(46)]; keyIds = [0, 1]; limit = 100; - identityId = Buffer.alloc(32).fill(0); + identityId = new Uint8Array(32); metadataFixture = getMetadataFixture(); proofFixture = getProofFixture(); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentityNonce/GetIdentityNonce.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentityNonce/GetIdentityNonce.spec.js index b79e8a27014..9efee085b83 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentityNonce/GetIdentityNonce.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentityNonce/GetIdentityNonce.spec.js @@ -56,7 +56,7 @@ describe('GetIdentityNonceResponse', () => { it('should return proof', () => { getIdentityNonceResponse = new GetIdentityNonceResponseClass( - Buffer.alloc(0), + new Uint8Array(0), new Metadata(metadataFixture), new Proof(proofFixture), ); @@ -64,7 +64,7 @@ describe('GetIdentityNonceResponse', () => { const IdentityNonce = getIdentityNonceResponse.getIdentityNonce(); const proof = getIdentityNonceResponse.getProof(); - expect(IdentityNonce).to.deep.equal(Buffer.alloc(0)); + expect(IdentityNonce).to.deep.equal(new Uint8Array(0)); expect(proof).to.be.an.instanceOf(Proof); expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getIdentityNonce/getIdentityNonceFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getIdentityNonce/getIdentityNonceFactory.spec.js index 564b7326da3..80237eb057e 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getIdentityNonce/getIdentityNonceFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getIdentityNonce/getIdentityNonceFactory.spec.js @@ -26,7 +26,7 @@ describe('getIdentityNonceFactory', () => { beforeEach(async function beforeEach() { nonce = BigInt(1); - identityId = Buffer.alloc(32).fill(0); + identityId = new Uint8Array(32); metadataFixture = getMetadataFixture(); proofFixture = getProofFixture(); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getProtocolVersionUpgradeVoteStatus/GetProtocolVersionUpgradeVoteStatusResponse.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getProtocolVersionUpgradeVoteStatus/GetProtocolVersionUpgradeVoteStatusResponse.spec.js index b6e6f887bd4..8ce67768cf7 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getProtocolVersionUpgradeVoteStatus/GetProtocolVersionUpgradeVoteStatusResponse.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getProtocolVersionUpgradeVoteStatus/GetProtocolVersionUpgradeVoteStatusResponse.spec.js @@ -13,6 +13,7 @@ const InvalidResponseError = require('../../../../../lib/methods/platform/respon const getProofFixture = require('../../../../../lib/test/fixtures/getProofFixture'); const Proof = require('../../../../../lib/methods/platform/response/Proof'); const Metadata = require('../../../../../lib/methods/platform/response/Metadata'); +const { bytesToHex, hexToBytes } = require('../../../../../lib/utils/bytes'); describe('GetProtocolVersionUpgradeVoteStatusResponse', () => { let getProtocolVersionUpgradeVoteStatus; @@ -23,7 +24,7 @@ describe('GetProtocolVersionUpgradeVoteStatusResponse', () => { beforeEach(async () => { metadataFixture = getMetadataFixture(); - versionSignalFixture = new VersionSignalClass(Buffer.alloc(32).toString('hex'), 1); + versionSignalFixture = new VersionSignalClass(bytesToHex(new Uint8Array(32)), 1); proofFixture = getProofFixture(); const { @@ -42,7 +43,7 @@ describe('GetProtocolVersionUpgradeVoteStatusResponse', () => { new GetProtocolVersionUpgradeVoteStatusResponseV0() .setVersions(new VersionSignals() .setVersionSignalsList([new VersionSignal() - .setProTxHash(Buffer.from(versionSignalFixture.getProTxHash(), 'hex')) + .setProTxHash(hexToBytes(versionSignalFixture.getProTxHash())) .setVersion(versionSignalFixture.getVersion()), ])) .setMetadata(metadata), @@ -125,9 +126,9 @@ describe('GetProtocolVersionUpgradeVoteStatusResponse', () => { const proof = getProtocolVersionUpgradeVoteStatus.getProof(); expect(proof).to.be.an.instanceOf(Proof); - expect(proof.getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); - expect(proof.getQuorumHash()).to.deep.equal(proofFixture.quorumHash); - expect(proof.getSignature()).to.deep.equal(proofFixture.signature); + expect(proof.getGrovedbProof()).to.deep.equal(new Uint8Array(proofFixture.merkleProof)); + expect(proof.getQuorumHash()).to.deep.equal(new Uint8Array(proofFixture.quorumHash)); + expect(proof.getSignature()).to.deep.equal(new Uint8Array(proofFixture.signature)); expect(proof.getRound()).to.deep.equal(proofFixture.round); }); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getProtocolVersionUpgradeVoteStatus/getProtocolVersionUpgradeVoteStatusFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getProtocolVersionUpgradeVoteStatus/getProtocolVersionUpgradeVoteStatusFactory.spec.js index ff36627aa02..c241a9871ec 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getProtocolVersionUpgradeVoteStatus/getProtocolVersionUpgradeVoteStatusFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getProtocolVersionUpgradeVoteStatus/getProtocolVersionUpgradeVoteStatusFactory.spec.js @@ -13,6 +13,7 @@ const VersionSignal = require('../../../../../lib/methods/platform/getProtocolVe const getMetadataFixture = require('../../../../../lib/test/fixtures/getMetadataFixture'); const getProofFixture = require('../../../../../lib/test/fixtures/getProofFixture'); const Proof = require('../../../../../lib/methods/platform/response/Proof'); +const { bytesToHex, hexToBytes } = require('../../../../../lib/utils/bytes'); describe('getProtocolVersionUpgradeVoteStatusFactory', () => { let grpcTransportMock; @@ -26,8 +27,8 @@ describe('getProtocolVersionUpgradeVoteStatusFactory', () => { let startProTxHash; beforeEach(async function beforeEach() { - startProTxHash = Buffer.alloc(32).fill('a').toString('hex'); - versionSignalFixture = new VersionSignal(Buffer.alloc(32).toString('hex'), 1); + startProTxHash = bytesToHex(new Uint8Array(32).fill(0x61)); + versionSignalFixture = new VersionSignal(bytesToHex(new Uint8Array(32)), 1); metadataFixture = getMetadataFixture(); proofFixture = getProofFixture(); @@ -50,7 +51,7 @@ describe('getProtocolVersionUpgradeVoteStatusFactory', () => { new GetProtocolVersionUpgradeVoteStatusResponseV0() .setVersions(new VersionSignals() .setVersionSignalsList([new VersionSignalProto() - .setProTxHash(Buffer.from(versionSignalFixture.getProTxHash(), 'hex')) + .setProTxHash(hexToBytes(versionSignalFixture.getProTxHash())) .setVersion(versionSignalFixture.getVersion())])) .setMetadata(metadata), ); @@ -84,7 +85,7 @@ describe('getProtocolVersionUpgradeVoteStatusFactory', () => { const request = new GetProtocolVersionUpgradeVoteStatusRequest(); request.setV0( new GetProtocolVersionUpgradeVoteStatusRequestV0() - .setStartProTxHash(Buffer.from(startProTxHash, 'hex')) + .setStartProTxHash(hexToBytes(startProTxHash)) .setCount(1) .setProve(!!options.prove), ); @@ -124,7 +125,7 @@ describe('getProtocolVersionUpgradeVoteStatusFactory', () => { const request = new GetProtocolVersionUpgradeVoteStatusRequest(); request.setV0( new GetProtocolVersionUpgradeVoteStatusRequestV0() - .setStartProTxHash(Buffer.from(startProTxHash, 'hex')) + .setStartProTxHash(hexToBytes(startProTxHash)) .setCount(1) .setProve(!!options.ascending), ); @@ -148,9 +149,9 @@ describe('getProtocolVersionUpgradeVoteStatusFactory', () => { .to.deep.equal(metadataFixture.protocolVersion); expect(result.getProof()).to.be.an.instanceOf(Proof); - expect(result.getProof().getGrovedbProof()).to.deep.equal(proofFixture.merkleProof); - expect(result.getProof().getQuorumHash()).to.deep.equal(proofFixture.quorumHash); - expect(result.getProof().getSignature()).to.deep.equal(proofFixture.signature); + expect(result.getProof().getGrovedbProof()).to.deep.equal(new Uint8Array(proofFixture.merkleProof)); + expect(result.getProof().getQuorumHash()).to.deep.equal(new Uint8Array(proofFixture.quorumHash)); + expect(result.getProof().getSignature()).to.deep.equal(new Uint8Array(proofFixture.signature)); expect(result.getProof().getRound()).to.deep.equal(proofFixture.round); }); @@ -165,7 +166,7 @@ describe('getProtocolVersionUpgradeVoteStatusFactory', () => { const request = new GetProtocolVersionUpgradeVoteStatusRequest(); request.setV0( new GetProtocolVersionUpgradeVoteStatusRequestV0() - .setStartProTxHash(Buffer.from(startProTxHash, 'hex')) + .setStartProTxHash(hexToBytes(startProTxHash)) .setCount(1) .setProve(!!options.ascending), ); diff --git a/packages/js-dapi-client/test/unit/methods/platform/getStatus/GetStatusResponse.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getStatus/GetStatusResponse.spec.js index 0639f6a3830..cb441388460 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getStatus/GetStatusResponse.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getStatus/GetStatusResponse.spec.js @@ -12,6 +12,7 @@ const ChainStatus = require('../../../../../lib/methods/platform/getStatus/Chain const NetworkStatus = require('../../../../../lib/methods/platform/getStatus/NetworkStatus'); const StateSyncStatus = require('../../../../../lib/methods/platform/getStatus/StateSyncStatus'); const TimeStatus = require('../../../../../lib/methods/platform/getStatus/TimeStatus'); +const { bytesToHex } = require('../../../../../lib/utils/bytes'); describe('GetStatusResponse', () => { let getStatusResponse; @@ -90,17 +91,17 @@ describe('GetStatusResponse', () => { ); const node = new NodeStatus( - Buffer.from(statusFixture.node.id).toString('hex'), - Buffer.from(statusFixture.node.proTxHash).toString('hex'), + bytesToHex(statusFixture.node.id), + bytesToHex(statusFixture.node.proTxHash), ); const chain = new ChainStatus( statusFixture.chain.catchingUp, - Buffer.from(statusFixture.chain.latestBlockHash).toString('hex'), - Buffer.from(statusFixture.chain.latestAppHash).toString('hex'), + bytesToHex(statusFixture.chain.latestBlockHash), + bytesToHex(statusFixture.chain.latestAppHash), BigInt(statusFixture.chain.latestBlockHeight), - Buffer.from(statusFixture.chain.earliestBlockHash).toString('hex'), - Buffer.from(statusFixture.chain.earliestAppHash).toString('hex'), + bytesToHex(statusFixture.chain.earliestBlockHash), + bytesToHex(statusFixture.chain.earliestAppHash), BigInt(statusFixture.chain.earliestBlockHeight), BigInt(statusFixture.chain.maxPeerBlockHeight), statusFixture.chain.coreChainLockedHeight, @@ -219,22 +220,22 @@ describe('GetStatusResponse', () => { .to.equal(statusFixture.version.protocol.drive.nextEpoch); expect(nodeStatus).to.be.an.instanceOf(NodeStatus); - expect(nodeStatus.getNodeId()).to.equal(Buffer.from(statusFixture.node.id).toString('hex')); - expect(nodeStatus.getProTxHash()).to.equal(Buffer.from(statusFixture.node.proTxHash).toString('hex')); + expect(nodeStatus.getNodeId()).to.equal(bytesToHex(statusFixture.node.id)); + expect(nodeStatus.getProTxHash()).to.equal(bytesToHex(statusFixture.node.proTxHash)); expect(chainStatus).to.be.an.instanceOf(ChainStatus); expect(chainStatus.isCatchingUp()) .to.equal(statusFixture.chain.catchingUp); expect(chainStatus.getLatestBlockHash()) - .to.equal(Buffer.from(statusFixture.chain.latestBlockHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.latestBlockHash)); expect(chainStatus.getLatestAppHash()) - .to.equal(Buffer.from(statusFixture.chain.latestAppHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.latestAppHash)); expect(chainStatus.getLatestBlockHeight()) .to.equal(BigInt(statusFixture.chain.latestBlockHeight)); expect(chainStatus.getEarliestBlockHash()) - .to.equal(Buffer.from(statusFixture.chain.earliestBlockHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.earliestBlockHash)); expect(chainStatus.getEarliestAppHash()) - .to.equal(Buffer.from(statusFixture.chain.earliestAppHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.earliestAppHash)); expect(chainStatus.getEarliestBlockHeight()) .to.equal(BigInt(statusFixture.chain.earliestBlockHeight)); expect(chainStatus.getMaxPeerBlockHeight()) @@ -297,22 +298,22 @@ describe('GetStatusResponse', () => { .to.equal(statusFixture.version.protocol.drive.latest); expect(nodeStatus).to.be.an.instanceOf(NodeStatus); - expect(nodeStatus.getNodeId()).to.equal(Buffer.from(statusFixture.node.id).toString('hex')); - expect(nodeStatus.getProTxHash()).to.equal(Buffer.from(statusFixture.node.proTxHash).toString('hex')); + expect(nodeStatus.getNodeId()).to.equal(bytesToHex(statusFixture.node.id)); + expect(nodeStatus.getProTxHash()).to.equal(bytesToHex(statusFixture.node.proTxHash)); expect(chainStatus).to.be.an.instanceOf(ChainStatus); expect(chainStatus.isCatchingUp()) .to.equal(statusFixture.chain.catchingUp); expect(chainStatus.getLatestBlockHash()) - .to.equal(Buffer.from(statusFixture.chain.latestBlockHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.latestBlockHash)); expect(chainStatus.getLatestAppHash()) - .to.equal(Buffer.from(statusFixture.chain.latestAppHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.latestAppHash)); expect(chainStatus.getLatestBlockHeight()) .to.equal(BigInt(statusFixture.chain.latestBlockHeight)); expect(chainStatus.getEarliestBlockHash()) - .to.equal(Buffer.from(statusFixture.chain.earliestBlockHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.earliestBlockHash)); expect(chainStatus.getEarliestAppHash()) - .to.equal(Buffer.from(statusFixture.chain.earliestAppHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.earliestAppHash)); expect(chainStatus.getEarliestBlockHeight()) .to.equal(BigInt(statusFixture.chain.earliestBlockHeight)); expect(chainStatus.getMaxPeerBlockHeight()) diff --git a/packages/js-dapi-client/test/unit/methods/platform/getStatus/getStatusFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/getStatus/getStatusFactory.spec.js index 40c91a9e4f5..63f69347b04 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/getStatus/getStatusFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/getStatus/getStatusFactory.spec.js @@ -14,6 +14,7 @@ const ChainStatus = require('../../../../../lib/methods/platform/getStatus/Chain const NetworkStatus = require('../../../../../lib/methods/platform/getStatus/NetworkStatus'); const StateSyncStatus = require('../../../../../lib/methods/platform/getStatus/StateSyncStatus'); const TimeStatus = require('../../../../../lib/methods/platform/getStatus/TimeStatus'); +const { bytesToHex } = require('../../../../../lib/utils/bytes'); describe('getStatusFactory', () => { let grpcTransportMock; @@ -131,22 +132,22 @@ describe('getStatusFactory', () => { .to.equal(statusFixture.version.protocol.drive.nextEpoch); expect(nodeStatus).to.be.an.instanceOf(NodeStatus); - expect(nodeStatus.getNodeId()).to.equal(Buffer.from(statusFixture.node.id).toString('hex')); - expect(nodeStatus.getProTxHash()).to.equal(Buffer.from(statusFixture.node.proTxHash).toString('hex')); + expect(nodeStatus.getNodeId()).to.equal(bytesToHex(statusFixture.node.id)); + expect(nodeStatus.getProTxHash()).to.equal(bytesToHex(statusFixture.node.proTxHash)); expect(chainStatus).to.be.an.instanceOf(ChainStatus); expect(chainStatus.isCatchingUp()) .to.equal(statusFixture.chain.catchingUp); expect(chainStatus.getLatestBlockHash()) - .to.equal(Buffer.from(statusFixture.chain.latestBlockHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.latestBlockHash)); expect(chainStatus.getLatestAppHash()) - .to.equal(Buffer.from(statusFixture.chain.latestAppHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.latestAppHash)); expect(chainStatus.getLatestBlockHeight()) .to.equal(BigInt(statusFixture.chain.latestBlockHeight)); expect(chainStatus.getEarliestBlockHash()) - .to.equal(Buffer.from(statusFixture.chain.earliestBlockHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.earliestBlockHash)); expect(chainStatus.getEarliestAppHash()) - .to.equal(Buffer.from(statusFixture.chain.earliestAppHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.earliestAppHash)); expect(chainStatus.getEarliestBlockHeight()) .to.equal(BigInt(statusFixture.chain.earliestBlockHeight)); expect(chainStatus.getMaxPeerBlockHeight()) @@ -227,22 +228,22 @@ describe('getStatusFactory', () => { .to.equal(statusFixture.version.protocol.drive.latest); expect(nodeStatus).to.be.an.instanceOf(NodeStatus); - expect(nodeStatus.getNodeId()).to.equal(Buffer.from(statusFixture.node.id).toString('hex')); + expect(nodeStatus.getNodeId()).to.equal(bytesToHex(statusFixture.node.id)); expect(nodeStatus.getProTxHash()).to.be.null(); expect(chainStatus).to.be.an.instanceOf(ChainStatus); expect(chainStatus.isCatchingUp()) .to.equal(statusFixture.chain.catchingUp); expect(chainStatus.getLatestBlockHash()) - .to.equal(Buffer.from(statusFixture.chain.latestBlockHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.latestBlockHash)); expect(chainStatus.getLatestAppHash()) - .to.equal(Buffer.from(statusFixture.chain.latestAppHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.latestAppHash)); expect(chainStatus.getLatestBlockHeight()) .to.equal(BigInt(statusFixture.chain.latestBlockHeight)); expect(chainStatus.getEarliestBlockHash()) - .to.equal(Buffer.from(statusFixture.chain.earliestBlockHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.earliestBlockHash)); expect(chainStatus.getEarliestAppHash()) - .to.equal(Buffer.from(statusFixture.chain.earliestAppHash).toString('hex')); + .to.equal(bytesToHex(statusFixture.chain.earliestAppHash)); expect(chainStatus.getEarliestBlockHeight()) .to.equal(BigInt(statusFixture.chain.earliestBlockHeight)); expect(chainStatus.getMaxPeerBlockHeight()) diff --git a/packages/js-dapi-client/test/unit/methods/platform/waitForStateTransitionResult/waitForStateTransitionResultFactory.spec.js b/packages/js-dapi-client/test/unit/methods/platform/waitForStateTransitionResult/waitForStateTransitionResultFactory.spec.js index 3f7d22932f7..fc9a3c3e012 100644 --- a/packages/js-dapi-client/test/unit/methods/platform/waitForStateTransitionResult/waitForStateTransitionResultFactory.spec.js +++ b/packages/js-dapi-client/test/unit/methods/platform/waitForStateTransitionResult/waitForStateTransitionResultFactory.spec.js @@ -13,6 +13,8 @@ const cbor = require('cbor'); const waitForStateTransitionResultFactory = require('../../../../../lib/methods/platform/waitForStateTransitionResult/waitForStateTransitionResultFactory'); const getMetadataFixture = require('../../../../../lib/test/fixtures/getMetadataFixture'); +const encoder = new TextEncoder(); + describe('waitForStateTransitionResultFactory', () => { let grpcTransportMock; let options; @@ -22,7 +24,7 @@ describe('waitForStateTransitionResultFactory', () => { let metadataFixture; beforeEach(function beforeEach() { - hash = Buffer.from('hash'); + hash = encoder.encode('hash'); metadataFixture = getMetadataFixture(); const metadata = new ResponseMetadata(); @@ -86,9 +88,9 @@ describe('waitForStateTransitionResultFactory', () => { it('should return response with proof', async () => { const proof = new Proof(); - proof.setGrovedbProof(Buffer.from('merkleProof')); - proof.setQuorumHash(Buffer.from('quorumHash')); - proof.setSignature(Buffer.from('signature')); + proof.setGrovedbProof(encoder.encode('merkleProof')); + proof.setQuorumHash(encoder.encode('quorumHash')); + proof.setSignature(encoder.encode('signature')); proof.setRound(42); response.getV0().setProof(proof); @@ -108,14 +110,14 @@ describe('waitForStateTransitionResultFactory', () => { expect(result.getError()).to.equal(undefined); expect(result.getProof()).to.be.deep.equal({ - merkleProof: Buffer.from('merkleProof'), - quorumHash: Buffer.from('quorumHash'), - signature: Buffer.from('signature'), + merkleProof: encoder.encode('merkleProof'), + quorumHash: encoder.encode('quorumHash'), + signature: encoder.encode('signature'), round: 42, }); - expect(result.getProof().getSignature()).to.deep.equal(Buffer.from('signature')); - expect(result.getProof().getGrovedbProof()).to.deep.equal(Buffer.from('merkleProof')); - expect(result.getProof().getQuorumHash()).to.deep.equal(Buffer.from('quorumHash')); + expect(result.getProof().getSignature()).to.deep.equal(encoder.encode('signature')); + expect(result.getProof().getGrovedbProof()).to.deep.equal(encoder.encode('merkleProof')); + expect(result.getProof().getQuorumHash()).to.deep.equal(encoder.encode('quorumHash')); expect(result.getProof().getRound()).to.deep.equal(42); const { WaitForStateTransitionResultRequestV0 } = WaitForStateTransitionResultRequest; @@ -161,7 +163,7 @@ describe('waitForStateTransitionResultFactory', () => { expect(result.getError()).to.be.deep.equal({ code: 2, message: 'Some error', - data: Buffer.from(data), + data: new Uint8Array(data), }); const { WaitForStateTransitionResultRequestV0 } = WaitForStateTransitionResultRequest;