github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/reapapis/middlebook.go (about)

     1  package reapapis
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/ethereum/go-ethereum/accounts"
     6  	"github.com/ethereum/go-ethereum/accounts/keystore"
     7  	"path/filepath"
     8  )
     9  
    10  type MiddleBooks struct {
    11  	ks    *keystore.KeyStore
    12  	books map[string]accounts.Account
    13  }
    14  
    15  func NewMiddleBooks() *MiddleBooks {
    16  	return &MiddleBooks{}
    17  }
    18  
    19  // H/L의 계정을 이용하여 랜덤계정을 생성,
    20  // private key들을 저장할 서버 디렉토리 지정한다.
    21  func (m *MiddleBooks) CreateMiddleBooks(path string) error {
    22  	status, err := IsWritable(path)
    23  	if !status {
    24  		return err
    25  	}
    26  	m.ks = keystore.NewKeyStore(filepath.Join(path, "keystore"), keystore.StandardScryptN, keystore.StandardScryptP)
    27  	return nil
    28  }
    29  
    30  // H/L의 계정(id)에 매칭되는 Reapchain 계정리턴 함
    31  func (m *MiddleBooks) Find(id string) (accounts.Account, bool) {
    32  	account, found := m.books[id]
    33  	return account, found
    34  }
    35  
    36  // H/L의 계정(id)에 매칭되는 Reapchain 계정을 생성.
    37  // 생성 후, 장부(Books)에 기록함.
    38  func (m *MiddleBooks) Create(id string) (accounts.Account ,error) {
    39  	newAccount, err := m.ks.NewAccount("")
    40  	if err != nil {
    41  		return newAccount, err
    42  	}
    43  	m.books[id] = newAccount
    44  	fmt.Println("ID : ", id, "=new=> : ", newAccount)
    45  	return newAccount, nil
    46  }