Skip to content

Commit

Permalink
Used bitcoinjs-lib for encodeToBitcoinAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Oct 3, 2023
1 parent 41b5c7d commit ef82c8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
31 changes: 5 additions & 26 deletions typescript/src/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import bcoin, { TX, Script } from "bcoin"
import wif from "wif"
import bufio from "bufio"
import { BigNumber, utils } from "ethers"
import { Hex } from "./hex"
Expand Down Expand Up @@ -502,26 +501,6 @@ export function compressPublicKey(publicKey: string | Hex): string {
return `${prefix}${publicKeyX}`
}

/**
* Creates a Bitcoin key ring based on the given private key.
* @param privateKey Private key that should be used to create the key ring
* @param witness Flag indicating whether the key ring will create witness
* or non-witness addresses
* @returns Bitcoin key ring.
*/
export function createKeyRing(
privateKey: string,
witness: boolean = true
): any {
const decodedPrivateKey = wif.decode(privateKey)

return new bcoin.KeyRing({
witness: witness,
privateKey: decodedPrivateKey.privateKey,
compressed: decodedPrivateKey.compressed,
})
}

/**
* Computes the HASH160 for the given text.
* @param text - Text the HASH160 is computed for.
Expand Down Expand Up @@ -580,13 +559,13 @@ export function hashLEToBigNumber(hash: Hex): BigNumber {
export function encodeToBitcoinAddress(
publicKeyHash: string,
witness: boolean,
network: BitcoinNetwork
bitcoinNetwork: BitcoinNetwork
): string {
const buffer = Buffer.from(publicKeyHash, "hex")
const bcoinNetwork = toBcoinNetwork(network)
const hash = Buffer.from(publicKeyHash, "hex")
const network = toBitcoinJsLibNetwork(bitcoinNetwork)
return witness
? bcoin.Address.fromWitnessPubkeyhash(buffer).toString(bcoinNetwork)
: bcoin.Address.fromPubkeyhash(buffer).toString(bcoinNetwork)
? payments.p2wpkh({ hash, network }).address!
: payments.p2pkh({ hash, network }).address!
}

/**
Expand Down
20 changes: 16 additions & 4 deletions typescript/test/bitcoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ describe("Bitcoin", () => {
true,
BitcoinNetwork.Mainnet
)
).to.throw("P2WPKH must be 20 bytes")
).to.throw(
'Expected property "hash" of type Buffer(Length: 20), got ' +
"Buffer(Length: 21)"
)
})
})
})
Expand Down Expand Up @@ -174,7 +177,10 @@ describe("Bitcoin", () => {
false,
BitcoinNetwork.Mainnet
)
).to.throw("P2PKH must be 20 bytes")
).to.throw(
'Expected property "hash" of type Buffer(Length: 20), got ' +
"Buffer(Length: 21)"
)
})
})
})
Expand Down Expand Up @@ -204,7 +210,10 @@ describe("Bitcoin", () => {
true,
BitcoinNetwork.Testnet
)
).to.throw("P2WPKH must be 20 bytes")
).to.throw(
'Expected property "hash" of type Buffer(Length: 20), got ' +
"Buffer(Length: 21)"
)
})
})
})
Expand Down Expand Up @@ -232,7 +241,10 @@ describe("Bitcoin", () => {
false,
BitcoinNetwork.Testnet
)
).to.throw("P2PKH must be 20 bytes")
).to.throw(
'Expected property "hash" of type Buffer(Length: 20), got ' +
"Buffer(Length: 21)"
)
})
})
})
Expand Down

0 comments on commit ef82c8c

Please sign in to comment.