Skip to content

Commit

Permalink
fix: crash of import account private key.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Jun 29, 2023
1 parent 33593f0 commit 1c4d334
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
4 changes: 4 additions & 0 deletions core/aptos/account.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aptos

import (
"crypto/ed25519"
"encoding/hex"
"errors"

Expand Down Expand Up @@ -28,6 +29,9 @@ func AccountWithPrivateKey(prikey string) (*Account, error) {
if err != nil {
return nil, err
}
if len(seed) != ed25519.SeedSize {
return nil, base.ErrInvalidPrivateKey
}
account := aptosaccount.NewAccount(seed)
return &Account{account: account}, nil
}
Expand Down
18 changes: 9 additions & 9 deletions core/aptos/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/coming-chat/wallet-SDK/core/testcase"
"github.com/stretchr/testify/require"
)

var (
Expand All @@ -16,17 +17,16 @@ var (
func TestAccount(t *testing.T) {
mnemonic := testcase.M1
account, err := NewAccountWithMnemonic(mnemonic)
if err != nil {
t.Fatal(err)
}
t.Log(account.PrivateKeyHex())
t.Log(account.PublicKeyHex())
t.Log(account.Address())
require.Nil(t, err)

prihex, _ := account.PrivateKeyHex()
acc2, err := AccountWithPrivateKey(prihex)
if err != nil {
t.Fatal(err)
}
require.Nil(t, err)

require.Equal(t, account.PublicKey(), acc2.PublicKey())
require.Equal(t, account.Address(), acc2.Address())
require.Equal(t, account.Address(), "0x11dd2037a613716fdc7cdbd96390b6450bce6754e46b9251cd3c8cd7733683bd")

t.Log(acc2.PrivateKeyHex())
t.Log(acc2.PublicKeyHex())
t.Log(acc2.Address())
Expand Down
3 changes: 3 additions & 0 deletions core/starcoin/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func AccountWithPrivateKey(privateKey string) (*Account, error) {
if err != nil {
return nil, err
}
if ed25519.SeedSize != len(key) {
return nil, base.ErrInvalidPrivateKey
}
return accountWithKey(key), nil
}

Expand Down
2 changes: 0 additions & 2 deletions core/starcoin/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ func TestAccount(t *testing.T) {
mnemonic := testcase.M1
account, err := NewAccountWithMnemonic(mnemonic)
require.Nil(t, err)
require.Equal(t, account.address, testcase.Accounts.Starcoin.Address)
t.Log(account.address)

// private key hex to account
priHex, err := account.PrivateKeyHex()
Expand Down
23 changes: 23 additions & 0 deletions core/wallet/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"testing"
"time"

"github.com/coming-chat/wallet-SDK/core/btc"
"github.com/coming-chat/wallet-SDK/core/cosmos"
"github.com/coming-chat/wallet-SDK/core/doge"
"github.com/coming-chat/wallet-SDK/core/testcase"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -147,3 +150,23 @@ var (
cacheKey: "empty",
}
)

func TestDecimalPrivatekey(t *testing.T) {
decimalKey := "2522809042406563759994430227158123451351879727850354505141300412651234567890"
argWallet := WalletStore{
cacheKey: "decimal_privatekey",
privateKey: decimalKey,
}

wallet := NewCacheWallet(&argWallet)
t.Log(wallet.PolkaAccountInfo(44).Address())
t.Log(wallet.BitcoinAccountInfo(btc.ChainMainnet, btc.AddressTypeComingTaproot).Address())
t.Log(wallet.EthereumAccountInfo().Address())
t.Log(wallet.CosmosAccountInfo(cosmos.CosmosAtom.Cointype, cosmos.CosmosAtom.Prefix).Address())
t.Log(wallet.DogecoinAccountInfo(doge.ChainMainnet).Address())
t.Log(wallet.SolanaAccountInfo().Address())
t.Log(wallet.AptosAccountInfo().Address())
t.Log(wallet.SuiAccountInfo().Address())
t.Log(wallet.StarcoinAccountInfo().Address())
t.Log(wallet.StarknetAccountInfo().Address())
}

0 comments on commit 1c4d334

Please sign in to comment.