// newManagedAddress returns a new managed address based on the passed account, // private key, and whether or not the public key is compressed. The managed // address will have access to the private and public keys. func newManagedAddress(m *Manager, account uint32, privKey chainec.PrivateKey) (*managedAddress, error) { if privKey == nil { err := fmt.Errorf("missing private key") return nil, managerError(ErrNoExist, "nil pointer", err) } // Encrypt the private key. // // NOTE: The privKeyBytes here are set into the managed address which // are cleared when locked, so they aren't cleared here. privKeyBytes := privKey.Serialize() privKeyEncrypted, err := m.cryptoKeyPriv.Encrypt(privKeyBytes) if err != nil { str := "failed to encrypt private key" return nil, managerError(ErrCrypto, str, err) } // Leverage the code to create a managed address without a private key // and then add the private key to it. pubx, puby := privKey.Public() ecPubKey := chainec.Secp256k1.NewPublicKey(pubx, puby) managedAddr, err := newManagedAddressWithoutPrivKey(m, account, ecPubKey, true) if err != nil { return nil, err } managedAddr.privKeyEncrypted = privKeyEncrypted managedAddr.privKeyCT = privKeyBytes return managedAddr, nil }