paulmillr / scure-btc-signer

Audited & minimal library for creating, signing & decoding Bitcoin transactions.
https://paulmillr.com/noble/#scure
MIT License
135 stars 33 forks source link

How to addOutput for unspendable outputs? #26

Closed radicleart closed 1 year ago

radicleart commented 1 year ago

Hi, I'm porting code from bitcoinjs to your library.

In bitcoinjs I can create an unspendable, OP_RETURN, output like this;

const data = globalThis.Buffer.from(stringy, 'utf8');
const embed = payments.embed({ data: [data] }); // import from bitcoinjs
psbt.addOutput({ script: embed.output, value: 0 });

Anything you can point me to figure the equivalent here greatly appreciated?

paulmillr commented 1 year ago
import * as btc from './index.js';
import { hex } from '@scure/base';
import { secp256k1 } from '@noble/curves/secp256k1';

const privKey = hex.decode('0101010101010101010101010101010101010101010101010101010101010101');
// Allow unknown outputs
const tx = new btc.Transaction({
  allowUnknowOutput: true,
});
tx.addInput({
  txid: hex.decode('fae21e319ca827df32462afc3225c17719338a8e8d3e3b3ddeb0c2387da3a4c7'),
  index: 0,
  witnessUtxo: {
    amount: 600n,
    script: btc.p2wpkh(secp256k1.getPublicKey(privKey, true)).script,
  },
});
// Add unspendable output with 'OP_RETURN'
tx.addOutput({ script: btc.Script.encode(['RETURN']), amount: 0n });
tx.sign(privKey);
tx.finalize();
console.log(tx.extract());
radicleart commented 1 year ago

Thank you!