-
Notifications
You must be signed in to change notification settings - Fork 179
/
hash.go
112 lines (93 loc) · 2.89 KB
/
hash.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"encoding/json"
"fmt"
)
// H160 is a hash containing 160 bits (20 bytes), typically used in blocks, extrinsics and as a sane default
type H160 [20]byte
// NewH160 creates a new H160 type
func NewH160(b []byte) H160 {
h := H160{}
copy(h[:], b)
return h
}
// Hex returns a hex string representation of the value (not of the encoded value)
func (h H160) Hex() string {
return fmt.Sprintf("%#x", h[:])
}
// H256 is a hash containing 256 bits (32 bytes), typically used in blocks, extrinsics and as a sane default
type H256 [32]byte
// NewH256 creates a new H256 type
func NewH256(b []byte) H256 {
h := H256{}
copy(h[:], b)
return h
}
// Hex returns a hex string representation of the value (not of the encoded value)
func (h H256) Hex() string {
return fmt.Sprintf("%#x", h[:])
}
// H512 is a hash containing 512 bits (64 bytes), typically used for signature
type H512 [64]byte
// NewH512 creates a new H512 type
func NewH512(b []byte) H512 {
h := H512{}
copy(h[:], b)
return h
}
// Hex returns a hex string representation of the value (not of the encoded value)
func (h H512) Hex() string {
return fmt.Sprintf("%#x", h[:])
}
// Hash is the default hash that is used across the system. It is just a thin wrapper around H256
type Hash H256
// NewHash creates a new Hash type
func NewHash(b []byte) Hash {
h := Hash{}
copy(h[:], b)
return h
}
// NewHashFromHexString creates a new Hash type from a hex string
func NewHashFromHexString(s string) (Hash, error) {
bz, err := HexDecodeString(s)
if err != nil {
return Hash{}, err
}
if len(bz) != 32 {
return Hash{}, fmt.Errorf("required result to be 32 bytes, but got %v", len(bz))
}
return NewHash(bz), nil
}
// Hex returns a hex string representation of the value (not of the encoded value)
func (h Hash) Hex() string {
return fmt.Sprintf("%#x", h[:])
}
// UnmarshalJSON fills h with the JSON encoded byte array given by b
func (h *Hash) UnmarshalJSON(b []byte) error {
var tmp string
err := json.Unmarshal(b, &tmp)
if err != nil {
return err
}
*h, err = NewHashFromHexString(tmp)
return err
}
// MarshalJSON returns a JSON encoded byte array of h
func (h Hash) MarshalJSON() ([]byte, error) {
return json.Marshal(h.Hex())
}