diff --git a/api/gateway/mntx/storage/mongo/store/payouts.go b/api/gateway/mntx/storage/mongo/store/payouts.go index 700a3fc9..151b2983 100644 --- a/api/gateway/mntx/storage/mongo/store/payouts.go +++ b/api/gateway/mntx/storage/mongo/store/payouts.go @@ -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" ) @@ -25,8 +21,8 @@ const ( ) type Payouts struct { - logger mlogger.Logger - coll *mongo.Collection + logger mlogger.Logger + repository repository.Repository } func NewPayouts(logger mlogger.Logger, db *mongo.Database) (*Payouts, error) { @@ -50,38 +46,20 @@ func NewPayouts(logger mlogger.Logger, db *mongo.Database) (*Payouts, error) { } p := &Payouts{ - logger: logger, - coll: db.Collection(payoutsCollection), + logger: logger, + 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) diff --git a/api/gateway/tron/internal/server/internal/serverimp.go b/api/gateway/tron/internal/server/internal/serverimp.go index c3a97b8b..f8822c21 100644 --- a/api/gateway/tron/internal/server/internal/serverimp.go +++ b/api/gateway/tron/internal/server/internal/serverimp.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/transfer/convert_fees.go b/api/gateway/tron/internal/service/gateway/commands/transfer/convert_fees.go index 58546d91..ede60049 100644 --- a/api/gateway/tron/internal/service/gateway/commands/transfer/convert_fees.go +++ b/api/gateway/tron/internal/service/gateway/commands/transfer/convert_fees.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/transfer/deps.go b/api/gateway/tron/internal/service/gateway/commands/transfer/deps.go index c0fd566e..d5bae96c 100644 --- a/api/gateway/tron/internal/service/gateway/commands/transfer/deps.go +++ b/api/gateway/tron/internal/service/gateway/commands/transfer/deps.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/transfer/fee.go b/api/gateway/tron/internal/service/gateway/commands/transfer/fee.go index 39049294..185eb5e5 100644 --- a/api/gateway/tron/internal/service/gateway/commands/transfer/fee.go +++ b/api/gateway/tron/internal/service/gateway/commands/transfer/fee.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/transfer/gas_topup.go b/api/gateway/tron/internal/service/gateway/commands/transfer/gas_topup.go index 2f67f40e..0f0f661b 100644 --- a/api/gateway/tron/internal/service/gateway/commands/transfer/gas_topup.go +++ b/api/gateway/tron/internal/service/gateway/commands/transfer/gas_topup.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/transfer/list.go b/api/gateway/tron/internal/service/gateway/commands/transfer/list.go index 65c123af..d288d231 100644 --- a/api/gateway/tron/internal/service/gateway/commands/transfer/list.go +++ b/api/gateway/tron/internal/service/gateway/commands/transfer/list.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/transfer/proto.go b/api/gateway/tron/internal/service/gateway/commands/transfer/proto.go index d8f0514e..abf42c57 100644 --- a/api/gateway/tron/internal/service/gateway/commands/transfer/proto.go +++ b/api/gateway/tron/internal/service/gateway/commands/transfer/proto.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/transfer/submit.go b/api/gateway/tron/internal/service/gateway/commands/transfer/submit.go index e6c88af1..beb5ea80 100644 --- a/api/gateway/tron/internal/service/gateway/commands/transfer/submit.go +++ b/api/gateway/tron/internal/service/gateway/commands/transfer/submit.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/wallet/balance.go b/api/gateway/tron/internal/service/gateway/commands/wallet/balance.go index ac860cd2..90afaa98 100644 --- a/api/gateway/tron/internal/service/gateway/commands/wallet/balance.go +++ b/api/gateway/tron/internal/service/gateway/commands/wallet/balance.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/wallet/create.go b/api/gateway/tron/internal/service/gateway/commands/wallet/create.go index 7672aa60..10297d41 100644 --- a/api/gateway/tron/internal/service/gateway/commands/wallet/create.go +++ b/api/gateway/tron/internal/service/gateway/commands/wallet/create.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/wallet/list.go b/api/gateway/tron/internal/service/gateway/commands/wallet/list.go index e0fa581c..3fdace22 100644 --- a/api/gateway/tron/internal/service/gateway/commands/wallet/list.go +++ b/api/gateway/tron/internal/service/gateway/commands/wallet/list.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/commands/wallet/proto.go b/api/gateway/tron/internal/service/gateway/commands/wallet/proto.go index 0c8f31b4..769e4752 100644 --- a/api/gateway/tron/internal/service/gateway/commands/wallet/proto.go +++ b/api/gateway/tron/internal/service/gateway/commands/wallet/proto.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/connector.go b/api/gateway/tron/internal/service/gateway/connector.go index 8ce87775..3b29d6b8 100644 --- a/api/gateway/tron/internal/service/gateway/connector.go +++ b/api/gateway/tron/internal/service/gateway/connector.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/driver/driver.go b/api/gateway/tron/internal/service/gateway/driver/driver.go index 52579dda..4cea1e3c 100644 --- a/api/gateway/tron/internal/service/gateway/driver/driver.go +++ b/api/gateway/tron/internal/service/gateway/driver/driver.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/driver/evm/evm.go b/api/gateway/tron/internal/service/gateway/driver/evm/evm.go index 9cbcbaa8..d9099364 100644 --- a/api/gateway/tron/internal/service/gateway/driver/evm/evm.go +++ b/api/gateway/tron/internal/service/gateway/driver/evm/evm.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/driver/evm/gas_topup.go b/api/gateway/tron/internal/service/gateway/driver/evm/gas_topup.go index 3f9a6bd7..56c9b0fc 100644 --- a/api/gateway/tron/internal/service/gateway/driver/evm/gas_topup.go +++ b/api/gateway/tron/internal/service/gateway/driver/evm/gas_topup.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/driver/evm/gas_topup_test.go b/api/gateway/tron/internal/service/gateway/driver/evm/gas_topup_test.go index 3c0fbb5d..7459e6f3 100644 --- a/api/gateway/tron/internal/service/gateway/driver/evm/gas_topup_test.go +++ b/api/gateway/tron/internal/service/gateway/driver/evm/gas_topup_test.go @@ -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" ) diff --git a/api/gateway/tron/internal/service/gateway/driver/tron/address.go b/api/gateway/tron/internal/service/gateway/driver/tron/address.go index ff6048a6..b1e99ff8 100644 --- a/api/gateway/tron/internal/service/gateway/driver/tron/address.go +++ b/api/gateway/tron/internal/service/gateway/driver/tron/address.go @@ -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 == "" { diff --git a/api/gateway/tron/internal/service/gateway/driver/tron/driver.go b/api/gateway/tron/internal/service/gateway/driver/tron/driver.go index 78225c17..7fd1b513 100644 --- a/api/gateway/tron/internal/service/gateway/driver/tron/driver.go +++ b/api/gateway/tron/internal/service/gateway/driver/tron/driver.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/driver/tron/driver_test.go b/api/gateway/tron/internal/service/gateway/driver/tron/driver_test.go index fb1b3db1..d922ca0f 100644 --- a/api/gateway/tron/internal/service/gateway/driver/tron/driver_test.go +++ b/api/gateway/tron/internal/service/gateway/driver/tron/driver_test.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/driver/tron/gas_topup.go b/api/gateway/tron/internal/service/gateway/driver/tron/gas_topup.go index bf7d23cc..411c8611 100644 --- a/api/gateway/tron/internal/service/gateway/driver/tron/gas_topup.go +++ b/api/gateway/tron/internal/service/gateway/driver/tron/gas_topup.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/driver/tron/gas_topup_test.go b/api/gateway/tron/internal/service/gateway/driver/tron/gas_topup_test.go index bdccb51c..430aef14 100644 --- a/api/gateway/tron/internal/service/gateway/driver/tron/gas_topup_test.go +++ b/api/gateway/tron/internal/service/gateway/driver/tron/gas_topup_test.go @@ -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" ) diff --git a/api/gateway/tron/internal/service/gateway/drivers/registry.go b/api/gateway/tron/internal/service/gateway/drivers/registry.go index 51debd85..f29247c0 100644 --- a/api/gateway/tron/internal/service/gateway/drivers/registry.go +++ b/api/gateway/tron/internal/service/gateway/drivers/registry.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/executor.go b/api/gateway/tron/internal/service/gateway/executor.go index 135e8c3a..23a31cf3 100644 --- a/api/gateway/tron/internal/service/gateway/executor.go +++ b/api/gateway/tron/internal/service/gateway/executor.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/options.go b/api/gateway/tron/internal/service/gateway/options.go index d49ee38b..a00aa6b1 100644 --- a/api/gateway/tron/internal/service/gateway/options.go +++ b/api/gateway/tron/internal/service/gateway/options.go @@ -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" ) diff --git a/api/gateway/tron/internal/service/gateway/rpcclient/clients.go b/api/gateway/tron/internal/service/gateway/rpcclient/clients.go index 724d7b3d..356a452f 100644 --- a/api/gateway/tron/internal/service/gateway/rpcclient/clients.go +++ b/api/gateway/tron/internal/service/gateway/rpcclient/clients.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/rpcclient/registry.go b/api/gateway/tron/internal/service/gateway/rpcclient/registry.go index 98317d5f..7d01ade4 100644 --- a/api/gateway/tron/internal/service/gateway/rpcclient/registry.go +++ b/api/gateway/tron/internal/service/gateway/rpcclient/registry.go @@ -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" ) diff --git a/api/gateway/tron/internal/service/gateway/service.go b/api/gateway/tron/internal/service/gateway/service.go index 42d0d01a..9947accb 100644 --- a/api/gateway/tron/internal/service/gateway/service.go +++ b/api/gateway/tron/internal/service/gateway/service.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/service_test.go b/api/gateway/tron/internal/service/gateway/service_test.go index 682180e7..59dc7b97 100644 --- a/api/gateway/tron/internal/service/gateway/service_test.go +++ b/api/gateway/tron/internal/service/gateway/service_test.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/shared/tron_addr.go b/api/gateway/tron/internal/service/gateway/shared/tron_addr.go deleted file mode 100644 index 681bf5c2..00000000 --- a/api/gateway/tron/internal/service/gateway/shared/tron_addr.go +++ /dev/null @@ -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 -} diff --git a/api/gateway/tron/internal/service/gateway/transfer_execution.go b/api/gateway/tron/internal/service/gateway/transfer_execution.go index 1be0685d..39d38f55 100644 --- a/api/gateway/tron/internal/service/gateway/transfer_execution.go +++ b/api/gateway/tron/internal/service/gateway/transfer_execution.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/tronclient/registry.go b/api/gateway/tron/internal/service/gateway/tronclient/registry.go index c30f8332..04da5a4b 100644 --- a/api/gateway/tron/internal/service/gateway/tronclient/registry.go +++ b/api/gateway/tron/internal/service/gateway/tronclient/registry.go @@ -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" diff --git a/api/gateway/tron/internal/service/gateway/shared/gas_topup.go b/api/gateway/tron/shared/gas_topup.go similarity index 100% rename from api/gateway/tron/internal/service/gateway/shared/gas_topup.go rename to api/gateway/tron/shared/gas_topup.go diff --git a/api/gateway/tron/internal/service/gateway/shared/helpers.go b/api/gateway/tron/shared/helpers.go similarity index 100% rename from api/gateway/tron/internal/service/gateway/shared/helpers.go rename to api/gateway/tron/shared/helpers.go diff --git a/api/gateway/tron/internal/service/gateway/shared/hex.go b/api/gateway/tron/shared/hex.go similarity index 100% rename from api/gateway/tron/internal/service/gateway/shared/hex.go rename to api/gateway/tron/shared/hex.go diff --git a/api/gateway/tron/internal/service/gateway/shared/hex_test.go b/api/gateway/tron/shared/hex_test.go similarity index 100% rename from api/gateway/tron/internal/service/gateway/shared/hex_test.go rename to api/gateway/tron/shared/hex_test.go diff --git a/api/gateway/tron/shared/tron_addr.go b/api/gateway/tron/shared/tron_addr.go new file mode 100644 index 00000000..bb6873a6 --- /dev/null +++ b/api/gateway/tron/shared/tron_addr.go @@ -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 +}