| 142 | } |
| 143 | |
| 144 | func (this *ClientImpl) NewAccount(label string, typeCode keypair.KeyType, curveCode byte, sigScheme s.SignatureScheme, passwd []byte) (*Account, error) { |
| 145 | if len(passwd) == 0 { |
| 146 | return nil, fmt.Errorf("password cannot empty") |
| 147 | } |
| 148 | prvkey, pubkey, err := keypair.GenerateKeyPair(typeCode, curveCode) |
| 149 | if err != nil { |
| 150 | return nil, fmt.Errorf("generateKeyPair error: %s", err) |
| 151 | } |
| 152 | address := types.AddressFromPubKey(pubkey) |
| 153 | addressBase58 := address.ToBase58() |
| 154 | prvSecret, err := keypair.EncryptPrivateKey(prvkey, addressBase58, passwd) |
| 155 | if err != nil { |
| 156 | return nil, fmt.Errorf("encryptPrivateKey error: %s", err) |
| 157 | } |
| 158 | accData := &AccountData{} |
| 159 | accData.Label = label |
| 160 | accData.SetKeyPair(prvSecret) |
| 161 | accData.SigSch = sigScheme.Name() |
| 162 | accData.PubKey = hex.EncodeToString(keypair.SerializePublicKey(pubkey)) |
| 163 | |
| 164 | err = this.addAccountData(accData) |
| 165 | if err != nil { |
| 166 | return nil, err |
| 167 | } |
| 168 | return &Account{ |
| 169 | PrivateKey: prvkey, |
| 170 | PublicKey: pubkey, |
| 171 | Address: address, |
| 172 | SigScheme: sigScheme, |
| 173 | }, nil |
| 174 | } |
| 175 | |
| 176 | func (this *ClientImpl) addAccountData(accData *AccountData) error { |
| 177 | if !this.checkSigScheme(accData.Alg, accData.SigSch) { |