From 65b4525b3bf061848e40ccfd87ff24a93ac4e2b5 Mon Sep 17 00:00:00 2001 From: Alexander Chuprov Date: Fri, 26 Jan 2024 14:41:55 +0300 Subject: [PATCH] [#198] object/user: Add ScriptHash method Signed-off-by: Alexander Chuprov --- user/id.go | 5 +++++ user/id_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/user/id.go b/user/id.go index 8535b01..31ff9b6 100644 --- a/user/id.go +++ b/user/id.go @@ -72,6 +72,11 @@ func (x *ID) SetScriptHash(scriptHash util.Uint160) { copy(x.w[21:], hash.Checksum(x.w[:21])) } +// ScriptHash calculates and returns script hash of ID. +func (x *ID) ScriptHash() (util.Uint160, error) { + return util.Uint160DecodeBytesBE(x.w[1:21]) +} + // WalletBytes returns FrostFS user ID as Neo3 wallet address in a binary format. // // Return value MUST NOT be mutated: to do this, first make a copy. diff --git a/user/id_test.go b/user/id_test.go index 6b1d0f8..00b0884 100644 --- a/user/id_test.go +++ b/user/id_test.go @@ -8,6 +8,7 @@ import ( . "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user" usertest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user/test" "github.com/mr-tron/base58" + "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/stretchr/testify/require" @@ -47,6 +48,19 @@ func TestID_SetScriptHash(t *testing.T) { require.True(t, id2.Equals(id)) } +func TestID_ScriptHash(t *testing.T) { + userID := usertest.ID() + + scriptHash, err := userID.ScriptHash() + require.NoError(t, err) + + ownerAddress := userID.EncodeToString() + decodedScriptHash, err := address.StringToUint160(ownerAddress) + require.NoError(t, err) + + require.True(t, scriptHash.Equals(decodedScriptHash)) +} + func TestV2_ID(t *testing.T) { id := usertest.ID() var m refs.OwnerID