keystore

package
v0.0.0-...-163fc3c Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PoCUsage    AddrUse = iota //0
	WalletUsage                //1

	// MaxAccountNum is the maximum allowed account number.  This value was
	// chosen because accounts are hardened children and therefore must not
	// exceed the hardened child range of extended keys and it provides a
	// reserved account at the top of the range for supporting imported
	// addresses.
	MaxAccountNum = hdkeychain.HardenedKeyStart - 2 // 2^31 - 2

	// MaxAddressesPerAccount is the maximum allowed number of addresses
	// per account number.  This value is based on the limitation of the
	// underlying hierarchical deterministic key derivation.
	MaxAddressesPerAccount = hdkeychain.HardenedKeyStart - 1

	// ExternalBranch is the child number to use when performing BIP0044
	// style hierarchical deterministic key derivation for the external
	// branch.
	// https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
	ExternalBranch uint32 = 0

	// InternalBranch is the child number to use when performing BIP0044
	// style hierarchical deterministic key derivation for the internal
	// branch.
	InternalBranch uint32 = 1
)

Variables

View Source
var (
	ErrKMPubKeyNotSet = errors.New("required KeystoreManager public key parameters not stored in database")

	ErrIllegalPassphrase  = errors.New("illegal passphrase")
	ErrIllegalNewPrivPass = errors.New("new private passphrase same as public passphrase")
	ErrIllegalNewPubPass  = errors.New("new public passphrase same as private passphrase")
	ErrSamePrivpass       = errors.New("new private passphrase same as the original one")
	ErrSamePubpass        = errors.New("new public passphrase same as the original one")
	ErrDifferentPrivPass  = errors.New("private passphrase of the imported keystore is different from the private passphrase of the existing keystores")
	ErrIllegalSeed        = errors.New("illegal seed")
	ErrIllegalRemarks     = errors.New("illegal remarks")

	ErrUnexpectError = errors.New("unexpected error")

	ErrAddressNotFound         = errors.New("address not found")
	ErrAccountNotFound         = errors.New("account not found")
	ErrCurrentKeystoreNotFound = errors.New("current keystore not found")
	ErrUnexpecteDBError        = errors.New("unexpected error occurred in DB")
	ErrKeyScopeNotFound        = errors.New("KeyScope definition not found")
	ErrScriptHashNotFound      = errors.New("scriptHash not found")
	ErrPubKeyNotFound          = errors.New("pubKey not found")
	ErrNilPointer              = errors.New("the pointer is nil")
	ErrBucketNotFound          = errors.New("bucket not found")
	ErrInvalidPassphrase       = errors.New("invalid passphrase for master private key")
	ErrDeriveMasterPrivKey     = errors.New("failed to derive master private key")
	ErrAccountType             = errors.New("invalid accountType")
	ErrAddrManagerLocked       = errors.New("addrManager is locked")

	ErrNoKeystoreActivated = errors.New("no keystore activated")
	ErrDuplicateSeed       = errors.New("duplicate seed in the wallet")

	ErrAddressVersion      = errors.New("unexpected address version")
	ErrInvalidKeystoreJson = errors.New("invalid keystore json")
)
View Source
var (
	TestnetKeyScope = KeyScope{
		Purpose: 44,
		Coin:    1,
	}

	MainnetKeyScope = KeyScope{
		Purpose: 44,
		Coin:    2021,
	}

	// Compatible  cointype
	MassNetKeyScope = KeyScope{
		Purpose: 44,
		Coin:    297,
	}

	PocDerivationPath = DerivationPath{
		Account: 0,
		Branch:  0,
	}

	WalletDerivationPath = DerivationPath{
		Account: 1,
		Branch:  0,
	}
)
View Source
var DefaultScryptOptions = ScryptOptions{
	N: 262144,
	R: 8,
	P: 1,
}

DefaultScryptOptions is the default options used with scrypt.

View Source
var Net2KeyScope = map[uint32]KeyScope{
	1:    TestnetKeyScope,
	2021: MainnetKeyScope,
	297:  MassNetKeyScope,
}

Functions

func GenerateSeed

func GenerateSeed() ([]byte, error)

generate seed

func NewPoCAddress

func NewPoCAddress(pubKey *pocec.PublicKey, net *config.Params) ([]byte, chainutil.Address, error)

func ValidatePassphrase

func ValidatePassphrase(pass []byte) bool

func ValidateSeed

func ValidateSeed(seed []byte) bool

Types

type AddrManager

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

func (*AddrManager) AddrUse

func (a *AddrManager) AddrUse() AddrUse

func (*AddrManager) Address

func (a *AddrManager) Address(addr string) (*ManagedAddress, error)

func (*AddrManager) CountAddresses

func (a *AddrManager) CountAddresses() (external int, internal int)

func (*AddrManager) KeyScope

func (a *AddrManager) KeyScope() KeyScope

func (*AddrManager) ListAddresses

func (a *AddrManager) ListAddresses() []string

func (*AddrManager) ManagedAddresses

func (a *AddrManager) ManagedAddresses() []*ManagedAddress

func (*AddrManager) Name

func (a *AddrManager) Name() string

func (*AddrManager) Remarks

func (a *AddrManager) Remarks() string

type AddrUse

type AddrUse uint8

type DerivationPath

type DerivationPath struct {
	// Account is the account, or the first immediate child from the scoped
	// manager's hardened coin type key.
	Account uint32

	// Branch is the branch to be derived from the account index above. For
	// BIP0044-like derivation, this is either 0 (external) or 1
	// (internal). However, we allow this value to vary arbitrarily within
	// its size range.
	Branch uint32

	// Index is the final child in the derivation path. This denotes the
	// key index within as a child of the account and branch.
	Index uint32
}

DerivationPath represents a derivation path from a particular key manager's scope. Each ScopedKeyManager starts key derivation from the end of their coinType hardened key: m/purpose'/coinType'. The fields in this struct allow further derivation to the next three child levels after the coin type key. This restriction is in the spirit of BIP0044 type derivation. We maintain a degree of coherency with the standard, but allow arbitrary derivations beyond the coinType key. The key derived using this path will be exactly: m/purpose'/coinType'/account/branch/index, where purpose' and coinType' are bound by the scope of a particular manager.

type EncryptorDecryptor

type EncryptorDecryptor interface {
	Encrypt(in []byte) ([]byte, error)
	Decrypt(in []byte) ([]byte, error)
	Bytes() []byte
	CopyBytes([]byte)
	Zero()
}

EncryptorDecryptor provides an abstraction on top of snacl.CryptoKey so that our tests can use dependency injection to force the behaviour they need.

type KeyScope

type KeyScope struct {
	// Purpose is the purpose of this key scope. This is the first child of
	// the master HD key.
	Purpose uint32
	// Coin is a value that represents the particular coin which is the
	// child of the purpose key. With this key, any accounts, or other
	// children can be derived at all.
	Coin uint32 // 1-testnet,  2021-mainnet
}

KeyScope represents a restricted key scope from the primary root key within the HD chain. From the root manager (m/) we can create a nearly arbitrary number of ScopedKeyManagers of key derivation path: m/purpose'/coinType'. These scoped managers can then me managed indecently, as they house the encrypted coinType key and can derive any child keys from there on. https://github.com/satoshilabs/slips/blob/master/slip-0044.md

type Keystore

type Keystore struct {
	Remark string     `json:"remark"`
	Crypto cryptoJSON `json:"crypto"`
	HDpath hdPath     `json:"hdPath"`
}

func GetKeystoreFromJson

func GetKeystoreFromJson(keysJson []byte) (*Keystore, error)

func (*Keystore) Bytes

func (k *Keystore) Bytes() []byte

type KeystoreManagerForPoC

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

func NewKeystoreManagerForPoC

func NewKeystoreManagerForPoC(store db.DB, pubPassphrase []byte, net *config.Params) (*KeystoreManagerForPoC, error)

NewKeystoreManagerForPoC pubPassphrase read form config.app.pub_password

func (*KeystoreManagerForPoC) ChainParams

func (kmc *KeystoreManagerForPoC) ChainParams() *config.Params

func (*KeystoreManagerForPoC) ChangePrivPassphrase

func (kmc *KeystoreManagerForPoC) ChangePrivPassphrase(oldPrivPass, newPrivPass []byte, scryptConfig *ScryptOptions) error

func (*KeystoreManagerForPoC) ChangePubPassphrase

func (kmc *KeystoreManagerForPoC) ChangePubPassphrase(oldPubPass, newPubPass []byte, scryptConfig *ScryptOptions) error

func (*KeystoreManagerForPoC) ChangeRemark

func (kmc *KeystoreManagerForPoC) ChangeRemark(accountID, newRemark string) error

func (*KeystoreManagerForPoC) DeleteKeystore

func (kmc *KeystoreManagerForPoC) DeleteKeystore(accountID string, privPassphrase []byte) (bool, error)

func (*KeystoreManagerForPoC) ExportKeystore

func (kmc *KeystoreManagerForPoC) ExportKeystore(accountID string, privPassphrase []byte) (keystore *Keystore, addrManager *AddrManager, err error)

ExportKeystore

func (*KeystoreManagerForPoC) ExportKeystores

func (kmc *KeystoreManagerForPoC) ExportKeystores(privPassphrase []byte) (map[string]*Keystore, map[string]*AddrManager, error)

func (*KeystoreManagerForPoC) GenerateNewPublicKey

func (kmc *KeystoreManagerForPoC) GenerateNewPublicKey(accountID string) (*pocec.PublicKey, uint32, error)

func (*KeystoreManagerForPoC) GenerateNewPublicKeyByCointype

func (kmc *KeystoreManagerForPoC) GenerateNewPublicKeyByCointype(cointype uint32) (*pocec.PublicKey, uint32, error)

func (*KeystoreManagerForPoC) GetAddrManager

func (kmc *KeystoreManagerForPoC) GetAddrManager(accountId string) (*AddrManager, bool)

func (*KeystoreManagerForPoC) GetAddressByPubKey

func (kmc *KeystoreManagerForPoC) GetAddressByPubKey(pubKey *pocec.PublicKey) (string, error)

func (*KeystoreManagerForPoC) GetManagedAddrManager

func (kmc *KeystoreManagerForPoC) GetManagedAddrManager() []*AddrManager

func (*KeystoreManagerForPoC) GetPublicKeyOrdinal

func (kmc *KeystoreManagerForPoC) GetPublicKeyOrdinal(pubKey *pocec.PublicKey) (uint32, bool)

func (*KeystoreManagerForPoC) ImportKeystore

func (kmc *KeystoreManagerForPoC) ImportKeystore(keystoreJson []byte, oldPrivPass, currentPrivPass []byte) (string, string, error)

ImportKeystore

func (*KeystoreManagerForPoC) IsLocked

func (kmc *KeystoreManagerForPoC) IsLocked() bool

func (*KeystoreManagerForPoC) ListKeystoreNames

func (kmc *KeystoreManagerForPoC) ListKeystoreNames() []string

func (*KeystoreManagerForPoC) Lock

func (kmc *KeystoreManagerForPoC) Lock()

func (*KeystoreManagerForPoC) NewKeystore

func (kmc *KeystoreManagerForPoC) NewKeystore(privPassphrase, seed []byte, remark string,
	net *config.Params, scryptConfig *ScryptOptions) (string, error)

func (*KeystoreManagerForPoC) NextAddresses

func (kmc *KeystoreManagerForPoC) NextAddresses(accountID string, internal bool, numAddresses uint32) ([]*ManagedAddress, error)

func (*KeystoreManagerForPoC) SignHash

func (kmc *KeystoreManagerForPoC) SignHash(pubKey *pocec.PublicKey, hash []byte) (*pocec.Signature, error)

func (*KeystoreManagerForPoC) SignMessage

func (kmc *KeystoreManagerForPoC) SignMessage(pubKey *pocec.PublicKey, message []byte) (*pocec.Signature, error)

func (*KeystoreManagerForPoC) Unlock

func (kmc *KeystoreManagerForPoC) Unlock(privPassphrase []byte) error

func (*KeystoreManagerForPoC) VerifySig

func (kmc *KeystoreManagerForPoC) VerifySig(sig *pocec.Signature, hash []byte, pubKey *pocec.PublicKey) (bool, error)

type ManagedAddress

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

func (*ManagedAddress) Account

func (mAddr *ManagedAddress) Account() string

func (*ManagedAddress) DerivationPath

func (mAddr *ManagedAddress) DerivationPath() DerivationPath

func (*ManagedAddress) IsChangeAddr

func (mAddr *ManagedAddress) IsChangeAddr() bool

func (*ManagedAddress) PrivKey

func (mAddr *ManagedAddress) PrivKey() *pocec.PrivateKey

func (*ManagedAddress) PubKey

func (mAddr *ManagedAddress) PubKey() *pocec.PublicKey

func (*ManagedAddress) ScriptAddress

func (mAddr *ManagedAddress) ScriptAddress() []byte

func (*ManagedAddress) String

func (mAddr *ManagedAddress) String() string

type ScryptOptions

type ScryptOptions struct {
	N, R, P int
}

ScryptOptions is used to hold the scrypt parameters needed when deriving new passphrase keys.

Directories

Path Synopsis
Package hdkeychain provides an API for bitcoin hierarchical deterministic extended keys (BIP0032).
Package hdkeychain provides an API for bitcoin hierarchical deterministic extended keys (BIP0032).
Package zero contains functions to clear data from byte slices and multi-precision integers.
Package zero contains functions to clear data from byte slices and multi-precision integers.

Jump to

Keyboard shortcuts

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