-
Notifications
You must be signed in to change notification settings - Fork 6
/
pack.go
48 lines (41 loc) · 1.07 KB
/
pack.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
package ast
import (
"fmt"
"github.com/dipdup-net/go-lib/tools/base"
"github.com/dipdup-net/go-lib/tools/encoding"
"github.com/dipdup-net/go-lib/tools/forge"
"golang.org/x/crypto/blake2b"
)
// Pack -
func Pack(node Base) (string, error) {
data, err := Forge(node, true)
if err != nil {
return "", err
}
return fmt.Sprintf("%s%s", forge.PackPrefix, data), nil
}
// BigMapKeyHashFromNode -
func BigMapKeyHashFromNode(node Node) (string, error) {
nodeBase, err := node.ToBaseNode(true)
if err != nil {
return "", err
}
return BigMapKeyHash(nodeBase)
}
// BigMapKeyHash -
func BigMapKeyHash(node *base.Node) (string, error) {
data, err := forge.Forge(node)
if err != nil {
return "", err
}
blakeHash := blake2b.Sum256(append([]byte{forge.PackPrefixByte}, data...))
return encoding.EncodeBase58(blakeHash[:], []byte(encoding.PrefixScriptExpr))
}
// BigMapKeyHashFromString -
func BigMapKeyHashFromString(str string) (string, error) {
var node base.Node
if err := json.UnmarshalFromString(str, &node); err != nil {
return "", err
}
return BigMapKeyHash(&node)
}