You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.9 KiB

  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. package blockchain
  17. import "fmt"
  18. import "github.com/deroproject/derosuite/crypto"
  19. import "github.com/deroproject/derosuite/address"
  20. import "github.com/deroproject/derosuite/crypto/ringct"
  21. import "github.com/deroproject/derosuite/transaction"
  22. // this function creates a miner tx, with specific blockreward
  23. // TODO we should consider hardfork version while creating a miner tx
  24. func Create_Miner_TX(hf_version, height, reward uint64, miner_address address.Address, reserve_size int) (tx transaction.Transaction, err error) {
  25. // initialize extra map in empty tx
  26. tx.Extra_map = map[transaction.EXTRA_TAG]interface{}{}
  27. //TODO need to fix hard fork version checks
  28. switch {
  29. case hf_version <= 6:
  30. tx.Version = 2
  31. default:
  32. err = fmt.Errorf("NO such hardfork version")
  33. }
  34. tx.Vin = append(tx.Vin, transaction.Txin_gen{Height: height}) // add input height
  35. // now lets create encrypted keys, so as the miner_address can spend them
  36. tx_secret_key, tx_public_key := crypto.NewKeyPair() // create new tx key pair
  37. //tx_public_key is added to extra and serialized
  38. tx.Extra_map[transaction.TX_PUBLIC_KEY] = *tx_public_key
  39. derivation := crypto.KeyDerivation(&miner_address.ViewKey, tx_secret_key) // keyderivation using miner address view key
  40. index_within_tx := uint64(0)
  41. // this becomes the key within Vout
  42. ehphermal_public_key := derivation.KeyDerivation_To_PublicKey(index_within_tx, miner_address.SpendKey)
  43. // added the amount and key in vout
  44. tx.Vout = append(tx.Vout, transaction.Tx_out{Amount: reward, Target: transaction.Txout_to_key{Key: ehphermal_public_key}})
  45. // add reserve size if requested
  46. if reserve_size != 0 {
  47. if reserve_size <= 255 {
  48. tx.Extra_map[transaction.TX_EXTRA_NONCE] = make([]byte, reserve_size, reserve_size)
  49. } else {
  50. // give a warning that nonce was requested but could not be created
  51. }
  52. }
  53. tx.Extra = tx.Serialize_Extra() // serialize the extra
  54. // add 0 byte ringct signature
  55. var sig ringct.RctSig
  56. tx.RctSignature = &sig
  57. return
  58. }