keystore

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: GPL-3.0, GPL-3.0 Imports: 37 Imported by: 0

Documentation

Overview

Package keystore implements encrypted storage of secp256k1 private keys.

Keys are stored as encrypted JSON files according to the Web3 Secret Storage specification. See https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition for more information.

Index

Constants

View Source
const (
	KeyType_HD_Seed      = "HDSeed"
	KeyType_ECDSA_KEY    = "ECDSA"
	KeyType_Outchain_KEY = "Outchain"
)
View Source
const (

	// StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB
	// memory and taking approximately 1s CPU time on a modern processor.
	StandardScryptN = 1 << 18

	// StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB
	// memory and taking approximately 1s CPU time on a modern processor.
	StandardScryptP = 1

	// LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB
	// memory and taking approximately 100ms CPU time on a modern processor.
	LightScryptN = 1 << 12

	// LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB
	// memory and taking approximately 100ms CPU time on a modern processor.
	LightScryptP = 6
)
View Source
const ACCOUNT0 = 0x80000000
View Source
const PTN_COIN_TYPE uint32 = 0x8050544e //PTN
View Source
const Purpose uint32 = 0x8000002C

Variables

View Source
var (
	ErrLocked      = accounts.NewAuthNeededError("password or unlock")
	ErrNoMatch     = errors.New("no key for given address or file")
	ErrTypeNoMatch = errors.New("no key type for given address or file")
	ErrDecrypt     = errors.New("could not decrypt key with given passphrase")
)
View Source
var KeyStoreScheme = "keystore"

KeyStoreScheme is the protocol scheme prefixing account and wallet URLs.

View Source
var KeyStoreType = reflect.TypeOf(&KeyStore{})

KeyStoreType is the reflect type of a keystore backend.

Functions

func EncryptKey

func EncryptKey(key *Key, auth string, scryptN, scryptP int) ([]byte, error)

EncryptKey encrypts a key using the specified scrypt parameters into a json blob that can be decrypted later on.

func MnemonicToSeed added in v1.0.6

func MnemonicToSeed(mnemonic string) ([]byte, error)

func NewAccountKey added in v1.0.6

func NewAccountKey(seed []byte, accountIndex uint32) ([]byte, []byte, error)

根据AccountIndex,返回私钥,公钥

func NewKeyFromMasterKey added in v1.0.6

func NewKeyFromMasterKey(masterKey *bip32.Key, coin, account, chain, addressIndex uint32) (*bip32.Key, error)

func StoreHdSeed added in v1.0.6

func StoreHdSeed(dir, auth string, scryptN, scryptP int) (common.Address, string, error)

func StoreKey

func StoreKey(dir, auth string, scryptN, scryptP int) (common.Address, error)

StoreKey generates a key, encrypts with 'auth' and stores in the given directory

func VerifyTXWithPK

func VerifyTXWithPK(sign []byte, tx interface{}, publicKey []byte) bool

func VerifyUnitWithPK

func VerifyUnitWithPK(sign []byte, unit interface{}, publicKey []byte) bool

func ZeroKey

func ZeroKey(k []byte)

ZeroKey zeroes a private key in memory.

Types

type AmbiguousAddrError

type AmbiguousAddrError struct {
	Addr    common.Address
	Matches []accounts.Account
}

AmbiguousAddrError is returned when attempting to unlock an address for which more than one file exists.

func (*AmbiguousAddrError) Error

func (err *AmbiguousAddrError) Error() string

type Key

type Key struct {
	Id uuid.UUID // Version 4 "random" for unique id not derived from key data
	// to simplify lookups we also store the address
	Address common.Address
	KeyType string
	// we only store privkey as pubkey/address can be derived from it
	// privkey in this struct is always in plaintext
	PrivateKey []byte
}

func DecryptKey

func DecryptKey(keyjson []byte, auth string) (*Key, error)

DecryptKey decrypts a key from a json blob, returning the private key itself.

func (*Key) MarshalJSON

func (k *Key) MarshalJSON() (j []byte, err error)

func (*Key) UnmarshalJSON

func (k *Key) UnmarshalJSON(j []byte) (err error)

type KeyStore

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

KeyStore manages a key storage directory on disk.

func NewKeyStore

func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore

NewKeyStore creates a keystore for the given directory.

func NewPlaintextKeyStore

func NewPlaintextKeyStore(keydir string) *KeyStore

NewPlaintextKeyStore creates a keystore for the given directory. Deprecated: Use NewKeyStore.

func (*KeyStore) Accounts

func (ks *KeyStore) Accounts() []accounts.Account

Accounts returns all key files present in the directory.

func (*KeyStore) Delete

func (ks *KeyStore) Delete(a accounts.Account, passphrase string) error

Delete deletes the key matched by account if the passphrase is correct. If the account contains no filename, the address must match a unique key.

func (*KeyStore) DumpKey

func (ks *KeyStore) DumpKey(a accounts.Account, passphrase string) (privateKey []byte, keyType string, err error)

func (*KeyStore) DumpPrivateKey

func (ks *KeyStore) DumpPrivateKey(a accounts.Account, passphrase string) (privateKey interface{}, err error)

func (*KeyStore) Export

func (ks *KeyStore) Export(a accounts.Account, passphrase, newPassphrase string) (keyJSON []byte, err error)

Export exports as a JSON key, encrypted with newPassphrase.

func (*KeyStore) Find

func (ks *KeyStore) Find(a accounts.Account) (accounts.Account, error)

Find resolves the given account into a unique entry in the keystore.

func (*KeyStore) GetHdAccount added in v1.0.6

func (ks *KeyStore) GetHdAccount(a accounts.Account, accountIndex uint32) (
	accounts.Account, error)

func (*KeyStore) GetHdAccountKeys added in v1.0.6

func (ks *KeyStore) GetHdAccountKeys(a accounts.Account, passphrase string, accountIndex uint32) (
	[]byte, []byte, common.Address, error)

返回HDWallet的私钥、公钥、地址

func (*KeyStore) GetHdAccountWithPassphrase added in v1.0.6

func (ks *KeyStore) GetHdAccountWithPassphrase(a accounts.Account, passphrase string, accountIndex uint32) (
	accounts.Account, error)

func (*KeyStore) GetPublicKey

func (ks *KeyStore) GetPublicKey(address common.Address) ([]byte, error)

func (*KeyStore) HasAddress

func (ks *KeyStore) HasAddress(addr common.Address) bool

HasAddress reports whether a key with the given address is present.

func (*KeyStore) Import

func (ks *KeyStore) Import(keyJSON []byte, passphrase, newPassphrase string) (accounts.Account, error)

Import stores the given encrypted JSON key into the key directory.

func (*KeyStore) ImportECDSA

func (ks *KeyStore) ImportECDSA(priv []byte, passphrase string) (accounts.Account, error)

ImportECDSA stores the given key into the key directory, encrypting it with the passphrase.

func (*KeyStore) ImportFromHex

func (ks *KeyStore) ImportFromHex(hexhash string, newPassphrase string) (accounts.Account, error)

Import stores the given encrypted JSON key into the key directory.

func (*KeyStore) ImportHdSeedFromMnemonic added in v1.0.6

func (ks *KeyStore) ImportHdSeedFromMnemonic(mnemonic string, passphrase string) (accounts.Account, error)

根据助记词,导入HD种子并保存,返回0号账号的地址

func (*KeyStore) ImportMnemonic added in v1.0.6

func (ks *KeyStore) ImportMnemonic(mnemonic string, passphrase string) (accounts.Account, error)

根据助记词,导入0号账号0号地址对应的私钥

func (*KeyStore) IsUnlock

func (ks *KeyStore) IsUnlock(addr common.Address) bool

func (*KeyStore) Lock

func (ks *KeyStore) Lock(addr common.Address) error

Lock removes the private key with the given address from memory.

func (*KeyStore) NewAccount

func (ks *KeyStore) NewAccount(passphrase string) (accounts.Account, error)

NewAccount generates a new key and stores it into the key directory, encrypting it with the passphrase.

func (*KeyStore) NewAccountOutchain added in v1.0.8

func (ks *KeyStore) NewAccountOutchain(passphrase string) (accounts.Account, error)

NewAccount generates a new key and stores it into the key directory, encrypting it with the passphrase.

func (*KeyStore) NewHdAccount added in v1.0.6

func (ks *KeyStore) NewHdAccount(passphrase string) (accounts.Account, string, error)

func (*KeyStore) SigData

func (ks *KeyStore) SigData(data interface{}, address common.Address) ([]byte, error)

func (*KeyStore) SigTX

func (ks *KeyStore) SigTX(tx interface{}, address common.Address) (R, S, V []byte, e error)

tx:TxMessages []Message

func (*KeyStore) SigTXWithPwd

func (ks *KeyStore) SigTXWithPwd(tx interface{}, privateKey []byte) ([]byte, error)

func (*KeyStore) SigUnit

func (ks *KeyStore) SigUnit(unitHeader *modules.Header, address common.Address) ([]byte, error)

func (*KeyStore) SigUnitWithPwd

func (ks *KeyStore) SigUnitWithPwd(unit interface{}, privateKey []byte) ([]byte, error)

func (*KeyStore) SignMessage added in v1.0.1

func (ks *KeyStore) SignMessage(addr common.Address, msg []byte) ([]byte, error)

SignHash calculates a ECDSA signature for the given hash. The produced signature is in the [R || S ] format .

func (*KeyStore) SignMessageByHdAccount added in v1.0.6

func (ks *KeyStore) SignMessageByHdAccount(addr common.Address, accountIndex uint32, msg []byte) ([]byte, error)

func (*KeyStore) SignMessageWithPassphrase added in v1.0.1

func (ks *KeyStore) SignMessageWithPassphrase(a accounts.Account, passphrase string,
	msg []byte) (signature []byte, err error)

SignHashWithPassphrase signs hash if the private key matching the given address can be decrypted with the given passphrase. The produced signature is in the [R || S ] format where V is 0 or 1.

func (*KeyStore) SignTx

func (ks *KeyStore) SignTx(a accounts.Account, tx *modules.Transaction,
	chainID *big.Int) (*modules.Transaction, error)

SignTx signs the given transaction with the requested account.

func (*KeyStore) SignTxWithPassphrase

func (ks *KeyStore) SignTxWithPassphrase(a accounts.Account, passphrase string, tx *modules.Transaction,
	chainID *big.Int) (*modules.Transaction, error)

SignTxWithPassphrase signs the transaction if the private key matching the given address can be decrypted with the given passphrase.

func (*KeyStore) Subscribe

func (ks *KeyStore) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription

Subscribe implements accounts.Backend, creating an async subscription to receive notifications on the addition or removal of keystore wallets.

func (*KeyStore) TimedUnlock

func (ks *KeyStore) TimedUnlock(a accounts.Account, passphrase string, timeout time.Duration) error

TimedUnlock unlocks the given account with the passphrase. The account stays unlocked for the duration of timeout. A timeout of 0 unlocks the account until the program exits. The account must match a unique key file.

If the account address is already unlocked for a duration, TimedUnlock extends or shortens the active unlock timeout. If the address was previously unlocked indefinitely the timeout is not altered.

func (*KeyStore) Unlock

func (ks *KeyStore) Unlock(a accounts.Account, passphrase string) error

Unlock unlocks the given account indefinitely.

func (*KeyStore) Update

func (ks *KeyStore) Update(a accounts.Account, passphrase, newPassphrase string) error

Update changes the passphrase of an existing account.

func (*KeyStore) VerifySignatureWithPassphrase

func (ks *KeyStore) VerifySignatureWithPassphrase(a accounts.Account, passphrase string, hash []byte,
	signature []byte) (pass bool, err error)

func (*KeyStore) Wallets

func (ks *KeyStore) Wallets() []accounts.Wallet

Wallets implements accounts.Backend, returning all single-key wallets from the keystore directory.

Jump to

Keyboard shortcuts

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