Skip to content

Commit

Permalink
Used bitcoinjs-lib for createOutputScriptFromAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Oct 3, 2023
1 parent 4c28279 commit a21fc6f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
17 changes: 13 additions & 4 deletions typescript/src/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import bcoin, { TX, Script } from "bcoin"
import bcoin, { TX } from "bcoin"
import bufio from "bufio"
import { BigNumber, utils } from "ethers"
import { Hex } from "./hex"
Expand Down Expand Up @@ -612,11 +612,20 @@ export function locktimeToNumber(locktimeLE: Buffer | string): number {

/**
* Creates the output script from the BTC address.
* @param address BTC address.
* @param bitcoinAddress Bitcoin address.
* @param bitcoinNetwork Bitcoin network.
* @returns The un-prefixed and not prepended with length output script.
*/
export function createOutputScriptFromAddress(address: string): Hex {
return Hex.from(Script.fromAddress(address).toRaw().toString("hex"))
export function createOutputScriptFromAddress(
bitcoinAddress: string,
bitcoinNetwork: BitcoinNetwork = BitcoinNetwork.Mainnet
): Hex {
return Hex.from(
address.toOutputScript(
bitcoinAddress,
toBitcoinJsLibNetwork(bitcoinNetwork)
)
)
}

/**
Expand Down
5 changes: 4 additions & 1 deletion typescript/src/deposit-refund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ export async function assembleDepositRefundTransaction(
utxo.outputIndex
)

const outputScript = createOutputScriptFromAddress(refunderAddress)
const outputScript = createOutputScriptFromAddress(
refunderAddress,
bitcoinNetwork
)
transaction.addOutput(outputScript.toBuffer(), outputValue.toNumber())

// In order to be able to spend the UTXO being refunded the transaction's
Expand Down
5 changes: 4 additions & 1 deletion typescript/src/deposit-sweep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ export async function assembleDepositSweepTransaction(
}
outputValue = outputValue.sub(fee)

const outputScript = createOutputScriptFromAddress(walletAddress)
const outputScript = createOutputScriptFromAddress(
walletAddress,
bitcoinNetwork
)
transaction.addOutput(outputScript.toBuffer(), outputValue.toNumber())

// Sign the main UTXO input if there is main UTXO.
Expand Down
12 changes: 10 additions & 2 deletions typescript/src/electrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ export class Client implements BitcoinClient {
): Promise<UnspentTransactionOutput[]> {
return this.withElectrum<UnspentTransactionOutput[]>(
async (electrum: Electrum) => {
const script = createOutputScriptFromAddress(address).toString()
const bitcoinNetwork = await this.getNetwork()
const script = createOutputScriptFromAddress(
address,
bitcoinNetwork
).toString()

// eslint-disable-next-line camelcase
type UnspentOutput = { tx_pos: number; value: number; tx_hash: string }
Expand Down Expand Up @@ -262,7 +266,11 @@ export class Client implements BitcoinClient {
limit?: number
): Promise<Transaction[]> {
return this.withElectrum<Transaction[]>(async (electrum: Electrum) => {
const script = createOutputScriptFromAddress(address).toString()
const bitcoinNetwork = await this.getNetwork()
const script = createOutputScriptFromAddress(
address,
bitcoinNetwork
).toString()

// eslint-disable-next-line camelcase
type HistoryItem = { height: number; tx_hash: string }
Expand Down
5 changes: 4 additions & 1 deletion typescript/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ export async function determineWalletMainUtxo(

// Get the wallet script based on the wallet address. This is required
// to find transaction outputs that lock funds on the wallet.
const walletScript = createOutputScriptFromAddress(walletAddress)
const walletScript = createOutputScriptFromAddress(
walletAddress,
bitcoinNetwork
)
const isWalletOutput = (output: TransactionOutput) =>
walletScript.equals(output.scriptPubKey)

Expand Down
8 changes: 6 additions & 2 deletions typescript/test/bitcoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,11 @@ describe("Bitcoin", () => {
).forEach(
([addressType, { address, scriptPubKey: expectedOutputScript }]) => {
it(`should create correct output script for ${addressType} address type`, () => {
const result = createOutputScriptFromAddress(address)
const network =
bitcoinNetwork === "mainnet"
? BitcoinNetwork.Mainnet
: BitcoinNetwork.Testnet
const result = createOutputScriptFromAddress(address, network)

expect(result.toString()).to.eq(expectedOutputScript.toString())
})
Expand All @@ -547,7 +551,7 @@ describe("Bitcoin", () => {
})
})

describe("getAddressFromScriptPubKey", () => {
describe("createAddressFromOutputScript", () => {
Object.keys(btcAddresses).forEach((bitcoinNetwork) => {
context(`with ${bitcoinNetwork} addresses`, () => {
Object.entries(
Expand Down
8 changes: 5 additions & 3 deletions typescript/test/data/redemption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "../../src/bitcoin"
import { RedemptionRequest } from "../../src/redemption"
import { Address } from "../../src/ethereum"
import { BitcoinTransaction, Hex } from "../../src"
import { BitcoinNetwork, BitcoinTransaction, Hex } from "../../src"
import { WalletState } from "../../src/wallet"

/**
Expand Down Expand Up @@ -723,7 +723,8 @@ export const findWalletForRedemptionData: {
outputIndex: 0,
value: BigNumber.from("791613461"),
scriptPubKey: createOutputScriptFromAddress(
"tb1qqwm566yn44rdlhgph8sw8vecta8uutg79afuja"
"tb1qqwm566yn44rdlhgph8sw8vecta8uutg79afuja",
BitcoinNetwork.Testnet
),
},
],
Expand Down Expand Up @@ -848,7 +849,8 @@ export const findWalletForRedemptionData: {
outputIndex: 0,
value: BigNumber.from("3370000"), // 0.0337 BTC
scriptPubKey: createOutputScriptFromAddress(
"tb1qx2xejtjltdcau5dpks8ucszkhxdg3fj88404lh"
"tb1qx2xejtjltdcau5dpks8ucszkhxdg3fj88404lh",
BitcoinNetwork.Testnet
),
},
],
Expand Down

0 comments on commit a21fc6f

Please sign in to comment.