wallet: export NewAccountFromPrivateKey()

Don't perform back-and-forth conversion, don't handle error
which never occur.
This commit is contained in:
Evgenii Stratonikov 2020-12-04 12:40:22 +03:00
parent 239a8c3de7
commit cd5219086a
5 changed files with 15 additions and 27 deletions

View file

@ -58,10 +58,7 @@ func (a *accountV2) convert(pass string) (*wallet.Account, error) {
} }
address.Prefix = address.NEO3Prefix address.Prefix = address.NEO3Prefix
newAcc, err := wallet.NewAccountFromWIF(priv.WIF()) newAcc := wallet.NewAccountFromPrivateKey(priv)
if err != nil {
return nil, err
}
if a.Contract != nil { if a.Contract != nil {
script, err := hex.DecodeString(a.Contract.Script) script, err := hex.DecodeString(a.Contract.Script)
if err != nil { if err != nil {

View file

@ -195,8 +195,7 @@ func TestCreateBasicChain(t *testing.T) {
priv0 := testchain.PrivateKeyByID(0) priv0 := testchain.PrivateKeyByID(0)
priv1 := testchain.PrivateKeyByID(1) priv1 := testchain.PrivateKeyByID(1)
priv0ScriptHash := priv0.GetScriptHash() priv0ScriptHash := priv0.GetScriptHash()
acc0, err := wallet.NewAccountFromWIF(priv0.WIF()) acc0 := wallet.NewAccountFromPrivateKey(priv0)
require.NoError(t, err)
// Prepare some transaction for future submission. // Prepare some transaction for future submission.
txSendRaw := newNEP17Transfer(bc.contracts.NEO.Hash, priv0ScriptHash, priv1.GetScriptHash(), int64(util.Fixed8FromInt64(1000))) txSendRaw := newNEP17Transfer(bc.contracts.NEO.Hash, priv0ScriptHash, priv1.GetScriptHash(), int64(util.Fixed8FromInt64(1000)))
@ -268,8 +267,7 @@ func initBasicChain(t *testing.T, bc *Blockchain) {
b.Header().EncodeBinary(buf.BinWriter) b.Header().EncodeBinary(buf.BinWriter)
t.Logf("header: %s", hex.EncodeToString(buf.Bytes())) t.Logf("header: %s", hex.EncodeToString(buf.Bytes()))
acc0, err := wallet.NewAccountFromWIF(priv0.WIF()) acc0 := wallet.NewAccountFromPrivateKey(priv0)
require.NoError(t, err)
// Push some contract into the chain. // Push some contract into the chain.
txDeploy, cHash := newDeployTx(t, priv0ScriptHash, prefix+"test_contract.go", "Rubl") txDeploy, cHash := newDeployTx(t, priv0ScriptHash, prefix+"test_contract.go", "Rubl")

View file

@ -132,16 +132,14 @@ func TestAddNetworkFee(t *testing.T) {
t.Run("Contract", func(t *testing.T) { t.Run("Contract", func(t *testing.T) {
tx := transaction.New(testchain.Network(), []byte{byte(opcode.PUSH1)}, 0) tx := transaction.New(testchain.Network(), []byte{byte(opcode.PUSH1)}, 0)
priv := testchain.PrivateKeyByID(0) priv := testchain.PrivateKeyByID(0)
acc1, err := wallet.NewAccountFromWIF(priv.WIF()) acc1 := wallet.NewAccountFromPrivateKey(priv)
require.NoError(t, err)
acc1.Contract.Deployed = true acc1.Contract.Deployed = true
acc1.Contract.Script, _ = hex.DecodeString(verifyContractAVM) acc1.Contract.Script, _ = hex.DecodeString(verifyContractAVM)
h, _ := util.Uint160DecodeStringLE(verifyContractHash) h, _ := util.Uint160DecodeStringLE(verifyContractHash)
tx.ValidUntilBlock = chain.BlockHeight() + 10 tx.ValidUntilBlock = chain.BlockHeight() + 10
t.Run("Valid", func(t *testing.T) { t.Run("Valid", func(t *testing.T) {
acc0, err := wallet.NewAccountFromWIF(priv.WIF()) acc0 := wallet.NewAccountFromPrivateKey(priv)
require.NoError(t, err)
tx.Signers = []transaction.Signer{ tx.Signers = []transaction.Signer{
{ {
Account: acc0.PrivateKey().GetScriptHash(), Account: acc0.PrivateKey().GetScriptHash(),
@ -173,8 +171,7 @@ func TestAddNetworkFee(t *testing.T) {
require.Error(t, c.AddNetworkFee(tx, 10, acc0, acc1)) require.Error(t, c.AddNetworkFee(tx, 10, acc0, acc1))
}) })
t.Run("InvalidContract", func(t *testing.T) { t.Run("InvalidContract", func(t *testing.T) {
acc0, err := wallet.NewAccountFromWIF(priv.WIF()) acc0 := wallet.NewAccountFromPrivateKey(priv)
require.NoError(t, err)
tx.Signers = []transaction.Signer{ tx.Signers = []transaction.Signer{
{ {
Account: acc0.PrivateKey().GetScriptHash(), Account: acc0.PrivateKey().GetScriptHash(),
@ -200,8 +197,7 @@ func TestSignAndPushInvocationTx(t *testing.T) {
require.NoError(t, c.Init()) require.NoError(t, c.Init())
priv := testchain.PrivateKey(0) priv := testchain.PrivateKey(0)
acc, err := wallet.NewAccountFromWIF(priv.WIF()) acc := wallet.NewAccountFromPrivateKey(priv)
require.NoError(t, err)
h, err := c.SignAndPushInvocationTx([]byte{byte(opcode.PUSH1)}, acc, 30, 0, []transaction.Signer{{ h, err := c.SignAndPushInvocationTx([]byte{byte(opcode.PUSH1)}, acc, 30, 0, []transaction.Signer{{
Account: priv.GetScriptHash(), Account: priv.GetScriptHash(),
Scopes: transaction.CalledByEntry, Scopes: transaction.CalledByEntry,
@ -239,8 +235,7 @@ func TestCreateTxFromScript(t *testing.T) {
require.NoError(t, c.Init()) require.NoError(t, c.Init())
priv := testchain.PrivateKey(0) priv := testchain.PrivateKey(0)
acc, err := wallet.NewAccountFromWIF(priv.WIF()) acc := wallet.NewAccountFromPrivateKey(priv)
require.NoError(t, err)
t.Run("NoSystemFee", func(t *testing.T) { t.Run("NoSystemFee", func(t *testing.T) {
tx, err := c.CreateTxFromScript([]byte{byte(opcode.PUSH1)}, acc, -1, 10) tx, err := c.CreateTxFromScript([]byte{byte(opcode.PUSH1)}, acc, -1, 10)
require.NoError(t, err) require.NoError(t, err)
@ -269,8 +264,7 @@ func TestCreateNEP17TransferTx(t *testing.T) {
require.NoError(t, c.Init()) require.NoError(t, c.Init())
priv := testchain.PrivateKeyByID(0) priv := testchain.PrivateKeyByID(0)
acc, err := wallet.NewAccountFromWIF(priv.WIF()) acc := wallet.NewAccountFromPrivateKey(priv)
require.NoError(t, err)
gasContractHash, err := c.GetNativeContractHash("gas") gasContractHash, err := c.GetNativeContractHash("gas")
require.NoError(t, err) require.NoError(t, err)

View file

@ -996,8 +996,7 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
}) })
priv0 := testchain.PrivateKeyByID(0) priv0 := testchain.PrivateKeyByID(0)
acc0, err := wallet.NewAccountFromWIF(priv0.WIF()) acc0 := wallet.NewAccountFromPrivateKey(priv0)
require.NoError(t, err)
addNetworkFee := func(tx *transaction.Transaction) { addNetworkFee := func(tx *transaction.Transaction) {
size := io.GetVarSize(tx) size := io.GetVarSize(tx)

View file

@ -90,7 +90,7 @@ func NewAccount() (*Account, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newAccountFromPrivateKey(priv), nil return NewAccountFromPrivateKey(priv), nil
} }
// SignTx signs transaction t and updates it's Witnesses. // SignTx signs transaction t and updates it's Witnesses.
@ -172,7 +172,7 @@ func NewAccountFromWIF(wif string) (*Account, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newAccountFromPrivateKey(privKey), nil return NewAccountFromPrivateKey(privKey), nil
} }
// NewAccountFromEncryptedWIF creates a new Account from the given encrypted WIF. // NewAccountFromEncryptedWIF creates a new Account from the given encrypted WIF.
@ -182,7 +182,7 @@ func NewAccountFromEncryptedWIF(wif string, pass string) (*Account, error) {
return nil, err return nil, err
} }
a := newAccountFromPrivateKey(priv) a := NewAccountFromPrivateKey(priv)
a.EncryptedWIF = wif a.EncryptedWIF = wif
return a, nil return a, nil
@ -216,8 +216,8 @@ func (a *Account) ConvertMultisig(m int, pubs []*keys.PublicKey) error {
return nil return nil
} }
// newAccountFromPrivateKey creates a wallet from the given PrivateKey. // NewAccountFromPrivateKey creates a wallet from the given PrivateKey.
func newAccountFromPrivateKey(p *keys.PrivateKey) *Account { func NewAccountFromPrivateKey(p *keys.PrivateKey) *Account {
pubKey := p.PublicKey() pubKey := p.PublicKey()
pubAddr := p.Address() pubAddr := p.Address()
wif := p.WIF() wif := p.WIF()