github.com/0chain/gosdk@v1.17.11/zcncore/mswallet_base.go (about)

     1  //go:build !mobile
     2  // +build !mobile
     3  
     4  package zcncore
     5  
     6  import (
     7  	"encoding/hex"
     8  	"fmt"
     9  	"time"
    10  
    11  	"github.com/0chain/errors"
    12  	"github.com/0chain/gosdk/core/encryption"
    13  	"github.com/0chain/gosdk/core/zcncrypto"
    14  )
    15  
    16  // MSVoteCallback callback definition multisig Vote function
    17  type MSVoteCallback interface {
    18  	OnVoteComplete(status int, proposal string, err string)
    19  }
    20  
    21  // CreateMSWallet returns multisig wallet information
    22  func CreateMSWallet(t, n int) (string, string, []string, error) {
    23  	if t < 1 || t > n {
    24  		return "", "", nil, errors.New("bls0_generate_threshold_key_shares", fmt.Sprintf("Given threshold (%d) is less than 1 or greater than numsigners (%d)", t, n))
    25  	}
    26  
    27  	id := 0
    28  	if _config.chain.SignatureScheme != "bls0chain" {
    29  		return "", "", nil, errors.New("", "encryption scheme for this blockchain is not bls0chain")
    30  
    31  	}
    32  
    33  	groupKey := zcncrypto.NewSignatureScheme(_config.chain.SignatureScheme)
    34  	wallet, err := groupKey.GenerateKeys()
    35  	if err != nil {
    36  		return "", "", nil, err
    37  	}
    38  
    39  	logging.Info(fmt.Sprintf("Wallet id: %s", wallet.ClientKey))
    40  
    41  	groupClientID := GetClientID(groupKey.GetPublicKey())
    42  	//Code modified to directly use BLS0ChainThresholdScheme
    43  	signerKeys, err := zcncrypto.GenerateThresholdKeyShares(t, n, groupKey)
    44  
    45  	if err != nil {
    46  		return "", "", nil, errors.Wrap(err, "Err in generateThresholdKeyShares")
    47  	}
    48  	var signerClientIDs []string
    49  	for _, key := range signerKeys {
    50  		signerClientIDs = append(signerClientIDs, GetClientID(key.GetPublicKey()))
    51  	}
    52  
    53  	msw := MSWallet{
    54  		Id:              id,
    55  		SignatureScheme: _config.chain.SignatureScheme,
    56  		GroupClientID:   groupClientID,
    57  		GroupKey:        groupKey,
    58  		SignerClientIDs: signerClientIDs,
    59  		SignerKeys:      signerKeys,
    60  		T:               t,
    61  		N:               n,
    62  	}
    63  
    64  	wallets, errw := getWallets(msw)
    65  
    66  	if errw != nil {
    67  		return "", "", nil, errw
    68  
    69  	}
    70  	smsw, er := msw.Marshal()
    71  	if er != nil {
    72  		return "", "", nil, er
    73  	}
    74  	return smsw, groupClientID, wallets, nil
    75  
    76  }
    77  
    78  func getWallets(msw MSWallet) ([]string, error) {
    79  
    80  	wallets := make([]string, 0, msw.N+1)
    81  
    82  	b0ss := msw.GroupKey
    83  
    84  	grw, err := makeWallet(b0ss.GetPrivateKey(), b0ss.GetPublicKey(), b0ss.GetMnemonic())
    85  
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	wallets = append(wallets, grw)
    91  
    92  	for _, signer := range msw.SignerKeys {
    93  		w, err := makeWallet(signer.GetPrivateKey(), signer.GetPublicKey(), "")
    94  		if err != nil {
    95  			return nil, err
    96  		}
    97  		wallets = append(wallets, w)
    98  	}
    99  	return wallets, nil
   100  }
   101  
   102  func makeWallet(privateKey, publicKey, mnemonic string) (string, error) {
   103  	w := &zcncrypto.Wallet{}
   104  	w.Keys = make([]zcncrypto.KeyPair, 1)
   105  	w.Keys[0].PrivateKey = privateKey
   106  	w.Keys[0].PublicKey = publicKey
   107  	w.ClientID = GetClientID(publicKey) //VerifyThis
   108  	w.ClientKey = publicKey
   109  	w.Mnemonic = mnemonic
   110  	w.Version = zcncrypto.CryptoVersion
   111  	w.DateCreated = time.Now().Format(time.RFC3339)
   112  
   113  	return w.Marshal()
   114  }
   115  
   116  // GetClientID -- computes Client ID from publickey
   117  func GetClientID(pkey string) string {
   118  	publicKeyBytes, err := hex.DecodeString(pkey)
   119  	if err != nil {
   120  		panic(err)
   121  	}
   122  
   123  	return encryption.Hash(publicKeyBytes)
   124  }
   125  
   126  func GetClientWalletKey() string {
   127  	return _config.wallet.ClientKey
   128  }
   129  
   130  func GetClientWalletID() string {
   131  	return _config.wallet.ClientID
   132  }