diff --git a/util/crypto/mnemonic.go b/util/crypto/mnemonic.go index b1026a4f..9aed9022 100644 --- a/util/crypto/mnemonic.go +++ b/util/crypto/mnemonic.go @@ -6,11 +6,12 @@ import ( "errors" "fmt" - "github.com/anyproto/any-sync/util/go-ethereum/accounts" "github.com/anyproto/go-slip10" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcutil/hdkeychain" "github.com/tyler-smith/go-bip39" + + "github.com/anyproto/any-sync/util/ethereum/accounts" ) var ( @@ -186,36 +187,6 @@ func derivePrivateKey(masterKey *hdkeychain.ExtendedKey, path accounts.Derivatio return privateKeyECDSA, nil } -/* -// this function was using go-ethereum codebase which is huge -// we got rid of it -// -func (m Mnemonic) ethereumKeyFromMnemonic(index uint32, path string) (pk *ecdsa.PrivateKey, err error) { - wallet, err := hdwallet.NewFromMnemonic(string(m)) - if err != nil { - return nil, err - } - - // m/44'/60'/0'/0/0 for first account - // m/44'/60'/0'/0/1 for second account, etc - fullPath := fmt.Sprintf("%s/%d", path, index) - - p := hdwallet.MustParseDerivationPath(fullPath) - account, err := wallet.Derive(p, false) - if err != nil { - return nil, err - } - - //addr = account.Address - - pk, err = wallet.PrivateKey(account) - if err != nil { - return nil, err - } - - return pk, nil -}*/ - func (m Mnemonic) ethereumKeyFromMnemonic(index uint32, path string) (pk *ecdsa.PrivateKey, err error) { seed, err := m.Seed() if err != nil { diff --git a/util/crypto/mnemonic_test.go b/util/crypto/mnemonic_test.go index 20758f85..a3bd76ee 100644 --- a/util/crypto/mnemonic_test.go +++ b/util/crypto/mnemonic_test.go @@ -52,33 +52,4 @@ func TestMnemonic(t *testing.T) { publicKey := res.EthereumIdentity.Public() _, ok := publicKey.(*ecdsa.PublicKey) require.Equal(t, true, ok) - //ethAddress := crypto.PubkeyToAddress(*publicKeyECDSA) - //require.Equal(t, common.HexToAddress("0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947"), ethAddress) } - -/* -// this test was using go-ethereum codebase which is huge -// we got rid of it -func TestMnemonic_ethereumKeyFromMnemonic(t *testing.T) { - var badPphrase Mnemonic = "tag volcano" - _, err := badPphrase.ethereumKeyFromMnemonic(0, defaultEthereumDerivation) - require.Error(t, err) - - // good - var phrase Mnemonic = "tag volcano eight thank tide danger coast health above argue embrace heavy" - - pk, err := phrase.ethereumKeyFromMnemonic(0, defaultEthereumDerivation) - require.NoError(t, err) - - // what wallet.PrivateKeyHex(account) does - bytes := crypto.FromECDSA(pk) - pkStr := hexutil.Encode(bytes)[2:] - require.Equal(t, "63e21d10fd50155dbba0e7d3f7431a400b84b4c2ac1ee38872f82448fe3ecfb9", pkStr) - - pk, err = phrase.ethereumKeyFromMnemonic(1, defaultEthereumDerivation) - require.NoError(t, err) - - bytes = crypto.FromECDSA(pk) - pkStr = hexutil.Encode(bytes)[2:] - require.Equal(t, "b31048b0aa87649bdb9016c0ee28c788ddfc45e52cd71cc0da08c47cb4390ae7", pkStr) -}*/ diff --git a/util/go-ethereum/accounts/hd.go b/util/ethereum/accounts/hd.go similarity index 66% rename from util/go-ethereum/accounts/hd.go rename to util/ethereum/accounts/hd.go index daca75eb..2ec577fc 100644 --- a/util/go-ethereum/accounts/hd.go +++ b/util/ethereum/accounts/hd.go @@ -17,7 +17,6 @@ package accounts import ( - "encoding/json" "errors" "fmt" "math" @@ -35,11 +34,6 @@ var DefaultRootDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, // at m/44'/60'/0'/0/1, etc. var DefaultBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0} -// LegacyLedgerBaseDerivationPath is the legacy base path from which custom derivation -// endpoints are incremented. As such, the first account will be at m/44'/60'/0'/0, the -// second at m/44'/60'/0'/1, etc. -var LegacyLedgerBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0} - // DerivationPath represents the computer friendly version of a hierarchical // deterministic wallet account derivation path. // @@ -116,65 +110,3 @@ func ParseDerivationPath(path string) (DerivationPath, error) { } return result, nil } - -// String implements the stringer interface, converting a binary derivation path -// to its canonical representation. -func (path DerivationPath) String() string { - result := "m" - for _, component := range path { - var hardened bool - if component >= 0x80000000 { - component -= 0x80000000 - hardened = true - } - result = fmt.Sprintf("%s/%d", result, component) - if hardened { - result += "'" - } - } - return result -} - -// MarshalJSON turns a derivation path into its json-serialized string -func (path DerivationPath) MarshalJSON() ([]byte, error) { - return json.Marshal(path.String()) -} - -// UnmarshalJSON a json-serialized string back into a derivation path -func (path *DerivationPath) UnmarshalJSON(b []byte) error { - var dp string - var err error - if err = json.Unmarshal(b, &dp); err != nil { - return err - } - *path, err = ParseDerivationPath(dp) - return err -} - -// DefaultIterator creates a BIP-32 path iterator, which progresses by increasing the last component: -// i.e. m/44'/60'/0'/0/0, m/44'/60'/0'/0/1, m/44'/60'/0'/0/2, ... m/44'/60'/0'/0/N. -func DefaultIterator(base DerivationPath) func() DerivationPath { - path := make(DerivationPath, len(base)) - copy(path[:], base[:]) - // Set it back by one, so the first call gives the first result - path[len(path)-1]-- - return func() DerivationPath { - path[len(path)-1]++ - return path - } -} - -// LedgerLiveIterator creates a bip44 path iterator for Ledger Live. -// Ledger Live increments the third component rather than the fifth component -// i.e. m/44'/60'/0'/0/0, m/44'/60'/1'/0/0, m/44'/60'/2'/0/0, ... m/44'/60'/N'/0/0. -func LedgerLiveIterator(base DerivationPath) func() DerivationPath { - path := make(DerivationPath, len(base)) - copy(path[:], base[:]) - // Set it back by one, so the first call gives the first result - path[2]-- - return func() DerivationPath { - // ledgerLivePathIterator iterates on the third component - path[2]++ - return path - } -}