Bitcoinjs-lib: Where does "nullDataOutput" function is ?

Created on 25 May 2021  路  8Comments  路  Source: bitcoinjs/bitcoinjs-lib

My code looks like this,

const data = new Buffer(memo); // Message is inserted
        const data_script = bitcoin.script.nullDataOutput(data);
        tx.addOutput(data_script, 0);

It doesn't work, I can't find the right function... I want to insert a message.

All 8 comments

Using your example:

const data = Buffer.from(memo, 'utf8');
const data_script = bitcoin.payments.embed({ data: [data] }).output;
tx.addOutput(data_script, 0);

Using your example:

const data = Buffer.from(memo, 'utf8');
const data_script = bitcoin.payments.embed({ data: [data] }).output;
tx.addOutput(data_script, 0);

It seems that I didn't saw much of my *...

The method I use is: addOutput(scriptPubKey: string | Buffer, value: number): number; so it doesn't handle the script.
I will look for converting my code to your previous comment in terms of "architecture"

If you're using TypeScript you need to use ! to remove the undefined.

Since payment's output is lazily defined, we can't know at compile time whether it is a Buffer or undefined.

const data = Buffer.from(memo, 'utf8');
const data_script = bitcoin.payments.embed({ data: [data] }).output!; // notice !
tx.addOutput(data_script, 0);

or

const data = Buffer.from(memo, 'utf8');
const data_script = bitcoin.payments.embed({ data: [data] }).output;
if (data_script === undefined) {
  throw new Error('Something went wrong!');
}
// now TS will see data_script as Buffer
tx.addOutput(data_script, 0);

OK, thanks for your time, how can I do to do it {with without typeof "undefined"} because it confuse me data_script should be defined if data is defined, how can a error throwaway could change the value inside "data_script", my mind doesn't understand something very important but it should be somewhere.

TypeScript is statically typed, wheras the payment output is lazily loaded at runtime.

! tells the compiler "it's ok, I guarantee the logic will never be undefined."

in this case you're right, if data is passed, output will never be undefined, so ! is safe.

I am assuming you are using TypeScript.

The error method is just another way to tell the compiler that data_script is not undefined, since if it were, the app would have thrown an Error.

In your case, ! is fine, but in general, if you don't know if something is defined you need to handle both possibilities.

I really want to improve myself on the project I am building, yet, I sometimes can't figure out what needs to be done to solve some error or to build some functionalities I want. this project https://github.com/crypto-red/crypto-red.github.io/blob/master/src/js/utils/api-btc-dash-doge-ltc.js is somehow using bitcoinjs-lib and it works, nevertheless, now the error "TypeError: Inconsistent network" is thrown if I try to run our solution, I will dig it, if you want, you can try to run the code in the link, but I won't ask you to do it. I'll come back ;) Again, thanks for your time and patience.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Beardcoding picture Beardcoding  路  3Comments

silence-may picture silence-may  路  3Comments

hoshsadiq picture hoshsadiq  路  3Comments

dcousens picture dcousens  路  3Comments

thrastarson picture thrastarson  路  3Comments