Skip to content

Commit 4cad59c

Browse files
committed
refactor: move eccLib to PaymentOptions
1 parent ddc1e8d commit 4cad59c

File tree

6 files changed

+16
-17
lines changed

6 files changed

+16
-17
lines changed

src/payments/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export declare type PaymentFunction = () => Payment;
3434
export interface PaymentOpts {
3535
validate?: boolean;
3636
allowIncomplete?: boolean;
37+
eccLib?: TinySecp256k1Interface;
3738
}
3839
export declare type StackElement = Buffer | number;
3940
export declare type Stack = StackElement[];

src/payments/p2tr.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
import { TinySecp256k1Interface } from '../types';
21
import { Payment, PaymentOpts } from './index';
3-
export declare function p2tr(a: Payment, opts?: PaymentOpts, eccLib?: TinySecp256k1Interface): Payment;
2+
export declare function p2tr(a: Payment, opts?: PaymentOpts): Payment;

src/payments/p2tr.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const testecc_1 = require('./testecc');
1212
const OPS = bscript.OPS;
1313
const TAPROOT_VERSION = 0x01;
1414
const ANNEX_PREFIX = 0x50;
15-
function p2tr(a, opts, eccLib) {
15+
function p2tr(a, opts) {
1616
if (
1717
!a.address &&
1818
!a.output &&
@@ -24,9 +24,9 @@ function p2tr(a, opts, eccLib) {
2424
throw new TypeError('Not enough data');
2525
opts = Object.assign({ validate: true }, opts || {});
2626
const _ecc = lazy.value(() => {
27-
if (!eccLib) throw new Error('ECC Library is missing for p2tr.');
28-
(0, testecc_1.testEcc)(eccLib);
29-
return eccLib;
27+
if (!opts.eccLib) throw new Error('ECC Library is missing for p2tr.');
28+
(0, testecc_1.testEcc)(opts.eccLib);
29+
return opts.eccLib;
3030
});
3131
(0, types_1.typeforce)(
3232
{

test/payments.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import { TinySecp256k1Interface } from '../src/types';
2121
const fixtures = require('./fixtures/' + p);
2222

2323
fixtures.valid.forEach((f: any) => {
24+
const options = Object.assign({ eccLib }, f.options || {});
2425
it(f.description + ' as expected', () => {
2526
const args = u.preform(f.arguments);
26-
const actual = fn(args, f.options, eccLib);
27+
const actual = fn(args, options, eccLib);
2728

2829
u.equate(actual, f.expected, f.arguments);
2930
});
@@ -32,7 +33,7 @@ import { TinySecp256k1Interface } from '../src/types';
3233
const args = u.preform(f.arguments);
3334
const actual = fn(
3435
args,
35-
Object.assign({}, f.options, {
36+
Object.assign({}, options, {
3637
validate: false,
3738
}),
3839
eccLib,
@@ -43,6 +44,7 @@ import { TinySecp256k1Interface } from '../src/types';
4344
});
4445

4546
fixtures.invalid.forEach((f: any) => {
47+
const options = Object.assign({ eccLib }, f.options || {});
4648
it(
4749
'throws ' +
4850
f.exception +
@@ -51,7 +53,7 @@ import { TinySecp256k1Interface } from '../src/types';
5153
const args = u.preform(f.arguments);
5254

5355
assert.throws(() => {
54-
fn(args, f.options, eccLib);
56+
fn(args, options, eccLib);
5557
}, new RegExp(f.exception));
5658
},
5759
);

ts_src/payments/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type PaymentFunction = () => Payment;
4141
export interface PaymentOpts {
4242
validate?: boolean;
4343
allowIncomplete?: boolean;
44+
eccLib?: TinySecp256k1Interface;
4445
}
4546

4647
export type StackElement = Buffer | number;

ts_src/payments/p2tr.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ const OPS = bscript.OPS;
1818
const TAPROOT_VERSION = 0x01;
1919
const ANNEX_PREFIX = 0x50;
2020

21-
export function p2tr(
22-
a: Payment,
23-
opts?: PaymentOpts,
24-
eccLib?: TinySecp256k1Interface,
25-
): Payment {
21+
export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
2622
if (
2723
!a.address &&
2824
!a.output &&
@@ -36,10 +32,10 @@ export function p2tr(
3632
opts = Object.assign({ validate: true }, opts || {});
3733

3834
const _ecc = lazy.value(() => {
39-
if (!eccLib) throw new Error('ECC Library is missing for p2tr.');
35+
if (!opts!.eccLib) throw new Error('ECC Library is missing for p2tr.');
4036

41-
testEcc(eccLib);
42-
return eccLib;
37+
testEcc(opts!.eccLib);
38+
return opts!.eccLib;
4339
});
4440

4541
typef(

0 commit comments

Comments
 (0)