Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix wallet address #13

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 60 additions & 19 deletions core/walletaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package core
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"os"
"strings"

"github.com/ethereum/go-ethereum/crypto"
"github.com/mr-tron/base58/base58"
"github.com/mr-tron/base58"
"github.com/tyler-smith/go-bip32"
"github.com/tyler-smith/go-bip39"
"golang.org/x/crypto/pbkdf2"
Expand Down Expand Up @@ -40,7 +40,7 @@ func GenerateWalletAddress() {
}

// Check if MNEMONIC is set
if mnemonic := os.Getenv("MNEMONIC"); mnemonic != "" {
if mnemonic := os.Getenv("MNEMONIC_SUI"); mnemonic != "" {
GenerateWalletAddressSui(mnemonic)
return
}
Expand Down Expand Up @@ -105,10 +105,36 @@ func GenerateEthereumWalletAddress(mnemonic string) {
keccak.Write(publicKeyBytes[1:]) // Skip the first byte (0x04) of the uncompressed public key
walletAddress := keccak.Sum(nil)[12:] // Take the last 20 bytes

WalletAddress = hex.EncodeToString(walletAddress)
// Convert to checksummed address
WalletAddress = toChecksumAddress(hex.EncodeToString(walletAddress))
log.Println("Ethereum Wallet Address:", WalletAddress)
}

// toChecksumAddress converts an address to checksummed format
func toChecksumAddress(address string) string {
address = strings.ToLower(address)
keccak := sha3.NewLegacyKeccak256()
keccak.Write([]byte(address))
hash := keccak.Sum(nil)

var checksumAddress strings.Builder
checksumAddress.WriteString("0x")

for i, c := range address {
if c >= '0' && c <= '9' {
checksumAddress.WriteRune(c)
} else {
if hash[i/2]>>uint(4*(1-i%2))&0xF >= 8 {
checksumAddress.WriteRune(c - 'a' + 'A')
} else {
checksumAddress.WriteRune(c)
}
}
}

return checksumAddress.String()
}

// GenerateWalletAddressSolana generates a Solana wallet address from the given mnemonic
func GenerateWalletAddressSolana(mnemonic string) {
// Validate the mnemonic
Expand All @@ -133,7 +159,7 @@ func GenerateWalletAddressSolana(mnemonic string) {
// Encode the public key to Base58
WalletAddress = base58.Encode(publicKey)

fmt.Printf("Wallet Address: %s\n", WalletAddress)
fmt.Printf("Solona Wallet Address: %s\n", WalletAddress)
}

// GenerateWalletAddressSui generates a Sui wallet address from the given mnemonic
Expand All @@ -153,8 +179,24 @@ func GenerateWalletAddressSui(mnemonic string) {
log.Fatal(err)
}

// Derive a child key
childKey, err := masterKey.NewChildKey(bip32.FirstHardenedChild)
// Derive a child key (using the Sui derivation path m/44'/784'/0'/0/0)
childKey, err := masterKey.NewChildKey(bip32.FirstHardenedChild + 44)
if err != nil {
log.Fatal(err)
}
childKey, err = childKey.NewChildKey(bip32.FirstHardenedChild + 784)
if err != nil {
log.Fatal(err)
}
childKey, err = childKey.NewChildKey(bip32.FirstHardenedChild + 0)
if err != nil {
log.Fatal(err)
}
childKey, err = childKey.NewChildKey(0)
if err != nil {
log.Fatal(err)
}
childKey, err = childKey.NewChildKey(0)
if err != nil {
log.Fatal(err)
}
Expand All @@ -166,14 +208,13 @@ func GenerateWalletAddressSui(mnemonic string) {
log.Println("Private Key:", hex.EncodeToString(privateKey))
log.Println("Public Key:", hex.EncodeToString(publicKey))

// Generate wallet address
hash := sha256.Sum256(publicKey)
walletAddress := hex.EncodeToString(hash[:])
log.Println("Wallet Address:", walletAddress)
// Generate wallet address (using SHA3-256)
hash := sha3.New256()
hash.Write(publicKey)
walletAddress := hash.Sum(nil)

// Assign the wallet address to the global variable
WalletAddress = walletAddress
log.Println("The final wallet address:", WalletAddress)
WalletAddress = "0x" + hex.EncodeToString(walletAddress)
log.Println("Sui Wallet Address:", WalletAddress)
}

// GenerateWalletAddressAptos generates an Aptos wallet address from the given mnemonic
Expand Down Expand Up @@ -222,11 +263,11 @@ func GenerateWalletAddressAptos(mnemonic string) {
log.Println("Private Key:", hex.EncodeToString(privateKey))
log.Println("Public Key:", hex.EncodeToString(publicKey))

// Generate wallet address
address := sha3.Sum256(publicKey[:])
walletAddress := hex.EncodeToString(address[:])
// Generate wallet address (using SHA3-256)
hash := sha3.New256()
hash.Write(publicKey)
walletAddress := hash.Sum(nil)

// Assign the wallet address to the global variable
WalletAddress = "0x" + walletAddress
WalletAddress = "0x" + hex.EncodeToString(walletAddress)
log.Println("Aptos Wallet Address:", WalletAddress)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ require (
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSD
github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw=
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k=
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
Expand Down