-
Notifications
You must be signed in to change notification settings - Fork 4
/
domaintypes.go
153 lines (140 loc) · 4.28 KB
/
domaintypes.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package forge
import (
"encoding/hex"
"fmt"
"strings"
"github.com/dipdup-net/go-lib/tools/consts"
"github.com/dipdup-net/go-lib/tools/encoding"
"github.com/pkg/errors"
)
// Address -
func Address(val string, tzOnly bool) ([]byte, error) {
prefix := val[:3]
address, err := encoding.DecodeBase58(val)
if err != nil {
return nil, err
}
switch prefix {
case encoding.PrefixPublicKeyTZ1:
address = append([]byte{0, 0}, address...)
case encoding.PrefixPublicKeyTZ2:
address = append([]byte{0, 1}, address...)
case encoding.PrefixPublicKeyTZ3:
address = append([]byte{0, 2}, address...)
case encoding.PrefixPublicKeyKT1:
address = append([]byte{1}, address...)
address = append(address, byte(0))
default:
return nil, errors.Errorf("Invalid address prefix: %s", prefix)
}
if tzOnly {
return address[1:], nil
}
return address, nil
}
// UnforgeAddress -
func UnforgeAddress(str string) (string, error) {
if len(str) != 44 && len(str) != 42 {
return str, errors.Wrapf(consts.ErrInvalidAddress, "UnforgeAddress: %s", str)
}
switch {
case len(str) == 42:
return UnforgeAddress("00" + str)
case strings.HasPrefix(str, "0000"):
return encoding.EncodeBase58String(str[4:], []byte(encoding.PrefixPublicKeyTZ1))
case strings.HasPrefix(str, "0001"):
return encoding.EncodeBase58String(str[4:], []byte(encoding.PrefixPublicKeyTZ2))
case strings.HasPrefix(str, "0002"):
return encoding.EncodeBase58String(str[4:], []byte(encoding.PrefixPublicKeyTZ3))
case strings.HasPrefix(str, "01") && strings.HasSuffix(str, "00"):
return encoding.EncodeBase58String(str[2:len(str)-2], []byte(encoding.PrefixPublicKeyKT1))
default:
return str, errors.Wrapf(consts.ErrInvalidAddress, "UnforgeAddress: %s", str)
}
}
// Contract -
func Contract(val string) (string, error) {
parts := strings.Split(val, "%")
if len(parts) == 1 {
parts = append(parts, consts.DefaultEntrypoint)
}
res, err := Address(parts[0], false)
if err != nil {
return "", err
}
if parts[1] != consts.DefaultEntrypoint {
decoded, err := hex.DecodeString(parts[1])
if err != nil {
return "", err
}
res = append(res, decoded...)
}
return hex.EncodeToString(res), nil
}
// UnforgeContract -
func UnforgeContract(str string) (string, error) {
if len(str) < 44 {
return "", errors.Wrapf(consts.ErrInvalidAddress, "UnforgeContract: %s", str)
}
res, err := UnforgeAddress(str[:44])
if err != nil {
return "", err
}
if len(str) > 44 {
return fmt.Sprintf("%s%%%s", res, str[44:]), nil
}
return res, nil
}
// PublicKey -
func PublicKey(val string) ([]byte, error) {
prefix := val[4:]
decoded, err := encoding.DecodeBase58String(val)
if err != nil {
return nil, err
}
switch prefix {
case encoding.PrefixED25519PublicKey:
return append([]byte{0}, decoded...), nil
case encoding.PrefixSecp256k1PublicKey:
return append([]byte{1}, decoded...), nil
case encoding.PrefixP256PublicKey:
return append([]byte{2}, decoded...), nil
default:
return nil, errors.Errorf("Invalid public key prefix: %s", prefix)
}
}
// UnforgePublicKey -
func UnforgePublicKey(str string) (string, error) {
if len(str) != 68 && len(str) != 66 {
return "", errors.Wrapf(consts.ErrInvalidAddress, "UnforgePublicKey: %s", str)
}
switch {
case strings.HasPrefix(str, "00"):
return encoding.EncodeBase58String(str[2:], []byte(encoding.PrefixED25519PublicKey))
case strings.HasPrefix(str, "01"):
return encoding.EncodeBase58String(str[2:], []byte(encoding.PrefixSecp256k1PublicKey))
case strings.HasPrefix(str, "02"):
return encoding.EncodeBase58String(str[2:], []byte(encoding.PrefixP256PublicKey))
default:
return str, nil
}
}
// UnforgeChainID -
func UnforgeChainID(str string) (string, error) {
return encoding.EncodeBase58String(str, []byte(encoding.PrefixChainID))
}
// UnforgeSignature -
func UnforgeSignature(str string) (string, error) {
return encoding.EncodeBase58String(str, []byte(encoding.PrefixGenericSignature))
}
// UnforgeBakerHash -
func UnforgeBakerHash(str string) (string, error) {
return encoding.EncodeBase58String(str, []byte(encoding.PrefixBakerHash))
}
// UnforgeOpgHash -
func UnforgeOpgHash(input string) (string, error) {
if len(input) != 51 {
return "", errors.Wrapf(consts.ErrInvalidOPGHash, "UnforgeOpgHash: %s", input)
}
return encoding.EncodeBase58String(input, []byte(encoding.PrefixOperationHash))
}