-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
131 lines (101 loc) · 3.43 KB
/
convert.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
package bananogo
import (
"encoding/binary"
"encoding/hex"
"filippo.io/edwards25519"
"fmt"
"github.com/shopspring/decimal"
"golang.org/x/crypto/blake2b"
"strings"
)
// SeedToPrivateKey converts a seed to a private key,
// seed: the seed to convert,
// index: the index of the private key to generate,
// returns the private key or an error.
func SeedToPrivateKey(seed string, index int) ([32]byte, error) {
seedBytes, err := hex.DecodeString(seed)
if err != nil {
return [32]byte{}, fmt.Errorf("could not decode seed: %v", err)
}
if len(seedBytes) != 32 {
return [32]byte{}, fmt.Errorf("seed length is not 32 bytes")
}
iBytes := make([]byte, 4)
binary.BigEndian.PutUint32(iBytes, uint32(index))
comb := append(seedBytes, iBytes...)
privKeyBytes := blake2b.Sum256(comb)
return privKeyBytes, nil
}
// PrivateKeyToPublicKey converts a private key to a public key,
// privateKey: the private key to convert,
// returns the public key or an error.
func PrivateKeyToPublicKey(privateKey [32]byte) ([32]byte, error) {
hashBytes := blake2b.Sum512(privateKey[:])
scalar, err := new(edwards25519.Scalar).SetBytesWithClamping(hashBytes[:32])
if err != nil {
return [32]byte{}, err
}
pubKeyBytes := new(edwards25519.Point).ScalarBaseMult(scalar).Bytes()
return [32]byte(pubKeyBytes), nil
}
// PublicKeyToAddress converts a public key to a wallet address,
// publicKey: the public key to convert,
// returns the address or an error.
func PublicKeyToAddress(publicKey [32]byte) (string, error) {
b32PubKey := base32Encode(publicKey[:])
h, err := blake2b.New(5, nil)
if err != nil {
return "", err
}
if _, err := h.Write(publicKey[:]); err != nil {
return "", err
}
hashBytes := h.Sum(nil)
b32Hash := base32Encode(revertBytes(hashBytes))
address := "ban_" + strings.Repeat("1", 52-len(b32PubKey)) + b32PubKey + strings.Repeat("1", 8-len(b32Hash)) + b32Hash
return address, nil
}
// AddressToPublicKey converts a wallet address to a public key,
// address: the address to get the public key from,
// returns the public key or an error.
func AddressToPublicKey(address string) ([32]byte, error) {
if len(address) == 64 {
bytes, err := base32Decode(address[4:56])
return [32]byte(bytes), err
} else if len(address) == 65 {
bytes, err := base32Decode(address[5:57])
return [32]byte(bytes), err
}
return [32]byte{}, fmt.Errorf("could not parse address (%s)", address)
}
// BananoToRaw converts a banano amount to raw,
// banano: the banano amount to convert,
// returns the raw or an error.
func BananoToRaw(banano string) (string, error) {
bananoDec, err := decimal.NewFromString(banano)
if err != nil {
return "", fmt.Errorf("could not parse nano: %v", err)
}
rawPerBanano, err := decimal.NewFromString("100000000000000000000000000000")
if err != nil {
return "", fmt.Errorf("could not parse raw per banano: %v", err)
}
raw := bananoDec.Mul(rawPerBanano)
return raw.String(), nil
}
// RawToBanano converts a raw to banano amount,
// raw: the raw to convert,
// returns the banano amount or an error.
func RawToBanano(raw string) (string, error) {
decimal.DivisionPrecision = 30
rawDec, err := decimal.NewFromString(raw)
if err != nil {
return "", fmt.Errorf("could not parse raw: %v", err)
}
rawPerBanano, err := decimal.NewFromString("100000000000000000000000000000")
if err != nil {
return "", fmt.Errorf("could not parse raw per banano: %v", err)
}
banano := rawDec.Div(rawPerBanano)
return banano.String(), nil
}