github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/blockchain/create_miner_tx.go (about)

     1  // Copyright 2017-2018 DERO Project. All rights reserved.
     2  // Use of this source code in any form is governed by RESEARCH license.
     3  // license can be found in the LICENSE file.
     4  // GPG: 0F39 E425 8C65 3947 702A  8234 08B2 0360 A03A 9DE8
     5  //
     6  //
     7  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
     8  // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     9  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    10  // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    11  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    12  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    13  // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    14  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
    15  // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    16  
    17  package blockchain
    18  
    19  import "fmt"
    20  
    21  import "github.com/deroproject/derosuite/config"
    22  import "github.com/deroproject/derosuite/crypto"
    23  import "github.com/deroproject/derosuite/address"
    24  import "github.com/deroproject/derosuite/crypto/ringct"
    25  import "github.com/deroproject/derosuite/transaction"
    26  
    27  // this function creates a miner tx, with specific blockreward
    28  // TODO we should consider hardfork  version while creating a miner tx
    29  func Create_Miner_TX(hf_version, height, reward uint64, miner_address address.Address, reserve_size int) (tx transaction.Transaction, err error) {
    30  
    31  	// initialize extra map in empty tx
    32  
    33  	tx.Extra_map = map[transaction.EXTRA_TAG]interface{}{}
    34  
    35  	//TODO need to fix hard fork version checks
    36  	tx.Version = 2
    37  
    38  	tx.Unlock_Time = height + config.MINER_TX_AMOUNT_UNLOCK       // miner tx unlocks in this much time
    39  	tx.Vin = append(tx.Vin, transaction.Txin_gen{Height: height}) // add input height
    40  
    41  	// now lets create encrypted keys, so as the miner_address can spend them
    42  
    43  	tx_secret_key, tx_public_key := crypto.NewKeyPair() // create new tx key pair
    44  
    45  	//tx_public_key is added to extra and serialized
    46  	tx.Extra_map[transaction.TX_PUBLIC_KEY] = *tx_public_key
    47  
    48  	derivation := crypto.KeyDerivation(&miner_address.ViewKey, tx_secret_key) // keyderivation using miner address view key
    49  
    50  	index_within_tx := uint64(0)
    51  
    52  	// this becomes the key within Vout
    53  	ehphermal_public_key := derivation.KeyDerivation_To_PublicKey(index_within_tx, miner_address.SpendKey)
    54  
    55  	// added the amount and key in vout
    56  	tx.Vout = append(tx.Vout, transaction.Tx_out{Amount: reward, Target: transaction.Txout_to_key{Key: ehphermal_public_key}})
    57  
    58  	// add reserve size if requested
    59  	if reserve_size != 0 {
    60  		if reserve_size <= 255 {
    61  			tx.Extra_map[transaction.TX_EXTRA_NONCE] = make([]byte, reserve_size, reserve_size)
    62  		} else {
    63  			// give a warning that nonce was requested but could not be created
    64  		}
    65  	}
    66  
    67  	tx.Extra = tx.Serialize_Extra() // serialize the extra
    68  	// add 0 byte ringct signature
    69  	var sig ringct.RctSig
    70  	tx.RctSignature = &sig
    71  
    72  	return
    73  }
    74  
    75  // this function creates a miner tx, with specific blockreward
    76  // TODO we should consider hardfork  version while creating a miner tx
    77  // even if the hash changes during hard-forks or between , the TX should still be spendable by the owner
    78  // the tx has might change due to serialization changes/ differences, however keyimage will still be the same and
    79  // thus not double spendable
    80  func Create_Miner_TX2(hf_version, height int64, miner_address address.Address) (tx transaction.Transaction, err error) {
    81  
    82  	// initialize extra map in empty tx
    83  
    84  	tx.Extra_map = map[transaction.EXTRA_TAG]interface{}{}
    85  
    86  	//TODO need to fix hard fork version checks
    87  	switch {
    88  	case hf_version <= 6:
    89  		tx.Version = 2
    90  	default:
    91  		err = fmt.Errorf("NO such hardfork version")
    92  	}
    93  
    94  	tx.Unlock_Time = uint64(height) + config.MINER_TX_AMOUNT_UNLOCK       // miner tx unlocks in this much time
    95  	tx.Vin = append(tx.Vin, transaction.Txin_gen{Height: uint64(height)}) // add input height
    96  
    97  	// now lets create encrypted keys, so as the miner_address can spend them
    98  	// but also everyone on planet generates the same key
    99  
   100  	//tx_secret_key := crypto.Key(seed)
   101  	//crypto.ScReduce32(&tx_secret_key)
   102  	// tx_public_key := tx_secret_key.PublicKey()
   103  
   104  	tx_secret_key, tx_public_key := crypto.NewKeyPair() // create new tx key pair
   105  
   106  	//tx_public_key is added to extra and serialized
   107  	tx.Extra_map[transaction.TX_PUBLIC_KEY] = *tx_public_key
   108  
   109  	derivation := crypto.KeyDerivation(&miner_address.ViewKey, tx_secret_key) // keyderivation using miner address view key
   110  
   111  	index_within_tx := uint64(0)
   112  
   113  	// this becomes the key within Vout
   114  	ehphermal_public_key := derivation.KeyDerivation_To_PublicKey(index_within_tx, miner_address.SpendKey)
   115  
   116  	// added the amount and key in vout
   117  	tx.Vout = append(tx.Vout, transaction.Tx_out{Amount: 0, Target: transaction.Txout_to_key{Key: ehphermal_public_key}})
   118  
   119  	tx.Extra = tx.Serialize_Extra() // serialize the extra
   120  	// add 0 byte ringct signature
   121  	var sig ringct.RctSig
   122  	tx.RctSignature = &sig
   123  
   124  	return
   125  }