Skip to content

Commit

Permalink
address from the seed & add funcs TronAddress (#2121)
Browse files Browse the repository at this point in the history
* AddressSeed gets int64 address from the seed.

    * format: `IBAX Address By Seed:` + seed

* add funcs TronAddress

* base58 package
  • Loading branch information
powerpook authored Jun 1, 2023
1 parent 5ab5ed8 commit e36d270
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
66 changes: 66 additions & 0 deletions packages/common/crypto/base58/base58.go
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]
}
}
9 changes: 9 additions & 0 deletions packages/common/crypto/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func Address(pubKey []byte) int64 {
return buildChecksumConvert(crc)
}

// AddressSeed gets int64 address from the seed.
// format: `IBAX Address By Seed:` + seed
func AddressSeed(seed string) int64 {
h := Hash([]byte("IBAX Address By Seed:" + seed))
h512 := sha512.Sum512(h[:])
crc := CalcChecksum(h512[:])
return buildChecksumConvert(crc)
}

func buildChecksumConvert(crc uint64) int64 {
num := strconv.FormatUint(crc, 10)
val := RepeatPrefixed(num)
Expand Down
11 changes: 11 additions & 0 deletions packages/smart/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"time"
"unicode/utf8"

"github.com/IBAX-io/go-ibax/packages/common/crypto/base58"

"golang.org/x/crypto/sha3"

"github.com/IBAX-io/go-ibax/packages/clbmanager"
Expand Down Expand Up @@ -217,6 +219,7 @@ func EmbedFuncs(vt script.VMType) map[string]any {
"Replace": Replace,
"Size": Size,
"PubToID": PubToID,
"SeedToID": crypto.AddressSeed,
"HexToBytes": HexToBytes,
"LangRes": LangRes,
"HasPrefix": strings.HasPrefix,
Expand Down Expand Up @@ -247,6 +250,7 @@ func EmbedFuncs(vt script.VMType) map[string]any {
"SortedKeys": SortedKeys,
"Append": Append,
"EthereumAddress": EthereumAddress,
"TronAddress": TronAddress,
"GetLogTxCount": GetLogTxCount,
"GetHistory": GetHistory,
"GetHistoryRow": GetHistoryRow,
Expand Down Expand Up @@ -2031,6 +2035,13 @@ func EthereumAddress(pub []byte) string {
return addr.Hex()
}

func TronAddress(pub []byte) (string, error) {
keccak256 := &hashalgo.Keccak256{}
hash := keccak256.GetHash(pub[:])
hex20 := "41" + hex.EncodeToString(hash[len(hash)-20:])
return base58.FromHexAddress(hex20)
}

func GetLogTxCount(sc *SmartContract, ecosystemID int64) (int64, error) {
return sqldb.GetLogTxCount(sc.DbTransaction, ecosystemID)
}
Expand Down

0 comments on commit e36d270

Please sign in to comment.