59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package aptos
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// GetTransactionOwnerAddress get Aptos transaction address
|
|
func GetTransactionOwnerAddress(contract, txHash, aptosNet string) (string, error) {
|
|
if aptosNet == "" {
|
|
aptosNet = "mainnet"
|
|
}
|
|
url := fmt.Sprintf("https://fullnode.%s.aptoslabs.com/v1/transactions/by_hash/%s", aptosNet, txHash)
|
|
|
|
// Make a GET request to fetch transaction details
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var result map[string]interface{}
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
payload, exists := result["payload"].(map[string]interface{})
|
|
if !exists || payload["function"] == nil || payload["arguments"] == nil {
|
|
return "", errors.New("invalid transaction format")
|
|
}
|
|
|
|
// Validate contract address
|
|
function := payload["function"].(string)
|
|
parts := strings.Split(function, "::")
|
|
if len(parts) < 2 || parts[0]+"::"+parts[1] != contract {
|
|
return "", errors.New("invalid contract address")
|
|
}
|
|
|
|
// Check if address exists
|
|
address, ok := payload["arguments"].([]interface{})[0].(string)
|
|
if !ok {
|
|
return "", errors.New("invalid address format")
|
|
}
|
|
return address, nil
|
|
}
|
|
|
|
// StrPadAptosAddress aptos 地址补全
|
|
func StrPadAptosAddress(address string) string {
|
|
if len(address) >= 66 {
|
|
return address
|
|
}
|
|
perfix := "0000000000000000000000000000000000000000000000000000000000000000"
|
|
address = address[2:]
|
|
return "0x" + perfix[:64-len(address)] + address
|
|
}
|