wallet

package
v0.0.0-...-7ece11e Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2023 License: MIT Imports: 20 Imported by: 14

README

Wallet

HD wallets generate a hierarchical tree-like structure of keys which start from the seed master key based on BIP 32. When you restore an HD wallet using the seed key, the wallet goes ahead and drives all the private keys of the tree using BIP 32.

Advantages of HD Wallets

  • You need to backup only one key (i.e. “seed key”). It is the only backup you will ever need.
  • You can generate many receiving addresses every time you receive bitcoins.
  • You can protect your financial privacy.
  • Confuse new users, as your receiving address changes every time.

Documentation

Index

Constants

View Source
const (
	PriKeyType                  = byte(0x0) // Serialize wallet account key into string with only PRIVATE KEY of account keyset
	PaymentAddressType          = byte(0x1) // Serialize wallet account key into string with only PAYMENT ADDRESS of account keyset
	ReadonlyKeyType             = byte(0x2) // Serialize wallet account key into string with only READONLY KEY of account keyset
	OTAKeyType                  = byte(0x3) // Serialize wallet account key into string with only OTA KEY of account keyset
	PrivateReceivingAddressType = byte(0x4) // prefix for marshalled receiving address (coin pk + txRandom), not used for KeyWallet
)
View Source
const (
	InvalidChecksumErr = iota
	WrongPassphraseErr
	ExistedAccountErr
	ExistedAccountNameErr
	UnexpectedErr
	EmptyWalletNameErr
	NotFoundAccountErr
	JsonMarshalErr
	JsonUnmarshalErr
	WriteFileErr
	ReadFileErr
	AESEncryptErr
	AESDecryptErr
	InvalidKeyTypeErr
	InvalidPlaintextErr
	NewChildKeyError
	NewEntropyError
	NewMnemonicError
	MnemonicInvalidError
	InvalidSeserializedKey
)

Variables

View Source
var ErrCodeMessage = map[int]struct {
	code    int
	message string
}{
	UnexpectedErr: {-1, "Unexpected error"},

	InvalidChecksumErr:     {-1000, "Checksum does not match"},
	WrongPassphraseErr:     {-1001, "Wrong passphrase"},
	ExistedAccountErr:      {-1002, "Existed account"},
	ExistedAccountNameErr:  {-1002, "Existed account name"},
	EmptyWalletNameErr:     {-1003, "Wallet name is empty"},
	NotFoundAccountErr:     {-1004, "Account wallet is not found"},
	JsonMarshalErr:         {-1005, "Can not json marshal"},
	JsonUnmarshalErr:       {-1006, "Can not json unmarshal"},
	WriteFileErr:           {-1007, "Can not write file"},
	ReadFileErr:            {-1008, "Can not read file"},
	AESEncryptErr:          {-1009, "Can not AES encrypt data"},
	AESDecryptErr:          {-1010, "Can not AES decrypt data"},
	InvalidKeyTypeErr:      {-1011, "Serialized key type is invalid"},
	InvalidPlaintextErr:    {-1012, "Plaintext is invalid"},
	NewChildKeyError:       {-1013, "Can not create new child key"},
	NewEntropyError:        {-1014, "Can not create entropy"},
	NewMnemonicError:       {-1015, "Can not create mnemonic"},
	MnemonicInvalidError:   {-1016, "Mnemonic is invalid"},
	InvalidSeserializedKey: {-1016, "Serialized key is invalid"},
}
View Source
var Logger = WalletLogger{}

Global instant to use

Functions

func ComparePaymentAddresses

func ComparePaymentAddresses(addr1, addr2 string) (bool, error)

ComparePaymentAddresses checks if two payment addresses are generated from the same private key.

Just need to compare PKs and TKs.

func GetBurningPublicKey

func GetBurningPublicKey() []byte

GetBurningPublicKey returns the public key of the burning address.

func GetPaymentAddressV1

func GetPaymentAddressV1(addr string, isNewEncoding bool) (string, error)

GetPaymentAddressV1 retrieves the payment address ver 1 from the payment address ver 2.

  • Payment Address V1 consists of: PK + TK
  • Payment Address V2 consists of: PK + TK + PublicOTA

If the input is a payment address ver 2, try to retrieve the corresponding payment address ver 1. Otherwise, return the input.

func GetPublicKeysFromPaymentAddresses

func GetPublicKeysFromPaymentAddresses(payments []string) []string

func InitPublicKeyBurningAddressByte

func InitPublicKeyBurningAddressByte() error

func IsPublicKeyBurningAddress

func IsPublicKeyBurningAddress(publicKey []byte) bool

func NewWordList

func NewWordList(language string) []string

Types

type AccountWallet

type AccountWallet struct {
	Name       string
	Key        KeyWallet
	Child      []AccountWallet
	IsImported bool
}

type KeySerializedData

type KeySerializedData struct {
	PrivateKey     string `json:"PrivateKey"`
	PaymentAddress string `json:"PaymentAddress"`
	Pubkey         string `json:"Pubkey"` // in hex encode string
	ReadonlyKey    string `json:"ReadonlyKey"`
	ValidatorKey   string `json:"ValidatorKey"` // in base58check encode string
}

type KeyWallet

type KeyWallet struct {
	Depth       byte   // 1 bytes
	ChildNumber []byte // 4 bytes
	ChainCode   []byte // 32 bytes
	KeySet      incognitokey.KeySet
}

KeyWallet represents with bip32 standard

func Base58CheckDeserialize

func Base58CheckDeserialize(data string) (*KeyWallet, error)

Base58CheckDeserialize deserializes a KeySet encoded in base58 encoding because data contains keyType and serialized data of corresponding key it returns KeySet just contain corresponding key

func NewMasterKey

func NewMasterKey(seed []byte) (*KeyWallet, error)

NewMasterKey creates a new master extended PubKey from a Seed Seed is a bytes array which any size

func (*KeyWallet) Base58CheckSerialize

func (key *KeyWallet) Base58CheckSerialize(keyType byte) string

Base58CheckSerialize encodes the key corresponding to keyType in KeySet in the standard Incognito base58 encoding It returns the encoding string of the key

func (*KeyWallet) NewChildKey

func (key *KeyWallet) NewChildKey(childIdx uint32) (*KeyWallet, error)

NewChildKey derives a Child KeyWallet from a given parent as outlined by bip32 2 child keys is derived from one key and a same child index are the same

func (*KeyWallet) Serialize

func (key *KeyWallet) Serialize(keyType byte, isNewCheckSum bool) ([]byte, error)

Serialize receives keyType and serializes key which has keyType to bytes array and append 4-byte checksum into bytes array

type MnemonicGenerator

type MnemonicGenerator struct{}

func (*MnemonicGenerator) NewSeed

func (mnemonicGen *MnemonicGenerator) NewSeed(mnemonic string, password string) []byte

NewSeed creates a hashed Seed output given a provided string and password. No checking is performed to validate that the string provided is a valid Mnemonic.

type Wallet

type Wallet struct {
	Seed          []byte
	Entropy       []byte
	PassPhrase    string
	Mnemonic      string
	MasterAccount AccountWallet
	Name          string
	// contains filtered or unexported fields
}

func (*Wallet) ContainPublicKey

func (wallet *Wallet) ContainPublicKey(pubKey []byte) bool

ContainPubKey checks whether the wallet contains any account with pubKey or not

func (*Wallet) CreateNewAccount

func (wallet *Wallet) CreateNewAccount(accountName string, shardID *byte) (*AccountWallet, error)

CreateNewAccount create new account with accountName it returns that new account and returns errors if accountName is existed If shardID is nil, new account will belong to any shards Otherwise, new account will belong to specific shard

func (*Wallet) DumpPrivateKey

func (wallet *Wallet) DumpPrivateKey(paymentAddrSerialized string) KeySerializedData

DumpPrivkey receives base58 check serialized payment address (paymentAddrSerialized) and returns KeySerializedData object contains PrivateKey which is corresponding to paymentAddrSerialized in all wallet accounts If there is not any wallet account corresponding to paymentAddrSerialized, it returns empty KeySerializedData object

func (*Wallet) ExportAccount

func (wallet *Wallet) ExportAccount(childIndex uint32) string

ExportAccount returns a private key string of account at childIndex in wallet It is base58 check serialized

func (*Wallet) GetAddressByAccName

func (wallet *Wallet) GetAddressByAccName(accountName string, shardID *byte) KeySerializedData

GetAddressByAccName receives accountName and shardID and returns corresponding account's KeySerializedData object contains base58 check serialized PaymentAddress, hex encoding Pubkey and base58 check serialized ReadonlyKey If there is not any account corresponding to accountName, we will create new account

func (*Wallet) GetAddressesByAccName

func (wallet *Wallet) GetAddressesByAccName(accountName string) []KeySerializedData

GetAddressesByAccName receives accountName and returns list of KeySerializedData of accounts which has accountName

func (Wallet) GetConfig

func (wallet Wallet) GetConfig() *WalletConfig

GetConfig returns configuration of wallet

func (*Wallet) ImportAccount

func (wallet *Wallet) ImportAccount(privateKeyStr string, accountName string, passPhrase string) (*AccountWallet, error)

ImportAccount adds account into wallet with privateKeyStr, accountName, and passPhrase which is used to init wallet It returns AccountWallet which is imported and errors (if any)

func (*Wallet) Init

func (wallet *Wallet) Init(passPhrase string, numOfAccount uint32, name string) error

Init initializes new wallet with pass phrase, number of accounts and wallet name It returns error if there are any errors when initializing wallet. Otherwise, it returns nil passPhrase can be empty string, it is used to generate seed and master key If numOfAccount equals zero, wallet is initialized with one account If name is empty string, it returns error

func (*Wallet) ListAccounts

func (wallet *Wallet) ListAccounts() map[string]AccountWallet

ListAccounts returns a map with key is account name and value is account wallet

func (*Wallet) LoadWallet

func (wallet *Wallet) LoadWallet(password string) error

LoadWallet loads encrypted wallet from file and then decrypts it to wallet struct It returns error if any

func (*Wallet) RemoveAccount

func (wallet *Wallet) RemoveAccount(privateKeyStr string, passPhrase string) error

func (*Wallet) Save

func (wallet *Wallet) Save(password string) error

Save saves encrypted wallet (using AES encryption scheme) in config data file of wallet It returns error if any

func (*Wallet) SetConfig

func (wallet *Wallet) SetConfig(config *WalletConfig)

SetConfig sets config to configuration of wallet

type WalletConfig

type WalletConfig struct {
	DataDir        string
	DataFile       string
	DataPath       string
	IncrementalFee uint64
	ShardID        *byte //default is nil -> create account for any shard
}

type WalletError

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

func NewWalletError

func NewWalletError(key int, err error) *WalletError

func (WalletError) Error

func (e WalletError) Error() string

func (WalletError) GetCode

func (e WalletError) GetCode() int

type WalletLogger

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

func (*WalletLogger) Init

func (walletLogger *WalletLogger) Init(inst common.Logger)

Jump to

Keyboard shortcuts

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