Skip to content

Commit

Permalink
Key pair variable rename
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Sep 27, 2023
1 parent 536c989 commit 59fb549
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions typescript/src/deposit-sweep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,12 @@ export async function assembleDepositSweepTransaction(

const network = toBitcoinJsLibNetwork(bitcoinNetwork)
// eslint-disable-next-line new-cap
const keyPair = ECPairFactory(tinysecp).fromWIF(walletPrivateKey, network)
const walletKeyPair = ECPairFactory(tinysecp).fromWIF(
walletPrivateKey,
network
)
const walletAddress = publicKeyToAddress(
Hex.from(keyPair.publicKey),
Hex.from(walletKeyPair.publicKey),
bitcoinNetwork,
witness
)
Expand Down Expand Up @@ -194,7 +197,7 @@ export async function assembleDepositSweepTransaction(
inputIndex,
previousOutput.script,
previousOutput.value,
keyPair,
walletKeyPair,
network
)
}
Expand All @@ -221,7 +224,7 @@ export async function assembleDepositSweepTransaction(
inputIndex,
deposit,
previousOutputValue,
keyPair
walletKeyPair
)
} else if (isP2WSHScript(previousOutputScript)) {
// P2WSH (deposit UTXO)
Expand All @@ -230,7 +233,7 @@ export async function assembleDepositSweepTransaction(
inputIndex,
deposit,
previousOutputValue,
keyPair
walletKeyPair
)
} else {
throw new Error("Unsupported UTXO script type")
Expand Down Expand Up @@ -259,7 +262,7 @@ export async function assembleDepositSweepTransaction(
* @param inputIndex - Index pointing to the input within the transaction.
* @param prevOutScript - The previous output script for the input.
* @param prevOutValue - The value from the previous transaction output.
* @param keyPair - A Signer object with the public and private key pair.
* @param walletKeyPair - A Signer object with the public and private key pair.
* @param network - The Bitcoin network type (mainnet or testnet).
* @returns An empty promise upon successful signing.
* @throws Error if the UTXO doesn't belong to the wallet, or if the script
Expand All @@ -270,10 +273,10 @@ async function signMainUtxoInput(
inputIndex: number,
prevOutScript: Buffer,
prevOutValue: number,
keyPair: Signer,
walletKeyPair: Signer,
network: networks.Network
) {
if (!ownsUtxo(keyPair, prevOutScript, network)) {
if (!ownsUtxo(walletKeyPair, prevOutScript, network)) {
throw new Error("UTXO does not belong to the wallet")
}

Expand All @@ -288,13 +291,13 @@ async function signMainUtxoInput(
)

const signature = script.signature.encode(
keyPair.sign(sigHash),
walletKeyPair.sign(sigHash),
sigHashType
)

const scriptSig = payments.p2pkh({
signature: signature,
pubkey: keyPair.publicKey,
pubkey: walletKeyPair.publicKey,
}).input!

transaction.ins[inputIndex].script = scriptSig
Expand Down Expand Up @@ -322,11 +325,11 @@ async function signMainUtxoInput(
)

const signature = script.signature.encode(
keyPair.sign(sigHash),
walletKeyPair.sign(sigHash),
sigHashType
)

transaction.ins[inputIndex].witness = [signature, keyPair.publicKey]
transaction.ins[inputIndex].witness = [signature, walletKeyPair.publicKey]
} else {
throw new Error("Unknown type of main UTXO")
}
Expand All @@ -338,20 +341,20 @@ async function signMainUtxoInput(
* @param inputIndex - Index pointing to the input within the transaction.
* @param deposit - Details of the deposit transaction.
* @param prevOutValue - The value from the previous transaction output.
* @param keyPair - A Signer object with the public and private key pair.
* @param walletKeyPair - A Signer object with the public and private key pair.
* @returns An empty promise upon successful signing.
*/
async function signP2SHDepositInput(
transaction: Transaction,
inputIndex: number,
deposit: Deposit,
prevOutValue: number,
keyPair: Signer
walletKeyPair: Signer
) {
const { walletPublicKey, depositScript } = await prepareInputSignData(
deposit,
prevOutValue,
keyPair
walletKeyPair
)

const sigHashType = Transaction.SIGHASH_ALL
Expand All @@ -362,7 +365,10 @@ async function signP2SHDepositInput(
sigHashType
)

const signature = script.signature.encode(keyPair.sign(sigHash), sigHashType)
const signature = script.signature.encode(
walletKeyPair.sign(sigHash),
sigHashType
)

const scriptSig: Stack = []
scriptSig.push(signature)
Expand All @@ -378,18 +384,18 @@ async function signP2SHDepositInput(
* @param inputIndex - Index pointing to the input within the transaction.
* @param deposit - Details of the deposit transaction.
* @param prevOutValue - The value from the previous transaction output.
* @param keyPair - A Signer object with the public and private key pair.
* @param walletKeyPair - A Signer object with the public and private key pair.
* @returns An empty promise upon successful signing.
*/
async function signP2WSHDepositInput(
transaction: Transaction,
inputIndex: number,
deposit: Deposit,
prevOutValue: number,
keyPair: Signer
walletKeyPair: Signer
) {
const { walletPublicKey, depositScript, previousOutputValue } =
await prepareInputSignData(deposit, prevOutValue, keyPair)
await prepareInputSignData(deposit, prevOutValue, walletKeyPair)

const sigHashType = Transaction.SIGHASH_ALL

Expand All @@ -400,7 +406,10 @@ async function signP2WSHDepositInput(
sigHashType
)

const signature = script.signature.encode(keyPair.sign(sigHash), sigHashType)
const signature = script.signature.encode(
walletKeyPair.sign(sigHash),
sigHashType
)

const witness: Buffer[] = []
witness.push(signature)
Expand Down

0 comments on commit 59fb549

Please sign in to comment.