diff --git a/typescript/src/bitcoin.ts b/typescript/src/bitcoin.ts index d7c04a2cf..2e269683e 100644 --- a/typescript/src/bitcoin.ts +++ b/typescript/src/bitcoin.ts @@ -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" @@ -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. @@ -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! } /** diff --git a/typescript/test/bitcoin.test.ts b/typescript/test/bitcoin.test.ts index 2c57ed006..3e43be062 100644 --- a/typescript/test/bitcoin.test.ts +++ b/typescript/test/bitcoin.test.ts @@ -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)" + ) }) }) }) @@ -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)" + ) }) }) }) @@ -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)" + ) }) }) }) @@ -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)" + ) }) }) })