1 // Package account stores and tracks accounts within a Bytom Core.
11 "github.com/golang/groupcache/lru"
12 log "github.com/sirupsen/logrus"
14 "github.com/vapor/blockchain/signers"
15 "github.com/vapor/blockchain/txbuilder"
16 "github.com/vapor/common"
17 "github.com/vapor/consensus"
18 "github.com/vapor/consensus/segwit"
19 "github.com/vapor/crypto"
20 "github.com/vapor/crypto/ed25519/chainkd"
21 "github.com/vapor/crypto/sha3pool"
22 dbm "github.com/vapor/database/leveldb"
23 "github.com/vapor/errors"
24 "github.com/vapor/protocol"
25 "github.com/vapor/protocol/bc"
26 "github.com/vapor/protocol/vm/vmutil"
30 maxAccountCache = 1000
32 // HardenedKeyStart bip32 hierarchical deterministic wallets
33 // keys with index ≥ 0x80000000 are hardened keys
34 HardenedKeyStart = 0x80000000
39 accountIndexPrefix = []byte("AccountIndex:")
40 accountPrefix = []byte("Account:")
41 aliasPrefix = []byte("AccountAlias:")
42 contractIndexPrefix = []byte("ContractIndex")
43 contractPrefix = []byte("Contract:")
44 miningAddressKey = []byte("MiningAddress")
45 CoinbaseAbKey = []byte("CoinbaseArbitrary")
48 // pre-define errors for supporting bytom errorFormatter
50 ErrDuplicateAlias = errors.New("Duplicate account alias")
51 ErrDuplicateIndex = errors.New("Duplicate account with same xPubs and index")
52 ErrFindAccount = errors.New("Failed to find account")
53 ErrMarshalAccount = errors.New("Failed to marshal account")
54 ErrInvalidAddress = errors.New("Invalid address")
55 ErrFindCtrlProgram = errors.New("Failed to find account control program")
56 ErrDeriveRule = errors.New("Invalid key derivation rule")
57 ErrContractIndex = errors.New("Exceeded maximum addresses per account")
58 ErrAccountIndex = errors.New("Exceeded maximum accounts per xpub")
59 ErrFindTransaction = errors.New("No transaction")
60 ErrAccountIDEmpty = errors.New("account_id is empty")
63 // ContractKey account control promgram store prefix
64 func ContractKey(hash common.Hash) []byte {
65 return append(contractPrefix, hash[:]...)
68 // Key account store prefix
69 func Key(name string) []byte {
70 return append(accountPrefix, []byte(name)...)
73 func aliasKey(name string) []byte {
74 return append(aliasPrefix, []byte(name)...)
77 func bip44ContractIndexKey(accountID string, change bool) []byte {
78 key := append(contractIndexPrefix, accountID...)
80 return append(key, []byte{1}...)
82 return append(key, []byte{0}...)
85 func contractIndexKey(accountID string) []byte {
86 return append(contractIndexPrefix, []byte(accountID)...)
89 // Account is structure of Bytom account
93 Alias string `json:"alias"`
96 //CtrlProgram is structure of account control program
97 type CtrlProgram struct {
101 ControlProgram []byte
102 Change bool // Mark whether this control program is for UTXO change
105 // Manager stores accounts and their associated control programs.
106 type Manager struct {
108 chain *protocol.Chain
109 utxoKeeper *utxoKeeper
113 aliasCache *lru.Cache
115 delayedACPsMu sync.Mutex
116 delayedACPs map[*txbuilder.TemplateBuilder][]*CtrlProgram
122 // NewManager creates a new account manager
123 func NewManager(walletDB dbm.DB, chain *protocol.Chain) *Manager {
127 utxoKeeper: newUtxoKeeper(chain.BestBlockHeight, walletDB),
128 cache: lru.New(maxAccountCache),
129 aliasCache: lru.New(maxAccountCache),
130 delayedACPs: make(map[*txbuilder.TemplateBuilder][]*CtrlProgram),
134 // AddUnconfirmedUtxo add untxo list to utxoKeeper
135 func (m *Manager) AddUnconfirmedUtxo(utxos []*UTXO) {
136 m.utxoKeeper.AddUnconfirmedUtxo(utxos)
139 // CreateAccount creates a new Account.
140 func CreateAccount(xpubs []chainkd.XPub, quorum int, alias string, acctIndex uint64, deriveRule uint8) (*Account, error) {
141 if acctIndex >= HardenedKeyStart {
142 return nil, ErrAccountIndex
145 signer, err := signers.Create("account", xpubs, quorum, acctIndex, deriveRule)
147 return nil, errors.Wrap(err)
150 id := signers.IDGenerate()
151 return &Account{Signer: signer, ID: id, Alias: strings.ToLower(strings.TrimSpace(alias))}, nil
154 func (m *Manager) saveAccount(account *Account, updateIndex bool) error {
155 rawAccount, err := json.Marshal(account)
157 return ErrMarshalAccount
160 storeBatch := m.db.NewBatch()
161 storeBatch.Set(Key(account.ID), rawAccount)
162 storeBatch.Set(aliasKey(account.Alias), []byte(account.ID))
164 storeBatch.Set(GetAccountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
170 // SaveAccount save a new account.
171 func (m *Manager) SaveAccount(account *Account) error {
173 defer m.accountMu.Unlock()
175 if existed := m.db.Get(aliasKey(account.Alias)); existed != nil {
176 return ErrDuplicateAlias
179 acct, err := m.GetAccountByXPubsIndex(account.XPubs, account.KeyIndex)
185 return ErrDuplicateIndex
188 currentIndex := uint64(0)
189 if rawIndexBytes := m.db.Get(GetAccountIndexKey(account.XPubs)); rawIndexBytes != nil {
190 currentIndex = common.BytesToUnit64(rawIndexBytes)
192 return m.saveAccount(account, account.KeyIndex > currentIndex)
195 // Create creates and save a new Account.
196 func (m *Manager) Create(xpubs []chainkd.XPub, quorum int, alias string, deriveRule uint8) (*Account, error) {
198 defer m.accountMu.Unlock()
200 if existed := m.db.Get(aliasKey(alias)); existed != nil {
201 return nil, ErrDuplicateAlias
204 acctIndex := uint64(1)
205 if rawIndexBytes := m.db.Get(GetAccountIndexKey(xpubs)); rawIndexBytes != nil {
206 acctIndex = common.BytesToUnit64(rawIndexBytes) + 1
208 account, err := CreateAccount(xpubs, quorum, alias, acctIndex, deriveRule)
213 if err := m.saveAccount(account, true); err != nil {
220 func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) (err error) {
222 defer m.accountMu.Unlock()
224 account, err := m.FindByID(accountID)
228 oldAlias := account.Alias
230 normalizedAlias := strings.ToLower(strings.TrimSpace(newAlias))
231 if existed := m.db.Get(aliasKey(normalizedAlias)); existed != nil {
232 return ErrDuplicateAlias
236 m.aliasCache.Remove(oldAlias)
239 account.Alias = normalizedAlias
240 rawAccount, err := json.Marshal(account)
242 return ErrMarshalAccount
245 storeBatch := m.db.NewBatch()
246 storeBatch.Delete(aliasKey(oldAlias))
247 storeBatch.Set(Key(accountID), rawAccount)
248 storeBatch.Set(aliasKey(normalizedAlias), []byte(accountID))
253 // CreateAddress generate an address for the select account
254 func (m *Manager) CreateAddress(accountID string, change bool) (cp *CtrlProgram, err error) {
256 defer m.addressMu.Unlock()
258 account, err := m.FindByID(accountID)
263 currentIdx, err := m.getCurrentContractIndex(account, change)
268 cp, err = CreateCtrlProgram(account, currentIdx+1, change)
273 return cp, m.saveControlProgram(cp, true)
276 // CreateBatchAddresses generate a batch of addresses for the select account
277 func (m *Manager) CreateBatchAddresses(accountID string, change bool, stopIndex uint64) error {
279 defer m.addressMu.Unlock()
281 account, err := m.FindByID(accountID)
286 currentIndex, err := m.getCurrentContractIndex(account, change)
291 for currentIndex++; currentIndex <= stopIndex; currentIndex++ {
292 cp, err := CreateCtrlProgram(account, currentIndex, change)
297 if err := m.saveControlProgram(cp, true); err != nil {
305 // deleteAccountControlPrograms deletes control program matching accountID
306 func (m *Manager) deleteAccountControlPrograms(accountID string) error {
307 cps, err := m.ListControlProgram()
313 for _, cp := range cps {
314 if cp.AccountID == accountID {
315 sha3pool.Sum256(hash[:], cp.ControlProgram)
316 m.db.Delete(ContractKey(hash))
319 m.db.Delete(bip44ContractIndexKey(accountID, false))
320 m.db.Delete(bip44ContractIndexKey(accountID, true))
321 m.db.Delete(contractIndexKey(accountID))
325 // deleteAccountUtxos deletes utxos matching accountID
326 func (m *Manager) deleteAccountUtxos(accountID string) error {
327 accountUtxoIter := m.db.IteratorPrefix([]byte(UTXOPreFix))
328 defer accountUtxoIter.Release()
329 for accountUtxoIter.Next() {
330 accountUtxo := &UTXO{}
331 if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
335 if accountID == accountUtxo.AccountID {
336 m.db.Delete(StandardUTXOKey(accountUtxo.OutputID))
342 // DeleteAccount deletes the account's ID or alias matching account ID.
343 func (m *Manager) DeleteAccount(accountID string) (err error) {
345 defer m.accountMu.Unlock()
347 account, err := m.FindByID(accountID)
352 if err := m.deleteAccountControlPrograms(accountID); err != nil {
355 if err := m.deleteAccountUtxos(accountID); err != nil {
360 m.aliasCache.Remove(account.Alias)
363 storeBatch := m.db.NewBatch()
364 storeBatch.Delete(aliasKey(account.Alias))
365 storeBatch.Delete(Key(account.ID))
370 // FindByAlias retrieves an account's Signer record by its alias
371 func (m *Manager) FindByAlias(alias string) (*Account, error) {
373 cachedID, ok := m.aliasCache.Get(alias)
376 return m.FindByID(cachedID.(string))
379 rawID := m.db.Get(aliasKey(alias))
381 return nil, ErrFindAccount
384 accountID := string(rawID)
386 m.aliasCache.Add(alias, accountID)
388 return m.FindByID(accountID)
391 // FindByID returns an account's Signer record by its ID.
392 func (m *Manager) FindByID(id string) (*Account, error) {
394 cachedAccount, ok := m.cache.Get(id)
397 return cachedAccount.(*Account), nil
400 rawAccount := m.db.Get(Key(id))
401 if rawAccount == nil {
402 return nil, ErrFindAccount
405 account := &Account{}
406 if err := json.Unmarshal(rawAccount, account); err != nil {
411 m.cache.Add(id, account)
416 // GetAccountByProgram return Account by given CtrlProgram
417 func (m *Manager) GetAccountByProgram(program *CtrlProgram) (*Account, error) {
418 rawAccount := m.db.Get(Key(program.AccountID))
419 if rawAccount == nil {
420 return nil, ErrFindAccount
423 account := &Account{}
424 return account, json.Unmarshal(rawAccount, account)
427 // GetAccountByXPubsIndex get account by xPubs and index
428 func (m *Manager) GetAccountByXPubsIndex(xPubs []chainkd.XPub, index uint64) (*Account, error) {
429 accounts, err := m.ListAccounts("")
434 for _, account := range accounts {
435 if reflect.DeepEqual(account.XPubs, xPubs) && account.KeyIndex == index {
442 // GetAliasByID return the account alias by given ID
443 func (m *Manager) GetAliasByID(id string) string {
444 rawAccount := m.db.Get(Key(id))
445 if rawAccount == nil {
446 log.Warn("GetAliasByID fail to find account")
450 account := &Account{}
451 if err := json.Unmarshal(rawAccount, account); err != nil {
457 func (m *Manager) GetCoinbaseArbitrary() []byte {
458 if arbitrary := m.db.Get(CoinbaseAbKey); arbitrary != nil {
464 // GetCoinbaseControlProgram will return a coinbase script
465 func (m *Manager) GetCoinbaseControlProgram() ([]byte, error) {
466 cp, err := m.GetCoinbaseCtrlProgram()
467 if err == ErrFindAccount {
468 log.Warningf("GetCoinbaseControlProgram: can't find any account in db")
469 return vmutil.DefaultCoinbaseProgram()
474 return cp.ControlProgram, nil
477 // GetCoinbaseCtrlProgram will return the coinbase CtrlProgram
478 func (m *Manager) GetCoinbaseCtrlProgram() (*CtrlProgram, error) {
479 if data := m.db.Get(miningAddressKey); data != nil {
481 return cp, json.Unmarshal(data, cp)
484 accountIter := m.db.IteratorPrefix([]byte(accountPrefix))
485 defer accountIter.Release()
486 if !accountIter.Next() {
487 return nil, ErrFindAccount
490 account := &Account{}
491 if err := json.Unmarshal(accountIter.Value(), account); err != nil {
495 program, err := m.CreateAddress(account.ID, false)
500 rawCP, err := json.Marshal(program)
505 m.db.Set(miningAddressKey, rawCP)
509 // GetContractIndex return the current index
510 func (m *Manager) GetContractIndex(accountID string) uint64 {
512 if rawIndexBytes := m.db.Get(contractIndexKey(accountID)); rawIndexBytes != nil {
513 index = common.BytesToUnit64(rawIndexBytes)
518 // GetBip44ContractIndex return the current bip44 contract index
519 func (m *Manager) GetBip44ContractIndex(accountID string, change bool) uint64 {
521 if rawIndexBytes := m.db.Get(bip44ContractIndexKey(accountID, change)); rawIndexBytes != nil {
522 index = common.BytesToUnit64(rawIndexBytes)
527 // GetLocalCtrlProgramByAddress return CtrlProgram by given address
528 func (m *Manager) GetLocalCtrlProgramByAddress(address string) (*CtrlProgram, error) {
529 program, err := m.getProgramByAddress(address)
535 sha3pool.Sum256(hash[:], program)
536 rawProgram := m.db.Get(ContractKey(hash))
537 if rawProgram == nil {
538 return nil, ErrFindCtrlProgram
542 return cp, json.Unmarshal(rawProgram, cp)
545 // GetMiningAddress will return the mining address
546 func (m *Manager) GetMiningAddress() (string, error) {
547 cp, err := m.GetCoinbaseCtrlProgram()
551 return cp.Address, nil
554 // IsLocalControlProgram check is the input control program belong to local
555 func (m *Manager) IsLocalControlProgram(prog []byte) bool {
557 sha3pool.Sum256(hash[:], prog)
558 bytes := m.db.Get(ContractKey(hash))
562 // ListAccounts will return the accounts in the db
563 func (m *Manager) ListAccounts(id string) ([]*Account, error) {
564 accounts := []*Account{}
565 accountIter := m.db.IteratorPrefix(Key(strings.TrimSpace(id)))
566 defer accountIter.Release()
568 for accountIter.Next() {
569 account := &Account{}
570 if err := json.Unmarshal(accountIter.Value(), &account); err != nil {
573 accounts = append(accounts, account)
578 // ListControlProgram return all the local control program
579 func (m *Manager) ListControlProgram() ([]*CtrlProgram, error) {
580 cps := []*CtrlProgram{}
581 cpIter := m.db.IteratorPrefix(contractPrefix)
582 defer cpIter.Release()
586 if err := json.Unmarshal(cpIter.Value(), cp); err != nil {
589 cps = append(cps, cp)
594 func (m *Manager) ListUnconfirmedUtxo(accountID string, isSmartContract bool) []*UTXO {
595 utxos := m.utxoKeeper.ListUnconfirmed()
597 for _, utxo := range utxos {
598 if segwit.IsP2WScript(utxo.ControlProgram) != isSmartContract && (accountID == utxo.AccountID || accountID == "") {
599 result = append(result, utxo)
605 // RemoveUnconfirmedUtxo remove utxos from the utxoKeeper
606 func (m *Manager) RemoveUnconfirmedUtxo(hashes []*bc.Hash) {
607 m.utxoKeeper.RemoveUnconfirmedUtxo(hashes)
610 // SetMiningAddress will set the mining address
611 func (m *Manager) SetMiningAddress(miningAddress string) (string, error) {
612 program, err := m.getProgramByAddress(miningAddress)
618 Address: miningAddress,
619 ControlProgram: program,
621 rawCP, err := json.Marshal(cp)
626 m.db.Set(miningAddressKey, rawCP)
627 return m.GetMiningAddress()
630 func (m *Manager) SetCoinbaseArbitrary(arbitrary []byte) {
631 m.db.Set(CoinbaseAbKey, arbitrary)
634 // CreateCtrlProgram generate an address for the select account
635 func CreateCtrlProgram(account *Account, addrIdx uint64, change bool) (cp *CtrlProgram, err error) {
636 path, err := signers.Path(account.Signer, signers.AccountKeySpace, change, addrIdx)
641 if len(account.XPubs) == 1 {
642 cp, err = createP2PKH(account, path)
644 cp, err = createP2SH(account, path)
649 cp.KeyIndex, cp.Change = addrIdx, change
653 func createP2PKH(account *Account, path [][]byte) (*CtrlProgram, error) {
654 derivedXPubs := chainkd.DeriveXPubs(account.XPubs, path)
655 derivedPK := derivedXPubs[0].PublicKey()
656 pubHash := crypto.Ripemd160(derivedPK)
658 address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
663 control, err := vmutil.P2WPKHProgram([]byte(pubHash))
669 AccountID: account.ID,
670 Address: address.EncodeAddress(),
671 ControlProgram: control,
675 func createP2SH(account *Account, path [][]byte) (*CtrlProgram, error) {
676 derivedXPubs := chainkd.DeriveXPubs(account.XPubs, path)
677 derivedPKs := chainkd.XPubKeys(derivedXPubs)
678 signScript, err := vmutil.P2SPMultiSigProgram(derivedPKs, account.Quorum)
682 scriptHash := crypto.Sha256(signScript)
684 address, err := common.NewAddressWitnessScriptHash(scriptHash, &consensus.ActiveNetParams)
689 control, err := vmutil.P2WSHProgram(scriptHash)
695 AccountID: account.ID,
696 Address: address.EncodeAddress(),
697 ControlProgram: control,
701 func GetAccountIndexKey(xpubs []chainkd.XPub) []byte {
704 cpy := append([]chainkd.XPub{}, xpubs[:]...)
705 sort.Sort(signers.SortKeys(cpy))
706 for _, xpub := range cpy {
707 xPubs = append(xPubs, xpub[:]...)
709 sha3pool.Sum256(hash[:], xPubs)
710 return append(accountIndexPrefix, hash[:]...)
713 func (m *Manager) getCurrentContractIndex(account *Account, change bool) (uint64, error) {
714 switch account.DeriveRule {
715 case signers.BIP0032:
716 return m.GetContractIndex(account.ID), nil
717 case signers.BIP0044:
718 return m.GetBip44ContractIndex(account.ID, change), nil
720 return 0, ErrDeriveRule
723 func (m *Manager) getProgramByAddress(address string) ([]byte, error) {
724 addr, err := common.DecodeAddress(address, &consensus.ActiveNetParams)
728 redeemContract := addr.ScriptAddress()
731 case *common.AddressWitnessPubKeyHash:
732 program, err = vmutil.P2WPKHProgram(redeemContract)
733 case *common.AddressWitnessScriptHash:
734 program, err = vmutil.P2WSHProgram(redeemContract)
736 return nil, ErrInvalidAddress
744 func (m *Manager) saveControlProgram(prog *CtrlProgram, updateIndex bool) error {
747 sha3pool.Sum256(hash[:], prog.ControlProgram)
748 acct, err := m.GetAccountByProgram(prog)
753 accountCP, err := json.Marshal(prog)
758 storeBatch := m.db.NewBatch()
759 storeBatch.Set(ContractKey(hash), accountCP)
761 switch acct.DeriveRule {
762 case signers.BIP0032:
763 storeBatch.Set(contractIndexKey(acct.ID), common.Unit64ToBytes(prog.KeyIndex))
764 case signers.BIP0044:
765 storeBatch.Set(bip44ContractIndexKey(acct.ID, prog.Change), common.Unit64ToBytes(prog.KeyIndex))
773 // SaveControlPrograms save account control programs
774 func (m *Manager) SaveControlPrograms(progs ...*CtrlProgram) error {
776 defer m.addressMu.Unlock()
778 for _, prog := range progs {
779 acct, err := m.GetAccountByProgram(prog)
784 currentIndex, err := m.getCurrentContractIndex(acct, prog.Change)
789 m.saveControlProgram(prog, prog.KeyIndex > currentIndex)