dart-bitcoin / bitcoin_flutter

A dart Bitcoin library for Flutter.
MIT License
165 stars 123 forks source link

Question about making transactions with P2WPKH outputs #34

Closed RohanKapurDEV closed 4 years ago

RohanKapurDEV commented 4 years ago

I noticed that in the transaction building example for P2WPKH inputs, you put the following code:

test('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input', () {
      final alice = ECPair.fromWIF('cUNfunNKXNNJDvUvsjxz5tznMR6ob1g5K6oa4WGbegoQD3eqf4am', network: NETWORKS.testnet);
      final p2wpkh = new P2WPKH(data: new PaymentData(pubkey: alice.publicKey), network: NETWORKS.testnet).data;
      final txb = new TransactionBuilder(network: NETWORKS.testnet);
      txb.setVersion(1);
      txb.addInput('53676626f5042d42e15313492ab7e708b87559dc0a8c74b7140057af51a2ed5b', 0, null, p2wpkh.output); // Alice's previous transaction output, has 200000 satoshis
      txb.addOutput("tb1qchsmnkk5c8wsjg8vxecmsntynpmkxme0yvh2yt", 1000000);
      txb.addOutput("tb1qn40fftdp6z2lvzmsz4s0gyks3gq86y2e8svgap", 8995000);

      txb.sign(vin: 0, keyPair: alice, witnessValue: 10000000);
      // // prepare for broadcast to the Bitcoin network, see "can broadcast a Transaction" below
      expect(txb.build().toHex(), '010000000001015beda251af570014b7748c0adc5975b808e7b72a491353e1422d04f5266667530000000000ffffffff0240420f0000000000160014c5e1b9dad4c1dd0920ec3671b84d649877636f2fb8408900000000001600149d5e94ada1d095f60b701560f412d08a007d11590247304402203c4670ff81d352924af311552e0379861268bebb2222eeb0e66b3cdd1d4345b60220585b57982d958208cdd52f4ead4ecb86cfa9ff7740c2f6933e77135f1cc4c58f012102f9f43a191c6031a5ffae27c5f9911218e78857923284ac1154abc2cc008544b200000000');
    });

I have a question specifically about this part of the code:

txb.addInput('53676626f5042d42e15313492ab7e708b87559dc0a8c74b7140057af51a2ed5b', 0, null, p2wpkh.output); // Alice's previous transaction output, has 200000 satoshis

I know that the first parameter is the transactionID, the second represents the output that we want to spend from the transaction. But what are the third and fourth parameters? They just say null and I don't know what the 4th refers to exactly. Can anyone help me clarify what that is?

I would also like to know what witnessValue refers to exactly. Is it just the value of the output we're trying to spend?

RohanKapurDEV commented 4 years ago

Also is there a specific order in which I have to add inputs, outputs and sign?

For example, can I do the following?

txb.addInput('xxxx', 0, null, p2wpkh.output);
txb.sign(vin: 0, keyPair: alice, witnessValue: 100);
txb.addInput('xxxx', 0, null, p2wpkh.output);
txb.sign(vin: 0, keyPair: bob, witnessValue: 29);
txb.addInput('xxxx', 0, null, p2wpkh.output);
txb.sign(vin: 0, keyPair: charles, witnessValue: 29383);

txb.addOutput("xxxx", 324232);
txb.addOutput("xxxx", 43242323);

txb.build().toHex()

Or do I have to do it like this?:

txb.addInput('xxxx', 0, null, p2wpkh.output);
txb.addInput('xxxx', 0, null, p2wpkh.output);
txb.addInput('xxxx', 0, null, p2wpkh.output);

txb.addOutput("xxxx", 324232);
txb.addOutput("xxxx", 43242323);

txb.sign(vin: 0, keyPair: alice, witnessValue: 100);
txb.sign(vin: 0, keyPair: bob, witnessValue: 100);
txb.sign(vin: 0, keyPair: charles, witnessValue: 100);

txb.build().toHex()

Any clarity would be much appreciated!

RohanKapurDEV commented 4 years ago

Cleared out my issues with this. If anyone else reading this has similar questions, feel free to ask and mention for me and I'll explain.

kaning commented 4 years ago

@RohanKapurDEV could you please explain how you resolved this?

kamike commented 3 years ago

解决了我与此有关的问题。如果其他人也有类似的问题,请随时为我提问和提起,我将进行解释。

how you resolved this???