Skip to content

Commit

Permalink
chore: review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nesopie authored and junderw committed Sep 8, 2024
1 parent 88fc490 commit 381bca1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 36 deletions.
14 changes: 6 additions & 8 deletions src/cjs/ecpair.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ exports.ECPairFactory = ECPairFactory;
const networks = __importStar(require('./networks.cjs'));
exports.networks = networks;
const types = __importStar(require('./types.cjs'));
// import randomBytes from 'randombytes';
const wif = __importStar(require('wif'));
const testecc_1 = require('./testecc.cjs');
const v = __importStar(require('valibot'));
Expand All @@ -58,15 +57,15 @@ const ECPairOptionsSchema = v.optional(
v.object({
compressed: v.optional(v.boolean()),
network: v.optional(types.NetworkSchema),
//https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
// https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
rng: v.optional(
v.pipe(
v.instance(Function),
v.transform((func) => {
return (arg) => {
const parsedArg = v.parse(v.optional(v.number()), arg);
const return_ = func(parsedArg);
const parsedReturn = v.parse(v.instance(Uint8Array), return_);
const returnedValue = func(parsedArg);
const parsedReturn = v.parse(v.instance(Uint8Array), returnedValue);
return parsedReturn;
};
}),
Expand All @@ -75,7 +74,7 @@ const ECPairOptionsSchema = v.optional(
}),
);
const toXOnly = (pubKey) =>
pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);
function ECPairFactory(ecc) {
(0, testecc_1.testEcc)(ecc);
function isPoint(maybePoint) {
Expand Down Expand Up @@ -142,8 +141,7 @@ function ECPairFactory(ecc) {
this.compressed =
options.compressed === undefined ? true : options.compressed;
this.network = options.network || networks.bitcoin;
if (__Q !== undefined)
this.__Q = Uint8Array.from(ecc.pointCompress(__Q, this.compressed));
if (__Q !== undefined) this.__Q = ecc.pointCompress(__Q, this.compressed);
}
get privateKey() {
return this.__D;
Expand Down Expand Up @@ -229,7 +227,7 @@ function ECPairFactory(ecc) {
: this.privateKey;
const tweakedPrivateKey = ecc.privateAdd(privateKey, t);
if (!tweakedPrivateKey) throw new Error('Invalid tweaked private key!');
return fromPrivateKey(Uint8Array.from(tweakedPrivateKey), {
return fromPrivateKey(tweakedPrivateKey, {
network: this.network,
compressed: this.compressed,
});
Expand Down
2 changes: 0 additions & 2 deletions src/cjs/ecpair.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ export interface Signer {
publicKey: Uint8Array;
network?: any;
sign(hash: Uint8Array, lowR?: boolean): Uint8Array;
getPublicKey?(): Uint8Array;
}
export interface SignerAsync {
publicKey: Uint8Array;
network?: any;
sign(hash: Uint8Array, lowR?: boolean): Promise<Uint8Array>;
getPublicKey?(): Uint8Array;
}
export interface ECPairInterface extends Signer {
compressed: boolean;
Expand Down
14 changes: 6 additions & 8 deletions src/esm/ecpair.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as networks from './networks';
import * as types from './types';
// import randomBytes from 'randombytes';
import * as wif from 'wif';
import { testEcc } from './testecc';
export { networks };
Expand All @@ -10,15 +9,15 @@ const ECPairOptionsSchema = v.optional(
v.object({
compressed: v.optional(v.boolean()),
network: v.optional(types.NetworkSchema),
//https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
// https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
rng: v.optional(
v.pipe(
v.instance(Function),
v.transform((func) => {
return (arg) => {
const parsedArg = v.parse(v.optional(v.number()), arg);
const return_ = func(parsedArg);
const parsedReturn = v.parse(v.instance(Uint8Array), return_);
const returnedValue = func(parsedArg);
const parsedReturn = v.parse(v.instance(Uint8Array), returnedValue);
return parsedReturn;
};
}),
Expand All @@ -27,7 +26,7 @@ const ECPairOptionsSchema = v.optional(
}),
);
const toXOnly = (pubKey) =>
pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);
export function ECPairFactory(ecc) {
testEcc(ecc);
function isPoint(maybePoint) {
Expand Down Expand Up @@ -94,8 +93,7 @@ export function ECPairFactory(ecc) {
this.compressed =
options.compressed === undefined ? true : options.compressed;
this.network = options.network || networks.bitcoin;
if (__Q !== undefined)
this.__Q = Uint8Array.from(ecc.pointCompress(__Q, this.compressed));
if (__Q !== undefined) this.__Q = ecc.pointCompress(__Q, this.compressed);
}
get privateKey() {
return this.__D;
Expand Down Expand Up @@ -181,7 +179,7 @@ export function ECPairFactory(ecc) {
: this.privateKey;
const tweakedPrivateKey = ecc.privateAdd(privateKey, t);
if (!tweakedPrivateKey) throw new Error('Invalid tweaked private key!');
return fromPrivateKey(Uint8Array.from(tweakedPrivateKey), {
return fromPrivateKey(tweakedPrivateKey, {
network: this.network,
compressed: this.compressed,
});
Expand Down
12 changes: 4 additions & 8 deletions test/ecpair.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,36 +304,32 @@ describe('ECPair', () => {
if (h === hash) {
assert.strictEqual(h, hash);
return signature;
} else {
return tinysecp.sign(h, d);
}
return tinysecp.sign(h, d);
};

const mockSignSchnorr = (h: any, d: any, e: any) => {
if (h === hash) {
assert.strictEqual(h, hash);
return signature;
} else {
return tinysecp.signSchnorr(h, d, e);
}
return tinysecp.signSchnorr(h, d, e);
};

const mockVerify = (h: any, Q: any, sig: any) => {
if (h === hash && sig === signature) {
assert.strictEqual(h, hash);
return true;
} else {
return tinysecp.verify(h, Q, sig);
}
return tinysecp.verify(h, Q, sig);
};

const mockVerifySchnorr = (h: any, Q: any, sig: any) => {
if (h === hash && sig === signature) {
assert.strictEqual(h, hash);
return true;
} else {
return tinysecp.verifySchnorr(h, Q, sig);
}
return tinysecp.verifySchnorr(h, Q, sig);
};

// @ts-ignore
Expand Down
16 changes: 6 additions & 10 deletions ts_src/ecpair.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Network } from './networks';
import * as networks from './networks';
import * as types from './types';
// import randomBytes from 'randombytes';
import * as wif from 'wif';
import { testEcc } from './testecc';
export { networks };
Expand All @@ -12,15 +11,15 @@ const ECPairOptionsSchema = v.optional(
v.object({
compressed: v.optional(v.boolean()),
network: v.optional(types.NetworkSchema),
//https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
// https://github.com/fabian-hiller/valibot/issues/243#issuecomment-2182514063
rng: v.optional(
v.pipe(
v.instance(Function),
v.transform((func) => {
return (arg?: number) => {
const parsedArg = v.parse(v.optional(v.number()), arg);
const return_ = func(parsedArg);
const parsedReturn = v.parse(v.instance(Uint8Array), return_);
const returnedValue = func(parsedArg);
const parsedReturn = v.parse(v.instance(Uint8Array), returnedValue);
return parsedReturn;
};
}),
Expand All @@ -32,20 +31,18 @@ const ECPairOptionsSchema = v.optional(
type ECPairOptions = v.InferOutput<typeof ECPairOptionsSchema>;

const toXOnly = (pubKey: Uint8Array) =>
pubKey.length === 32 ? pubKey : pubKey.slice(1, 33);
pubKey.length === 32 ? pubKey : pubKey.subarray(1, 33);

export interface Signer {
publicKey: Uint8Array;
network?: any;
sign(hash: Uint8Array, lowR?: boolean): Uint8Array;
getPublicKey?(): Uint8Array;
}

export interface SignerAsync {
publicKey: Uint8Array;
network?: any;
sign(hash: Uint8Array, lowR?: boolean): Promise<Uint8Array>;
getPublicKey?(): Uint8Array;
}

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

if (__Q !== undefined)
this.__Q = Uint8Array.from(ecc.pointCompress(__Q, this.compressed));
if (__Q !== undefined) this.__Q = ecc.pointCompress(__Q, this.compressed);
}

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

return fromPrivateKey(Uint8Array.from(tweakedPrivateKey), {
return fromPrivateKey(tweakedPrivateKey, {
network: this.network,
compressed: this.compressed,
});
Expand Down

0 comments on commit 381bca1

Please sign in to comment.