keybox

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2021 License: GPL-3.0 Imports: 19 Imported by: 0

README

Keybox

[TOC]

Keybox 是一个安全密钥箱的工具包。使用HD钱包的思想进行私钥的保管,同时还能支持子私钥的恢复找回。
在walletctl 钱包生成工具

特性

  • 支持BTC、ETH的密钥生成及导入导出
  • 支持p256、s256、sm2的签名算法
  • 支持账户恢复
  • 支持之账户私钥恢复找回

钱包生成工具说明

  • 通用参数:
参数 说明
-f --path,钱包文件路径
-p --password,钱包文件加解密密码
--isSaveSubKey 是否保存子账户(默认false)
--isSaveExtendedKey 是否保存子账户的扩展私钥(默认false)
--isSaveMnemonic 是否保存主账户的助记词(默认true)
--mnemonicType 助记词类型,类型有en,zh-cn,zh-tw,fr,it,ja,ko,es(默认en)
--mnemonic 助记词(用于恢复钱包)
--isUsePwdBlur 是否使用Password进行混淆(默认false)
--prvKeyBase58 扩展私钥(用于恢复钱包)
--networkType 网络类型,类型有:mainnet,testnet,devnet(默认mainnet)
  • childPath结构说明

/44/60/0/0/0:其含义为:/BIP44格式/币种类型/组织/地址类型(默认0)/地址索引

主账户导出

参数说明:

参数 说明
--exportMasterMn 是否导出助记词(默认false)
--exportMasterRawKey 是否导出16进制私钥(默认false)
--exportMasterExtendedKey 是否导出扩展私钥,base58进制(默认false)
  • 示例:
## 助记词恢复
./walletctl master -f "./wallet1.dat" -p "123456" --mnemonic "security traffic pluck dawn enlist above bunker worth pencil ten garage ribbon"
## 导出扩展私钥
./walletctl master -f "./wallet1.dat" -p "123456" --exportMasterExtendedKey
## 导出助记词
./walletctl master -f "./wallet1.dat" -p "123456" --exportMasterMn
子账户生成

参数说明:

参数 说明
-t --chainType,链类型,包含有eth、btc(默认eth)
--purposeType purpose 类型,包含44,45(默认44)
--org 当purpose=45时,才被使用(默认0)
--coinType 币种类型(默认0)
--account account账户空间(默认0)
--addressIndex 地址索引(默认0)
  • 示例:
## 创建子账户
./walletctl geneChild -f "./wallet1.dat" -p "123456" --chainType "eth" --addressIndex 1

./walletctl geneChild -f "./wallet1.dat" -p "123456" --chainType "eth" --addressIndex 0
导出子账户

参数说明:

参数 说明
-t --chainType,链类型,包含有eth、btc(默认eth)
--childAddress 子账户地址
--childKeyPath 子账户的路径
--exportChildRawKey 是否导出16进制私钥(默认false)
--exportChildExtendedKey 是否导出扩展私钥(默认false)
--exportChildKeystore 是否导出keystore(默认false)
--childKeystorePwd 导出keystore时的加密密码
  • 示例:
## 导出扩展私钥
./walletctl exportChild -f "./wallet1.dat" -p "123456" --chainType "eth" --childAddress "0xb3d988aFDe88653dc1e2C48f770d7DC5AE93547C" --childKeyPath "/44/0/0/0/0" --exportChildExtendedKey
签名

参数说明:

参数 说明
-t --chainType,链类型,包含有eth、btc(默认eth)
--childAddress 子账户地址
--childKeyPath 子账户的路径
--signHash 交易体Hash
  • 示例:
## 签名
./walletctl sign -f "./wallet1.dat" -p "123456" --chainType "eth" --childAddress "0xAeff996F0Efb374fCf95Eb6b38fd4aA5E4bbC1b1" --childKeyPath "/44/60/0/0/0" --signHash "0x123456"

LICENSE

Please refer to LICENSE file.

Copyright@2020 chain5j

Documentation

Overview

description: keybox

@author: xwc1125 @date: 2020/8/18 0018

description: keybox

@author: xwc1125 @date: 2020/8/6 0006

description: keybox

@author: xwc1125 @date: 2020/8/18 0018

Index

Constants

View Source
const (
	TypeBTC  uint32 = 0x80000000
	TypeETH  uint32 = 0x8000003c
	TypeOMNI uint32 = 0x800000c8
)

https://github.com/satoshilabs/slips/blob/master/slip-0044.md

View Source
const (
	Purpose45 uint32 = 0x8000002d
)

Variables

View Source
var (
	ChainInfoBTC = &ChainInfo{
		ChainName:     "BTC",
		ChainType:     bip44.CoinTypeBTC,
		AlgorithmName: "S256",
		Algorithm:     0x80000200,
	}
	ChainInfoETH = &ChainInfo{
		ChainName:     "ETH",
		ChainType:     TypeETH,
		AlgorithmName: "S256",
		Algorithm:     0x80000200,
	}
)

Functions

func SetBip39MnemonicType

func SetBip39MnemonicType(mnemonicType MnemonicType)

==========================主账户============================ 设置助记词的类型【默认是使用English助记词,如果更换,需要在最前面初始化】

func SetIsLog

func SetIsLog(b bool)

==========================设置============================ 设置是否打印日志

Types

type ChainAPI

type ChainAPI interface {
	algorithm.AlgorithmAPI
	ChainInfo() *ChainInfo                                                 // 链内容
	ExportPrivateKey(priKey []byte, isCompressPubKey bool) (string, error) // 导出私钥
	GetAddressFromPubKey(pubKey []byte) (string, error)                    // 通过公钥获取地址
	SignToStr(priKey []byte, hash []byte) (string, error)                  // 签名直接返回签名的string
}

type ChainInfo

type ChainInfo struct {
	ChainName     string // 链名称(eth,btc)
	ChainType     uint32 // 链分配的类型值
	AlgorithmName string // 链的算法名称(s256,p256,gm2)
	Algorithm     uint32 // 链算法类型值
}

type ChildKeyPropertyInfo

type ChildKeyPropertyInfo struct {
	Purpose       uint32 `json:"purpose"`
	ChainType     uint32 `json:"chainType"`
	AlgorithmType uint32 `json:"algorithmType"`
	Org           uint32 `json:"org"`
	CoinType      uint32 `json:"coinType"`
	Time          uint32 `json:"time"`
	Key           []byte `json:"key"`
}

type ExtendedKey

type ExtendedKey struct {
	Key       []byte `json:"key"`        // 33 bytes
	ChainCode []byte `json:"chain_code"` // 32 bytes
}

type MnemonicType

type MnemonicType string
const (
	MnemonicType_Chinese_Simplified  MnemonicType = "chinese_simplified"
	MnemonicType_Chinese_Traditional MnemonicType = "chinese_traditional"
	MnemonicType_English             MnemonicType = "english"
	MnemonicType_French              MnemonicType = "french"
	MnemonicType_Italian             MnemonicType = "italian"
	MnemonicType_Japanese            MnemonicType = "japanese"
	MnemonicType_Korean              MnemonicType = "korean"
	MnemonicType_Spanish             MnemonicType = "spanish"
)

func ParseMnemonicType

func ParseMnemonicType(t string) MnemonicType

type Wallet

type Wallet struct {
	Path           string                           `json:"path"`
	Mnemonic       string                           `json:"mnemonic"`
	Password       string                           `json:"password"`
	Key            *bip32.Key                       `json:"key"`
	Time           uint32                           `json:"time"`
	AddrLinkPubkey map[string]string                `json:"addrLinkPubkey"` // 地址和公钥的配置
	ChildKeyInfo   map[string]*ChildKeyPropertyInfo `json:"childKeyInfo"`   // 公钥对应的子私钥内容

	IsSaveSubKey      bool `json:"isSaveSubKey"`      // 是否保存子私钥
	IsSaveExtendedKey bool `json:"isSaveExtendedKey"` // 是否保存扩展私钥
	// contains filtered or unexported fields
}

Wallet 管理钱包文件

func LoadWalletFromMnemonic

func LoadWalletFromMnemonic(path string, password string, mnemonic string, isUsePwdBlur bool) (*Wallet, error)

从助记词中恢复主钱包 isUsePwdBlur 是否使用Password进行混淆

func LoadWalletFromPrvKey

func LoadWalletFromPrvKey(path string, password string, prvKeyBase58 string) (*Wallet, error)

从私钥中恢复钱包

func NewWallet

func NewWallet(path string, password string) (*Wallet, error)

NewWallet 创建钱包文件实例

func (*Wallet) CreateAccount

func (w *Wallet) CreateAccount(purpose, coinType, org, _account, change, addressIndex uint32, api ChainAPI) (addr string, keyPath string, err error)

创建账户[同一机构下,同一中签名算法,的同一用户只会保留一个私钥] purpose purpose=44时,不使用org org:组织 coinType:币种 _account:将密钥空间划分为独立的用户身份[每一个用户对应一个地址空间] change:0用于外部接收地址 1用于找零地址 addressIndex:地址索引[官方推荐不超过20]

func (*Wallet) DelMnemonic

func (w *Wallet) DelMnemonic() error

删除助记词

func (*Wallet) ExportExtendedKey

func (w *Wallet) ExportExtendedKey(address, keyPath string, api ChainAPI) (extendedKey string, err error)

导出扩展私钥

func (*Wallet) ExportKeyStore

func (w *Wallet) ExportKeyStore(address, keyPath string, keystorePwd string, api ChainAPI) (keyStore string, err error)

将地址对应的私钥转换成keystore

func (*Wallet) ExportMasterExtendedKey

func (w *Wallet) ExportMasterExtendedKey() string

导出主账户的扩展私钥

func (*Wallet) ExportMasterMnemonic

func (w *Wallet) ExportMasterMnemonic() string

==========================主账户============================ 导出主账户的助记词

func (*Wallet) ExportMasterRawKey

func (w *Wallet) ExportMasterRawKey() string

导出主账户的扩展私钥

func (*Wallet) ExportRawKey

func (w *Wallet) ExportRawKey(address, keyPath string, api ChainAPI) (key string, err error)

将地址对应的私钥直接输出

func (*Wallet) GetPriKeyFromAddress

func (w *Wallet) GetPriKeyFromAddress(address, keyPath string, api ChainAPI) ([]byte, error)

GetPriKeyFromAddress 获取某个地址的私钥

func (*Wallet) ImportKeyStore

func (w *Wallet) ImportKeyStore(key []byte, password string) (address string, err error)

导入keystore

func (*Wallet) ImportRawKey

func (w *Wallet) ImportRawKey(key string, password string) (address string, err error)

导入私钥

func (*Wallet) ListAccount

func (w *Wallet) ListAccount() ([]string, error)

ListAccount list all account

func (*Wallet) SetIsSaveExtendedKey

func (w *Wallet) SetIsSaveExtendedKey(b bool)

是否保存ExtendedKey

func (*Wallet) SetIsSaveSubKey

func (w *Wallet) SetIsSaveSubKey(b bool)

设置是否保存子私钥

func (*Wallet) Sign

func (w *Wallet) Sign(address, keyPath string, hash []byte, api ChainAPI) (string, error)

Sign

Directories

Path Synopsis
description: keybox
description: keybox
gm2
https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
description: keybox
description: keybox
btc
description: keybox
description: keybox
btc/helpers
Package helpers provides convenience functions to simplify wallet code.
Package helpers provides convenience functions to simplify wallet code.
btc/txauthor
Package txauthor provides transaction creation code for wallets.
Package txauthor provides transaction creation code for wallets.
btc/txrules
Package txrules provides transaction rules that should be followed by transaction authors for wide mempool acceptance and quick mining.
Package txrules provides transaction rules that should be followed by transaction authors for wide mempool acceptance and quick mining.
eth
crypto
address
description: keybox
description: keybox
scrypt
description: subchain-sdk-go
description: subchain-sdk-go
util
convutil
description: xblog
description: xblog
dateutil
description: keybox
description: keybox
description: keybox
description: keybox

Jump to

Keyboard shortcuts

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