This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
model_merkle_proof.go
79 lines (68 loc) · 1.63 KB
/
model_merkle_proof.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
package bux
import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
"reflect"
"github.com/libsv/go-bc"
)
// MerkleProof represents Merkle Proof type
type MerkleProof bc.MerkleProof
// ToCompoundMerklePath transform Merkle Proof to Compound Merkle Path
func (m MerkleProof) ToCompoundMerklePath() CompoundMerklePath {
height := len(m.Nodes)
if height == 0 {
return nil
}
cmp := make(CompoundMerklePath, height)
pathMap := make(map[string]uint64, 2)
offset := m.Index
pathMap[m.TxOrID] = offset
pathMap[m.Nodes[0]] = offsetPair(offset)
cmp[0] = pathMap
for i := 1; i < height; i++ {
path := make(map[string]uint64, 1)
offset = parrentOffset(offset)
path[m.Nodes[i]] = offset
cmp[i] = path
}
return cmp
}
func offsetPair(offset uint64) uint64 {
if offset%2 == 0 {
return offset + 1
}
return offset - 1
}
func parrentOffset(offset uint64) uint64 {
return offsetPair(offset / 2)
}
// Scan scan value into Json, implements sql.Scanner interface
func (m *MerkleProof) Scan(value interface{}) error {
if value == nil {
return nil
}
xType := fmt.Sprintf("%T", value)
var byteValue []byte
if xType == ValueTypeString {
byteValue = []byte(value.(string))
} else {
byteValue = value.([]byte)
}
if bytes.Equal(byteValue, []byte("")) || bytes.Equal(byteValue, []byte("\"\"")) {
return nil
}
return json.Unmarshal(byteValue, &m)
}
// Value return json value, implement driver.Valuer interface
func (m MerkleProof) Value() (driver.Value, error) {
if reflect.DeepEqual(m, MerkleProof{}) {
return nil, nil
}
marshal, err := json.Marshal(m)
if err != nil {
return nil, err
}
return string(marshal), nil
}