Skip to content

Commit b4d7760

Browse files
committed
chore: take the garbage out. Remove tweakSigner() and ecpair dep from PSBT
1 parent fdb8698 commit b4d7760

File tree

7 files changed

+4
-162
lines changed

7 files changed

+4
-162
lines changed

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as script from './script';
66
export { address, crypto, networks, payments, script };
77
export { Block } from './block';
88
export { TaggedHashPrefix } from './crypto';
9-
export { Psbt, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, tweakSigner, } from './psbt';
9+
export { Psbt, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, } from './psbt';
1010
export { OPS as opcodes } from './ops';
1111
export { Transaction } from './transaction';
1212
export { Network } from './networks';

src/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
3-
exports.Transaction = exports.opcodes = exports.tweakSigner = exports.Psbt = exports.Block = exports.script = exports.payments = exports.networks = exports.crypto = exports.address = void 0;
3+
exports.Transaction = exports.opcodes = exports.Psbt = exports.Block = exports.script = exports.payments = exports.networks = exports.crypto = exports.address = void 0;
44
const address = require('./address');
55
exports.address = address;
66
const crypto = require('./crypto');
@@ -25,12 +25,6 @@ Object.defineProperty(exports, 'Psbt', {
2525
return psbt_1.Psbt;
2626
},
2727
});
28-
Object.defineProperty(exports, 'tweakSigner', {
29-
enumerable: true,
30-
get: function() {
31-
return psbt_1.tweakSigner;
32-
},
33-
});
3428
var ops_1 = require('./ops');
3529
Object.defineProperty(exports, 'opcodes', {
3630
enumerable: true,

src/psbt.d.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/// <reference types="node" />
22
import { Psbt as PsbtBase } from 'bip174';
33
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate } from 'bip174/src/lib/interfaces';
4-
import { TinySecp256k1Interface as ECPairTinySecp256k1Interface } from 'ecpair';
54
import { Network } from './networks';
65
import { Transaction } from './transaction';
76
import { TinySecp256k1Interface } from './types';
@@ -109,17 +108,6 @@ export declare class Psbt {
109108
addUnknownKeyValToOutput(outputIndex: number, keyVal: KeyValue): this;
110109
clearFinalizedInput(inputIndex: number): this;
111110
}
112-
/**
113-
* Helper method for converting a normal Signer into a Taproot Signer.
114-
* Note that this helper method requires the Private Key of the Signer to be present.
115-
* Steps:
116-
* - if the Y coordinate of the Signer Public Key is odd then negate the Private Key
117-
* - tweak the private key with the provided hash (should be empty for key-path spending)
118-
* @param signer - a taproot signer object, the Private Key must be present
119-
* @param opts - tweak options
120-
* @returns a Signer having the Private and Public keys tweaked
121-
*/
122-
export declare function tweakSigner(signer: Signer, opts: TaprootSignerOpts): Signer;
123111
interface PsbtOptsOptional {
124112
network?: Network;
125113
maximumFeeRate?: number;
@@ -169,25 +157,11 @@ export interface HDSignerAsync extends HDSignerBase {
169157
}
170158
export interface Signer {
171159
publicKey: Buffer;
172-
/**
173-
* Private Key is optional, it is required only if the signer must be tweaked.
174-
* See the `tweakSigner()` method.
175-
*/
176-
privateKey?: Buffer;
177160
network?: any;
178161
sign(hash: Buffer, lowR?: boolean): Buffer;
179162
signSchnorr?(hash: Buffer): Buffer;
180163
getPublicKey?(): Buffer;
181164
}
182-
/**
183-
* Options for tweaking a Signer into a valid Taproot Signer
184-
*/
185-
export interface TaprootSignerOpts {
186-
network?: Network;
187-
eccLib: TinySecp256k1Interface & ECPairTinySecp256k1Interface;
188-
/** The hash used to tweak the Signer */
189-
tweakHash?: Buffer;
190-
}
191165
export interface SignerAsync {
192166
publicKey: Buffer;
193167
network?: any;

src/psbt.js

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
3-
exports.tweakSigner = exports.Psbt = void 0;
4-
const ecpair_1 = require('ecpair');
3+
exports.Psbt = void 0;
54
const bip174_1 = require('bip174');
65
const varuint = require('bip174/src/lib/converter/varint');
76
const utils_1 = require('bip174/src/lib/utils');
@@ -12,7 +11,6 @@ const networks_1 = require('./networks');
1211
const payments = require('./payments');
1312
const bscript = require('./script');
1413
const transaction_1 = require('./transaction');
15-
const taprootutils_1 = require('./payments/taprootutils');
1614
/**
1715
* These are the default arguments for a Psbt instance.
1816
*/
@@ -648,41 +646,6 @@ class Psbt {
648646
}
649647
}
650648
exports.Psbt = Psbt;
651-
/**
652-
* Helper method for converting a normal Signer into a Taproot Signer.
653-
* Note that this helper method requires the Private Key of the Signer to be present.
654-
* Steps:
655-
* - if the Y coordinate of the Signer Public Key is odd then negate the Private Key
656-
* - tweak the private key with the provided hash (should be empty for key-path spending)
657-
* @param signer - a taproot signer object, the Private Key must be present
658-
* @param opts - tweak options
659-
* @returns a Signer having the Private and Public keys tweaked
660-
*/
661-
function tweakSigner(signer, opts) {
662-
// todo: test ecc??
663-
let privateKey = signer.privateKey;
664-
if (!privateKey) {
665-
throw new Error('Private key is required for tweaking signer!');
666-
}
667-
if (signer.publicKey[0] === 3) {
668-
privateKey = opts.eccLib.privateNegate(privateKey);
669-
}
670-
const tweakedPrivateKey = opts.eccLib.privateAdd(
671-
privateKey,
672-
(0, taprootutils_1.tapTweakHash)(
673-
signer.publicKey.slice(1, 33),
674-
opts.tweakHash,
675-
),
676-
);
677-
if (!tweakedPrivateKey) {
678-
throw new Error('Invalid tweaked private key!');
679-
}
680-
const ECPair = (0, ecpair_1.ECPairFactory)(opts.eccLib);
681-
return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
682-
network: opts.network,
683-
});
684-
}
685-
exports.tweakSigner = tweakSigner;
686649
/**
687650
* This function is needed to pass to the bip174 base class's fromBuffer.
688651
* It takes the "transaction buffer" portion of the psbt buffer and returns a

test/psbt.spec.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@ import { describe, it } from 'mocha';
88
const bip32 = BIP32Factory(ecc);
99
const ECPair = ECPairFactory(ecc);
1010

11-
import {
12-
networks as NETWORKS,
13-
payments,
14-
Psbt,
15-
Signer,
16-
SignerAsync,
17-
tweakSigner,
18-
} from '..';
11+
import { networks as NETWORKS, payments, Psbt, Signer, SignerAsync } from '..';
1912

2013
import * as preFixtures from './fixtures/psbt.json';
2114

@@ -1013,35 +1006,6 @@ describe(`Psbt`, () => {
10131006
});
10141007
});
10151008

1016-
describe('tweakSigner', () => {
1017-
it('Throws error if signer is missing private key', () => {
1018-
const keyPair = Object.assign({}, ECPair.makeRandom(), {
1019-
privateKey: null,
1020-
});
1021-
assert.throws(() => {
1022-
tweakSigner(keyPair, { eccLib: ecc });
1023-
}, new RegExp('Private key is required for tweaking signer!'));
1024-
});
1025-
1026-
it('Correctly creates tweaked signer', () => {
1027-
const keyPair = ECPair.fromPrivateKey(
1028-
Buffer.from(
1029-
'accaf12e04e11b08fc28f5fe75b47ea663843b698981e31f1cafa2224d6e28c0',
1030-
'hex',
1031-
),
1032-
);
1033-
const tweakedSigner: Signer = tweakSigner(keyPair, { eccLib: ecc });
1034-
assert.strictEqual(
1035-
'029421e734b0f9d2c467ea7dd197c61acb4467cdcbc9f4cb0c571f8b63a5c40cae',
1036-
tweakedSigner.publicKey.toString('hex'),
1037-
);
1038-
assert.strictEqual(
1039-
'1853f5034982ec659e015873a0a958a73eac785850f425fd3444b12430d58692',
1040-
tweakedSigner.privateKey!.toString('hex'),
1041-
);
1042-
});
1043-
});
1044-
10451009
describe('create 1-to-1 transaction', () => {
10461010
const alice = ECPair.fromWIF(
10471011
'L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr',

ts_src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export {
1616
SignerAsync,
1717
HDSigner,
1818
HDSignerAsync,
19-
tweakSigner,
2019
} from './psbt';
2120
export { OPS as opcodes } from './ops';
2221
export { Transaction } from './transaction';

ts_src/psbt.ts

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { ECPairFactory } from 'ecpair';
2-
31
import { Psbt as PsbtBase } from 'bip174';
42
import * as varuint from 'bip174/src/lib/converter/varint';
53
import {
@@ -15,7 +13,6 @@ import {
1513
TransactionFromBuffer,
1614
} from 'bip174/src/lib/interfaces';
1715
import { checkForInput, checkForOutput } from 'bip174/src/lib/utils';
18-
import { TinySecp256k1Interface as ECPairTinySecp256k1Interface } from 'ecpair';
1916

2017
import { fromOutputScript, toOutputScript } from './address';
2118
import { cloneBuffer, reverseBuffer } from './bufferutils';
@@ -24,7 +21,6 @@ import { bitcoin as btcNetwork, Network } from './networks';
2421
import * as payments from './payments';
2522
import * as bscript from './script';
2623
import { Output, Transaction } from './transaction';
27-
import { tapTweakHash } from './payments/taprootutils';
2824
import { TinySecp256k1Interface } from './types';
2925

3026
export interface TransactionInput {
@@ -782,40 +778,6 @@ export class Psbt {
782778
}
783779
}
784780

785-
/**
786-
* Helper method for converting a normal Signer into a Taproot Signer.
787-
* Note that this helper method requires the Private Key of the Signer to be present.
788-
* Steps:
789-
* - if the Y coordinate of the Signer Public Key is odd then negate the Private Key
790-
* - tweak the private key with the provided hash (should be empty for key-path spending)
791-
* @param signer - a taproot signer object, the Private Key must be present
792-
* @param opts - tweak options
793-
* @returns a Signer having the Private and Public keys tweaked
794-
*/
795-
export function tweakSigner(signer: Signer, opts: TaprootSignerOpts): Signer {
796-
// todo: test ecc??
797-
let privateKey: Uint8Array | undefined = signer.privateKey;
798-
if (!privateKey) {
799-
throw new Error('Private key is required for tweaking signer!');
800-
}
801-
if (signer.publicKey[0] === 3) {
802-
privateKey = opts.eccLib.privateNegate(privateKey!);
803-
}
804-
805-
const tweakedPrivateKey = opts.eccLib.privateAdd(
806-
privateKey,
807-
tapTweakHash(signer.publicKey.slice(1, 33), opts.tweakHash),
808-
);
809-
if (!tweakedPrivateKey) {
810-
throw new Error('Invalid tweaked private key!');
811-
}
812-
813-
const ECPair = ECPairFactory(opts.eccLib);
814-
return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), {
815-
network: opts.network,
816-
});
817-
}
818-
819781
interface PsbtCache {
820782
__NON_WITNESS_UTXO_TX_CACHE: Transaction[];
821783
__NON_WITNESS_UTXO_BUF_CACHE: Buffer[];
@@ -889,25 +851,11 @@ export interface HDSignerAsync extends HDSignerBase {
889851

890852
export interface Signer {
891853
publicKey: Buffer;
892-
/**
893-
* Private Key is optional, it is required only if the signer must be tweaked.
894-
* See the `tweakSigner()` method.
895-
*/
896-
privateKey?: Buffer;
897854
network?: any;
898855
sign(hash: Buffer, lowR?: boolean): Buffer;
899856
signSchnorr?(hash: Buffer): Buffer;
900857
getPublicKey?(): Buffer;
901858
}
902-
/**
903-
* Options for tweaking a Signer into a valid Taproot Signer
904-
*/
905-
export interface TaprootSignerOpts {
906-
network?: Network;
907-
eccLib: TinySecp256k1Interface & ECPairTinySecp256k1Interface;
908-
/** The hash used to tweak the Signer */
909-
tweakHash?: Buffer;
910-
}
911859

912860
export interface SignerAsync {
913861
publicKey: Buffer;

0 commit comments

Comments
 (0)