bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.68k stars 2.1k forks source link

Does creating a transaction require ECPair and WIF encoded keys? #1752

Closed moblizeitllc closed 2 years ago

moblizeitllc commented 2 years ago

i was looking at 1 to 1 transaction example and confused with this part

describe('bitcoinjs-lib (transactions with psbt)', () => { it('can create a 1-to-1 Transaction', () => { const alice = ECPair.fromWIF( 'L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr', );

what exactly is this string 'L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr' here ? A hardcoded from address example?

junderw commented 2 years ago

L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr is a WIF formatted private key.

tbh single key management is a very old way of dealing with keys. Nowadays 100% of wallets use HD keys (in bitcoinjs that would be done with bip32 library)

moblizeitllc commented 2 years ago

So is it the same thing what ECPair.privateKey prints?

junderw commented 2 years ago

no.

const pair = ECPair.fromWIF('L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr')
console.log(pair.toWIF())
// will output L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr
moblizeitllc commented 2 years ago

ok bit confused now. this is how i am generating pub and pvt keys and a address

const keyPair = ECPair.ECPair.makeRandom();
                  functions.logger.log("keyPair", keyPair.privateKey)
                  const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: diviMainnet });

now if i have to initiate a 1-1 txn from above address to say a address XXX then what would go in ECPair.fromWIF( ????) from your example?

junderw commented 2 years ago

functions.logger.log("keyPair", keyPair.toWIF()) will log the WIF format of the privateKey.

So if you want to use fromWIF(x) when importing a private key, you should use toWIF() when exporting.

If you want to use fromPrivateKey() when importing a private key, you should use privateKey when exporting. (Like your example)

The difference between WIF and raw private keys are that WIF encodes the information of 1. which network is used. and 2. whether the public key should be shown in compressed format.

The information your key pair will contain with ECPair.ECPair.fromWIF(wifString) where you only need to provide one string, Is the same as the information your key pair will contain with ECPair.ECPair.fromPrivateKey(privateKey, { network: xxx, compressed: yyy }) where you need to provide one Buffer, a network object, and a boolean flag. (However, if you don't provide the last 2 items for fromPrivateKey, they have default values, the bitcoin mainnet network and true respectively)

You are overthinking this, and missing the forest for the trees.

The example is not saying "YOU MUST USE ECPAIR, YOU MUST IMPORT USING WIF!!!"

It is just saying "you need a private key and public key. for convenience, we give a single example here and decide to import it via a single string hard coded, but that's not important. What is important is that you somehow get an object with the attributes { publicKey, privateKey } and a sign() method and ECPair just so happens to fulfill that requirement."

The bip32 module also fulfills that requirement. So you can just as easily create a wallet using bip39 module to generate a secret phrase, convert it into a bip32 object, derive some key path, then use the {publicKey, privateKey} of that result. Here's an example using BIP49 derivation of p2sh-p2wpkh keys using such a method

How you get there is not important in regards to creating a transaction.

However, how you get there in regards to managing keys for your wallet is very important.

moblizeitllc commented 2 years ago

This is very helpful. I am going to store using toWIF and then exporting using the same. this makes a lot of sense to me at this point

moblizeitllc commented 2 years ago

So, I have used your guidance and came up to next point of confusion. So, wallet is an object in my db where i am storing the wif rather separate pvt and public keys. Now, i am proceeding with the txn part. What is this hash here? where would i get it in my case? Obviously, i have no idea what is index: 0 here and do i need to change it?

Buffer.from as well has multiple strings that I am clear what i need to fill them? . As of now, in my db i am storing the address of a wallet, that i generated using above code, and storing the WIF and the balance this address would have. I would also know my target transferTo address.

      ```

const alice = ECPair.fromWIF( wallet.wif(), );

      const psbt = new bitcoin.Psbt();

      psbt.addInput({
        // if hash is string, txid, if hash is Buffer, is reversed compared to txid
        hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e',
        index: 0,

        nonWitnessUtxo: Buffer.from(
          '0200000001f9f34e95b9d5c8abcd20fc5bd4a825d1517be62f0f775e5f36da944d9' +
            '452e550000000006b483045022100c86e9a111afc90f64b4904bd609e9eaed80d48' +
            'ca17c162b1aca0a788ac3526f002207bb79b60d4fc6526329bf18a77135dc566020' +
            '9e761da46e1c2f1152ec013215801210211755115eabf846720f5cb18f248666fec' +
            '631e5e1e66009ce3710ceea5b1ad13ffffffff01' +
            // value in satoshis (Int64LE) = 0x015f90 = 90000
            '905f010000000000' +
            // scriptPubkey length
            '19' +
            // scriptPubkey
            '76a9148bbc95d2709c71607c60ee3f097c1217482f518d88ac' +
            // locktime
            '00000000',
          'hex',
        ),});
junderw commented 2 years ago

Here's a helpful guide: https://developer.bitcoin.org/devguide/transactions.html

This issue should focus only on specific details about this library, and general questions about Bitcoin should not be asked here.

Also

  1. Get balance of an address
  2. Get utxos of an address

Are out of scope for this library. It is assumed that you know how bitcoin works on a basic level before you start using this library. (hint: if you don't know what a utxo is or what that means, you need to study Bitcoin a little more before using this library (or building a wallet that will handle money!!!))

moblizeitllc commented 2 years ago

Plz advise where can i get this kind of questions answered? i am new. yes i do understand utxo and how it works though

junderw commented 2 years ago

https://bitcoin.stackexchange.com/ is a great place.

Extremely great.

You can search your question first, as many people ask their question there.