Skip to content
Merged
100 changes: 71 additions & 29 deletions pkg/client/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/fbsobreira/gotron-sdk/pkg/common"
"github.com/fbsobreira/gotron-sdk/pkg/proto/api"
"github.com/fbsobreira/gotron-sdk/pkg/proto/core"
"google.golang.org/protobuf/proto"
)

// GetAccount from BASE58 address
Expand All @@ -38,6 +37,11 @@ func (g *GrpcClient) GetAccountCtx(ctx context.Context, addr string) (*core.Acco
if err != nil {
return nil, err
}
// A substituted WalletClient can return (nil, nil); a real gRPC round trip
// always materialises a message. Check before field access.
if acc == nil {
return nil, fmt.Errorf("account not found")
}
if !bytes.Equal(acc.Address, account.Address) {
return nil, fmt.Errorf("account not found")
}
Expand Down Expand Up @@ -111,11 +115,8 @@ func (g *GrpcClient) CreateAccountCtx(ctx context.Context, from, addr string) (*
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "create account"); err != nil {
return nil, err
}
return tx, nil
}
Expand All @@ -141,11 +142,8 @@ func (g *GrpcClient) UpdateAccountCtx(ctx context.Context, from, accountName str
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "update account"); err != nil {
return nil, err
}
return tx, nil
}
Expand Down Expand Up @@ -361,11 +359,8 @@ func (g *GrpcClient) WithdrawBalanceCtx(ctx context.Context, from string) (*api.
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "withdraw balance"); err != nil {
return nil, err
}
return tx, nil
}
Expand Down Expand Up @@ -436,6 +431,23 @@ func (g *GrpcClient) UpdateAccountPermission(from string, owner, witness map[str
return g.UpdateAccountPermissionCtx(ctx, from, owner, witness, actives)
}

// permField reads one field from a caller-supplied permission map with a checked
// assertion. The public API takes map[string]interface{}, so a missing key — or a
// plain int where int64 was meant — would otherwise panic part-way through a
// multisig permission update rather than returning an error.
func permField[T any](m map[string]interface{}, scope, key string) (T, error) {
var zero T
v, ok := m[key]
if !ok {
return zero, fmt.Errorf("%s permission: missing %q", scope, key)
}
t, ok := v.(T)
if !ok {
return zero, fmt.Errorf("%s permission: %q must be %T, got %T", scope, key, zero, v)
}
return t, nil
}

// UpdateAccountPermissionCtx is the context-aware version of UpdateAccountPermission.
func (g *GrpcClient) UpdateAccountPermissionCtx(ctx context.Context, from string, owner, witness map[string]interface{}, actives []map[string]interface{}) (*api.TransactionExtention, error) {
ctx = g.withAPIKey(ctx)
Expand All @@ -447,13 +459,21 @@ func (g *GrpcClient) UpdateAccountPermissionCtx(ctx context.Context, from string
if owner == nil {
return nil, fmt.Errorf("owner is mandatory")
}
ownerThreshold, err := permField[int64](owner, "owner", "threshold")
if err != nil {
return nil, err
}
ownerKeys, err := permField[map[string]int64](owner, "owner", "keys")
if err != nil {
return nil, err
}
ownerPermission, err := makePermission(
"owner",
core.Permission_Owner,
0,
owner["threshold"].(int64),
ownerThreshold,
nil,
owner["keys"].(map[string]int64),
ownerKeys,
)
if err != nil {
return nil, err
Expand All @@ -469,13 +489,30 @@ func (g *GrpcClient) UpdateAccountPermissionCtx(ctx context.Context, from string
if actives != nil {
activesPermission := make([]*core.Permission, 0)
for i, active := range actives {
scope := fmt.Sprintf("active[%d]", i)
activeName, err := permField[string](active, scope, "name")
if err != nil {
return nil, err
}
activeThreshold, err := permField[int64](active, scope, "threshold")
if err != nil {
return nil, err
}
activeOps, err := permField[map[string]bool](active, scope, "operations")
if err != nil {
return nil, err
}
activeKeys, err := permField[map[string]int64](active, scope, "keys")
if err != nil {
return nil, err
}
activeP, err := makePermission(
active["name"].(string),
activeName,
core.Permission_Active,
int32(2+i),
active["threshold"].(int64),
active["operations"].(map[string]bool),
active["keys"].(map[string]int64),
activeThreshold,
activeOps,
activeKeys,
)
if err != nil {
return nil, err
Expand All @@ -486,13 +523,21 @@ func (g *GrpcClient) UpdateAccountPermissionCtx(ctx context.Context, from string
}

if witness != nil {
witnessThreshold, err := permField[int64](witness, "witness", "threshold")
if err != nil {
return nil, err
}
witnessKeys, err := permField[map[string]int64](witness, "witness", "keys")
if err != nil {
return nil, err
}
witnessPermission, err := makePermission(
"witness",
core.Permission_Witness,
1,
witness["threshold"].(int64),
witnessThreshold,
nil,
witness["keys"].(map[string]int64),
witnessKeys,
)
if err != nil {
return nil, err
Expand All @@ -504,11 +549,8 @@ func (g *GrpcClient) UpdateAccountPermissionCtx(ctx context.Context, from string
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "update account permission"); err != nil {
return nil, err
}
return tx, nil
}
36 changes: 10 additions & 26 deletions pkg/client/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/fbsobreira/gotron-sdk/pkg/common"
"github.com/fbsobreira/gotron-sdk/pkg/proto/api"
"github.com/fbsobreira/gotron-sdk/pkg/proto/core"
"google.golang.org/protobuf/proto"
)

// GetAssetIssueByAccount list asset issued by account
Expand Down Expand Up @@ -170,11 +169,8 @@ func (g *GrpcClient) AssetIssueCtx(ctx context.Context, from, name, description,
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "asset issue"); err != nil {
return nil, err
}
return tx, nil
}
Expand Down Expand Up @@ -207,11 +203,8 @@ func (g *GrpcClient) UpdateAssetIssueCtx(ctx context.Context, from, description,
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "update asset issue"); err != nil {
return nil, err
}
return tx, nil
}
Expand Down Expand Up @@ -244,11 +237,8 @@ func (g *GrpcClient) TransferAssetCtx(ctx context.Context, from, toAddress,
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "transfer asset"); err != nil {
return nil, err
}
return tx, nil
}
Expand Down Expand Up @@ -281,11 +271,8 @@ func (g *GrpcClient) ParticipateAssetIssueCtx(ctx context.Context, from, issuerA
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "participate asset issue"); err != nil {
return nil, err
}
return tx, nil
}
Expand All @@ -311,11 +298,8 @@ func (g *GrpcClient) UnfreezeAssetCtx(ctx context.Context, from string) (*api.Tr
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "unfreeze asset"); err != nil {
return nil, err
}
return tx, nil
}
33 changes: 10 additions & 23 deletions pkg/client/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/fbsobreira/gotron-sdk/pkg/common"
"github.com/fbsobreira/gotron-sdk/pkg/proto/api"
"github.com/fbsobreira/gotron-sdk/pkg/proto/core"
"google.golang.org/protobuf/proto"
)

// FreezeBalance from base58 address
Expand Down Expand Up @@ -45,11 +44,8 @@ func (g *GrpcClient) FreezeBalanceCtx(ctx context.Context, from, delegateTo stri
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "freeze balance"); err != nil {
return nil, err
}
return tx, nil
}
Expand Down Expand Up @@ -85,11 +81,8 @@ func (g *GrpcClient) FreezeBalanceV2Ctx(ctx context.Context, from string,
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "freeze balance v2"); err != nil {
return nil, err
}
return tx, nil
}
Expand Down Expand Up @@ -124,11 +117,8 @@ func (g *GrpcClient) UnfreezeBalanceCtx(ctx context.Context, from, delegateTo st
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "unfreeze balance"); err != nil {
return nil, err
}
return tx, nil
}
Expand Down Expand Up @@ -162,11 +152,8 @@ func (g *GrpcClient) UnfreezeBalanceV2Ctx(ctx context.Context, from string, reso
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
}
if tx.GetResult().GetCode() != 0 {
return nil, fmt.Errorf("%s", tx.GetResult().GetMessage())
if err := requireTxExtension(tx, "unfreeze balance v2"); err != nil {
return nil, err
}
return tx, nil
}
Expand Down Expand Up @@ -246,8 +233,8 @@ func (g *GrpcClient) WithdrawExpireUnfreezeCtx(ctx context.Context, from string,
if err != nil {
return nil, err
}
if proto.Size(tx) == 0 {
return nil, fmt.Errorf("bad transaction")
if err := requireTxExtension(tx, "withdraw expire unfreeze"); err != nil {
return nil, err
}
return tx, nil
}
Loading
Loading