OSDN Git Service

init push for list address (#445)
authorPaladz <yzhu101@uottawa.ca>
Wed, 21 Mar 2018 04:39:59 +0000 (12:39 +0800)
committerGitHub <noreply@github.com>
Wed, 21 Mar 2018 04:39:59 +0000 (12:39 +0800)
blockchain/account/accounts.go
blockchain/accounts.go
blockchain/rpc_reactor.go

index 1bf13ba..eb3e052 100755 (executable)
@@ -443,3 +443,20 @@ func (m *Manager) ListAccounts(id string) ([]*Account, error) {
 
        return accounts, nil
 }
+
+// ListControlProgram return all the local control program
+func (m *Manager) ListControlProgram() ([]*CtrlProgram, error) {
+       cps := []*CtrlProgram{}
+       cpIter := m.db.IteratorPrefix([]byte(accountCPPrefix))
+       defer cpIter.Release()
+
+       for cpIter.Next() {
+               cp := &CtrlProgram{}
+               if err := json.Unmarshal(cpIter.Value(), cp); err != nil {
+                       return nil, err
+               }
+               cps = append(cps, cp)
+       }
+
+       return cps, nil
+}
index f6d4aba..251f367 100644 (file)
@@ -95,3 +95,24 @@ func (bcr *BlockchainReactor) validateAddress(ctx context.Context, ins struct {
        resp.IsLocal = bcr.accounts.IsLocalControlProgram(program)
        return NewSuccessResponse(resp)
 }
+
+type addressResp struct {
+       AccountID string `json:"account_id"`
+       Address   string `json:"address"`
+}
+
+func (bcr *BlockchainReactor) listAddresses(ctx context.Context) Response {
+       cps, err := bcr.accounts.ListControlProgram()
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+
+       addresses := []*addressResp{}
+       for _, cp := range cps {
+               if cp.Address == "" {
+                       continue
+               }
+               addresses = append(addresses, &addressResp{AccountID: cp.AccountID, Address: cp.Address})
+       }
+       return NewSuccessResponse(addresses)
+}
index 5a3917d..8ca8754 100644 (file)
@@ -58,6 +58,7 @@ func (bcr *BlockchainReactor) BuildHandler() {
                m.Handle("/update-account-tags", jsonHandler(bcr.updateAccountTags))
                m.Handle("/create-account-receiver", jsonHandler(bcr.createAccountReceiver))
                m.Handle("/list-accounts", jsonHandler(bcr.listAccounts))
+               m.Handle("/list-addresses", jsonHandler(bcr.listAddresses))
                m.Handle("/delete-account", jsonHandler(bcr.deleteAccount))
                m.Handle("/validate-address", jsonHandler(bcr.validateAddress))