Skip to content

Commit 381bca1

Browse files
Nesopiejunderw
authored andcommitted
chore: review fixes
1 parent 88fc490 commit 381bca1

File tree

5 files changed

+22
-36
lines changed

5 files changed

+22
-36
lines changed

src/cjs/ecpair.cjs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ exports.ECPairFactory = ECPairFactory;
4949
const networks = __importStar(require('./networks.cjs'));
5050
exports.networks = networks;
5151
const types = __importStar(require('./types.cjs'));
52-
// import randomBytes from 'randombytes';
5352
const wif = __importStar(require('wif'));
5453
const testecc_1 = require('./testecc.cjs');
5554
const v = __importStar(require('valibot'));
@@ -58,15 +57,15 @@ const ECPairOptionsSchema = v.optional(
5857
v.object({
5958
compressed: v.optional(v.boolean()),
6059
network: v.optional(types.NetworkSchema),
61-
//https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
60+
// https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
6261
rng: v.optional(
6362
v.pipe(
6463
v.instance(Function),
6564
v.transform((func) => {
6665
return (arg) => {
6766
const parsedArg = v.parse(v.optional(v.number()), arg);
68-
const return_ = func(parsedArg);
69-
const parsedReturn = v.parse(v.instance(Uint8Array), return_);
67+
const returnedValue = func(parsedArg);
68+
const parsedReturn = v.parse(v.instance(Uint8Array), returnedValue);
7069
return parsedReturn;
7170
};
7271
}),
@@ -75,7 +74,7 @@ const ECPairOptionsSchema = v.optional(
7574
}),
7675
);
7776
const toXOnly = (pubKey) =>
78-
pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
77+
pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);
7978
function ECPairFactory(ecc) {
8079
(0, testecc_1.testEcc)(ecc);
8180
function isPoint(maybePoint) {
@@ -142,8 +141,7 @@ function ECPairFactory(ecc) {
142141
this.compressed =
143142
options.compressed === undefined ? true : options.compressed;
144143
this.network = options.network || networks.bitcoin;
145-
if (__Q !== undefined)
146-
this.__Q = Uint8Array.from(ecc.pointCompress(__Q, this.compressed));
144+
if (__Q !== undefined) this.__Q = ecc.pointCompress(__Q, this.compressed);
147145
}
148146
get privateKey() {
149147
return this.__D;
@@ -229,7 +227,7 @@ function ECPairFactory(ecc) {
229227
: this.privateKey;
230228
const tweakedPrivateKey = ecc.privateAdd(privateKey, t);
231229
if (!tweakedPrivateKey) throw new Error('Invalid tweaked private key!');
232-
return fromPrivateKey(Uint8Array.from(tweakedPrivateKey), {
230+
return fromPrivateKey(tweakedPrivateKey, {
233231
network: this.network,
234232
compressed: this.compressed,
235233
});

src/cjs/ecpair.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ export interface Signer {
2222
publicKey: Uint8Array;
2323
network?: any;
2424
sign(hash: Uint8Array, lowR?: boolean): Uint8Array;
25-
getPublicKey?(): Uint8Array;
2625
}
2726
export interface SignerAsync {
2827
publicKey: Uint8Array;
2928
network?: any;
3029
sign(hash: Uint8Array, lowR?: boolean): Promise<Uint8Array>;
31-
getPublicKey?(): Uint8Array;
3230
}
3331
export interface ECPairInterface extends Signer {
3432
compressed: boolean;

src/esm/ecpair.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as networks from './networks';
22
import * as types from './types';
3-
// import randomBytes from 'randombytes';
43
import * as wif from 'wif';
54
import { testEcc } from './testecc';
65
export { networks };
@@ -10,15 +9,15 @@ const ECPairOptionsSchema = v.optional(
109
v.object({
1110
compressed: v.optional(v.boolean()),
1211
network: v.optional(types.NetworkSchema),
13-
//https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
12+
// https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
1413
rng: v.optional(
1514
v.pipe(
1615
v.instance(Function),
1716
v.transform((func) => {
1817
return (arg) => {
1918
const parsedArg = v.parse(v.optional(v.number()), arg);
20-
const return_ = func(parsedArg);
21-
const parsedReturn = v.parse(v.instance(Uint8Array), return_);
19+
const returnedValue = func(parsedArg);
20+
const parsedReturn = v.parse(v.instance(Uint8Array), returnedValue);
2221
return parsedReturn;
2322
};
2423
}),
@@ -27,7 +26,7 @@ const ECPairOptionsSchema = v.optional(
2726
}),
2827
);
2928
const toXOnly = (pubKey) =>
30-
pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
29+
pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);
3130
export function ECPairFactory(ecc) {
3231
testEcc(ecc);
3332
function isPoint(maybePoint) {
@@ -94,8 +93,7 @@ export function ECPairFactory(ecc) {
9493
this.compressed =
9594
options.compressed === undefined ? true : options.compressed;
9695
this.network = options.network || networks.bitcoin;
97-
if (__Q !== undefined)
98-
this.__Q = Uint8Array.from(ecc.pointCompress(__Q, this.compressed));
96+
if (__Q !== undefined) this.__Q = ecc.pointCompress(__Q, this.compressed);
9997
}
10098
get privateKey() {
10199
return this.__D;
@@ -181,7 +179,7 @@ export function ECPairFactory(ecc) {
181179
: this.privateKey;
182180
const tweakedPrivateKey = ecc.privateAdd(privateKey, t);
183181
if (!tweakedPrivateKey) throw new Error('Invalid tweaked private key!');
184-
return fromPrivateKey(Uint8Array.from(tweakedPrivateKey), {
182+
return fromPrivateKey(tweakedPrivateKey, {
185183
network: this.network,
186184
compressed: this.compressed,
187185
});

test/ecpair.spec.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,36 +304,32 @@ describe('ECPair', () => {
304304
if (h === hash) {
305305
assert.strictEqual(h, hash);
306306
return signature;
307-
} else {
308-
return tinysecp.sign(h, d);
309307
}
308+
return tinysecp.sign(h, d);
310309
};
311310

312311
const mockSignSchnorr = (h: any, d: any, e: any) => {
313312
if (h === hash) {
314313
assert.strictEqual(h, hash);
315314
return signature;
316-
} else {
317-
return tinysecp.signSchnorr(h, d, e);
318315
}
316+
return tinysecp.signSchnorr(h, d, e);
319317
};
320318

321319
const mockVerify = (h: any, Q: any, sig: any) => {
322320
if (h === hash && sig === signature) {
323321
assert.strictEqual(h, hash);
324322
return true;
325-
} else {
326-
return tinysecp.verify(h, Q, sig);
327323
}
324+
return tinysecp.verify(h, Q, sig);
328325
};
329326

330327
const mockVerifySchnorr = (h: any, Q: any, sig: any) => {
331328
if (h === hash && sig === signature) {
332329
assert.strictEqual(h, hash);
333330
return true;
334-
} else {
335-
return tinysecp.verifySchnorr(h, Q, sig);
336331
}
332+
return tinysecp.verifySchnorr(h, Q, sig);
337333
};
338334

339335
// @ts-ignore

ts_src/ecpair.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Network } from './networks';
22
import * as networks from './networks';
33
import * as types from './types';
4-
// import randomBytes from 'randombytes';
54
import * as wif from 'wif';
65
import { testEcc } from './testecc';
76
export { networks };
@@ -12,15 +11,15 @@ const ECPairOptionsSchema = v.optional(
1211
v.object({
1312
compressed: v.optional(v.boolean()),
1413
network: v.optional(types.NetworkSchema),
15-
//https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
14+
// https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
1615
rng: v.optional(
1716
v.pipe(
1817
v.instance(Function),
1918
v.transform((func) => {
2019
return (arg?: number) => {
2120
const parsedArg = v.parse(v.optional(v.number()), arg);
22-
const return_ = func(parsedArg);
23-
const parsedReturn = v.parse(v.instance(Uint8Array), return_);
21+
const returnedValue = func(parsedArg);
22+
const parsedReturn = v.parse(v.instance(Uint8Array), returnedValue);
2423
return parsedReturn;
2524
};
2625
}),
@@ -32,20 +31,18 @@ const ECPairOptionsSchema = v.optional(
3231
type ECPairOptions = v.InferOutput<typeof ECPairOptionsSchema>;
3332

3433
const toXOnly = (pubKey: Uint8Array) =>
35-
pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
34+
pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);
3635

3736
export interface Signer {
3837
publicKey: Uint8Array;
3938
network?: any;
4039
sign(hash: Uint8Array, lowR?: boolean): Uint8Array;
41-
getPublicKey?(): Uint8Array;
4240
}
4341

4442
export interface SignerAsync {
4543
publicKey: Uint8Array;
4644
network?: any;
4745
sign(hash: Uint8Array, lowR?: boolean): Promise<Uint8Array>;
48-
getPublicKey?(): Uint8Array;
4946
}
5047

5148
export interface ECPairInterface extends Signer {
@@ -190,8 +187,7 @@ export function ECPairFactory(ecc: TinySecp256k1Interface): ECPairAPI {
190187
options.compressed === undefined ? true : options.compressed;
191188
this.network = options.network || networks.bitcoin;
192189

193-
if (__Q !== undefined)
194-
this.__Q = Uint8Array.from(ecc.pointCompress(__Q, this.compressed));
190+
if (__Q !== undefined) this.__Q = ecc.pointCompress(__Q, this.compressed);
195191
}
196192

197193
get privateKey(): Uint8Array | undefined {
@@ -290,7 +286,7 @@ export function ECPairFactory(ecc: TinySecp256k1Interface): ECPairAPI {
290286
const tweakedPrivateKey = ecc.privateAdd(privateKey!, t);
291287
if (!tweakedPrivateKey) throw new Error('Invalid tweaked private key!');
292288

293-
return fromPrivateKey(Uint8Array.from(tweakedPrivateKey), {
289+
return fromPrivateKey(tweakedPrivateKey, {
294290
network: this.network,
295291
compressed: this.compressed,
296292
});

0 commit comments

Comments
 (0)