wallet: add (*Account).CanSign API

This commit is contained in:
Roman Khimov 2022-08-24 18:50:31 +03:00
parent 8b132cba0c
commit fe50879bb7
2 changed files with 10 additions and 0 deletions

View file

@ -136,6 +136,12 @@ func (a *Account) SignTx(net netmode.Magic, t *transaction.Transaction) error {
return nil
}
// CanSign returns true when account is not locked and has a decrypted private
// key inside, so it's ready to create real signatures.
func (a *Account) CanSign() bool {
return !a.Locked && a.privateKey != nil
}
// GetVerificationScript returns account's verification script.
func (a *Account) GetVerificationScript() []byte {
if a.Contract != nil {

View file

@ -86,6 +86,7 @@ func TestContract_MarshalJSON(t *testing.T) {
func TestContractSignTx(t *testing.T) {
acc, err := NewAccount()
require.NoError(t, err)
require.True(t, acc.CanSign())
accNoContr := *acc
accNoContr.Contract = nil
@ -100,6 +101,7 @@ func TestContractSignTx(t *testing.T) {
acc2, err := NewAccount()
require.NoError(t, err)
require.True(t, acc2.CanSign())
require.Error(t, acc2.SignTx(0, tx))
@ -135,10 +137,12 @@ func TestContractSignTx(t *testing.T) {
require.Equal(t, 66, len(tx.Scripts[0].InvocationScript))
acc2.Locked = true
require.False(t, acc2.CanSign())
require.Error(t, acc2.SignTx(0, tx)) // Locked account.
acc2.Locked = false
acc2.privateKey = nil
require.False(t, acc2.CanSign())
require.Error(t, acc2.SignTx(0, tx)) // No private key.
tx.Scripts = append(tx.Scripts, transaction.Witness{