-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof.go
44 lines (38 loc) · 867 Bytes
/
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
package c_type
import (
"encoding/hex"
"fmt"
"github.com/axis-cash/go-axis/crypto/sha3"
)
type Proof [PROOF_WIDTH]byte
func (b Proof) MarshalText() ([]byte, error) {
result := make([]byte, len(b)*2+2)
copy(result, `0x`)
hex.Encode(result[2:], b[:])
return result, nil
}
func (b *Proof) UnmarshalText(input []byte) error {
if len(input) < 2 {
return fmt.Errorf("hex string length must > 2 : current is %v", len(input))
}
raw := input[2:]
if len(raw) == 0 {
return nil
}
dec := Proof{}
if len(raw)/2 != len(dec[:]) {
return fmt.Errorf("hex string has length %d, want %d for %s", len(raw), len(dec[:])*2, "Proof")
}
if _, err := hex.Decode(dec[:], raw); err != nil {
return err
} else {
*b = dec
}
return nil
}
func (self *Proof) ToHash() (ret Uint256) {
d := sha3.NewKeccak256()
d.Write(self[:])
copy(ret[:], d.Sum(nil))
return
}