Skip to content

Commit 582cfb4

Browse files
refactor: use getVertexHeaderIdFromBuffer method where possible
1 parent 38a7b97 commit 582cfb4

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/models/transaction.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { OutputValueType } from '../types';
3838
import type Header from '../headers/base';
3939
import NanoContractHeader from '../nano_contracts/header';
4040
import HeaderParser from '../headers/parser';
41+
import { getVertexHeaderIdFromBuffer } from '../headers/types';
4142

4243
enum txType {
4344
BLOCK = 'Block',
@@ -610,9 +611,8 @@ class Transaction {
610611
// so we must exhaust the buffer until it's empty
611612
// or we will throw an error
612613
while (buf.length > 0) {
613-
const headerId = buf.subarray(0, 1);
614-
const headerIdHex = headerId.toString('hex');
615-
const headerClass = HeaderParser.getHeader(headerIdHex);
614+
const headerId = getVertexHeaderIdFromBuffer(buf);
615+
const headerClass = HeaderParser.getHeader(headerId);
616616
let header;
617617
// eslint-disable-next-line prefer-const -- To split this declaration would be confusing
618618
[header, buf] = headerClass.deserialize(buf);

src/nano_contracts/header.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,17 @@ class NanoContractHeader extends Header {
151151
// Copies buffer locally, not to change the original parameter
152152
let buf = Buffer.from(srcBuf);
153153

154-
/* eslint-disable prefer-const -- To split these declarations would be confusing.
155-
* In all of them the first parameter should be a const and the second a let. */
156-
let headerId;
157-
[headerId, buf] = [buf.subarray(0, 1), buf.subarray(1)];
158-
159-
if (getVertexHeaderIdFromBuffer(headerId) !== VertexHeaderId.NANO_HEADER) {
154+
if (getVertexHeaderIdFromBuffer(buf) !== VertexHeaderId.NANO_HEADER) {
160155
throw new Error('Invalid vertex header id for nano header.');
161156
}
162157

158+
buf = buf.subarray(1);
159+
163160
// Create empty header to fill with the deserialization
164161
const header = new NanoContractHeader('', '', [], [], Buffer.from([]));
165162

163+
/* eslint-disable prefer-const -- To split these declarations would be confusing.
164+
* In all of them the first parameter should be a const and the second a let. */
166165
// nc info version
167166
[header.nc_info_version, buf] = unpackToInt(1, false, buf);
168167

0 commit comments

Comments
 (0)