summa-tx / riemann

rapid prototyping transaction toolbox for Bitcoin-style chains 🎈 🎈
Other
114 stars 19 forks source link

How do I create a decreed offline signature? #87

Closed waliguder closed 6 years ago

waliguder commented 6 years ago

This library is really great, but I don't know how to use it. Can you give an example?

waliguder commented 6 years ago

Thanks to the example provided by the questioner #58, very unexpected, I encountered the same error as him, this problem should be solved. According to his solution, this error was indeed skipped --- AttributeError: 'NoneType' object has no attribute 'to_bytes', but unfortunately, I encountered another error:

Traceback (most recent call last):    File "E:/zt_6.27/py2/testDCR.py", line 15, in      Tx_out = simple.output(input_value - tx_fee, receiving_address)    File "E:\zt_6.27\python\lib\site-packages\riemann\simple.py", line 67, in output      Script = addr.to_output_script(address)    File "E:\zt_6.27\python\lib\site-packages\riemann\encoding\addresses.py", line 184, in to_output_script      Raise ValueError('Cannot parse output script from address.') ValueError: Cannot parse output script from address.

But my address can be found on the testnet explorer, is it only support the address generated by this library, because I did not find the private key signature transaction in the process of signing

waliguder commented 6 years ago

@prestwich @dmorris99

waliguder commented 6 years ago

By the way, how to write zcash's offline signature transaction

waliguder commented 6 years ago

It’s my own too careless, this library only supports online trading.

waliguder commented 6 years ago

Can I sign unsign tx with a private key to become a raw transaction? Then I can go to the explorer broadcast

prestwich commented 6 years ago

Decred support is in early stages, and I'm not sure if sighash is working properly. @drmoog has a PR open for it that I need to review, but it is not high on our priorities list

Zcash can be used as zcash_overwinter_main or zcash_sapling_main. Sprout support should come later this week.

To make an offline tx, use riemann.simple to build the transaction. Then call sighash_all on the transaction object. You can find examples in riemann/examples/. Use your preferred signing method to sign the sighash, and then build a scriptsig or witness as appropriate. I've attached some code with a bitcoin segwit example. Zcash is similar, but uses scriptsigs

def build_refund_tx(
        sweep_input, value, prevout_script, change_address, keypair, fee):
    '''
    Builds a sweep transaction
    A  sweep tx should have 1 input
    A sweep tx should have 1 output (sending all to the change address)
    '''

    # NB: We expect this tx to be ~380 bytes
    #     The 3800 default fee works out to ~10 SAT/byte
    #     Pass in a higher fee if necessary
    # TODO: Improve this
    if fee is None:
        fee = 3800

    # build inputs and outputs
    tx_out = simple.output(value - fee, change_address)
    refund_tx = simple.unsigned_witness_tx([sweep_input], [tx_out])

    # Calculate the sighash
    value = rutils.i2le_padded(value, 8)
    sighash_bytes = refund_tx.sighash_all(
        index=0,
        script=prevout_script,
        prevout_value=value)

    # Sign the sighash and build the witness
    sig = utils.sign_hash(sighash_bytes, keypair[0])
    wit = tx.make_witness(
        [bytes.fromhex(sig),
         bytes.fromhex(keypair[1])])

    # Insert the witness and return the signed tx
    return refund_tx.copy(tx_witnesses=[wit])
waliguder commented 6 years ago

@prestwich Thank you for your answer, but I can't understand what some parameters mean? For example, sweep_input, prevout_script, keypair. I only have utxo and private keys now. I used to create an offline transaction with zcash-hackworks/bitcore-lib-zcash, but unfortunately the raw_hex created could not be broadcast.

prestwich commented 6 years ago

In this case sweep_input is a riemann.tx.DecredTxIn object with your outpoint and sequence number, prevout_script is the output script of the output you're trying to spend (only needed if it's a p2sh), and keypair is a tuple of (privkey_as_bytes, pubkey_as_bytes).

Decred has made significant changes to the tx format, so you won't be able to use any non-decred tools to make the tx.

On Sun, Oct 28, 2018, 20:55 waliguder notifications@github.com wrote:

@prestwich https://github.com/prestwich Thank you for your answer, but I can't understand what some parameters mean? For example, sweep_input, prevout_script, keypair. I only have utxo and private keys now. I used to create an offline transaction with zcash-hackworks/bitcore-lib-zcash, but unfortunately the raw_hex created could not be broadcast.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/summa-tx/riemann/issues/87#issuecomment-433781229, or mute the thread https://github.com/notifications/unsubscribe-auth/AJreMd7YSJXYkedcfNutBFGFkiJOnd_Hks5upnxEgaJpZM4X7mVA .

prestwich commented 6 years ago

Important notes:

This code won't work for decred as written. You need to make a DecredInputWitness instead of an InputWitness

I am not 100% sure that our implementation of Decred's sighash algorithm works correctly

On Mon, Oct 29, 2018, 08:57 James Prestwich james@prestwi.ch wrote:

In this case sweep_input is a riemann.tx.DecredTxIn object with your outpoint and sequence number, prevout_script is the output script of the output you're trying to spend (only needed if it's a p2sh), and keypair is a tuple of (privkey_as_bytes, pubkey_as_bytes).

Decred has made significant changes to the tx format, so you won't be able to use any non-decred tools to make the tx.

On Sun, Oct 28, 2018, 20:55 waliguder notifications@github.com wrote:

@prestwich https://github.com/prestwich Thank you for your answer, but I can't understand what some parameters mean? For example, sweep_input, prevout_script, keypair. I only have utxo and private keys now. I used to create an offline transaction with zcash-hackworks/bitcore-lib-zcash, but unfortunately the raw_hex created could not be broadcast.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/summa-tx/riemann/issues/87#issuecomment-433781229, or mute the thread https://github.com/notifications/unsubscribe-auth/AJreMd7YSJXYkedcfNutBFGFkiJOnd_Hks5upnxEgaJpZM4X7mVA .