-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
46 lines (38 loc) · 1.05 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
package addrtypes
import (
"github.com/Focinfi/ckb-sdk-go/types"
"github.com/Focinfi/ckb-sdk-go/types/errtypes"
)
type CodeHashIndex = uint8
// Code hash index
// doc: https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md#short-payload-format
const (
CodeHashIndex0 CodeHashIndex = 0
CodeHashIndex1 CodeHashIndex = 1
)
var codeHashIndexList = []CodeHashIndex{CodeHashIndex0, CodeHashIndex1}
func IsAllowedHashType(hashType CodeHashIndex) bool {
for _, ht := range codeHashIndexList {
if hashType == ht {
return true
}
}
return false
}
type HashInfo struct {
HashType CodeHashIndex
Data *types.HexStr
}
func NewHashInfo(payload []byte) (*HashInfo, error) {
if len(payload) == 0 {
return nil, errtypes.WrapErr(errtypes.AddressErrTooShort, nil)
}
hashType := CodeHashIndex(payload[0])
if !IsAllowedHashType(hashType) {
return nil, errtypes.WrapErr(errtypes.AddressErrInvalidHashTypeIndex, nil)
}
return &HashInfo{
HashType: hashType,
Data: types.NewHexStr(payload[1:]),
}, nil
}