-
Notifications
You must be signed in to change notification settings - Fork 76
/
nrt.go
58 lines (47 loc) · 1.17 KB
/
nrt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//// Name Reservation Transaction (NRT)
package transaction
import (
"crypto/rand"
"encoding/gob"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/gitchain/gitchain/types"
"github.com/gitchain/gitchain/util"
)
func init() {
gob.Register(&NameReservation{})
}
const (
NAME_RESERVATION_VERSION = 1
)
type NameReservation struct {
Version uint32
Hashed types.Hash
}
func (tx *NameReservation) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"Type": "Name Reservation Tranasction",
"Version": tx.Version,
"Hashed": hex.EncodeToString(tx.Hashed),
})
}
func NewNameReservation(name string) (*NameReservation, []byte) {
buf := make([]byte, 4)
rand.Read(buf)
return &NameReservation{
Version: NAME_RESERVATION_VERSION,
Hashed: util.SHA256(append([]byte(name), buf...))}, buf
}
func (txn *NameReservation) Valid() bool {
return (txn.Version == NAME_RESERVATION_VERSION && len(txn.Hashed) == 32)
}
func (txn *NameReservation) Encode() ([]byte, error) {
return encode(txn)
}
func (txn *NameReservation) Hash() types.Hash {
return hash(txn)
}
func (txn *NameReservation) String() string {
return fmt.Sprintf("NRT %s", txn.Hashed)
}