wallet: add GetAccount() method to Wallet

It is useful to be able to get account specified by script hash.
This commit is contained in:
Evgenii Stratonikov 2020-01-15 18:10:40 +03:00
parent a798e4e0fa
commit 36cfa5b1f4
2 changed files with 38 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import (
"os"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/util"
)
const (
@ -117,3 +118,14 @@ func (w *Wallet) Close() {
rc.Close()
}
}
// GetAccount returns account corresponding to the provided scripthash.
func (w *Wallet) GetAccount(h util.Uint160) *Account {
for _, acc := range w.Accounts {
if c := acc.Contract; c != nil && h.Equals(c.ScriptHash()) {
return acc
}
}
return nil
}

View file

@ -6,6 +6,7 @@ import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -119,3 +120,28 @@ func removeWallet(t *testing.T, walletPath string) {
err := os.RemoveAll(walletPath)
require.NoError(t, err)
}
func TestWallet_GetAccount(t *testing.T) {
wallet := checkWalletConstructor(t)
accounts := []*Account{
{
Contract: &Contract{
Script: []byte{0, 1, 2, 3},
},
},
{
Contract: &Contract{
Script: []byte{3, 2, 1, 0},
},
},
}
for _, acc := range accounts {
wallet.AddAccount(acc)
}
for i, acc := range accounts {
h := acc.Contract.ScriptHash()
assert.Equal(t, acc, wallet.GetAccount(h), "can't get %d account", i)
}
}