完善数据上报,每日支付任务认证
This commit is contained in:
59
internal/pkg/aptos/aptos.go
Normal file
59
internal/pkg/aptos/aptos.go
Normal file
@@ -0,0 +1,59 @@
|
||||
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, "::")
|
||||
fmt.Println(parts[0] + "::" + parts[1])
|
||||
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
|
||||
}
|
||||
17
internal/pkg/aptos/aptos_test.go
Normal file
17
internal/pkg/aptos/aptos_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package aptos
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetTransactionOwnerAddress(t *testing.T) {
|
||||
addr, err := GetTransactionOwnerAddress("0x5a0ad9e31a2f452504429b6f7073cb325994c2c66204f5deb8e0561a9e950c3c::TeviStar", "0xaa42ec3ca61f398e52ed05fe12daee7df768468e77079184665846a1a1891217", "")
|
||||
require.Nil(t, err)
|
||||
fmt.Println(addr)
|
||||
}
|
||||
|
||||
func TestStrPadAptosAddress(t *testing.T) {
|
||||
fmt.Println(StrPadAptosAddress("0x69d09434572ff312fcd4dc805318c29acf8e5daa2938cd38e674b7eb446063"))
|
||||
}
|
||||
Reference in New Issue
Block a user