Skip to content

Commit ed9457f

Browse files
committed
chore: linter changes
1 parent 6098b73 commit ed9457f

File tree

6 files changed

+130
-61
lines changed

6 files changed

+130
-61
lines changed

__tests__/nano_contracts/deserializer.test.ts

+50-14
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ test('Bool', () => {
1515
const deserializer = new Deserializer(new Network('testnet'));
1616

1717
const serializedFalse = serializer.serializeFromType(false, 'bool');
18-
const { value: deserializedFalse, bytesRead: bytesFalse } = deserializer.deserializeFromType(serializedFalse, 'bool');
18+
const { value: deserializedFalse, bytesRead: bytesFalse } = deserializer.deserializeFromType(
19+
serializedFalse,
20+
'bool'
21+
);
1922
expect(deserializedFalse).toStrictEqual(false);
2023
expect(bytesFalse).toStrictEqual(1);
2124

2225
const serializedTrue = serializer.serializeFromType(true, 'bool');
23-
const { value: deserializedTrue, bytesRead: bytesTrue } = deserializer.deserializeFromType(serializedTrue, 'bool');
26+
const { value: deserializedTrue, bytesRead: bytesTrue } = deserializer.deserializeFromType(
27+
serializedTrue,
28+
'bool'
29+
);
2430
expect(deserializedTrue).toStrictEqual(true);
2531
expect(bytesTrue).toStrictEqual(1);
2632
});
@@ -64,35 +70,44 @@ test('Bytes', () => {
6470

6571
const value = Buffer.from([0x74, 0x65, 0x73, 0x74]);
6672
const serialized = serializer.serializeFromType(value, 'bytes');
67-
const { value: deserialized, bytesRead: bytesReadBytes } = deserializer.deserializeFromType(serialized, 'bytes');
73+
const { value: deserialized, bytesRead: bytesReadBytes } = deserializer.deserializeFromType(
74+
serialized,
75+
'bytes'
76+
);
6877

6978
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
7079
expect(deserialized).toMatchBuffer(value);
7180
expect(bytesReadBytes).toStrictEqual(5); // 1 byte of length + 4 bytes of value
7281

7382
const serializedVertex = serializer.serializeFromType(value, 'VertexId');
74-
const { value: deserializedVertex, bytesRead: bytesReadVertex } = deserializer.deserializeFromType(serializedVertex, 'VertexId');
83+
const { value: deserializedVertex, bytesRead: bytesReadVertex } =
84+
deserializer.deserializeFromType(serializedVertex, 'VertexId');
7585

7686
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
7787
expect(deserializedVertex).toMatchBuffer(value);
7888
expect(bytesReadVertex).toStrictEqual(5); // 1 byte of length + 4 bytes of value
7989

8090
const serializedToken = serializer.serializeFromType(value, 'TokenUid');
81-
const { value: deserializedToken, bytesRead: bytesReadToken } = deserializer.deserializeFromType(serializedToken, 'TokenUid');
91+
const { value: deserializedToken, bytesRead: bytesReadToken } = deserializer.deserializeFromType(
92+
serializedToken,
93+
'TokenUid'
94+
);
8295

8396
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
8497
expect(deserializedToken).toMatchBuffer(value);
8598
expect(bytesReadToken).toStrictEqual(5); // 1 byte of length + 4 bytes of value
8699

87100
const serializedScript = serializer.serializeFromType(value, 'TxOutputScript');
88-
const { value: deserializedScript, bytesRead: bytesReadScript } = deserializer.deserializeFromType(serializedScript, 'TxOutputScript');
101+
const { value: deserializedScript, bytesRead: bytesReadScript } =
102+
deserializer.deserializeFromType(serializedScript, 'TxOutputScript');
89103

90104
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
91105
expect(deserializedScript).toMatchBuffer(value);
92106
expect(bytesReadScript).toStrictEqual(5); // 1 byte of length + 4 bytes of value
93107

94108
const serializedContract = serializer.serializeFromType(value, 'ContractId');
95-
const { value: deserializedContract, bytesRead: bytesReadContract } = deserializer.deserializeFromType(serializedContract, 'ContractId');
109+
const { value: deserializedContract, bytesRead: bytesReadContract } =
110+
deserializer.deserializeFromType(serializedContract, 'ContractId');
96111

97112
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
98113
expect(deserializedContract).toMatchBuffer(value);
@@ -105,7 +120,10 @@ test('Optional', () => {
105120

106121
const valueEmptyInt = null;
107122
const serializedEmptyInt = serializer.serializeFromType(valueEmptyInt, 'int?');
108-
const { value: deserializedEmptyInt } = deserializer.deserializeFromType(serializedEmptyInt, 'int?');
123+
const { value: deserializedEmptyInt } = deserializer.deserializeFromType(
124+
serializedEmptyInt,
125+
'int?'
126+
);
109127

110128
expect(deserializedEmptyInt).toBe(valueEmptyInt);
111129

@@ -117,7 +135,10 @@ test('Optional', () => {
117135

118136
const valueEmptyBool = null;
119137
const serializedEmptyBool = serializer.serializeFromType(valueEmptyBool, 'bool?');
120-
const { value: deserializedEmptyBool } = deserializer.deserializeFromType(serializedEmptyBool, 'bool?');
138+
const { value: deserializedEmptyBool } = deserializer.deserializeFromType(
139+
serializedEmptyBool,
140+
'bool?'
141+
);
121142

122143
expect(deserializedEmptyBool).toBe(valueEmptyBool);
123144

@@ -129,7 +150,10 @@ test('Optional', () => {
129150

130151
const valueEmptyStr = null;
131152
const serializedEmptyStr = serializer.serializeFromType(valueEmptyStr, 'str?');
132-
const { value: deserializedEmptyStr } = deserializer.deserializeFromType(serializedEmptyStr, 'str?');
153+
const { value: deserializedEmptyStr } = deserializer.deserializeFromType(
154+
serializedEmptyStr,
155+
'str?'
156+
);
133157

134158
expect(deserializedEmptyStr).toBe(valueEmptyStr);
135159

@@ -141,7 +165,10 @@ test('Optional', () => {
141165

142166
const valueEmptyBytes = null;
143167
const serializedEmptyBytes = serializer.serializeFromType(valueEmptyBytes, 'bytes?');
144-
const { value: deserializedEmptyBytes } = deserializer.deserializeFromType(serializedEmptyBytes, 'bytes?');
168+
const { value: deserializedEmptyBytes } = deserializer.deserializeFromType(
169+
serializedEmptyBytes,
170+
'bytes?'
171+
);
145172

146173
expect(deserializedEmptyBytes).toBe(valueEmptyBytes);
147174

@@ -159,19 +186,28 @@ test('Signed', () => {
159186

160187
const valueInt = '74657374,300,int';
161188
const serializedInt = serializer.serializeFromType(valueInt, 'SignedData[int]');
162-
const { value: deserializedInt } = deserializer.deserializeFromType(serializedInt, 'SignedData[int]');
189+
const { value: deserializedInt } = deserializer.deserializeFromType(
190+
serializedInt,
191+
'SignedData[int]'
192+
);
163193

164194
expect(deserializedInt).toBe(valueInt);
165195

166196
const valueStr = '74657374,test,str';
167197
const serializedStr = serializer.serializeFromType(valueStr, 'SignedData[str]');
168-
const { value: deserializedStr } = deserializer.deserializeFromType(serializedStr, 'SignedData[str]');
198+
const { value: deserializedStr } = deserializer.deserializeFromType(
199+
serializedStr,
200+
'SignedData[str]'
201+
);
169202

170203
expect(deserializedStr).toBe(valueStr);
171204

172205
const valueBytes = '74657374,74657374,bytes';
173206
const serializedBytes = serializer.serializeFromType(valueBytes, 'SignedData[bytes]');
174-
const { value: deserializedBytes } = deserializer.deserializeFromType(serializedBytes, 'SignedData[bytes]');
207+
const { value: deserializedBytes } = deserializer.deserializeFromType(
208+
serializedBytes,
209+
'SignedData[bytes]'
210+
);
175211

176212
expect(deserializedBytes).toBe(valueBytes);
177213

__tests__/nano_contracts/serializer.test.ts

+42-18
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ test('String', () => {
2525
const bigStr = Array(2048).fill('A').join('');
2626
const bigUtf8 = Buffer.from(bigStr, 'utf-8');
2727
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
28-
expect(serializer.fromString(bigStr)).toMatchBuffer(Buffer.concat([
29-
Buffer.from([0x80, 0x10]), // 2048 in unsigned leb128
30-
bigUtf8,
31-
]));
28+
expect(serializer.fromString(bigStr)).toMatchBuffer(
29+
Buffer.concat([
30+
Buffer.from([0x80, 0x10]), // 2048 in unsigned leb128
31+
bigUtf8,
32+
])
33+
);
3234
});
3335

3436
test('Int', () => {
@@ -40,23 +42,29 @@ test('Int', () => {
4042
test('Bytes', () => {
4143
const serializer = new Serializer();
4244
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
43-
expect(serializer.fromBytes(Buffer.from([0x74, 0x65, 0x73, 0x74]))).toMatchBuffer(Buffer.from([0x04, 0x74, 0x65, 0x73, 0x74]));
45+
expect(serializer.fromBytes(Buffer.from([0x74, 0x65, 0x73, 0x74]))).toMatchBuffer(
46+
Buffer.from([0x04, 0x74, 0x65, 0x73, 0x74])
47+
);
4448

4549
// Encoding a big string
4650
const bigBuffer = Buffer.from(Array(2048).fill('A').join(''), 'utf-8');
4751
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
48-
expect(serializer.fromBytes(bigBuffer)).toMatchBuffer(Buffer.concat([
49-
Buffer.from([0x80, 0x10]), // 2048 in unsigned leb128
50-
bigBuffer,
51-
]));
52+
expect(serializer.fromBytes(bigBuffer)).toMatchBuffer(
53+
Buffer.concat([
54+
Buffer.from([0x80, 0x10]), // 2048 in unsigned leb128
55+
bigBuffer,
56+
])
57+
);
5258
});
5359

5460
test('Optional', () => {
5561
const serializer = new Serializer();
5662
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
5763
expect(serializer.fromOptional(null, 'int')).toMatchBuffer(Buffer.from([0x00]));
5864
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
59-
expect(serializer.fromOptional(300, 'int')).toMatchBuffer(Buffer.from([0x01, 0x00, 0x00, 0x01, 0x2c]));
65+
expect(serializer.fromOptional(300, 'int')).toMatchBuffer(
66+
Buffer.from([0x01, 0x00, 0x00, 0x01, 0x2c])
67+
);
6068

6169
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
6270
expect(serializer.fromOptional(null, 'bool')).toMatchBuffer(Buffer.from([0x00]));
@@ -66,33 +74,49 @@ test('Optional', () => {
6674
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
6775
expect(serializer.fromOptional(null, 'str')).toMatchBuffer(Buffer.from([0x00]));
6876
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
69-
expect(serializer.fromOptional('test', 'str')).toMatchBuffer(Buffer.from([0x01, 0x04, 0x74, 0x65, 0x73, 0x74]));
77+
expect(serializer.fromOptional('test', 'str')).toMatchBuffer(
78+
Buffer.from([0x01, 0x04, 0x74, 0x65, 0x73, 0x74])
79+
);
7080

7181
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
7282
expect(serializer.fromOptional(null, 'bytes')).toMatchBuffer(Buffer.from([0x00]));
7383
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
74-
expect(serializer.fromOptional(Buffer.from([0x74, 0x65, 0x73, 0x74]), 'bytes')).toMatchBuffer(Buffer.from([0x01, 0x04, 0x74, 0x65, 0x73, 0x74]));
84+
expect(serializer.fromOptional(Buffer.from([0x74, 0x65, 0x73, 0x74]), 'bytes')).toMatchBuffer(
85+
Buffer.from([0x01, 0x04, 0x74, 0x65, 0x73, 0x74])
86+
);
7587
});
7688

7789
test('Signed', () => {
7890
const serializer = new Serializer();
7991
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
80-
expect(serializer.fromSigned('74657374,300,int')).toMatchBuffer(Buffer.from([0x00, 0x00, 0x01, 0x2c, 0x04, 0x74, 0x65, 0x73, 0x74]));
92+
expect(serializer.fromSigned('74657374,300,int')).toMatchBuffer(
93+
Buffer.from([0x00, 0x00, 0x01, 0x2c, 0x04, 0x74, 0x65, 0x73, 0x74])
94+
);
8195

8296
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
83-
expect(serializer.fromSigned('74657374,300,VarInt')).toMatchBuffer(Buffer.from([0xac, 0x02, 0x04, 0x74, 0x65, 0x73, 0x74]));
97+
expect(serializer.fromSigned('74657374,300,VarInt')).toMatchBuffer(
98+
Buffer.from([0xac, 0x02, 0x04, 0x74, 0x65, 0x73, 0x74])
99+
);
84100

85101
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
86-
expect(serializer.fromSigned('74657374,test,str')).toMatchBuffer(Buffer.from([0x04, 0x74, 0x65, 0x73, 0x74, 0x04, 0x74, 0x65, 0x73, 0x74]));
102+
expect(serializer.fromSigned('74657374,test,str')).toMatchBuffer(
103+
Buffer.from([0x04, 0x74, 0x65, 0x73, 0x74, 0x04, 0x74, 0x65, 0x73, 0x74])
104+
);
87105

88106
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
89-
expect(serializer.fromSigned('74657374,74657374,bytes')).toMatchBuffer(Buffer.from([0x04, 0x74, 0x65, 0x73, 0x74, 0x04, 0x74, 0x65, 0x73, 0x74]));
107+
expect(serializer.fromSigned('74657374,74657374,bytes')).toMatchBuffer(
108+
Buffer.from([0x04, 0x74, 0x65, 0x73, 0x74, 0x04, 0x74, 0x65, 0x73, 0x74])
109+
);
90110

91111
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
92-
expect(serializer.fromSigned('74657374,false,bool')).toMatchBuffer(Buffer.from([0x00, 0x04, 0x74, 0x65, 0x73, 0x74]));
112+
expect(serializer.fromSigned('74657374,false,bool')).toMatchBuffer(
113+
Buffer.from([0x00, 0x04, 0x74, 0x65, 0x73, 0x74])
114+
);
93115

94116
// @ts-expect-error: toMatchBuffer is defined in our setupTests.js so the type check fails.
95-
expect(serializer.fromSigned('74657374,true,bool')).toMatchBuffer(Buffer.from([0x01, 0x04, 0x74, 0x65, 0x73, 0x74]));
117+
expect(serializer.fromSigned('74657374,true,bool')).toMatchBuffer(
118+
Buffer.from([0x01, 0x04, 0x74, 0x65, 0x73, 0x74])
119+
);
96120
});
97121

98122
test('VarInt', () => {

src/nano_contracts/deserializer.ts

+25-11
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ class Deserializer {
3737
* @memberof Deserializer
3838
* @inner
3939
*/
40-
deserializeFromType(buf: Buffer, type: string): DeserializeResult<NanoContractArgumentType | null> {
40+
deserializeFromType(
41+
buf: Buffer,
42+
type: string
43+
): DeserializeResult<NanoContractArgumentType | null> {
4144
const isContainerType = getContainerType(type) !== null;
4245
if (isContainerType) {
4346
return this.deserializeContainerType(buf, type);
@@ -69,10 +72,13 @@ class Deserializer {
6972
}
7073
}
7174

72-
deserializeContainerType(buf: Buffer, type: string): DeserializeResult<NanoContractArgumentType | null> {
75+
deserializeContainerType(
76+
buf: Buffer,
77+
type: string
78+
): DeserializeResult<NanoContractArgumentType | null> {
7379
const [containerType, internalType] = getContainerInternalType(type);
7480

75-
switch(containerType) {
81+
switch (containerType) {
7682
case 'Optional':
7783
return this.toOptional(buf, internalType);
7884
case 'SignedData':
@@ -96,7 +102,11 @@ class Deserializer {
96102
// INFO: maxBytes is set to 3 becuase the max allowed length in bytes for a string is
97103
// NC_ARGS_MAX_BYTES_LENGTH which is encoded as 3 bytes in leb128 unsigned.
98104
// If we read a fourth byte we are definetely reading a higher number than allowed.
99-
const { value: lengthBN, rest, bytesRead: bytesReadForLength } = leb128Util.decodeUnsigned(buf, 3);
105+
const {
106+
value: lengthBN,
107+
rest,
108+
bytesRead: bytesReadForLength,
109+
} = leb128Util.decodeUnsigned(buf, 3);
100110
if (lengthBN > NC_ARGS_MAX_BYTES_LENGTH) {
101111
throw new Error('String length in bytes is higher than max allowed');
102112
}
@@ -123,8 +133,12 @@ class Deserializer {
123133
// INFO: maxBytes is set to 3 becuase the max allowed length in bytes for a string is
124134
// NC_ARGS_MAX_BYTES_LENGTH which is encoded as 3 bytes in leb128 unsigned.
125135
// If we read a fourth byte we are definetely reading a higher number than allowed.
126-
const { value: lengthBN, rest, bytesRead: bytesReadForLength } = leb128Util.decodeUnsigned(buf, 3);
127-
if(lengthBN > BigInt(NC_ARGS_MAX_BYTES_LENGTH)) {
136+
const {
137+
value: lengthBN,
138+
rest,
139+
bytesRead: bytesReadForLength,
140+
} = leb128Util.decodeUnsigned(buf, 3);
141+
if (lengthBN > BigInt(NC_ARGS_MAX_BYTES_LENGTH)) {
128142
throw new Error('String length in bytes is higher than max allowed');
129143
}
130144
// If lengthBN is lower than 64 KiB than its safe to convert to Number
@@ -273,14 +287,14 @@ class Deserializer {
273287
}
274288

275289
if (internalType === 'bool') {
276-
parsed = parsed as boolean ? 'true' : 'false';
290+
parsed = (parsed as boolean) ? 'true' : 'false';
277291
}
278292

279293
// Reading signature
280-
const {
281-
value: parsedSignature,
282-
bytesRead: bytesReadFromSignature,
283-
} = this.deserializeFromType(signedData.subarray(bytesReadFromValue), 'bytes');
294+
const { value: parsedSignature, bytesRead: bytesReadFromSignature } = this.deserializeFromType(
295+
signedData.subarray(bytesReadFromValue),
296+
'bytes'
297+
);
284298

285299
return {
286300
value: `${bufferToHex(parsedSignature as Buffer)},${parsed},${internalType}`,

src/nano_contracts/parser.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Address from '../models/address';
1010
import Network from '../models/network';
1111
import Deserializer from './deserializer';
1212
import ncApi from '../api/nano';
13-
import { unpackToInt } from '../utils/buffer';
1413
import { getAddressFromPubkey } from '../utils/address';
1514
import { NanoContractTransactionParseError } from '../errors';
1615
import { MethodArgInfo, NanoContractArgumentType, NanoContractParsedArgument } from './types';

src/nano_contracts/serializer.ts

+4-14
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {
9-
hexToBuffer,
10-
intToBytes,
11-
signedIntToBytes,
12-
bigIntToBytes,
13-
} from '../utils/buffer';
8+
import { hexToBuffer, intToBytes, signedIntToBytes, bigIntToBytes } from '../utils/buffer';
149
import { NanoContractArgumentType } from './types';
1510
import { OutputValueType } from '../types';
1611
import leb128Util from '../utils/leb128';
@@ -94,10 +89,7 @@ class Serializer {
9489
*/
9590
fromString(value: string): Buffer {
9691
const buf = Buffer.from(value, 'utf8');
97-
return Buffer.concat([
98-
leb128Util.encodeUnsigned(buf.length),
99-
buf,
100-
]);
92+
return Buffer.concat([leb128Util.encodeUnsigned(buf.length), buf]);
10193
}
10294

10395
/**
@@ -109,10 +101,7 @@ class Serializer {
109101
* @inner
110102
*/
111103
fromBytes(value: Buffer): Buffer {
112-
return Buffer.concat([
113-
leb128Util.encodeUnsigned(value.length),
114-
Buffer.from(value),
115-
]);
104+
return Buffer.concat([leb128Util.encodeUnsigned(value.length), Buffer.from(value)]);
116105
}
117106

118107
/**
@@ -218,6 +207,7 @@ class Serializer {
218207
value = BigInt(splittedValue[1]);
219208
} else {
220209
// For the other types
210+
// eslint-disable-next-line prefer-destructuring
221211
value = splittedValue[1];
222212
}
223213

0 commit comments

Comments
 (0)