Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace bcoin with bitcoinjs-lib for deposit-sweep #700

Merged
merged 34 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bbfa3c6
Added bitcoinjs-lib in version 6.0.2
tomaszslabon Sep 15, 2023
b9f8ce4
Build deposit script using bitcoinjs-lib
tomaszslabon Sep 18, 2023
85567ff
Added functionalities for checking input type
tomaszslabon Sep 18, 2023
f0c5dc2
Added basic structure for assembling deposit sweep transaction
tomaszslabon Sep 18, 2023
450579a
Added ecpair for handling keys
tomaszslabon Sep 18, 2023
53a4bbb
Added signing of P2SH and P2WSH inputs
tomaszslabon Sep 19, 2023
e609cee
Lint fixes
tomaszslabon Sep 20, 2023
9aa8323
Merge branch 'install-bitcoinjs-lib' into deposit-sweep-use-bitcoinjs…
tomaszslabon Sep 21, 2023
acc089c
Added signing of main UTXO
tomaszslabon Sep 21, 2023
8416229
Added check for mismatch between utxo and deposit values
tomaszslabon Sep 22, 2023
6a06c63
Added Bitcoin network argument
tomaszslabon Sep 25, 2023
dcf7314
Added address extraction from key pair
tomaszslabon Sep 25, 2023
e63197b
Added check for own UTXO
tomaszslabon Sep 25, 2023
da9856a
Updated unit tests for assembleDepositSweepTransaction
tomaszslabon Sep 25, 2023
4bc749f
Simplified signing inputs
tomaszslabon Sep 26, 2023
d1a8c9a
Added function descriptions
tomaszslabon Sep 26, 2023
b082b57
Merge branch 'main' into deposit-sweep-use-bitcoinjs-lib
lukasz-zimnoch Sep 27, 2023
bc969a9
Renamed functions for checking script type
tomaszslabon Sep 27, 2023
3fee283
Disabled linitng error
tomaszslabon Sep 27, 2023
d021ddc
Refactored address generating function
tomaszslabon Sep 27, 2023
770032c
Renamed UTXO value sum variable
tomaszslabon Sep 27, 2023
039b2a9
Moved input adding and output value calculation under the same loop
tomaszslabon Sep 27, 2023
536c989
Used createOutputScriptFromAddress
tomaszslabon Sep 27, 2023
59fb549
Key pair variable rename
tomaszslabon Sep 27, 2023
be1b1fe
Replaced network type with Bitcoin Network
tomaszslabon Sep 27, 2023
783a3bb
Passed whole previous output instead of splitting
tomaszslabon Sep 27, 2023
98b622a
Refactored deposit assembling
tomaszslabon Sep 27, 2023
e5c6ea2
Renamed output script variable
tomaszslabon Sep 27, 2023
6099e46
Refactored UTXO ownership validation function
tomaszslabon Sep 27, 2023
b0eac23
Refactor function checking if UTXO can be spent
tomaszslabon Sep 27, 2023
3fa34d9
Simplified script calculation for p2wpkh main UTXO
tomaszslabon Sep 27, 2023
bfb593a
Added unit tests for createAddressFromPublicKey
tomaszslabon Sep 27, 2023
96c204a
Added unit tests for toBitcoinJsLibNetwork
tomaszslabon Sep 27, 2023
a5ef9e8
Added unit tests for script type functions
tomaszslabon Sep 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
"bcoin": "git+https://github.com/keep-network/bcoin.git#5accd32c63e6025a0d35d67739c4a6e84095a1f8",
"bitcoinjs-lib": "6.0.2",
"bufio": "^1.0.6",
"ecpair": "^2.1.0",
"electrum-client-js": "git+https://github.com/keep-network/electrum-client-js.git#v0.1.1",
"ethers": "^5.5.2",
"p-timeout": "^4.1.0",
"tiny-secp256k1": "^2.2.3",
"wif": "2.0.6"
},
"devDependencies": {
Expand Down
24 changes: 24 additions & 0 deletions typescript/src/bitcoin-network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Hex } from "./hex"
import { networks } from "bitcoinjs-lib"

/**
* Bitcoin networks.
Expand Down Expand Up @@ -64,3 +65,26 @@ export function toBcoinNetwork(bitcoinNetwork: BitcoinNetwork): string {
}
}
}

/**
* Converts the provided {@link BitcoinNetwork} enumeration to a format expected
* by the `bitcoinjs-lib` library.
* @param bitcoinNetwork - Specified Bitcoin network.
* @returns Network representation compatible with the `bitcoinjs-lib` library.
* @throws An error if the network is not supported by `bitcoinjs-lib`.
*/
export function toBitcoinJsLibNetwork(
bitcoinNetwork: BitcoinNetwork
): networks.Network {
switch (bitcoinNetwork) {
case BitcoinNetwork.Mainnet: {
return networks.bitcoin
}
case BitcoinNetwork.Testnet: {
return networks.testnet
}
default: {
throw new Error(`network not supported`)
}
}
}
88 changes: 87 additions & 1 deletion typescript/src/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import wif from "wif"
import bufio from "bufio"
import { BigNumber, utils } from "ethers"
import { Hex } from "./hex"
import { BitcoinNetwork, toBcoinNetwork } from "./bitcoin-network"
import {
BitcoinNetwork,
toBcoinNetwork,
toBitcoinJsLibNetwork,
} from "./bitcoin-network"
import { payments } from "bitcoinjs-lib"

/**
* Represents a transaction hash (or transaction ID) as an un-prefixed hex
Expand Down Expand Up @@ -644,6 +649,31 @@ export function createAddressFromOutputScript(
?.toString(toBcoinNetwork(network))
}

/**
* Creates the Bitcoin address from the public key. Supports SegWit (P2WPKH) and
* Legacy (P2PKH) formats.
* @param publicKey - Public key used to derive the Bitcoin address.
* @param bitcoinNetwork - Target Bitcoin network.
* @param witness - Flag to determine address format: true for SegWit (P2WPKH)
* and false for Legacy (P2PKH). Default is true.
* @returns The derived Bitcoin address.
*/
export function createAddressFromPublicKey(
publicKey: Hex,
bitcoinNetwork: BitcoinNetwork,
witness: boolean = true
): string {
const network = toBitcoinJsLibNetwork(bitcoinNetwork)

if (witness) {
// P2WPKH (SegWit)
return payments.p2wpkh({ pubkey: publicKey.toBuffer(), network }).address!
} else {
// P2PKH (Legacy)
return payments.p2pkh({ pubkey: publicKey.toBuffer(), network }).address!
}
}

/**
* Reads the leading compact size uint from the provided variable length data.
*
Expand Down Expand Up @@ -683,3 +713,59 @@ export function readCompactSizeUint(varLenData: Hex): {
}
}
}

/**
* Checks if the provided script comes from a P2PKH input.
* @param script The script to be checked.
* @returns True if the script is P2PKH, false otherwise.
*/
export function isP2PKHScript(script: Buffer): boolean {
try {
payments.p2pkh({ output: script })
return true
} catch (err) {
return false
}
}

/**
* Checks if the provided script comes from a P2WPKH input.
* @param script The script to be checked.
* @returns True if the script is P2WPKH, false otherwise.
*/
export function isP2WPKHScript(script: Buffer): boolean {
try {
payments.p2wpkh({ output: script })
return true
} catch (err) {
return false
}
}

/**
* Checks if the provided script comes from a P2SH input.
* @param script The script to be checked.
* @returns True if the script is P2SH, false otherwise.
*/
export function isP2SHScript(script: Buffer): boolean {
try {
payments.p2sh({ output: script })
return true
} catch (err) {
return false
}
}

/**
* Checks if the provided script comes from a P2PKH input.
* @param script The script to be checked.
* @returns True if the script is P2WSH, false otherwise.
*/
export function isP2WSHScript(script: Buffer): boolean {
try {
payments.p2wsh({ output: script })
return true
} catch (err) {
return false
}
}
Loading