neo-go/pkg/wallet/account_test.go

150 lines
4.1 KiB
Go
Raw Normal View History

package wallet
import (
"encoding/hex"
"encoding/json"
"testing"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/internal/keytestcases"
2019-11-24 15:40:07 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewAccount(t *testing.T) {
acc, err := NewAccount()
require.NoError(t, err)
require.NotNil(t, acc)
}
func TestDecryptAccount(t *testing.T) {
for _, testCase := range keytestcases.Arr {
acc := &Account{EncryptedWIF: testCase.EncryptedWif}
assert.Nil(t, acc.PrivateKey())
err := acc.Decrypt(testCase.Passphrase)
2019-11-24 15:40:07 +00:00
if testCase.Invalid {
assert.Error(t, err)
continue
}
2019-11-24 15:40:07 +00:00
assert.NoError(t, err)
assert.NotNil(t, acc.PrivateKey())
assert.Equal(t, testCase.PrivateKey, acc.privateKey.String())
}
// No encrypted key.
acc := &Account{}
require.Error(t, acc.Decrypt("qwerty"))
}
func TestNewFromWif(t *testing.T) {
for _, testCase := range keytestcases.Arr {
acc, err := NewAccountFromWIF(testCase.Wif)
2019-11-24 15:40:07 +00:00
if testCase.Invalid {
assert.Error(t, err)
continue
}
2019-11-24 15:40:07 +00:00
assert.NoError(t, err)
compareFields(t, testCase, acc)
}
}
func TestNewAccountFromEncryptedWIF(t *testing.T) {
for _, tc := range keytestcases.Arr {
acc, err := NewAccountFromEncryptedWIF(tc.EncryptedWif, tc.Passphrase)
if tc.Invalid {
assert.Error(t, err)
continue
}
assert.NoError(t, err)
compareFields(t, tc, acc)
}
}
func TestContract_MarshalJSON(t *testing.T) {
var c Contract
data := []byte(`{"script":"0102","parameters":[{"name":"name0", "type":"Signature"}],"deployed":false}`)
require.NoError(t, json.Unmarshal(data, &c))
require.Equal(t, []byte{1, 2}, c.Script)
result, err := json.Marshal(c)
require.NoError(t, err)
require.JSONEq(t, string(data), string(result))
data = []byte(`1`)
require.Error(t, json.Unmarshal(data, &c))
data = []byte(`{"script":"ERROR","parameters":[1],"deployed":false}`)
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 TestAccount_ConvertMultisig(t *testing.T) {
// test is based on a wallet1_solo.json accounts from neo-local
a, err := NewAccountFromEncryptedWIF("6PYLmjBYJ4wQTCEfqvnznGJwZeW9pfUcV5m5oreHxqryUgqKpTRAFt9L8Y", "one")
require.NoError(t, err)
hexs := []string{
"02b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc2", // <- this is our key
"02103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e",
"02a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd62",
"03d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee699",
}
t.Run("invalid number of signatures", func(t *testing.T) {
pubs := convertPubs(t, hexs)
require.Error(t, a.ConvertMultisig(0, pubs))
})
t.Run("account key is missing from multisig", func(t *testing.T) {
pubs := convertPubs(t, hexs[1:])
require.Error(t, a.ConvertMultisig(1, pubs))
})
t.Run("1/1 multisig", func(t *testing.T) {
pubs := convertPubs(t, hexs[:1])
require.NoError(t, a.ConvertMultisig(1, pubs))
require.Equal(t, "AbU69m8WUZJSWanfr1Cy66cpEcsmMcX7BR", a.Address)
})
t.Run("3/4 multisig", func(t *testing.T) {
pubs := convertPubs(t, hexs)
require.NoError(t, a.ConvertMultisig(3, pubs))
require.Equal(t, "AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU", a.Address)
})
}
func convertPubs(t *testing.T, hexKeys []string) []*keys.PublicKey {
pubs := make([]*keys.PublicKey, len(hexKeys))
for i := range pubs {
var err error
pubs[i], err = keys.NewPublicKeyFromString(hexKeys[i])
require.NoError(t, err)
}
return pubs
}
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)
}
if want, have := tk.Wif, acc.wif; want != have {
t.Fatalf("expected %s got %s", want, have)
}
if want, have := tk.PublicKey, hex.EncodeToString(acc.publicKey); want != have {
t.Fatalf("expected %s got %s", want, have)
}
if want, have := tk.PrivateKey, acc.privateKey.String(); want != have {
t.Fatalf("expected %s got %s", want, have)
}
}