Bump dependencies, allow preallocated encode
This commit is contained in:
parent
7b24ed2d11
commit
18ecc51ae6
|
@ -13,12 +13,12 @@ type Address struct {
|
||||||
func (a *Address) Base58() (result string) {
|
func (a *Address) Base58() (result string) {
|
||||||
prefix := []byte{byte(a.network)}
|
prefix := []byte{byte(a.network)}
|
||||||
checksum := GetChecksum(prefix, a.spendingKey, a.viewingKey)
|
checksum := GetChecksum(prefix, a.spendingKey, a.viewingKey)
|
||||||
result = EncodeMoneroBase58(prefix, a.spendingKey, a.viewingKey, checksum[:])
|
result = string(EncodeMoneroBase58(prefix, a.spendingKey, a.viewingKey, checksum[:]))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAddress(address string) (result *Address, err string) {
|
func NewAddress(address string) (result *Address, err string) {
|
||||||
raw := DecodeMoneroBase58(address)
|
raw := DecodeMoneroBase58([]byte(address))
|
||||||
if len(raw) != 69 {
|
if len(raw) != 69 {
|
||||||
err = "Address is the wrong length"
|
err = "Address is the wrong length"
|
||||||
return
|
return
|
||||||
|
|
27
base58.go
27
base58.go
|
@ -59,7 +59,7 @@ func encodeChunkTail(raw []byte, buf []byte) []byte {
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeChunk(buf []byte, encoded string) []byte {
|
func decodeChunk(buf []byte, encoded []byte) []byte {
|
||||||
var intResult uint64
|
var intResult uint64
|
||||||
currentMultiplier := uint64(1)
|
currentMultiplier := uint64(1)
|
||||||
for i := len(encoded) - 1; i >= 0; i-- {
|
for i := len(encoded) - 1; i >= 0; i-- {
|
||||||
|
@ -94,32 +94,41 @@ func decodeChunk(buf []byte, encoded string) []byte {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func EncodeMoneroBase58(data ...[]byte) string {
|
func EncodeMoneroBase58(data ...[]byte) []byte {
|
||||||
|
return EncodeMoneroBase58PreAllocated(make([]byte, 0, func() (result int) {
|
||||||
|
for _, v := range data {
|
||||||
|
result += len(v)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}()), data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EncodeMoneroBase58PreAllocated(buf []byte, data ...[]byte) []byte {
|
||||||
//preallocate common case
|
//preallocate common case
|
||||||
combined := make([]byte, 0, 96)
|
combined := make([]byte, 0, 96)
|
||||||
for _, item := range data {
|
for _, item := range data {
|
||||||
combined = append(combined, item...)
|
combined = append(combined, item...)
|
||||||
}
|
}
|
||||||
|
|
||||||
result := make([]byte, 0, len(combined)*2)
|
result := buf
|
||||||
buf := make([]byte, 0, len(combined)*2)
|
tmpBuf := make([]byte, 0, len(combined)*2)
|
||||||
length := len(combined)
|
length := len(combined)
|
||||||
rounds := length / 8
|
rounds := length / 8
|
||||||
for i := 0; i < rounds; i++ {
|
for i := 0; i < rounds; i++ {
|
||||||
result = append(result, encodeChunk(combined[i*8:(i+1)*8], buf[:0])...)
|
result = append(result, encodeChunk(combined[i*8:(i+1)*8], tmpBuf[:0])...)
|
||||||
}
|
}
|
||||||
if length%8 > 0 {
|
if length%8 > 0 {
|
||||||
result = append(result, encodeChunkTail(combined[rounds*8:], buf[:0])...)
|
result = append(result, encodeChunkTail(combined[rounds*8:], tmpBuf[:0])...)
|
||||||
}
|
}
|
||||||
return string(result)
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeMoneroBase58(data string) (result []byte) {
|
func DecodeMoneroBase58(data []byte) (result []byte) {
|
||||||
//common case
|
//common case
|
||||||
return DecodeMoneroBase58PreAllocated(make([]byte, 0, 69), data)
|
return DecodeMoneroBase58PreAllocated(make([]byte, 0, 69), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeMoneroBase58PreAllocated(buf []byte, data string) (result []byte) {
|
func DecodeMoneroBase58PreAllocated(buf []byte, data []byte) (result []byte) {
|
||||||
result = buf
|
result = buf
|
||||||
length := len(data)
|
length := len(data)
|
||||||
rounds := length / 11
|
rounds := length / 11
|
||||||
|
|
4
go.mod
4
go.mod
|
@ -2,6 +2,6 @@ module git.gammaspectra.live/P2Pool/moneroutil
|
||||||
|
|
||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require golang.org/x/crypto v0.9.0
|
require golang.org/x/crypto v0.11.0
|
||||||
|
|
||||||
require golang.org/x/sys v0.8.0 // indirect
|
require golang.org/x/sys v0.10.0 // indirect
|
||||||
|
|
8
go.sum
8
go.sum
|
@ -1,4 +1,4 @@
|
||||||
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
|
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
|
||||||
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
|
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
|
||||||
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
|
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
|
||||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
|
Reference in a new issue