MCPcopy Create free account
hub / github.com/NoteProtocol/NoteWallet / createP2TRCommitDataPsbt

Function createP2TRCommitDataPsbt

src/btc/btc-p2tr-commit-data.ts:32–121  ·  view source on GitHub ↗
(
  privateKey: ECPairInterface,
  msgpackEncodedData: Buffer,
  noteUtxo: IUtxo,
  payUtxos: IUtxo[],
  to: ISendToAddress,
  change: string, // Change address
  network: bitcoin.Network,
  feeRate: number,
  fee = 1000 // Assume the fee is 1000
)

Source from the content-addressed store, hash-verified

30// According to the protocol, the first input of the transaction is the NOTE information protocol, and the unlock script is the NOTE script hash
31// The transaction output is the account notification
32export function createP2TRCommitDataPsbt(
33 privateKey: ECPairInterface,
34 msgpackEncodedData: Buffer,
35 noteUtxo: IUtxo,
36 payUtxos: IUtxo[],
37 to: ISendToAddress,
38 change: string, // Change address
39 network: bitcoin.Network,
40 feeRate: number,
41 fee = 1000 // Assume the fee is 1000
42) {
43 assert(noteUtxo.type === "P2TR-COMMIT-DATA");
44
45 const pubkey = privateKey.publicKey;
46
47 const p2note = generateP2TRCommitDataInfo(
48 msgpackEncodedData,
49 pubkey,
50 network
51 );
52
53 const tapLeafScript = {
54 leafVersion: p2note.noteRedeem.redeemVersion,
55 script: p2note.noteRedeem.output,
56 controlBlock:
57 p2note.noteP2TR.witness![p2note.noteP2TR.witness!.length - 1]!,
58 };
59
60 const psbt = new bitcoin.Psbt({network});
61 psbt.setVersion(2);
62 psbt.setLocktime(0);
63 let totalInput = 0;
64 // Construct NOTE disclosure information
65 const input = {
66 hash: noteUtxo.txId,
67 index: noteUtxo.outputIndex,
68 sequence: MAX_SEQUENCE,
69 witnessUtxo: {
70 script: p2note.noteP2TR.output!,
71 value: noteUtxo.satoshis,
72 },
73 tapLeafScript: [tapLeafScript], // MAST script with NOTE redemption script
74 };
75 psbt.addInput(input);
76 totalInput += noteUtxo.satoshis;
77
78 // Add recharge input
79 totalInput += addPsbtPayUtxos(privateKey, psbt, payUtxos, network);
80
81 // Output
82 let totalOutput = 0;
83 psbt.addOutput({
84 address: to.address,
85 value: Number(to.amount),
86 });
87 totalOutput += Number(to.amount);
88
89 // Add change

Callers 1

Calls 5

assertFunction · 0.90
addPsbtPayUtxosFunction · 0.90
signPsbtInputFunction · 0.90
finalizeCommitInputFunction · 0.85

Tested by

no test coverage detected