github.com/turingchain2020/turingchain@v1.1.21/wallet/bipwallet/transformer/transformer.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package transformer 通过私钥生成所选币种的公钥和地址
     6  package transformer
     7  
     8  import (
     9  	"fmt"
    10  	"sync"
    11  )
    12  
    13  // Transformer 过私钥生成所选币种的公钥和地址
    14  type Transformer interface {
    15  	PrivKeyToPub(keyTy uint32, priv []byte) (pub []byte, err error)
    16  	PubKeyToAddress(pub []byte) (add string, err error)
    17  }
    18  
    19  var (
    20  	driversMu sync.RWMutex
    21  	drivers   = make(map[string]Transformer)
    22  )
    23  
    24  // Register 对不同币种的Transformer进行注册
    25  func Register(name string, driver Transformer) {
    26  	driversMu.Lock()
    27  	defer driversMu.Unlock()
    28  	if driver == nil {
    29  		panic("transformer: Register driver is nil")
    30  	}
    31  	if _, dup := drivers[name]; dup {
    32  		panic("transformer: Register called twice for driver " + name)
    33  	}
    34  	drivers[name] = driver
    35  }
    36  
    37  // New 提供币种名称返回相应的Transformer对象
    38  func New(name string) (t Transformer, err error) {
    39  	driversMu.RLock()
    40  	defer driversMu.RUnlock()
    41  	t, ok := drivers[name]
    42  	if !ok {
    43  		err = fmt.Errorf("unknown Transformer %q", name)
    44  		return
    45  	}
    46  
    47  	return t, nil
    48  }