onchain / paper-wallet-spender

Create transactions for multiple currencies from private keys.
0 stars 4 forks source link

Add signatures to an unsigned transaction. #5

Closed 9876691 closed 6 years ago

9876691 commented 6 years ago

This is more or less a re-write of an existing ruby method from here https://github.com/onchain/onchain-gem/blob/master/lib/onchain/transaction.rb#L373

 def sign_single_signature_transaction(transaction_hex, sig_list, 
      hash_type = Bitcoin::Script::SIGHASH_TYPE[:all],
      network = :bitcoin)

      tx = Bitcoin::Protocol::Tx.create_from_hex(transaction_hex, network)

      tx.in.each_with_index do |txin, index|

        public_key_hex = sig_list[index].keys.first

        sig = sig_list[index][public_key_hex]['sig']

        txin.script = Bitcoin::Script.to_pubkey_script_sig(
          OnChain.hex_to_bin(sig), 
          OnChain.hex_to_bin(public_key_hex), hash_type)
      end

      return OnChain::bin_to_hex(tx.to_network_payload(network))
  end

Add a method to https://github.com/onchain/paper-wallet-spender/blob/master/src/onchain/protocol/utxo_transaction.cr such as

def sign_transaction(signatures : Array(Signature))

You'll need to create the signature model. i.e.

module OnChain
  struct SignedHash

    getter hash_to_sign : String
    getter public_key 
    getter input_index 
    getter signature : String # In DER format.

The method should add the signatures to the transaction inputs. Then calling the to_hex method on the transaction object should return a hex representation of the tranastion signed and ready to broadcast.