generator

package
v0.110.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2022 License: MPL-2.0 Imports: 16 Imported by: 0

README

Account Generator

The Account Generator is used to generate, import, derive child keys, and store accounts. It is instantiated in the account.Manager struct and it's accessible from the lib and mobile package through functions with the MultiAccount prefix:

  • MultiAccountGenerate
  • MultiAccountGenerateAndDeriveAddresses
  • MultiAccountImportMnemonic
  • MultiAccountDeriveAddresses
  • MultiAccountStoreDerivedAccounts
  • MultiAccountImportPrivateKey
  • MultiAccountStoreAccount
  • MultiAccountLoadAccount
  • MultiAccountReset

Using Generate and ImportMnemonic, a master key is loaded in memory and a random temporarily id is returned. Bare in mind these accounts are not saved. They are in memory until StoreAccount or StoreDerivedAccounts are called. Calling Reset or restarting the application will remove everything from memory. Logging-in and Logging-out will do the same.

Since Generate and ImportMnemonic create extended keys, we can use those keys to derive new child keys. MultiAccountDeriveAddresses(id, paths) returns a list of addresses/pubKey, one for each path. This can be used to check balances on those addresses and show them to the user.

Once the user is happy with some specific derivation paths, we can store them using StoreDerivedAccounts(id, passwordToEncryptKey, paths). StoreDerivedAccounts returns an address/pubKey for each path. The address can be use in the future to load them in memory again. Calling StoreDerivedAccounts will encrypt and store the keys, each one in a keystore json file, and remove all the keys from memory. Since they are derived from an extended key, they are extended keys too, so they can be used in the future to derive more child keys. StoreAccount stores the key identified by its ID, so in case the key comes from Generate or ImportPrivateKey, it will store the master key. In general we want to avoid saving master keys, so we should only use StoreDerivedAccounts for extended keys, and StoreAccount for normal keys.

Calling Load(address, password) will unlock the key specified by addresses using password, and load it in memory. Load returns a new id that can be used again with DeriveAddresses, StoreAccount, and StoreDerivedAccounts.

ImportPrivateKey imports a raw private key specified by its hex form. It's not an extended key, so it can't be used to derive child addresses. You can call DeriveAddresses to derive the address/pubKey of a normal key passing an empty string as derivation path. StoreAccount will save the key without deriving a child key.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAccountNotFoundByID is returned when the selected account doesn't exist in memory.
	ErrAccountNotFoundByID = errors.New("account not found")
	// ErrAccountCannotDeriveChildKeys is returned when trying to derive child accounts from a normal key.
	ErrAccountCannotDeriveChildKeys = errors.New("selected account cannot derive child keys")
	// ErrAccountManagerNotSet is returned when the account mananger instance is not set.
	ErrAccountManagerNotSet = errors.New("account manager not set")
)
View Source
var (
	// ErrInvalidKeystoreExtendedKey is returned when the decrypted keystore file
	// contains some old Status keys.
	// The old version used to store the BIP44 account at index 0 as PrivateKey,
	// and the BIP44 account at index 1 as ExtendedKey.
	// The current version stores the same key as PrivateKey and ExtendedKey.
	ErrInvalidKeystoreExtendedKey  = errors.New("PrivateKey and ExtendedKey are different")
	ErrInvalidMnemonicPhraseLength = errors.New("invalid mnemonic phrase length; valid lengths are 12, 15, 18, 21, and 24")
)

Functions

func MnemonicPhraseLengthToEntropyStrength

func MnemonicPhraseLengthToEntropyStrength(length int) (extkeys.EntropyStrength, error)

MnemonicPhraseLengthToEntropyStrength returns the entropy strength for a given mnemonic length

func ValidateKeystoreExtendedKey

func ValidateKeystoreExtendedKey(key *types.Key) error

ValidateKeystoreExtendedKey validates the keystore keys, checking that ExtendedKey is the extended key of PrivateKey.

Types

type Account added in v0.65.0

type Account struct {
	// contains filtered or unexported fields
}

func NewAccount added in v0.65.0

func NewAccount(privateKey *ecdsa.PrivateKey, extKey *extkeys.ExtendedKey) Account

func (*Account) ToAccountInfo added in v0.65.0

func (a *Account) ToAccountInfo() AccountInfo

func (*Account) ToGeneratedAccountInfo added in v0.65.0

func (a *Account) ToGeneratedAccountInfo(id string, mnemonic string) GeneratedAccountInfo

func (*Account) ToIdentifiedAccountInfo added in v0.65.0

func (a *Account) ToIdentifiedAccountInfo(id string) IdentifiedAccountInfo

type AccountInfo

type AccountInfo struct {
	PublicKey string `json:"publicKey"`
	Address   string `json:"address"`
}

AccountInfo contains a PublicKey and an Address of an account.

type AccountManager

type AccountManager interface {
	AddressToDecryptedAccount(address, password string) (types.Account, *types.Key, error)
	ImportSingleExtendedKey(extKey *extkeys.ExtendedKey, password string) (address, pubKey string, err error)
	ImportAccount(privateKey *ecdsa.PrivateKey, password string) (types.Address, error)
}

type GeneratedAccountInfo

type GeneratedAccountInfo struct {
	IdentifiedAccountInfo
	Mnemonic string `json:"mnemonic"`
}

GeneratedAccountInfo contains IdentifiedAccountInfo and the mnemonic of an account.

type GeneratedAndDerivedAccountInfo

type GeneratedAndDerivedAccountInfo struct {
	GeneratedAccountInfo
	Derived map[string]AccountInfo `json:"derived"`
}

GeneratedAndDerivedAccountInfo contains GeneratedAccountInfo and derived AccountInfo mapped by derivation path.

type Generator

type Generator struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func New

func New(am AccountManager) *Generator

func (*Generator) CreateAccountFromMnemonic added in v0.106.3

func (g *Generator) CreateAccountFromMnemonic(mnemonicPhrase string, bip39Passphrase string) (GeneratedAccountInfo, error)

func (*Generator) DeriveAddresses

func (g *Generator) DeriveAddresses(accountID string, pathStrings []string) (map[string]AccountInfo, error)

func (*Generator) Generate

func (g *Generator) Generate(mnemonicPhraseLength int, n int, bip39Passphrase string) ([]GeneratedAccountInfo, error)

func (*Generator) GenerateAndDeriveAddresses

func (g *Generator) GenerateAndDeriveAddresses(mnemonicPhraseLength int, n int, bip39Passphrase string, pathStrings []string) ([]GeneratedAndDerivedAccountInfo, error)

func (*Generator) ImportJSONKey

func (g *Generator) ImportJSONKey(json string, password string) (IdentifiedAccountInfo, error)

func (*Generator) ImportMnemonic

func (g *Generator) ImportMnemonic(mnemonicPhrase string, bip39Passphrase string) (GeneratedAccountInfo, error)

func (*Generator) ImportPrivateKey

func (g *Generator) ImportPrivateKey(privateKeyHex string) (IdentifiedAccountInfo, error)

func (*Generator) LoadAccount

func (g *Generator) LoadAccount(address string, password string) (IdentifiedAccountInfo, error)

func (*Generator) Reset

func (g *Generator) Reset()

Reset resets the accounts map removing all the accounts from memory.

func (*Generator) StoreAccount

func (g *Generator) StoreAccount(accountID string, password string) (AccountInfo, error)

func (*Generator) StoreDerivedAccounts

func (g *Generator) StoreDerivedAccounts(accountID string, password string, pathStrings []string) (map[string]AccountInfo, error)

type IdentifiedAccountInfo

type IdentifiedAccountInfo struct {
	AccountInfo
	ID string `json:"id"`
	// KeyUID is calculated as sha256 of the master public key and used for key
	// identification. This is the only information available about the master
	// key stored on a keycard before the card is paired.
	// KeyUID name is chosen over KeyID in order to make it consistent with
	// the name already used in Status and Keycard codebases.
	KeyUID string `json:"keyUid"`
}

IdentifiedAccountInfo contains AccountInfo and the ID of an account.

func (*IdentifiedAccountInfo) ToMultiAccount added in v0.65.0

func (iai *IdentifiedAccountInfo) ToMultiAccount() *multiaccounts.Account

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL