2018-03-02 15:24:09 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2020-01-15 13:36:57 +00:00
|
|
|
"encoding/json"
|
2018-03-02 15:24:09 +00:00
|
|
|
"testing"
|
2019-08-27 13:29:42 +00:00
|
|
|
|
2020-01-15 15:08:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/internal/keytestcases"
|
2019-11-24 15:40:07 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-09 15:30:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-02 15:24:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewAccount(t *testing.T) {
|
2020-01-09 15:33:26 +00:00
|
|
|
acc, err := NewAccount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, acc)
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecryptAccount(t *testing.T) {
|
2019-08-27 13:29:42 +00:00
|
|
|
for _, testCase := range keytestcases.Arr {
|
2020-01-09 15:30:25 +00:00
|
|
|
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
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
2019-11-24 15:40:07 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2020-01-09 15:30:25 +00:00
|
|
|
assert.NotNil(t, acc.PrivateKey())
|
|
|
|
assert.Equal(t, testCase.PrivateKey, acc.privateKey.String())
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
2020-01-09 15:30:25 +00:00
|
|
|
// No encrypted key.
|
|
|
|
acc := &Account{}
|
|
|
|
require.Error(t, acc.Decrypt("qwerty"))
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFromWif(t *testing.T) {
|
2019-08-27 13:29:42 +00:00
|
|
|
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
|
2018-03-02 15:24:09 +00:00
|
|
|
}
|
2019-11-24 15:40:07 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-03-02 15:24:09 +00:00
|
|
|
compareFields(t, testCase, acc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 09:22:40 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:36:57 +00:00
|
|
|
func TestContract_MarshalJSON(t *testing.T) {
|
|
|
|
var c Contract
|
|
|
|
|
2020-02-20 10:28:35 +00:00
|
|
|
data := []byte(`{"script":"0102","parameters":[{"name":"name0", "type":"Signature"}],"deployed":false}`)
|
2020-01-15 13:36:57 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2020-01-15 15:08:25 +00:00
|
|
|
func TestContract_ScriptHash(t *testing.T) {
|
|
|
|
script := []byte{0, 1, 2, 3}
|
|
|
|
c := &Contract{Script: script}
|
|
|
|
|
|
|
|
require.Equal(t, hash.Hash160(script), c.ScriptHash())
|
|
|
|
}
|
|
|
|
|
2019-08-27 13:29:42 +00:00
|
|
|
func compareFields(t *testing.T, tk keytestcases.Ktype, acc *Account) {
|
|
|
|
if want, have := tk.Address, acc.Address; want != have {
|
2018-03-02 15:24:09 +00:00
|
|
|
t.Fatalf("expected %s got %s", want, have)
|
|
|
|
}
|
2019-08-27 13:29:42 +00:00
|
|
|
if want, have := tk.Wif, acc.wif; want != have {
|
2018-03-02 15:24:09 +00:00
|
|
|
t.Fatalf("expected %s got %s", want, have)
|
|
|
|
}
|
2019-08-27 13:29:42 +00:00
|
|
|
if want, have := tk.PublicKey, hex.EncodeToString(acc.publicKey); want != have {
|
2018-03-02 15:24:09 +00:00
|
|
|
t.Fatalf("expected %s got %s", want, have)
|
|
|
|
}
|
2019-08-27 13:29:42 +00:00
|
|
|
if want, have := tk.PrivateKey, acc.privateKey.String(); want != have {
|
2018-03-02 15:24:09 +00:00
|
|
|
t.Fatalf("expected %s got %s", want, have)
|
|
|
|
}
|
|
|
|
}
|