From 74769323bea433a7a141ab2c0d25ab9329b4cb86 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 10 Mar 2021 15:07:06 +0300 Subject: [PATCH] [#261] pkg/client: Use self address as default in `ListContainers`/`GetBalance` This will allow us to overwrite key for every request. Signed-off-by: Evgenii Stratonikov --- pkg/client/accounting.go | 24 +++++++++++++----------- pkg/client/container.go | 24 +++++++++++++----------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pkg/client/accounting.go b/pkg/client/accounting.go index 7446233..9b3f129 100644 --- a/pkg/client/accounting.go +++ b/pkg/client/accounting.go @@ -12,15 +12,7 @@ import ( ) func (c Client) GetSelfBalance(ctx context.Context, opts ...CallOption) (*accounting.Decimal, error) { - w, err := owner.NEO3WalletFromPublicKey(&c.key.PublicKey) - if err != nil { - return nil, err - } - - ownerID := new(owner.ID) - ownerID.SetNeo3Wallet(w) - - return c.GetBalance(ctx, ownerID, opts...) + return c.GetBalance(ctx, nil, opts...) } func (c Client) GetBalance(ctx context.Context, owner *owner.ID, opts ...CallOption) (*accounting.Decimal, error) { @@ -33,7 +25,7 @@ func (c Client) GetBalance(ctx context.Context, owner *owner.ID, opts ...CallOpt } } -func (c Client) getBalanceV2(ctx context.Context, owner *owner.ID, opts ...CallOption) (*accounting.Decimal, error) { +func (c Client) getBalanceV2(ctx context.Context, ownerID *owner.ID, opts ...CallOption) (*accounting.Decimal, error) { // apply all available options callOptions := c.defaultCallOptions() @@ -41,8 +33,18 @@ func (c Client) getBalanceV2(ctx context.Context, owner *owner.ID, opts ...CallO opts[i].apply(&callOptions) } + if ownerID == nil { + w, err := owner.NEO3WalletFromPublicKey(&c.key.PublicKey) + if err != nil { + return nil, err + } + + ownerID = new(owner.ID) + ownerID.SetNeo3Wallet(w) + } + reqBody := new(v2accounting.BalanceRequestBody) - reqBody.SetOwnerID(owner.ToV2()) + reqBody.SetOwnerID(ownerID.ToV2()) req := new(v2accounting.BalanceRequest) req.SetBody(reqBody) diff --git a/pkg/client/container.go b/pkg/client/container.go index 6c7ef73..4e7e6fa 100644 --- a/pkg/client/container.go +++ b/pkg/client/container.go @@ -94,15 +94,7 @@ func (c Client) ListContainers(ctx context.Context, owner *owner.ID, opts ...Cal } func (c Client) ListSelfContainers(ctx context.Context, opts ...CallOption) ([]*container.ID, error) { - w, err := owner.NEO3WalletFromPublicKey(&c.key.PublicKey) - if err != nil { - return nil, err - } - - ownerID := new(owner.ID) - ownerID.SetNeo3Wallet(w) - - return c.ListContainers(ctx, ownerID, opts...) + return c.ListContainers(ctx, nil, opts...) } func (c Client) DeleteContainer(ctx context.Context, id *container.ID, opts ...CallOption) error { @@ -282,7 +274,7 @@ func (c Client) getContainerV2(ctx context.Context, id *container.ID, opts ...Ca } } -func (c Client) listContainerV2(ctx context.Context, owner *owner.ID, opts ...CallOption) ([]*container.ID, error) { +func (c Client) listContainerV2(ctx context.Context, ownerID *owner.ID, opts ...CallOption) ([]*container.ID, error) { // apply all available options callOptions := c.defaultCallOptions() @@ -290,8 +282,18 @@ func (c Client) listContainerV2(ctx context.Context, owner *owner.ID, opts ...Ca opts[i].apply(&callOptions) } + if ownerID == nil { + w, err := owner.NEO3WalletFromPublicKey(&c.key.PublicKey) + if err != nil { + return nil, err + } + + ownerID = new(owner.ID) + ownerID.SetNeo3Wallet(w) + } + reqBody := new(v2container.ListRequestBody) - reqBody.SetOwnerID(owner.ToV2()) + reqBody.SetOwnerID(ownerID.ToV2()) req := new(v2container.ListRequest) req.SetBody(reqBody)