-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
address from the seed & add funcs TronAddress (#2121)
* AddressSeed gets int64 address from the seed. * format: `IBAX Address By Seed:` + seed * add funcs TronAddress * base58 package
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package base58 | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"math/big" | ||
) | ||
|
||
var base58Alphabets = []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") | ||
|
||
func ToHexAddress(address string) string { | ||
return hex.EncodeToString(base58Decode([]byte(address))) | ||
} | ||
|
||
func FromHexAddress(hexAddress string) (string, error) { | ||
addrByte, err := hex.DecodeString(hexAddress) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
sha := sha256.New() | ||
sha.Write(addrByte) | ||
shaStr := sha.Sum(nil) | ||
|
||
sha2 := sha256.New() | ||
sha2.Write(shaStr) | ||
shaStr2 := sha2.Sum(nil) | ||
|
||
addrByte = append(addrByte, shaStr2[:4]...) | ||
return string(base58Encode(addrByte)), nil | ||
} | ||
|
||
func base58Encode(input []byte) []byte { | ||
x := big.NewInt(0).SetBytes(input) | ||
base := big.NewInt(58) | ||
zero := big.NewInt(0) | ||
mod := &big.Int{} | ||
var result []byte | ||
for x.Cmp(zero) != 0 { | ||
x.DivMod(x, base, mod) | ||
result = append(result, base58Alphabets[mod.Int64()]) | ||
} | ||
reverseBytes(result) | ||
return result | ||
} | ||
|
||
func base58Decode(input []byte) []byte { | ||
result := big.NewInt(0) | ||
for _, b := range input { | ||
charIndex := bytes.IndexByte(base58Alphabets, b) | ||
result.Mul(result, big.NewInt(58)) | ||
result.Add(result, big.NewInt(int64(charIndex))) | ||
} | ||
decoded := result.Bytes() | ||
if input[0] == base58Alphabets[0] { | ||
decoded = append([]byte{0x00}, decoded...) | ||
} | ||
return decoded[:len(decoded)-4] | ||
} | ||
|
||
func reverseBytes(data []byte) { | ||
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 { | ||
data[i], data[j] = data[j], data[i] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters