feat: create acc from private key endpoint added
`CreateAccountFromPrivateKey` function added, it generates an account based on provided private key, but doesn't store anything anywhere.
This commit is contained in:
parent
f10bd00ceb
commit
a812365525
|
@ -69,6 +69,20 @@ func (g *Generator) Generate(mnemonicPhraseLength int, n int, bip39Passphrase st
|
|||
return infos, err
|
||||
}
|
||||
|
||||
func (g *Generator) CreateAccountFromPrivateKey(privateKeyHex string) (IdentifiedAccountInfo, error) {
|
||||
privateKeyHex = strings.TrimPrefix(privateKeyHex, "0x")
|
||||
privateKey, err := crypto.HexToECDSA(privateKeyHex)
|
||||
if err != nil {
|
||||
return IdentifiedAccountInfo{}, err
|
||||
}
|
||||
|
||||
acc := &Account{
|
||||
privateKey: privateKey,
|
||||
}
|
||||
|
||||
return acc.ToIdentifiedAccountInfo(""), nil
|
||||
}
|
||||
|
||||
func (g *Generator) ImportPrivateKey(privateKeyHex string) (IdentifiedAccountInfo, error) {
|
||||
privateKeyHex = strings.TrimPrefix(privateKeyHex, "0x")
|
||||
privateKey, err := crypto.HexToECDSA(privateKeyHex)
|
||||
|
@ -124,8 +138,7 @@ func (g *Generator) CreateAccountFromMnemonicAndDeriveAccountsForPaths(mnemonicP
|
|||
}
|
||||
}
|
||||
|
||||
id := uuid.NewRandom().String()
|
||||
accInfo := acc.ToGeneratedAccountInfo(id, mnemonicPhrase)
|
||||
accInfo := acc.ToGeneratedAccountInfo("", mnemonicPhrase)
|
||||
|
||||
return accInfo.toGeneratedAndDerived(derivedAccountsInfo), nil
|
||||
}
|
||||
|
|
|
@ -44,6 +44,11 @@ const testAccountJSONFile = `{
|
|||
"version":3
|
||||
}`
|
||||
|
||||
const (
|
||||
path0 = "m/44'/60'/0'/0/0"
|
||||
path1 = "m/44'/60'/0'/0/1"
|
||||
)
|
||||
|
||||
func TestGenerator_Generate(t *testing.T) {
|
||||
g := New(nil)
|
||||
assert.Equal(t, 0, len(g.accounts))
|
||||
|
@ -58,6 +63,17 @@ func TestGenerator_Generate(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGenerator_CreateAccountFromPrivateKey(t *testing.T) {
|
||||
g := New(nil)
|
||||
assert.Equal(t, 0, len(g.accounts))
|
||||
|
||||
info, err := g.CreateAccountFromPrivateKey(testAccount.bip44Key0)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(g.accounts))
|
||||
assert.Equal(t, 66, len(info.KeyUID))
|
||||
}
|
||||
|
||||
func TestGenerator_ImportPrivateKey(t *testing.T) {
|
||||
g := New(nil)
|
||||
assert.Equal(t, 0, len(g.accounts))
|
||||
|
@ -74,12 +90,14 @@ func TestGenerator_CreateAccountFromMnemonicAndDeriveAccountsForPaths(t *testing
|
|||
g := New(nil)
|
||||
assert.Equal(t, 0, len(g.accounts))
|
||||
|
||||
info, err := g.CreateAccountFromMnemonicAndDeriveAccountsForPaths(testAccount.mnemonic, testAccount.bip39Passphrase, []string{})
|
||||
info, err := g.CreateAccountFromMnemonicAndDeriveAccountsForPaths(testAccount.mnemonic, testAccount.bip39Passphrase, []string{path0, path1})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(g.accounts))
|
||||
assert.Equal(t, 36, len(info.ID))
|
||||
assert.Equal(t, 66, len(info.KeyUID))
|
||||
|
||||
assert.Equal(t, testAccount.bip44Address0, info.Derived[path0].Address)
|
||||
assert.Equal(t, testAccount.bip44Address1, info.Derived[path1].Address)
|
||||
}
|
||||
|
||||
func TestGenerator_ImportMnemonic(t *testing.T) {
|
||||
|
@ -121,9 +139,6 @@ func TestGenerator_DeriveAddresses(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, len(g.accounts))
|
||||
|
||||
path0 := "m/44'/60'/0'/0/0"
|
||||
path1 := "m/44'/60'/0'/0/1"
|
||||
|
||||
addresses, err := g.DeriveAddresses(info.ID, []string{path0, path1})
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
|
|
@ -138,6 +138,27 @@ func MultiAccountStoreDerivedAccounts(paramsJSON string) string {
|
|||
return string(out)
|
||||
}
|
||||
|
||||
// CreateAccountFromPrivateKey returns an account derived from the private key without storing it
|
||||
func CreateAccountFromPrivateKey(paramsJSON string) string {
|
||||
var p MultiAccountImportPrivateKeyParams
|
||||
|
||||
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
||||
return makeJSONResponse(err)
|
||||
}
|
||||
|
||||
resp, err := statusBackend.AccountManager().AccountsGenerator().CreateAccountFromPrivateKey(p.PrivateKey)
|
||||
if err != nil {
|
||||
return makeJSONResponse(err)
|
||||
}
|
||||
|
||||
out, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
return makeJSONResponse(err)
|
||||
}
|
||||
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// MultiAccountImportPrivateKey imports a raw private key without storing it.
|
||||
func MultiAccountImportPrivateKey(paramsJSON string) string {
|
||||
var p MultiAccountImportPrivateKeyParams
|
||||
|
|
Loading…
Reference in New Issue