;; To make it easier for a new user to register a name
;; by submitting only a single transaction rather than
;; traditionally two separate transactions.
;; To avoid the loss of STX when pre-order transaction succeeds
;; but the register transaction fails.
;; To avoid confusion when pre-ordering/registering a BNS name.
;; --------------------------------------------------------------
;;  101BNS - dartman
;; --------------------------------------------------------------

;; ST000000000000000000002AMW42H.bns -> testnet BNS
;; SP000000000000000000002Q6VF78.bns -> mainnet BNS

;; Register a BNS name by calling a couple of BNS functions:
;;  1. Pre-Order a BNS name
;;  2. Register the BNS name

;; title: registrar
;; version:
;; summary:
;; description:
;; traits
;; token definitions
;; constants
;; data vars
;; data maps
;; public functions
;; read only functions
;; private functions

;; 0x2e = '.'
;; 0x73616c74 = 'salt' -> does salt have to be 20 bytes?

(define-public (register (namespace (buff 20)) (name (buff 48)) (stx-to-burn uint) (zonefile-hash (buff 20)))
  (let
    (
      (hashed-salted-fqn (hash160 (concat (concat (concat name 0x2e) namespace) 0x73616c74)))
    )
    ;;(
    ;;  (hashed-salted-fqn u1)
    ;;)
    ;;(begin
    ;;(ok hashed-salted-fqn)
    ;;)
    (begin
      (try! (contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.bns name-preorder hashed-salted-fqn stx-to-burn))
      (try! (contract-call? 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.bns name-register namespace name 0x64756d6d79 zonefile-hash))
      (ok true)
    )
  )
)

;; NAME_PREORDER
;; This is the first transaction to be sent. It tells all BNS nodes the salted hash of the BNS name,
;; and it burns the registration fee.
;; (define-public (name-preorder (hashed-salted-fqn (buff 20))
;;                               (stx-to-burn uint))

;; NAME_REGISTRATION
;; This is the second transaction to be sent. It reveals the salt and the name to all BNS nodes,
;; and assigns the name an initial public key hash and zone file hash
;; (define-public (name-register (namespace (buff 20))
;;                               (name (buff 48))
;;                               (salt (buff 20))
;;                               (zonefile-hash (buff 20)))


;; ------------------------------------------------------------------
;;  For testing only. Delete before deploying to mainnet.
;; ------------------------------------------------------------------
(define-read-only (echo (shout-out (string-ascii 100))) (ok (concat "Right back at you - " shout-out)))  ;; echo
(define-read-only (get-contract-caller) (ok contract-caller ))           ;; returns contract-caller
(define-read-only (get-tx-sender) (ok tx-sender ))                       ;; returns tx-sender
