github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/hash.go (about) 1 // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls 2 // 3 // Copyright 2020 Stafi Protocol 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package types 18 19 import ( 20 "encoding/json" 21 "fmt" 22 ) 23 24 // H160 is a hash containing 160 bits (20 bytes), typically used in blocks, extrinsics and as a sane default 25 type H160 [20]byte 26 27 // NewH160 creates a new H160 type 28 func NewH160(b []byte) H160 { 29 h := H160{} 30 copy(h[:], b) 31 return h 32 } 33 34 // Hex returns a hex string representation of the value (not of the encoded value) 35 func (h H160) Hex() string { 36 return fmt.Sprintf("%#x", h[:]) 37 } 38 39 // H256 is a hash containing 256 bits (32 bytes), typically used in blocks, extrinsics and as a sane default 40 type H256 [32]byte 41 42 // NewH256 creates a new H256 type 43 func NewH256(b []byte) H256 { 44 h := H256{} 45 copy(h[:], b) 46 return h 47 } 48 49 // Hex returns a hex string representation of the value (not of the encoded value) 50 func (h H256) Hex() string { 51 return fmt.Sprintf("%#x", h[:]) 52 } 53 54 // H512 is a hash containing 512 bits (64 bytes), typically used for signature 55 type H512 [64]byte 56 57 // NewH512 creates a new H512 type 58 func NewH512(b []byte) H512 { 59 h := H512{} 60 copy(h[:], b) 61 return h 62 } 63 64 // Hex returns a hex string representation of the value (not of the encoded value) 65 func (h H512) Hex() string { 66 return fmt.Sprintf("%#x", h[:]) 67 } 68 69 // Hash is the default hash that is used across the system. It is just a thin wrapper around H256 70 type Hash H256 71 72 // NewHash creates a new Hash type 73 func NewHash(b []byte) Hash { 74 h := Hash{} 75 copy(h[:], b) 76 return h 77 } 78 79 // NewHashFromHexString creates a new Hash type from a hex string 80 func NewHashFromHexString(s string) (Hash, error) { 81 bz, err := HexDecodeString(s) 82 if err != nil { 83 return Hash{}, err 84 } 85 86 if len(bz) != 32 { 87 return Hash{}, fmt.Errorf("required result to be 32 bytes, but got %v", len(bz)) 88 } 89 90 return NewHash(bz), nil 91 } 92 93 // Hex returns a hex string representation of the value (not of the encoded value) 94 func (h Hash) Hex() string { 95 return fmt.Sprintf("%#x", h[:]) 96 } 97 98 // UnmarshalJSON fills h with the JSON encoded byte array given by b 99 func (h *Hash) UnmarshalJSON(b []byte) error { 100 var tmp string 101 err := json.Unmarshal(b, &tmp) 102 if err != nil { 103 return err 104 } 105 *h, err = NewHashFromHexString(tmp) 106 return err 107 } 108 109 // MarshalJSON returns a JSON encoded byte array of h 110 func (h Hash) MarshalJSON() ([]byte, error) { 111 return json.Marshal(h.Hex()) 112 }