forked from echovl/cardano-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
address.go
87 lines (73 loc) · 1.86 KB
/
address.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
package cardano
import (
"github.com/echovl/bech32"
"github.com/tclairet/cardano-go/crypto"
"golang.org/x/crypto/blake2b"
)
type Network byte
const (
Testnet Network = 0
Mainnet Network = 1
)
// Address is the bech32 representation of a cardano address
type Address string
// Bytes returns the byte slice representation of the address.
func (addr *Address) Bytes() []byte {
_, bytes, err := bech32.DecodeToBase256(string(*addr))
if err != nil {
panic(err)
}
return bytes
}
func DecodeAddress(data []byte) (Address, Address, error) {
testnet, err := bech32.EncodeFromBase256("addr_test", data)
if err != nil {
return "", "", err
}
mainnet, err := bech32.EncodeFromBase256("addr", data)
if err != nil {
return "", "", err
}
return Address(mainnet), Address(testnet), nil
}
func NewEnterpriseAddress(xvk crypto.ExtendedVerificationKey, network Network) Address {
addressBytes := make([]byte, 29)
header := 0x60 | (byte(network) & 0xFF)
hash, err := blake2b.New(224/8, nil)
if err != nil {
panic(err)
}
hash.Write(xvk[:32])
paymentHash := hash.Sum(nil)
addressBytes[0] = header
copy(addressBytes[1:], paymentHash)
hrp := getHrp(network)
address, err := bech32.EncodeFromBase256(hrp, addressBytes)
if err != nil {
panic(err)
}
return Address(address)
}
// Bech32ToAddress creates an Address from a bech32 encoded string.
func Bech32ToAddress(addr string) (Address, error) {
_, _, err := bech32.DecodeToBase256(addr)
if err != nil {
return "", err
}
return Address(addr), nil
}
// BytesToAddress creates an Address from a byte slice.
func BytesToAddress(addr []byte, network Network) (Address, error) {
encoded, err := bech32.EncodeFromBase256(getHrp(network), addr)
if err != nil {
return "", nil
}
return Address(encoded), nil
}
func getHrp(network Network) string {
if network == Testnet {
return "addr_test"
} else {
return "addr"
}
}