From a798e4e0fa2fb90c2c197344473554cf2628531c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 15 Jan 2020 18:08:25 +0300 Subject: [PATCH] wallet: add ScriptHash() method to Contract --- pkg/wallet/account.go | 7 +++++++ pkg/wallet/account_test.go | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/wallet/account.go b/pkg/wallet/account.go index 47ba95cd5..591e1971e 100644 --- a/pkg/wallet/account.go +++ b/pkg/wallet/account.go @@ -5,7 +5,9 @@ import ( "encoding/json" "errors" + "github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/keys" + "github.com/CityOfZion/neo-go/pkg/util" ) // Account represents a NEO account. It holds the private and public key @@ -66,6 +68,11 @@ type contract struct { Deployed bool `json:"deployed"` } +// ScriptHash returns the hash of contract's script. +func (c Contract) ScriptHash() util.Uint160 { + return hash.Hash160(c.Script) +} + // MarshalJSON implements json.Marshaler interface. func (c Contract) MarshalJSON() ([]byte, error) { var cc contract diff --git a/pkg/wallet/account_test.go b/pkg/wallet/account_test.go index e786749cb..81eef739b 100644 --- a/pkg/wallet/account_test.go +++ b/pkg/wallet/account_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "testing" + "github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/internal/keytestcases" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -66,6 +67,13 @@ func TestContract_MarshalJSON(t *testing.T) { require.Error(t, json.Unmarshal(data, &c)) } +func TestContract_ScriptHash(t *testing.T) { + script := []byte{0, 1, 2, 3} + c := &Contract{Script: script} + + require.Equal(t, hash.Hash160(script), c.ScriptHash()) +} + func compareFields(t *testing.T, tk keytestcases.Ktype, acc *Account) { if want, have := tk.Address, acc.Address; want != have { t.Fatalf("expected %s got %s", want, have)