/
utils.go
96 lines (86 loc) · 2.96 KB
/
utils.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package objs
import (
"github.com/MadBase/MadNet/constants"
"github.com/MadBase/MadNet/crypto"
"github.com/MadBase/MadNet/errorz"
"github.com/MadBase/MadNet/utils"
)
// MakeUTXOID will create the UTXOID for a utxo given a transaction hash and
// index.
func MakeUTXOID(txHash []byte, idx uint32) []byte {
if idx == constants.MaxUint32 {
return utils.CopySlice(txHash)
}
idxBytes := utils.MarshalUint32(idx)
msg := utils.CopySlice(txHash)
msg = append(msg, idxBytes...)
return crypto.Hasher(msg)
}
func extractSignature(owner []byte, curveSpec constants.CurveSpec) ([]byte, []byte, error) {
switch curveSpec {
case constants.CurveSecp256k1:
if len(owner) < constants.CurveSecp256k1SigLen {
return nil, nil, errorz.ErrInvalid{}.New("Invalid Secp256k1 signature")
}
return owner[0:constants.CurveSecp256k1SigLen], utils.CopySlice(owner[constants.CurveSecp256k1SigLen:]), nil
case constants.CurveBN256Eth:
if len(owner) < constants.CurveBN256EthSigLen {
return nil, nil, errorz.ErrInvalid{}.New("Invalid BN256Eth signature")
}
return owner[0:constants.CurveBN256EthSigLen], utils.CopySlice(owner[constants.CurveBN256EthSigLen:]), nil
default:
return nil, nil, errorz.ErrInvalid{}.New("Invalid curveSpec")
}
}
func validateSignatureLen(sig []byte, curveSpec constants.CurveSpec) error {
switch curveSpec {
case constants.CurveSecp256k1:
if len(sig) == constants.CurveSecp256k1SigLen {
return nil
}
return errorz.ErrInvalid{}.New("Invalid secp sig len")
case constants.CurveBN256Eth:
if len(sig) == constants.CurveBN256EthSigLen {
return nil
}
return errorz.ErrInvalid{}.New("Invalid bn sig len")
default:
return errorz.ErrInvalid{}.New("Invalid curveSpec")
}
}
func extractCurveSpec(owner []byte) (constants.CurveSpec, []byte, error) {
if len(owner) < 1 {
return 0, nil, errorz.ErrInvalid{}.New("Invalid curveSpec")
}
return constants.CurveSpec(owner[0]), utils.CopySlice(owner[1:]), nil
}
func extractSignerRole(owner []byte) (SignerRole, []byte, error) {
if len(owner) < 1 {
return 0, nil, errorz.ErrInvalid{}.New("Invalid SignerRole")
}
return SignerRole(owner[0]), utils.CopySlice(owner[1:]), nil
}
func extractAccount(owner []byte) ([]byte, []byte, error) {
if len(owner) < constants.OwnerLen {
return nil, nil, errorz.ErrInvalid{}.New("Invalid Account length")
}
return owner[0:constants.OwnerLen], utils.CopySlice(owner[constants.OwnerLen:]), nil
}
func extractSVA(owner []byte) (SVA, []byte, error) {
if len(owner) < 1 {
return 0, nil, errorz.ErrInvalid{}.New("extractSVA: Invalid SVA")
}
return SVA(owner[0]), utils.CopySlice(owner[1:]), nil
}
func extractHash(owner []byte) ([]byte, []byte, error) {
if len(owner) < constants.HashLen {
return nil, nil, errorz.ErrInvalid{}.New("extractHash: Invalid Hash")
}
return owner[0:constants.HashLen], utils.CopySlice(owner[constants.HashLen:]), nil
}
func extractZero(owner []byte) error {
if len(owner) != 0 {
return errorz.ErrInvalid{}.New("bytes remaining at end")
}
return nil
}