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

Method buildCommitDataTransaction

src/btc/btc-wallet.ts:339–426  ·  view source on GitHub ↗
(
    data: any,
    toAddress?: string,
    noteUtxo?: IUtxo,
    payUtxos?: IUtxo[],
    feePerKb?: number
  )

Source from the content-addressed store, hash-verified

337 }
338
339 async buildCommitDataTransaction(
340 data: any,
341 toAddress?: string,
342 noteUtxo?: IUtxo,
343 payUtxos?: IUtxo[],
344 feePerKb?: number
345 ) {
346 const msgpackEncodedData = msgpackEncode(data);
347 const commitAddress = this.commitPayloadAddress(msgpackEncodedData);
348 if (undefined === noteUtxo) {
349 let noteUtxos = await this.urchain.utxos([commitAddress.scriptHash]);
350 if (noteUtxos.length === 0) {
351 const result = await this.send([
352 {address: commitAddress.address!, amount: MIN_SATOSHIS},
353 ]);
354 if (result.success) {
355 for (let i = 0; i < 10; i++) {
356 noteUtxos = await this.urchain.utxos([commitAddress.scriptHash]);
357 if (noteUtxos.length > 0) {
358 break;
359 } else if (i === 9) {
360 throw new Error("can not get commit note utxo");
361 }
362 await sleep(1000);
363 }
364 } else {
365 throw new Error(result.error);
366 }
367 }
368 noteUtxo = noteUtxos[0]!;
369 noteUtxo.type = "P2TR-COMMIT-DATA";
370 }
371 if (undefined === toAddress) {
372 toAddress = this.currentAccount.tokenAddress!.address!;
373 }
374 const to: ISendToAddress = {
375 address: toAddress!,
376 amount: MIN_SATOSHIS,
377 };
378
379 payUtxos = payUtxos ?? (await this.fetchAllAccountUtxos());
380 feePerKb = feePerKb ?? (await this.getFeePerKb()).avgFee;
381 const network =
382 bitcoin.networks[
383 this.config.network === "livenet" ? "bitcoin" : "testnet"
384 ];
385
386 const privateKeyBuffer = new PrivateKey(
387 this.currentAccount.privateKey
388 ).toBuffer();
389 const privateKey = ECPair.fromPrivateKey(privateKeyBuffer);
390
391 const estimatedPsbt = createP2TRCommitDataPsbt(
392 privateKey,
393 msgpackEncodedData,
394 noteUtxo,
395 payUtxos,
396 to,

Callers 2

publishSmartContractFunction · 0.80
deployMethod · 0.80

Calls 6

commitPayloadAddressMethod · 0.95
sendMethod · 0.95
getFeePerKbMethod · 0.95
sleepFunction · 0.90
createP2TRCommitDataPsbtFunction · 0.90
utxosMethod · 0.80

Tested by

no test coverage detected