func AddUpdateAddress(address *objects.AddressDetail) error { localMutex.Lock() defer localMutex.Unlock() var err error if address.Address == nil { address.Address = encryption.StringToAddress(address.String) } if address.Address == nil { return errors.New("Invalid Address!") } addrHash := objects.MakeHash(address.Address) if Contains(addrHash) == ADDRESS { // Exists in message database, update pubkey, privkey, and registration err = LocalDB.Exec("UPDATE addressbook SET registered=?, subscribed=?, label=? WHERE hash=?", address.IsRegistered, address.IsSubscribed, address.Label, addrHash.GetBytes()) if err != nil { return err } if address.Pubkey != nil { err = LocalDB.Exec("UPDATE addressbook SET pubkey=? WHERE hash=?", address.Pubkey, addrHash.GetBytes()) if err != nil { return err } } if address.Privkey != nil { err = LocalDB.Exec("UPDATE addressbook SET privkey=? WHERE hash=?", address.Privkey, addrHash.GetBytes()) if err != nil { return err } } if address.EncPrivkey != nil { err = LocalDB.Exec("UPDATE addressbook SET encprivkey=? WHERE hash=?", address.EncPrivkey, addrHash.GetBytes()) if err != nil { return err } } } else { // Doesn't exist yet, insert it! err = LocalDB.Exec("INSERT INTO addressbook VALUES (?, ?, ?, ?, ?, ?, ?, ?)", addrHash.GetBytes(), address.Address, address.IsRegistered, address.Pubkey, address.Privkey, address.Label, address.IsSubscribed, address.EncPrivkey) if err != nil { return err } Add(addrHash, ADDRESS) } return nil }
func (service *EMPService) CreateAddress(r *http.Request, args *NilParam, reply *objects.AddressDetail) error { if !basicAuth(service.Config, r) { service.Config.Log <- fmt.Sprintf("Unauthorized RPC Request from: %s", r.RemoteAddr) return errors.New("Unauthorized") } // Create Address priv, x, y := encryption.CreateKey(service.Config.Log) reply.Privkey = priv if x == nil { return errors.New("Key Pair Generation Error") } reply.Pubkey = encryption.MarshalPubkey(x, y) reply.IsRegistered = true reply.Address = encryption.GetAddress(service.Config.Log, x, y) if reply.Address == nil { return errors.New("Could not create address, function returned nil.") } reply.String = encryption.AddressToString(reply.Address) // Add Address to Database err := localdb.AddUpdateAddress(reply) if err != nil { service.Config.Log <- fmt.Sprintf("Error Adding Address: ", err) return err } // Send Pubkey to Network encPub := new(objects.EncryptedPubkey) encPub.AddrHash = objects.MakeHash(reply.Address) encPub.IV, encPub.Payload, err = encryption.SymmetricEncrypt(reply.Address, string(reply.Pubkey)) if err != nil { service.Config.Log <- fmt.Sprintf("Error Encrypting Pubkey: ", err) return nil } // Record Pubkey for Network service.Config.RecvQueue <- *objects.MakeFrame(objects.PUBKEY, objects.BROADCAST, encPub) return nil }