fixed db operations #434
@@ -2,9 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
storage "github.com/tech/sendico/gateway/mntx/storage"
|
||||
"github.com/tech/sendico/gateway/mntx/storage/model"
|
||||
@@ -12,9 +10,7 @@ import (
|
||||
ri "github.com/tech/sendico/pkg/db/repository/index"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -26,7 +22,7 @@ const (
|
||||
|
||||
type Payouts struct {
|
||||
logger mlogger.Logger
|
||||
coll *mongo.Collection
|
||||
repository repository.Repository
|
||||
}
|
||||
|
||||
func NewPayouts(logger mlogger.Logger, db *mongo.Database) (*Payouts, error) {
|
||||
@@ -51,37 +47,19 @@ func NewPayouts(logger mlogger.Logger, db *mongo.Database) (*Payouts, error) {
|
||||
|
||||
p := &Payouts{
|
||||
logger: logger,
|
||||
coll: db.Collection(payoutsCollection),
|
||||
repository: repo,
|
||||
}
|
||||
p.logger.Debug("Payouts store initialised")
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *Payouts) findOneByField(ctx context.Context, field, value string) (*model.CardPayout, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return nil, merrors.InvalidArgument("lookup key is required", field)
|
||||
}
|
||||
|
||||
var result model.CardPayout
|
||||
err := p.coll.FindOne(ctx, bson.M{field: value}).Decode(&result)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
||||
p.logger.Warn("Payout record lookup failed",
|
||||
zap.String("field", field),
|
||||
zap.String("value", value),
|
||||
zap.Error(err))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
var res model.CardPayout
|
||||
return &res, p.repository.FindOneByFilter(ctx, repository.Filter(field, value), &res)
|
||||
}
|
||||
|
||||
func (p *Payouts) FindByIdempotencyKey(ctx context.Context, key string) (*model.CardPayout, error) {
|
||||
return p.findOneByField(ctx, payoutIdemField, key) // operationRef
|
||||
return p.findOneByField(ctx, payoutIdemField, key)
|
||||
}
|
||||
|
||||
func (p *Payouts) FindByPaymentID(ctx context.Context, paymentID string) (*model.CardPayout, error) {
|
||||
@@ -103,35 +81,7 @@ func (p *Payouts) Upsert(ctx context.Context, record *model.CardPayout) error {
|
||||
return merrors.InvalidArgument("operation ref is required", "operation_ref")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if record.CreatedAt.IsZero() {
|
||||
record.CreatedAt = now
|
||||
}
|
||||
record.UpdatedAt = now
|
||||
|
||||
// critical: never let caller override _id
|
||||
record.ID = bson.NilObjectID
|
||||
|
||||
update := bson.M{
|
||||
"$set": record,
|
||||
}
|
||||
|
||||
_, err := p.coll.UpdateOne(
|
||||
ctx,
|
||||
bson.M{payoutIdemField: record.OperationRef},
|
||||
update,
|
||||
options.UpdateOne().SetUpsert(true),
|
||||
)
|
||||
if err != nil {
|
||||
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
||||
p.logger.Warn("Failed to upsert payout record",
|
||||
zap.String("operation_ref", record.OperationRef),
|
||||
zap.String("payout_id", record.PayoutID),
|
||||
zap.Error(err))
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return p.repository.Insert(ctx, record, repository.Filter(payoutIdemField, record.IdempotencyKey))
|
||||
}
|
||||
|
||||
var _ storage.PayoutsStore = (*Payouts)(nil)
|
||||
|
||||
@@ -14,8 +14,8 @@ import (
|
||||
gatewayservice "github.com/tech/sendico/gateway/tron/internal/service/gateway"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/drivers"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/rpcclient"
|
||||
gatewayshared "github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/tronclient"
|
||||
gatewayshared "github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage"
|
||||
gatewaymongo "github.com/tech/sendico/gateway/tron/storage/mongo"
|
||||
"github.com/tech/sendico/pkg/api/routers"
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
chainv1 "github.com/tech/sendico/pkg/proto/gateway/chain/v1"
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"github.com/tech/sendico/gateway/tron/internal/keymanager"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/drivers"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/rpcclient"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/tronclient"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage"
|
||||
clockpkg "github.com/tech/sendico/pkg/clock"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/driver"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/commands/wallet"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/driver/evm"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/driver/tron"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package transfer
|
||||
|
||||
import (
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
chainv1 "github.com/tech/sendico/pkg/proto/gateway/chain/v1"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
|
||||
@@ -3,7 +3,7 @@ package wallet
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
describablev1 "github.com/tech/sendico/pkg/proto/common/describable/v1"
|
||||
chainv1 "github.com/tech/sendico/pkg/proto/gateway/chain/v1"
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/appversion"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
chainasset "github.com/tech/sendico/pkg/chain"
|
||||
"github.com/tech/sendico/pkg/connector/params"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/tech/sendico/gateway/tron/internal/keymanager"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/rpcclient"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/tronclient"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/driver"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
|
||||
)
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"math/big"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
)
|
||||
|
||||
@@ -41,7 +42,7 @@ func rpcAddress(address string) (string, error) {
|
||||
if strings.HasPrefix(trimmed, tronHexPrefix) || isHexString(trimmed) {
|
||||
return normalizeHexRPC(trimmed)
|
||||
}
|
||||
return base58ToHex(trimmed)
|
||||
return shared.TronBase58ToHex(trimmed)
|
||||
}
|
||||
|
||||
func hexToBase58(address string) (string, error) {
|
||||
@@ -53,17 +54,6 @@ func hexToBase58(address string) (string, error) {
|
||||
return base58Encode(payload), nil
|
||||
}
|
||||
|
||||
func base58ToHex(address string) (string, error) {
|
||||
decoded, err := base58Decode(address)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := validateChecksum(decoded); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return tronHexPrefix + hex.EncodeToString(decoded[1:21]), nil
|
||||
}
|
||||
|
||||
func parseHexAddress(address string) ([]byte, error) {
|
||||
trimmed := strings.TrimPrefix(strings.TrimSpace(address), tronHexPrefix)
|
||||
if trimmed == "" {
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/driver"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/driver/evm"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/driver"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
|
||||
"go.uber.org/zap"
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
|
||||
)
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/driver"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/driver/tron"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"go.uber.org/zap"
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/rpcclient"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/keymanager"
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
"github.com/tech/sendico/gateway/tron/internal/keymanager"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/drivers"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/rpcclient"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/tronclient"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
clockpkg "github.com/tech/sendico/pkg/clock"
|
||||
)
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"go.uber.org/zap"
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
)
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/commands/wallet"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/drivers"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/rpcclient"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/tronclient"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage"
|
||||
"github.com/tech/sendico/pkg/api/routers"
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/keymanager"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/drivers"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/shengdoushi/base58"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
)
|
||||
|
||||
func TronBase58ToHex(addr string) (string, error) {
|
||||
const (
|
||||
tronAddrLen = 25
|
||||
tronPrefix = byte(0x41)
|
||||
payloadLen = 21
|
||||
checksumBytes = 4
|
||||
)
|
||||
|
||||
raw, err := base58.Decode(addr, base58.BitcoinAlphabet)
|
||||
if err != nil {
|
||||
return "", merrors.InvalidArgument(fmt.Sprintf("tron address: base58 decode failed: %s", err.Error()))
|
||||
}
|
||||
|
||||
if len(raw) != tronAddrLen {
|
||||
return "", merrors.DataConflict("tron address: invalid length")
|
||||
}
|
||||
|
||||
// 21 байт: prefix + 20 байт EVM адреса
|
||||
payload := raw[:payloadLen]
|
||||
|
||||
if payload[0] != tronPrefix {
|
||||
return "", merrors.DataConflict("tron address: invalid prefix")
|
||||
}
|
||||
|
||||
evm := payload[1:payloadLen]
|
||||
return "0x" + hex.EncodeToString(evm), nil
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/driver"
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/gateway/tron/storage/model"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"go.uber.org/zap"
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/tron/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/gateway/tron/shared"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"go.uber.org/zap"
|
||||
|
||||
52
api/gateway/tron/shared/tron_addr.go
Normal file
52
api/gateway/tron/shared/tron_addr.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/shengdoushi/base58"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
)
|
||||
|
||||
func TronBase58ToHex(addr string) (string, error) {
|
||||
const (
|
||||
tronAddrLen = 25
|
||||
tronPrefix = byte(0x41)
|
||||
)
|
||||
|
||||
raw, err := base58.Decode(strings.TrimSpace(addr), base58.BitcoinAlphabet)
|
||||
if err != nil {
|
||||
return "", merrors.InvalidArgument(
|
||||
fmt.Sprintf("tron address: base58 decode failed: %s", err.Error()),
|
||||
)
|
||||
}
|
||||
|
||||
if len(raw) != tronAddrLen {
|
||||
return "", merrors.DataConflict(
|
||||
fmt.Sprintf("tron address: invalid length %d", len(raw)),
|
||||
)
|
||||
}
|
||||
|
||||
// 21 байт: prefix + 20 байт EVM
|
||||
payload := raw[:21]
|
||||
checksum := raw[21:]
|
||||
|
||||
if payload[0] != tronPrefix {
|
||||
return "", merrors.DataConflict("tron address: invalid prefix")
|
||||
}
|
||||
|
||||
// validate checksum (double sha256)
|
||||
h1 := sha256.Sum256(payload)
|
||||
h2 := sha256.Sum256(h1[:])
|
||||
expectedChecksum := h2[:4]
|
||||
|
||||
if !bytes.Equal(expectedChecksum, checksum) {
|
||||
return "", merrors.DataConflict("tron address: invalid checksum")
|
||||
}
|
||||
|
||||
evm := payload[1:]
|
||||
return "0x" + hex.EncodeToString(evm), nil
|
||||
}
|
||||
Reference in New Issue
Block a user