/
hname.go
85 lines (68 loc) · 1.76 KB
/
hname.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
package isc
import (
"fmt"
"io"
"strconv"
"github.com/iotaledger/wasp/packages/hashing"
"github.com/iotaledger/wasp/packages/util/rwutil"
)
// Hname is calculated as the first 4 bytes of the BLAKE2b hash of a string,
// interpreted as a little-endian uint32.
type Hname uint32
const HnameLength = 4
// FuncInit is a name of the init function for any smart contract
const FuncInit = "init"
// well known hnames
var (
EntryPointInit = Hn(FuncInit)
)
// HnameNil is the value used to represent a non-existent Hname.
const HnameNil = Hname(0)
// Hn calculates the hname for the given string.
// For any given string s, it is guaranteed that Hn(s) != HnameNil.
func Hn(name string) (ret Hname) {
h := hashing.HashStrings(name)
for i := 0; i < hashing.HashSize; i += HnameLength {
ret, _ = HnameFromBytes(h[i : i+HnameLength])
if ret != HnameNil {
return ret
}
}
// astronomically unlikely to end up here
return 1
}
func HnameFromBytes(data []byte) (ret Hname, err error) {
_, err = rwutil.ReadFromBytes(data, &ret)
return ret, err
}
func HnameFromString(s string) (Hname, error) {
n, err := strconv.ParseUint(s, 16, 32)
if err != nil {
return HnameNil, fmt.Errorf("cannot parse hname: %w", err)
}
return Hname(n), nil
}
func (hn Hname) Bytes() []byte {
return rwutil.WriteToBytes(&hn)
}
func (hn Hname) Clone() Hname {
return hn
}
func (hn Hname) IsNil() bool {
return hn == HnameNil
}
func (hn Hname) String() string {
return fmt.Sprintf("%08x", int(hn))
}
func (hn *Hname) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
*hn = Hname(rr.ReadUint32())
return rr.Err
}
func (hn *Hname) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.WriteUint32(uint32(*hn))
return ww.Err
}