diff --git a/src/Methods/Order/CreateSignablePsbt.ts b/src/Methods/Order/CreateSignablePsbt.ts index bb7dcb3..ec89256 100644 --- a/src/Methods/Order/CreateSignablePsbt.ts +++ b/src/Methods/Order/CreateSignablePsbt.ts @@ -2,7 +2,6 @@ import { BadRequestError, method } from "@valkyr/api"; import { payments, Psbt } from "bitcoinjs-lib"; import Schema, { string } from "computed-types"; -import { Wallet } from "../../Libraries/Wallet"; import { Lookup } from "../../Services/Lookup"; import { utils } from "../../Utilities"; import { validate } from "../../Validators"; @@ -16,6 +15,8 @@ export const createSignablePsbt = method({ }), handler: async ({ network, location, maker, pubkey }) => { const [hash, index] = utils.parse.location(location); + const tapInternalKey = pubkey ? Buffer.from(pubkey, "hex") : undefined; + const btcNetwork = utils.bitcoin.getBitcoinNetwork(network); const address = utils.bitcoin.getBitcoinAddress(maker); if (address === undefined) { @@ -32,18 +33,17 @@ export const createSignablePsbt = method({ throw new BadRequestError("Provided maker address does not match location output"); } - const psbt = new Psbt({ network: utils.bitcoin.getBitcoinNetwork(network) }); + const psbt = new Psbt({ network: btcNetwork }); - if (pubkey !== undefined) { - const wallet = Wallet.fromPublicKey(pubkey, network); + if (tapInternalKey !== undefined) { psbt.addInput({ hash, index, witnessUtxo: { - script: wallet.output, + script: utils.taproot.getPaymentOutput(tapInternalKey, btcNetwork), value: 0, }, - tapInternalKey: wallet.internalPubkey, + tapInternalKey, }); } else { psbt.addInput({ hash, index }); diff --git a/src/Utilities/Taproot.ts b/src/Utilities/Taproot.ts index b5ea932..4041a04 100644 --- a/src/Utilities/Taproot.ts +++ b/src/Utilities/Taproot.ts @@ -20,6 +20,7 @@ export const taproot = { generateMnemonic, getMasterNode, getBip84Account, + getPaymentOutput, }; /* @@ -67,3 +68,19 @@ function getBip84Account(masterNode: BIP32Interface, network: Network, account: .deriveHardened(network === "mainnet" ? 0 : 1) .derive(account); } + +/** + * Get a taproot script output for a specific internal public key. + * + * @param internalPubkey - Internal public key to generate output for. + * @param network - Network to generate output for. + * + * @returns taproot script output. + */ +function getPaymentOutput(internalPubkey: Buffer, network: btc.Network): Buffer { + const { output } = btc.payments.p2tr({ internalPubkey, network }); + if (output === undefined) { + throw new Error("Failed to generate output"); + } + return output; +}