(
signer: bitcoin.Signer | ECPairInterface,
opts: any = {}
)
| 14 | export type {TapLeafScript} from "bip174/src/lib/interfaces"; |
| 15 | |
| 16 | export function tweakSigner( |
| 17 | signer: bitcoin.Signer | ECPairInterface, |
| 18 | opts: any = {} |
| 19 | ): bitcoin.Signer { |
| 20 | // @ts-ignore |
| 21 | let privateKey: Uint8Array | undefined = signer.privateKey; |
| 22 | if (!privateKey) { |
| 23 | throw new Error("Private key is required for tweaking signer!"); |
| 24 | } |
| 25 | if (signer.publicKey[0] === 3) { |
| 26 | privateKey = ecc.privateNegate(privateKey); |
| 27 | } |
| 28 | |
| 29 | const tweakedPrivateKey = ecc.privateAdd( |
| 30 | privateKey, |
| 31 | tapTweakHash(toXOnly(signer.publicKey), opts.tweakHash) |
| 32 | ); |
| 33 | if (!tweakedPrivateKey) { |
| 34 | throw new Error("Invalid tweaked private key!"); |
| 35 | } |
| 36 | |
| 37 | return ECPair.fromPrivateKey(Buffer.from(tweakedPrivateKey), { |
| 38 | network: opts.network, |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | export function tapTweakHash(pubKey: Buffer, h: Buffer | undefined): Buffer { |
| 43 | return bitcoin.crypto.taggedHash( |
no test coverage detected