Im trying to create a transaction to activate a tron address after creating it. below is my code for creting the address:
<code> func CreateTRC20Address() (string, *ecdsa.PrivateKey, error) {
// Generate private key
privateKey, err := crypto.GenerateKey()
if err != nil {
return "", nil, err
}
fmt.Println("private key: " + fmt.Sprintf("%v", privateKey))
// Get public key from private key
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return "", nil, fmt.Errorf("error casting public key to ECDSA")
}
// Step 1: Hash the public key using SHA-256
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
sha256Hash := sha256.New()
sha256Hash.Write(publicKeyBytes)
sha256HashedPubKey := sha256Hash.Sum(nil)
// Step 2: Hash the result using RIPEMD-160
ripemd160Hash := ripemd160.New()
ripemd160Hash.Write(sha256HashedPubKey)
ripemd160HashedPubKey := ripemd160Hash.Sum(nil)
// Step 3: Add Tron address prefix (0x41) to the RIPEMD-160 hash
tronAddressBytes := append([]byte{0x41}, ripemd160HashedPubKey...)
// Step 4: Base58Check encode the Tron address
checkSum := sha256.Sum256(tronAddressBytes)
checkSum = sha256.Sum256(checkSum[:])
tronAddressBytes = append(tronAddressBytes, checkSum[:4]...)
// Base58 encode the final Tron address
tronAddress := base58.Encode(tronAddressBytes)
return tronAddress, privateKey, nil
}
</code>
<code> func CreateTRC20Address() (string, *ecdsa.PrivateKey, error) {
// Generate private key
privateKey, err := crypto.GenerateKey()
if err != nil {
return "", nil, err
}
fmt.Println("private key: " + fmt.Sprintf("%v", privateKey))
// Get public key from private key
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return "", nil, fmt.Errorf("error casting public key to ECDSA")
}
// Step 1: Hash the public key using SHA-256
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
sha256Hash := sha256.New()
sha256Hash.Write(publicKeyBytes)
sha256HashedPubKey := sha256Hash.Sum(nil)
// Step 2: Hash the result using RIPEMD-160
ripemd160Hash := ripemd160.New()
ripemd160Hash.Write(sha256HashedPubKey)
ripemd160HashedPubKey := ripemd160Hash.Sum(nil)
// Step 3: Add Tron address prefix (0x41) to the RIPEMD-160 hash
tronAddressBytes := append([]byte{0x41}, ripemd160HashedPubKey...)
// Step 4: Base58Check encode the Tron address
checkSum := sha256.Sum256(tronAddressBytes)
checkSum = sha256.Sum256(checkSum[:])
tronAddressBytes = append(tronAddressBytes, checkSum[:4]...)
// Base58 encode the final Tron address
tronAddress := base58.Encode(tronAddressBytes)
return tronAddress, privateKey, nil
}
</code>
func CreateTRC20Address() (string, *ecdsa.PrivateKey, error) {
// Generate private key
privateKey, err := crypto.GenerateKey()
if err != nil {
return "", nil, err
}
fmt.Println("private key: " + fmt.Sprintf("%v", privateKey))
// Get public key from private key
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return "", nil, fmt.Errorf("error casting public key to ECDSA")
}
// Step 1: Hash the public key using SHA-256
publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
sha256Hash := sha256.New()
sha256Hash.Write(publicKeyBytes)
sha256HashedPubKey := sha256Hash.Sum(nil)
// Step 2: Hash the result using RIPEMD-160
ripemd160Hash := ripemd160.New()
ripemd160Hash.Write(sha256HashedPubKey)
ripemd160HashedPubKey := ripemd160Hash.Sum(nil)
// Step 3: Add Tron address prefix (0x41) to the RIPEMD-160 hash
tronAddressBytes := append([]byte{0x41}, ripemd160HashedPubKey...)
// Step 4: Base58Check encode the Tron address
checkSum := sha256.Sum256(tronAddressBytes)
checkSum = sha256.Sum256(checkSum[:])
tronAddressBytes = append(tronAddressBytes, checkSum[:4]...)
// Base58 encode the final Tron address
tronAddress := base58.Encode(tronAddressBytes)
return tronAddress, privateKey, nil
}
And below is my code for activating the new trc 20 address by sending 0.1trx:
<code> func CreateTransaction(fromAddress, toAddress string, amount int64) (*Transaction, error) {
// Convert fromAddress and toAddress to hex
fromAddressHex, err := ConvertBase58CheckToHex(fromAddress)
if err != nil {
return nil, fmt.Errorf("invalid fromAddress: %v", err)
}
toAddressHex, err := ConvertBase58CheckToHex(toAddress)
if err != nil {
return nil, fmt.Errorf("invalid toAddress: %v", err)
}
fmt.Printf("fromAddressHex: %s, toAddressHex: %sn", fromAddressHex, toAddressHex)
url := fmt.Sprintf("%s/wallet/createtransaction", tronGridBaseURL)
payload := map[string]interface{}{
"to_address": toAddressHex, // Use hex format
"owner_address": fromAddressHex, // Use hex format
"amount": amount, // Amount in SUN
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal payload: %v", err)
}
fmt.Printf("CreateTransaction Payload: %sn", string(jsonPayload))
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, fmt.Errorf("failed to send create transaction request: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
fmt.Printf("CreateTransaction Response: %sn", string(body))
var transaction Transaction
err = json.Unmarshal(body, &transaction)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal transaction: %v", err)
}
return &transaction, nil
}
</code>
<code> func CreateTransaction(fromAddress, toAddress string, amount int64) (*Transaction, error) {
// Convert fromAddress and toAddress to hex
fromAddressHex, err := ConvertBase58CheckToHex(fromAddress)
if err != nil {
return nil, fmt.Errorf("invalid fromAddress: %v", err)
}
toAddressHex, err := ConvertBase58CheckToHex(toAddress)
if err != nil {
return nil, fmt.Errorf("invalid toAddress: %v", err)
}
fmt.Printf("fromAddressHex: %s, toAddressHex: %sn", fromAddressHex, toAddressHex)
url := fmt.Sprintf("%s/wallet/createtransaction", tronGridBaseURL)
payload := map[string]interface{}{
"to_address": toAddressHex, // Use hex format
"owner_address": fromAddressHex, // Use hex format
"amount": amount, // Amount in SUN
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal payload: %v", err)
}
fmt.Printf("CreateTransaction Payload: %sn", string(jsonPayload))
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, fmt.Errorf("failed to send create transaction request: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
fmt.Printf("CreateTransaction Response: %sn", string(body))
var transaction Transaction
err = json.Unmarshal(body, &transaction)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal transaction: %v", err)
}
return &transaction, nil
}
</code>
func CreateTransaction(fromAddress, toAddress string, amount int64) (*Transaction, error) {
// Convert fromAddress and toAddress to hex
fromAddressHex, err := ConvertBase58CheckToHex(fromAddress)
if err != nil {
return nil, fmt.Errorf("invalid fromAddress: %v", err)
}
toAddressHex, err := ConvertBase58CheckToHex(toAddress)
if err != nil {
return nil, fmt.Errorf("invalid toAddress: %v", err)
}
fmt.Printf("fromAddressHex: %s, toAddressHex: %sn", fromAddressHex, toAddressHex)
url := fmt.Sprintf("%s/wallet/createtransaction", tronGridBaseURL)
payload := map[string]interface{}{
"to_address": toAddressHex, // Use hex format
"owner_address": fromAddressHex, // Use hex format
"amount": amount, // Amount in SUN
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal payload: %v", err)
}
fmt.Printf("CreateTransaction Payload: %sn", string(jsonPayload))
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, fmt.Errorf("failed to send create transaction request: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
fmt.Printf("CreateTransaction Response: %sn", string(body))
var transaction Transaction
err = json.Unmarshal(body, &transaction)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal transaction: %v", err)
}
return &transaction, nil
}
But the response I’m getting is as below:
Create Transaction Response: {“Error”:”class org.tron.core.services.http.JsonFormat$ParseException : 1:34: INVALID hex String”}.
What might I be doing wrong?