keystore

package module
v0.0.0-...-4dd0036 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2023 License: MIT Imports: 20 Imported by: 59

README

keystore

Seed key management

Documentation

Index

Constants

View Source
const (
	MnemonicLang_cn = "cn" //简体中文
	MnemonicLang_en = "en" //英文
)

Variables

View Source
var ERROR_DHKey_password_fail = errors.New("DHKey password fail") //DHKey密码错误
View Source
var ERROR_address_empty = errors.New("address empty") //地址为空
View Source
var ERROR_get_address_info_errer = errors.New("get address info errer") //获取地址信息错误
View Source
var ERROR_get_dhkey_errer = errors.New("get DHKey errer") //获取DHKey错误
View Source
var ERROR_get_netaddr_errer = errors.New("get NetAddr errer") //获取网络地址错误
View Source
var ERROR_netAddr_password_fail = errors.New("net address password fail") //网络地址密码错误
View Source
var ERROR_netaddr_empty = errors.New("get NetAddr empty") //网络地址为空
View Source
var ERROR_wallet_address_password_fail = errors.New("wallet address password fail") //钱包地址密码错误
View Source
var ERROR_wallet_password_fail = errors.New("wallet password fail") //钱包密码错误
View Source
var Salt = []byte{53, 111, 103, 103, 87, 66, 54, 103, 53, 108, 65, 81, 73, 53, 70, 43} //加密盐

var Salt = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07} //加密盐

Functions

func CheckCreateDir

func CheckCreateDir(dir_path string)

检查目录是否存在,不存在则创建

func CreateKeystore

func CreateKeystore(fileAbsPath, addrPre, password string) error

CreateKeystore @Description: 创建一个新的keystore @param fileAbsPath @param addrPre @param password 钱包密码 @return error

func CreateKeystoreRand

func CreateKeystoreRand(fileAbsPath, addrPre string, seed []byte, password, firstCoinAddressPassword, netAddressAndDHkeyPassword string) error

CreateKeystoreRand @Description: 使用随机数创建一个新的keystore @param fileAbsPath @param addrPre @param seed @param rand1 @param rand2 @param password 钱包密码 @param firstCoinAddressPassword 首个钱包地址的密码 @param firstAddressPassword 首个网络地址和DHkey的密码 @return error

func CreateNetAddr

func CreateNetAddr(password, netAddressPassword string) (ed25519.PrivateKey, ed25519.PublicKey, error)

GetNewNetAddr @Description: 新建网络地址 @param pwd 网络地址的密码 @return ed25519.PrivateKey 地址私钥 @return ed25519.PublicKey 地址公钥 @return error

func ExportMnemonic

func ExportMnemonic(pwd string) (string, error)

ExportMnemonic @Description: 导出助记词 @param pwd 钱包密码 @return string 助记词 @return error

func FilePathSplit

func FilePathSplit(path string) []string

把文件全路径切分成子路径

func GetKeyByAddr

func GetKeyByAddr(addr crypto.AddressCoin, password string) (rand []byte, prk ed25519.PrivateKey, puk ed25519.PublicKey, err error)

GetKeyByAddr @Description: 通过地址获取密钥对 @param addr @param password 地址密码 @return rand @return prk 地址私钥 @return puk 地址公钥 @return err

func GetKeyByPuk

func GetKeyByPuk(puk []byte, password string) (rand []byte, prk ed25519.PrivateKey, err error)

GetKeyByPuk @Description: 通过公钥获取密钥 @param puk 地址公钥 @param password 地址密码 @return rand @return prk 地址私钥 @return err

func GetNetAddr

func GetNetAddr(pwd string) (ed25519.PrivateKey, ed25519.PublicKey, error)

GetNetAddr @Description: 获取本钱包的网络地址 @param pwd 网络地址的密码 @return ed25519.PrivateKey 地址私钥 @return ed25519.PublicKey 地址公钥 @return error

func GetNetAddrKeyByMnemonic

func GetNetAddrKeyByMnemonic(words string, mnemonicLang []string) (prk ed25519.PrivateKey, puk ed25519.PublicKey, err error)

GetNetAddrKeyByMnemonic @Description: 根据助记词获取网络地址公私钥 @param words 助记词 @param mnemonicLang 助记词版本 @return prk @return puk @return err

func GetNewAddr

func GetNewAddr(password, newAddressPassword string) (crypto.AddressCoin, error)

GetNewAddr @Description: 获取一个新的地址 @param password 钱包密码 @param newAddressPassword 新地址密码 @return crypto.AddressCoin @return error

func GetNewAddrByName

func GetNewAddrByName(name, password, newAddressPassword string) (crypto.AddressCoin, error)

GetNewAddrByName @Description: 获取一个新的地址,并且给新地址一个昵称 @param name @param password 钱包密码 @param newAddressPassword 新地址密码 @return crypto.AddressCoin @return error

func GetPukByAddr

func GetPukByAddr(addr crypto.AddressCoin) (puk ed25519.PublicKey, ok bool)

GetPukByAddr @Description: 通过地址获取公钥 @param addr @return puk 地址公钥 @return ok

func ImportMnemonic

func ImportMnemonic(words, pwd, firstCoinAddressPassword, netAddressAndDHkeyPassword string) error

ImportMnemonic @Description: 导入助记词 @param words 助记词 @param pwd 钱包密码 @param firstCoinAddressPassword 首个钱包地址的密码 @param firstAddressPassword 首个网络地址和DHkey的密码 @return error

func ImportMnemonicCreateMoreCoinAddr

func ImportMnemonicCreateMoreCoinAddr(words, pwd, firstCoinAddressPassword, netAddressAndDHkeyPassword string, coinAddrNum int) error

ImportMnemonicCreateMoreCoinAddr @Description: 导入助记词生成多个钱包地址 @param words 助记词 @param pwd 钱包密码 @param firstCoinAddressPassword 首个钱包地址的密码 @param firstAddressPassword 首个网络地址和DHkey的密码 @param coinAddrNum 创建多少个钱包地址 @return error

func Load

func Load(fileAbsPath, addrPre string) error

加载种子

func Mkdir

func Mkdir(path string) error

递归创建目录

func PathExists

func PathExists(path string) (bool, error)

判断一个路径的文件是否存在

func Println

func Println()

func RenameTempFile

func RenameTempFile(name string) error

func SaveFile

func SaveFile(name string, bs *[]byte) error

保存文件 保存文件步骤: 1.创建临时文件 2.

func SaveJsonFile

func SaveJsonFile(name string, o interface{}) error

保存对象为json格式

func SetCoinbase

func SetCoinbase(index int)

设置基础地址

func Sign

func Sign(prk ed25519.PrivateKey, content []byte) []byte

签名

func UpdateAddrName

func UpdateAddrName(name, password string, addr crypto.AddressCoin) error

UpdateAddrName @Description: 修改一个地址的昵称 @param name @param password 地址密码 @param addr @return error

func UpdateAddrPwd

func UpdateAddrPwd(addr, oldpwd, newpwd string) (ok bool, err error)

UpdateAddrPwd @Description: 修改钱包地址密码 @param addr @param oldpwd 地址旧密码 @param newpwd 地址新密码 @return ok @return err

func UpdateDHKeyPwd

func UpdateDHKeyPwd(oldpwd, newpwd string) (ok bool, err error)

UpdateDHKeyPwd @Description: 修改DHKey密码 @param oldpwd 旧密码 @param newpwd 新密码 @return ok @return err

func UpdateNetAddrPwd

func UpdateNetAddrPwd(oldpwd, newpwd string) (ok bool, err error)

UpdateAddrPwd @Description: 修改网络地址密码 @param addr @param oldpwd 地址旧密码 @param newpwd 地址新密码 @return ok @return err

func UpdatePwd

func UpdatePwd(oldpwd, newpwd string) (ok bool, err error)

修改钱包密码

Types

type AddressInfo

type AddressInfo struct {
	Index     uint64             `json:"index"`     //棘轮数量
	Nickname  string             `json:"nickname"`  //地址昵称
	Addr      crypto.AddressCoin `json:"addr"`      //收款地址
	Puk       ed25519.PublicKey  `json:"puk"`       //公钥
	SubKey    []byte             `json:"subKey"`    //子密钥
	AddrStr   string             `json:"-"`         //
	PukStr    string             `json:"-"`         //
	CheckHash []byte             `json:"checkhash"` //主私钥和链编码加密验证hash值
	Version   int                `json:"version"`   //地址版本
}

func FindAddress

func FindAddress(addr crypto.AddressCoin) (addrInfo AddressInfo, ok bool)

钱包中查找地址,判断地址是否属于本钱包

func FindPuk

func FindPuk(puk []byte) (addrInfo AddressInfo, ok bool)

钱包中查找公钥是否存在

func GetAddr

func GetAddr() []*AddressInfo

获取钱包地址列表,不包括导入的钱包地址

func GetAddrAll

func GetAddrAll() []*AddressInfo

获取地址列表,包括导入的钱包地址

func GetCoinbase

func GetCoinbase() *AddressInfo

获取基础地址

func (*AddressInfo) GetAddrStr

func (this *AddressInfo) GetAddrStr() string

func (*AddressInfo) GetPukStr

func (this *AddressInfo) GetPukStr() string

type DHKeyPair

type DHKeyPair struct {
	Index     uint64     `json:"index"`     //棘轮数量
	KeyPair   dh.KeyPair `json:"keypair"`   //
	CheckHash []byte     `json:"checkhash"` //主私钥和链编码加密验证hash值
	SubKey    []byte     `json:"subKey"`    //子密钥
}

func GetDHKeyPair

func GetDHKeyPair() DHKeyPair

获取DH公钥

type Keystore

type Keystore struct {
	AddrPre  string `json:"-"`        //
	Coinbase uint64 `json:"coinbase"` //当前默认使用的收付款地址
	DHIndex  uint64 `json:"dhindex"`  //DH密钥,指向钱包位置

	MnemonicLang []string       `json:"-"`
	Seed         []byte         `json:"seed"`      //种子
	CheckHash    []byte         `json:"checkhash"` //主私钥和链编码加密验证hash值
	Addrs        []*AddressInfo `json:"addrs"`     //已经生成的地址列表
	DHKey        []DHKeyPair    `json:"dhkey"`     //DH密钥
	NetAddr      *NetAddrInfo   `json:"netaddr"`
	// contains filtered or unexported fields
}

func GetKeyStore

func GetKeyStore() *Keystore

func NewKeyStoreTmpNetAddr

func NewKeyStoreTmpNetAddr(prk []byte) *Keystore

NewKeyStoreTmpNetAddr @Description: 根据NetAddr私钥获取一个Keystore @param prk @return *Keystore

func NewKeystore

func NewKeystore(filepath, addrPre string) *Keystore

func (*Keystore) AddrDecrypt

func (this *Keystore) AddrDecrypt(addr crypto.AddressCoin, pwdbs [32]byte) (bool, []byte, error)

AddrDecrypt @Description: 使用钱包地址密码解密钱包地址子秘钥 @receiver this @param addr 地址 @param pwdbs 地址密码 @return ok 密码是否正确 @return subKeyBs 子密私钥 @return err

func (*Keystore) CheckIntact

func (this *Keystore) CheckIntact() bool

检查钱包是否完整

func (*Keystore) CreateNetAddr

func (this *Keystore) CreateNetAddr(password, netAddressPassword string) (prk ed25519.PrivateKey, puk ed25519.PublicKey, err error)

CreateNetAddr @Description: 新建网络地址 @receiver this @param password 钱包的密码 @param netAddressPassword 网络地址的密码 @return prk 地址私钥 @return puk 地址公钥 @return err

func (*Keystore) CreateNewKeystore

func (this *Keystore) CreateNewKeystore(password string) error

创建一个新的种子文件

func (*Keystore) CreateNewWalletRand

func (this *Keystore) CreateNewWalletRand(seedSrc []byte, password, firstCoinAddressPassword, netAddressAndDHkeyPassword string, coinAddrNum int) error

使用随机数创建一个新的种子文件

func (*Keystore) Decrypt

func (this *Keystore) Decrypt(pwdbs [32]byte) (bool, []byte, []byte, error)

Decrypt @Description: 使用钱包密码解密钱包种子,获得钱包私钥和链编码 @receiver this @param pwdbs 钱包密码 @return ok 密码是否正确 @return key 生成私钥的随机数 @return code 链编码 @return err

func (*Keystore) ExportMnemonic

func (this *Keystore) ExportMnemonic(pwd string) (string, error)

ExportMnemonic @Description: 导出助记词 @receiver this @param pwd 钱包密码 @return string 助词记 @return error

func (*Keystore) FindAddress

func (this *Keystore) FindAddress(addr crypto.AddressCoin) (addrInfo AddressInfo, ok bool)

钱包中查找地址,判断地址是否属于本钱包

func (*Keystore) FindPuk

func (this *Keystore) FindPuk(puk []byte) (addrInfo AddressInfo, ok bool)

钱包中查找公钥是否存在

func (*Keystore) GetAddr

func (this *Keystore) GetAddr() (addrs []*AddressInfo)

获取地址列表

func (*Keystore) GetAddrAll

func (this *Keystore) GetAddrAll() []*AddressInfo

获取地址列表,包括导入的钱包地址

func (*Keystore) GetCoinbase

func (this *Keystore) GetCoinbase() *AddressInfo

func (*Keystore) GetDHKeyPair

func (this *Keystore) GetDHKeyPair() DHKeyPair

获取DH公钥

func (*Keystore) GetFilePath

func (this *Keystore) GetFilePath() string

func (*Keystore) GetKeyByAddr

func (this *Keystore) GetKeyByAddr(addr crypto.AddressCoin, password string) (rand []byte, prk ed25519.PrivateKey, puk ed25519.PublicKey, err error)

GetKeyByAddr @Description: 通过地址获取密钥对 @receiver this @param addr 地址 @param password 地址密码 @return rand @return prk 地址私钥 @return puk 地址公钥 @return err

func (*Keystore) GetKeyByPuk

func (this *Keystore) GetKeyByPuk(puk []byte, password string) (rand []byte, prk ed25519.PrivateKey, err error)

GetKeyByPuk @Description: 通过公钥获取密钥 @receiver this @param puk 地址公钥 @param password 地址密码 @return rand @return prk 地址私钥 @return err

func (*Keystore) GetNetAddr

func (this *Keystore) GetNetAddr(password string) (prk ed25519.PrivateKey, puk ed25519.PublicKey, err error)

GetNetAddr @Description: 获取网络地址 @receiver this @param password 网络地址的密码 @return prk 地址私钥 @return puk 地址公钥 @return err

func (*Keystore) GetNetAddrPwd

func (this *Keystore) GetNetAddrPwd(prk []byte) (string, error)

GetNetAddrPwd @Description: 获取密码 @receiver this @param prk @return string @return error

func (*Keystore) GetNewAddr

func (this *Keystore) GetNewAddr(password, newAddressPassword string) (crypto.AddressCoin, error)

GetNewAddr @Description: 获取一个新的地址 @receiver this @param password 钱包密码 @param newAddressPassword 新地址密码 @return crypto.AddressCoin @return error

func (*Keystore) GetNewAddrByName

func (this *Keystore) GetNewAddrByName(name, password, newAddressPassword string) (crypto.AddressCoin, error)

GetNewAddrByName @Description: 获取一个新的地址,并且给新地址一个昵称 @receiver this @param name 昵称 @param password 钱包密码 @param newAddressPassword 新地址密码 @return crypto.AddressCoin @return error

func (*Keystore) GetNewDHKey

func (this *Keystore) GetNewDHKey(password string, newDHKeyPassword string) (*dh.KeyPair, error)

GetNewDHKey @Description: 创建DHKey协商秘钥 @receiver this @param password 钱包密码 @param newDHKeyPassword DHKey密码 @return *dh.KeyPair @return error

func (*Keystore) GetPukByAddr

func (this *Keystore) GetPukByAddr(addr crypto.AddressCoin) (puk ed25519.PublicKey, ok bool)

通过地址获取公钥

func (*Keystore) ImportMnemonic

func (this *Keystore) ImportMnemonic(words, pwd, firstCoinAddressPassword, netAddressAndDHkeyPassword string) error

ImportMnemonic @Description: 导入助记词 @receiver this @param words 助记词 @param pwd 钱包密码 @param firstCoinAddressPassword 首个钱包地址的密码 @param firstAddressPassword 首个网络地址和DHkey的密码 @return error

func (*Keystore) ImportMnemonicCreateMoreCoinAddr

func (this *Keystore) ImportMnemonicCreateMoreCoinAddr(words, pwd, firstCoinAddressPassword, netAddressAndDHkeyPassword string, coinAddrNum int) error
 	ImportMnemonicCreateMoreCoinAddr
 	@Description: 导入助记词生成多个钱包地址
 	@receiver this
 	@param words 助记词
	@param pwd 钱包密码
	@param firstCoinAddressPassword 首个钱包地址的密码
	@param firstAddressPassword 首个网络地址和DHkey的密码
 	@param coinAddrNum 创建多少个钱包地址
 	@return error

func (*Keystore) Load

func (this *Keystore) Load() error

从磁盘文件加载keystore

func (*Keystore) NewWallet

func (this *Keystore) NewWallet(seed *[]byte, pwd *[32]byte) error

func (*Keystore) Save

func (this *Keystore) Save() error

从磁盘文件加载keystore

func (*Keystore) SetCoinbase

func (this *Keystore) SetCoinbase(index uint64) bool

设置默认收付款地址

func (*Keystore) SetKeyStore

func (this *Keystore) SetKeyStore(filepath, addrPre string) error

SetKeyStore @Description: 根据keystore文件设置Keystore对象 @receiver this @param filepath 文件路径 @param addrPre 地址前缀 @return error

func (*Keystore) SetLang

func (this *Keystore) SetLang(lan string)

设置助记词语言

func (*Keystore) UpdateAddrName

func (this *Keystore) UpdateAddrName(name, password string, addr crypto.AddressCoin) error

UpdateAddrName @Description: 修改地址的昵称 @receiver this @param name 新昵称 @param password 地址密码 @param addr 地址 @return error

func (*Keystore) UpdateAddrPwd

func (this *Keystore) UpdateAddrPwd(addr, oldpwd, newpwd string) (ok bool, err error)

修改钱包子地址密码

func (*Keystore) UpdateDHKeyPwd

func (this *Keystore) UpdateDHKeyPwd(oldpwd, newpwd string) (ok bool, err error)

UpdateDHKeyPwd @Description: 修改DHKey密碼 @receiver this @param index @param oldpwd 旧密码 @param newpwd 新密码 @return ok @return err

func (*Keystore) UpdateNetAddrPwd

func (this *Keystore) UpdateNetAddrPwd(oldpwd, newpwd string) (ok bool, err error)

修改网络地址密码

func (*Keystore) UpdatePwd

func (this *Keystore) UpdatePwd(oldpwd, newpwd string) (ok bool, err error)

修改钱包密码

type KeystoreInterface

type KeystoreInterface interface {
	GetAddr() []*AddressInfo
	GetDHKeyPair() DHKeyPair
	CreateNetAddr(password, netAddressPassword string) (ed25519.PrivateKey, ed25519.PublicKey, error)
	GetNetAddr(pwd string) (ed25519.PrivateKey, ed25519.PublicKey, error)
	UpdateNetAddrPwd(oldpwd, newpwd string) (ok bool, err error)
	GetAddrAll() []*AddressInfo
	GetCoinbase() *AddressInfo
	FindAddress(addr crypto.AddressCoin) (addrInfo AddressInfo, ok bool)
	GetNewAddr(password, newAddressPassword string) (crypto.AddressCoin, error)
	GetKeyByAddr(addr crypto.AddressCoin, password string) (rand []byte, prk ed25519.PrivateKey, puk ed25519.PublicKey, err error)
	GetKeyByPuk(puk []byte, password string) (rand []byte, prk ed25519.PrivateKey, err error)
	GetPukByAddr(addr crypto.AddressCoin) (puk ed25519.PublicKey, ok bool)
	FindPuk(puk []byte) (addrInfo AddressInfo, ok bool)
	UpdatePwd(oldpwd, newpwd string) (ok bool, err error)
	UpdateAddrPwd(addr, oldpwd, newpwd string) (ok bool, err error)
	ImportMnemonic(words, pwd, firstCoinAddressPassword, netAddressAndDHkeyPassword string) error
	ExportMnemonic(pwd string) (string, error)
	GetFilePath() string
}

type NetAddrInfo

type NetAddrInfo struct {
	Puk       ed25519.PublicKey `json:"puk"`       //公钥
	SubKey    []byte            `json:"subKey"`    //子密钥
	CheckHash []byte            `json:"checkhash"` //加密验证hash值
	Prkpwd    []byte            `json:"prkpwd"`    //私钥加密密码
}

网络地址信息

Directories

Path Synopsis
crc
Package crc implements generic CRC calculations up to 64 bits wide.
Package crc implements generic CRC calculations up to 64 bits wide.
dh

Jump to

Keyboard shortcuts

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