Skip to content

Commit

Permalink
chore(psbt): expect sliced pubkey for signable psbt
Browse files Browse the repository at this point in the history
  • Loading branch information
kodemon committed Jul 7, 2023
1 parent 818fd7e commit 38bac8f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Methods/Order/CreateSignablePsbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) {
Expand All @@ -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 });
Expand Down
17 changes: 17 additions & 0 deletions src/Utilities/Taproot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const taproot = {
generateMnemonic,
getMasterNode,
getBip84Account,
getPaymentOutput,
};

/*
Expand Down Expand Up @@ -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;
}

0 comments on commit 38bac8f

Please sign in to comment.