9 votes
Accepted

How do you validate a bitcoin address using bitcoinjs library in javascript?

If you call bitcoin.address.toOutputScript(address, bitcoin.networks.bitcoin) It will throw an error if the address cannot be parsed as P2SH or P2PKH, or if the checksum is not valid. Replace the ...
Nick ODell's user avatar
  • 29.4k
9 votes
Accepted

How to sweep a segwit bech32 private key?

Use Electrum 3.1.1 and prepend "p2wpkh:" to the private key before importing or sweeping it. For example: p2wpkh:5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y
Abdussamad's user avatar
  • 2,699
4 votes
Accepted

How to get xpriv and xpub from keyPair with bitcoinjs-lib

The short answer is: you probably shouldn't. That is not a typical way to generate an xpriv/xpub. You are trying to convert a key pair to an extended key pair. The difference is the extended keys ...
JBaczuk's user avatar
  • 7,388
4 votes
Accepted

How to build a transaction using bitcoinjs-lib

In P2SH, the output script is created by applying HASH160 (SHA256+RIPEMD160) to the redeem script, which is then revealed at spend time. So if you created the previous output, you should know what the ...
Vojtěch Strnad's user avatar
3 votes
Accepted

Using BitcoinJS - How do you verify a message using a segwit bech32 (bc1…) / P2SH (3…) addresses?

A Trezor signed message with a 3 address can be verified here: https://jhoenicke.github.io/brainwallet.github.io/#verify Or your can verify it with another Trezor wallet.
xtraelv's user avatar
  • 46
3 votes
Accepted

Creating an OP_CSV transaction with bitcoinjs

In bitcoin-js you could do something like this aliceToBobRedeemScript = bitcoin.script.compile([, bitcoin.opcodes.OP_IF, bitcoin.opcodes.OP_HASH160, aliceSecretHash, bitcoin.opcodes....
MandelDuck's user avatar
3 votes
Accepted

How to push a raw transaction using BitcoinJS (and Request)?

Body must be a string. You can use form instead of body. Reference: https://www.npmjs.com/package/request#forms
siddolo's user avatar
  • 46
3 votes

How to use bitcoinjs-lib with Regtest?

Oops! Noob mistake. I thought that testnet and regtest had different version prefixes (not compatible) but they are (the bitcoin wiki and bitcoin developers page doesn't clarify this, though). So I ...
joe.js's user avatar
  • 592
3 votes

How do you switch your bitcoinjs integrationfrom TransactionBuilder to transactions-psbt.js?

You're only signing the first input? tx.sign(0, owner); Usually every input has to be signed somehow. I also struggled with the switch to Psbt. Biggest difference was basically requirement to have a ...
ildarmgt's user avatar
3 votes

How to read the balance on a wallet address

To get a balance of the address you need a view of the blockchain's current utxo set. This is typically done by syncing a node. I don't believe bitcoinjs has the capability to be a node (nor it is ...
benthecarman's user avatar
3 votes
Accepted

"Witness program hash mismatch" code -26 when broadcasting a transaction

"Witness program hash mismatch" in a Taproot script path spend means the Taproot commitment wasn't successfully verified, in other words the internal key tweaked with the taptweak hash (...
Vojtěch Strnad's user avatar
3 votes
Accepted

non-mandatory-script-verify-flag (Invalid Schnorr signature) when trying to reveal a TR output

Your transaction is a key path spend, not a script path spend (it only has one witness item, the signature). Perhaps you've calculated the sighash for a script path spend but then only included the ...
Vojtěch Strnad's user avatar
3 votes
Accepted

Mnemonic from Node.js app not restoring in other wallets

You are running into the gap limit. When restoring a wallet from a mnemonic, the wallet software checks the generated addresses one by one until it finds a long enough gap of addresses that have never ...
Vojtěch Strnad's user avatar
2 votes

Create OP_RETURN tx with Bitcoinjs-lib

Here's how I was able to accomplish this with the latest bitcoinjs-lib version (5.2.0) using psbt. const alice = bitcoin.ECPair.fromWIF(wif, bitcoin.networks.regtest); const inputData1 = { hash: ...
pseudozach's user avatar
2 votes

HD wallet implementation

I'm doing something similar but using segwit addresses (BIP49). If you are using BIP32 with a standard derivation of m/0'/0/0 you could do something like this - See: https://github.com/bitcoinjs/...
wmpedersen's user avatar
2 votes
Accepted

ScriptHash from bitcoin address with BitcoinJS

Got the answer via github let script = bitcoin.address.toOutputScript(address) let hash = bitcoin.crypto.sha256(script) let reversedHash = new Buffer(hash.reverse()) console.log(address, ' maps to ',...
youssef's user avatar
  • 266
2 votes
Accepted

How to decode transaction inputs from raw data using bitcoinjs-lib?

You will, by default, only see the values of the outputs and not the inputs. To gather all values for inputs, you will need to loop through your inputs and use an API to gather the transactions where ...
Monstrum's user avatar
  • 1,334
2 votes
Accepted

Can't generate same addresses as bitcoincore using xprv from bitcoin-core

looks like address from dump is multisig? It is not multisig. Bitcoin Core does not generate multisig addresses. That address is a P2SH wrapped Segwit address. The same private key can be associated ...
Ava Chow's user avatar
  • 70.5k
2 votes
Accepted

Understanding signing messages with bitcoinjs-lib

That particular line of code asserts that the private key fits into a 32 byte-sized byte array properly with the necessary zero paddings. As the private key is simply some random Big Integer between 0 ...
rny's user avatar
  • 2,418
2 votes

How to retrieve readable private key from bitcoinjs-lib

use .toString('hex') instead. Don't export / import individual private keys when you are using BIP32 HD keys. IT IS DANGEROUS. .toString('hex') and .toString('base64') are readable string ...
mesqueeb's user avatar
  • 131
2 votes

How to calculate fee in BitcoinJS raw transaction?

The TX fee is the difference between the totals of all the input values and all the output values. If you are looking at a single TX you can see all the output values, but how do you get the input ...
pinhead's user avatar
  • 5,144
2 votes
Accepted

problem sending relative locktime (OP_CSV) transaction

OP_CHECKSEQUENCEVERIFY is only available for transactions with their version number set to 2. Your transactions have a version number of 1, and so they fail.
Ava Chow's user avatar
  • 70.5k
2 votes

bitcoinjs-lib is installed but cannot use its functions

You seem to be using an old version of bitcoinjs-lib. The payments interface was added in version 4.0.0 which was released in 2018. Note that the ECPair interface was removed in 6.0.0 so this code won'...
Vojtěch Strnad's user avatar
2 votes
Accepted

Estimating virtual bytes of a transaction seems circular

Standard script types generally have known maximum sizes, so you can use that to estimate the size of the transaction to determine the fee. Murch has created a table with the sizes of these standard ...
Ava Chow's user avatar
  • 70.5k
1 vote

Cannot read property 'toWIF' of undefined

First, note that HDNode was removed in 4.0.0, so you should use the BIP32 module you included. Second, you don't need the extra keyPair property, so your code becomes: var bip32 = require('bip32') ...
JBaczuk's user avatar
  • 7,388
1 vote
Accepted

How do you sign data with a private key? [javascript]

You could try using eccrypto index.js: var crypto = require("crypto"); var eccrypto = require("eccrypto"); // A new random 32-byte private key. var privateKey = Buffer.from('...
JBaczuk's user avatar
  • 7,388
1 vote
Accepted

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'fromSeedBuffer' of undefined

Try using the bip32 module. Also there are a few other things you'll need to fix as well. See Use BIP39 to generate BIP32 addresses. Note, this will generate a P2PKH address, see getAddress() function....
JBaczuk's user avatar
  • 7,388
1 vote

Using BitcoinJS - How do you verify a message using a segwit bech32 (bc1…) / P2SH (3…) addresses?

There is no established standard (yet) for signatures with addresses other than P2PKH (1...) addresses. So whatever signature you created, it will only verify against the corresponding P2PKH address. ...
Pieter Wuille's user avatar
1 vote

How to sweep a segwit bech32 private key?

Instructions for Bitcoin Core 23.0 You will need: the private key in WIF format to know the context in which this key was used (for what type of scripts) optionally, the approximate date at which you ...
Antoine Poinsot's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible