forked from TrueCloudLab/neoneo-go
wallet: add GetAccount() method to Wallet
It is useful to be able to get account specified by script hash.
This commit is contained in:
parent
a798e4e0fa
commit
36cfa5b1f4
2 changed files with 38 additions and 0 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
||||||
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -117,3 +118,14 @@ func (w *Wallet) Close() {
|
||||||
rc.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
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -119,3 +120,28 @@ func removeWallet(t *testing.T, walletPath string) {
|
||||||
err := os.RemoveAll(walletPath)
|
err := os.RemoveAll(walletPath)
|
||||||
require.NoError(t, err)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue