Skip to content

Commit 0b8c68e

Browse files
refactor(dapi-client)!: expose Uint8Array instead of Buffer in public API
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).
1 parent 9812cf7 commit 0b8c68e

65 files changed

Lines changed: 324 additions & 228 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/js-dapi-client/lib/SimplifiedMasternodeListProvider/SimplifiedMasternodeListProvider.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const SimplifiedMNListDiff = require('@dashevo/dashcore-lib/lib/deterministicmnl
33
const cbor = require('cbor');
44

55
const logger = require('../logger');
6+
const { bytesToHex } = require('../utils/bytes');
67

78
class SimplifiedMasternodeListProvider {
89
/**
@@ -100,11 +101,11 @@ class SimplifiedMasternodeListProvider {
100101

101102
let simplifiedMNListDiff;
102103
let simplifiedMNListDiffObject;
103-
let simplifiedMNListDiffBuffer;
104+
let simplifiedMNListDiffBytes;
104105
try {
105-
simplifiedMNListDiffBuffer = Buffer.from(response.getMasternodeListDiff_asU8());
106+
simplifiedMNListDiffBytes = new Uint8Array(response.getMasternodeListDiff_asU8());
106107

107-
simplifiedMNListDiffObject = cbor.decodeFirstSync(simplifiedMNListDiffBuffer);
108+
simplifiedMNListDiffObject = cbor.decodeFirstSync(simplifiedMNListDiffBytes);
108109

109110
simplifiedMNListDiff = new SimplifiedMNListDiff(
110111
simplifiedMNListDiffObject,
@@ -118,7 +119,7 @@ class SimplifiedMasternodeListProvider {
118119
network: this.options.network,
119120
error: e,
120121
simplifiedMNListDiffObject,
121-
simplifiedMNListDiffBytes: simplifiedMNListDiffBuffer.toString('hex'),
122+
simplifiedMNListDiffBytes: bytesToHex(simplifiedMNListDiffBytes),
122123
},
123124
);
124125

packages/js-dapi-client/lib/methods/core/getBlockByHashFactory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function getBlockByHashFactory(grpcTransport) {
1515
* @typedef {getBlockByHash}
1616
* @param {string} hash
1717
* @param {DAPIClientOptions} [options]
18-
* @returns {Promise<null|Buffer>}
18+
* @returns {Promise<null|Uint8Array>}
1919
*/
2020
async function getBlockByHash(hash, options = {}) {
2121
const getBlockRequest = new GetBlockRequest();
@@ -29,7 +29,7 @@ function getBlockByHashFactory(grpcTransport) {
2929
);
3030
const blockBinaryArray = response.getBlock();
3131

32-
return Buffer.from(blockBinaryArray);
32+
return new Uint8Array(blockBinaryArray);
3333
}
3434

3535
return getBlockByHash;

packages/js-dapi-client/lib/methods/core/getBlockByHeightFactory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function getBlockByHeightFactory(grpcTransport) {
1515
* @typedef {getBlockByHeight}
1616
* @param {number} height
1717
* @param {DAPIClientOptions} [options]
18-
* @returns {Promise<null|Buffer>}
18+
* @returns {Promise<null|Uint8Array>}
1919
*/
2020
async function getBlockByHeight(height, options = {}) {
2121
const getBlockRequest = new GetBlockRequest();
@@ -30,7 +30,7 @@ function getBlockByHeightFactory(grpcTransport) {
3030

3131
const blockBinaryArray = response.getBlock();
3232

33-
return Buffer.from(blockBinaryArray);
33+
return new Uint8Array(blockBinaryArray);
3434
}
3535

3636
return getBlockByHeight;

packages/js-dapi-client/lib/methods/core/getBlockchainStatusFactory.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ function getBlockchainStatusFactory(grpcTransport) {
2929

3030
const responseObject = response.toObject();
3131

32-
// Respond with Buffers instead of base64 for binary fields
32+
// Respond with Uint8Arrays instead of base64 for binary fields
3333

3434
if (response.getChain()) {
3535
if (response.getChain()
3636
.getBestBlockHash()) {
37-
responseObject.chain.bestBlockHash = Buffer.from(response.getChain()
37+
responseObject.chain.bestBlockHash = new Uint8Array(response.getChain()
3838
.getBestBlockHash());
3939
}
4040

4141
if (response.getChain()
4242
.getChainWork()) {
43-
responseObject.chain.chainWork = Buffer.from(response.getChain()
43+
responseObject.chain.chainWork = new Uint8Array(response.getChain()
4444
.getChainWork());
4545
}
4646
}

packages/js-dapi-client/lib/methods/core/getMasternodeStatusFactory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
CorePromiseClient,
66
},
77
} = require('@dashevo/dapi-grpc');
8+
const { base64ToBytes } = require('../../utils/bytes');
89

910
/**
1011
* @param {GrpcTransport} grpcTransport
@@ -34,7 +35,7 @@ function getMasternodeStatusFactory(grpcTransport) {
3435
responseObject.status = Object.keys(GetMasternodeStatusResponse.Status)
3536
.find((key) => GetMasternodeStatusResponse.Status[key] === responseObject.status);
3637

37-
responseObject.proTxHash = Buffer.from(responseObject.proTxHash, 'base64');
38+
responseObject.proTxHash = base64ToBytes(responseObject.proTxHash);
3839

3940
return responseObject;
4041
}

packages/js-dapi-client/lib/methods/core/getTransaction/GetTransactionResponse.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class GetTransactionResponse {
44
/**
55
*
66
* @param {object} properties
7-
* @param {Buffer} properties.transaction
8-
* @param {Buffer} properties.blockHash
7+
* @param {Uint8Array} properties.transaction
8+
* @param {Uint8Array} properties.blockHash
99
* @param {number} properties.height
1010
* @param {number} properties.confirmations
1111
* @param {boolean} properties.isInstantLocked
@@ -22,15 +22,15 @@ class GetTransactionResponse {
2222

2323
/**
2424
* Get transaction
25-
* @returns {Buffer}
25+
* @returns {Uint8Array}
2626
*/
2727
getTransaction() {
2828
return this.transaction;
2929
}
3030

3131
/**
3232
* Get block hash
33-
* @returns {Buffer}
33+
* @returns {Uint8Array}
3434
*/
3535
getBlockHash() {
3636
return this.blockHash;
@@ -75,8 +75,8 @@ class GetTransactionResponse {
7575
}
7676

7777
return new GetTransactionResponse({
78-
transaction: Buffer.from(transactionBinaryArray),
79-
blockHash: Buffer.from(proto.getBlockHash()),
78+
transaction: new Uint8Array(transactionBinaryArray),
79+
blockHash: new Uint8Array(proto.getBlockHash()),
8080
height: proto.getHeight(),
8181
confirmations: proto.getConfirmations(),
8282
isInstantLocked: proto.getIsInstantLocked(),

packages/js-dapi-client/lib/methods/core/subscribeToBlockHeadersWithChainLocksFactory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
} = require('@dashevo/dapi-grpc');
77

88
const DAPIClientError = require('../../errors/DAPIClientError');
9+
const { hexToBytes } = require('../../utils/bytes');
910

1011
/**
1112
* @param {GrpcTransport} grpcTransport
@@ -41,7 +42,7 @@ function subscribeToBlockHeadersWithChainLocksFactory(grpcTransport) {
4142

4243
if (options.fromBlockHash) {
4344
request.setFromBlockHash(
44-
Buffer.from(options.fromBlockHash, 'hex'),
45+
hexToBytes(options.fromBlockHash),
4546
);
4647
}
4748

packages/js-dapi-client/lib/methods/core/subscribeToTransactionsWithProofsFactory.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
} = require('@dashevo/dapi-grpc');
88

99
const DAPIClientError = require('../../errors/DAPIClientError');
10+
const { hexToBytes } = require('../../utils/bytes');
1011

1112
/**
1213
* @param {GrpcTransport} grpcTransport
@@ -65,7 +66,7 @@ function subscribeToTransactionsWithProofsFactory(grpcTransport) {
6566

6667
if (options.fromBlockHash) {
6768
request.setFromBlockHash(
68-
Buffer.from(options.fromBlockHash, 'hex'),
69+
hexToBytes(options.fromBlockHash),
6970
);
7071
}
7172

packages/js-dapi-client/lib/methods/platform/getDataContract/GetDataContractResponse.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const InvalidResponseError = require('../response/errors/InvalidResponseError');
33

44
class GetDataContractResponse extends AbstractResponse {
55
/**
6-
* @param {Buffer} dataContract
6+
* @param {Uint8Array} dataContract
77
* @param {Metadata} metadata
88
* @param {Proof} [proof]
99
*/
@@ -14,7 +14,7 @@ class GetDataContractResponse extends AbstractResponse {
1414
}
1515

1616
/**
17-
* @returns {Buffer}
17+
* @returns {Uint8Array}
1818
*/
1919
getDataContract() {
2020
return this.dataContract;
@@ -33,7 +33,7 @@ class GetDataContractResponse extends AbstractResponse {
3333
}
3434

3535
return new GetDataContractResponse(
36-
Buffer.from(dataContract),
36+
new Uint8Array(dataContract),
3737
metadata,
3838
proof,
3939
);

packages/js-dapi-client/lib/methods/platform/getDataContract/getDataContractFactory.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ function getDataContractFactory(grpcTransport) {
1616
/**
1717
* Fetch Data Contract by id
1818
* @typedef {getDataContract}
19-
* @param {Buffer} contractId
19+
* @param {Uint8Array} contractId
2020
* @param {DAPIClientOptions & {prove: boolean}} [options]
2121
* @returns {Promise<GetDataContractResponse>}
2222
*/
2323
async function getDataContract(contractId, options = {}) {
2424
const { GetDataContractRequestV0 } = GetDataContractRequest;
2525
const getDataContractRequest = new GetDataContractRequest();
2626

27-
// need to convert objects inherited from Buffer to pure buffer as google protobuf
27+
// need to convert objects inherited from Uint8Array to pure Uint8Array as google protobuf
2828
// doesn't support extended buffers
2929
// https://github.com/protocolbuffers/protobuf/blob/master/js/binary/utils.js#L1049
30-
if (Buffer.isBuffer(contractId)) {
30+
if (contractId instanceof Uint8Array) {
3131
// eslint-disable-next-line no-param-reassign
32-
contractId = Buffer.from(contractId);
32+
contractId = new Uint8Array(contractId);
3333
}
3434

3535
getDataContractRequest.setV0(

0 commit comments

Comments
 (0)